import m from "mithril"
import SuratKelulusan from "./components/SuratKelulusan"
import SKHasilBelajar from "./components/SKHasilBelajar"
import SKK13HasilBelajar from "./components/SKK13HasilBelajar"
import Pendopo from "./components/Pendopo"
import Siswa from "./models/Siswa"
import AccessLog from "./models/AccessLog"

if (document.body.querySelector('.container#pendopo') != null) {
    m.mount(document.body.querySelector('.container#pendopo'), Pendopo);
}
else if (document.body.querySelector('.container#k13') != null) {
    m.mount(document.body.querySelector('.container#k13'), SKK13HasilBelajar);
}
else {
    m.route.prefix('#');
    m.route(document.body.querySelector('.container'), '/', {
        '/': SuratKelulusan,
        '/hasil-belajar': SKHasilBelajar,
    });

    // Below is to handle Ctrl+P or MouseR->Print
    window.addEventListener("beforeprint", function () {
        if (m.route.get() === '/hasil-belajar') {
            // If the print button did not exists, show nothing when printing
            if (document.body.querySelector('button.print-button') === null)
                document.body.style.display = 'none';
            else {
                if (!component.confirmPrint) {
                    alert('Surat Keterangan ini hanya dapat dicetak satu kali. Lanjutkan?');
                    component.confirmPrint = 1;
                    AccessLog.create({siswaId: Siswa.current.id, src: 'sk-hasil-belajar'}).then(() => {
                        AccessLog.fetch('sk-hasil-belajar');
                    })
                        .catch(e => {
                            if (e.code === 0) {
                                AccessLog.error = {
                                    message: e.message,
                                    errors: {
                                        create: ['Terjadi kesalahan saat menghubungkan ke server.'],
                                    },
                                };
                            }
                            else {
                                AccessLog.error = JSON.parse(e.message);
                            }
                        });
                }
            }
        }
    });

    window.addEventListener("afterprint", function () {
        if (m.route.get() === '/hasil-belajar') {
            // Give back the whole document.body
            if (document.body.querySelector('button.print-button') === null)
                document.body.style.display = 'block';

            // Hide the print button
            if (document.body.querySelector('button.print-button') !== null)
                document.body.querySelector('button.print-button').style.display = 'none';

            // To redraw the content to prevent printing
            let nisn = document.body.querySelector('input[name=nisn]').value,
                tanggalLahir = document.body.querySelector('input[name=tanggalLahir]').value;
            Siswa.cariData({
                nisn,
                tanggalLahir,
                src: 'sk-hasil-belajar',
                with: ['hasilBelajar', 'accessLog'],
            });
        }
    });
}
