const express = require('express');
const router = express.Router();
const dotenv = require('dotenv');
const conn = require('../conn');    
const connection = conn.connection;
const md5 = require('md5');

connection.connect((err) => {
    if (err) {
        console.error('Error connecting to MySQL database: ' + err.stack);
        return;
    }
    console.log('Connected to MySQL database as id ' + connection.threadId);
})

dotenv.config();

router.get('/', async (req, res) => {
    res.sendFile(__dirname + '/ui/admin/index.html');
})


// begin kelas
router.get('/kelas', async (req, res) => {
    res.sendFile(__dirname + '/ui/admin/kelas.html');
})

router.get('/kelas/get', async (req, res) => {
    const query = 'SELECT * FROM tb_kelas';
    connection.query(query, (error, results) => {
        if (error) {
            console.log('error get kelas', error);
            return res.status(500).json({ message: 'Internal server error', status: false });
        }
        return res.status(200).json({ success: true, data: results, status: true });
    })
})

router.post('/kelas', async (req, res) => {
    const { kelas } = req.body;
    const query = 'INSERT INTO tb_kelas (nama_kelas) VALUES (?)';
    connection.query(query, [ kelas], (error, results) => {
        if (error) {
            console.log('error insert kelas', error);
            return res.status(500).json({ message: 'Internal server error', status: false });
        }
        return res.status(200).json({ success: true, data: results, status: true });
    })
})

router.put('/kelas', async (req, res) => {
    const { id, kelas } = req.body;
    const query = 'UPDATE tb_kelas SET nama_kelas = ? WHERE id_kelas = ?';
    connection.query(query, [ kelas, id], (error, results) => {
        if (error) {
            console.log('error update kelas', error);
            return res.status(500).json({ message: 'Internal server error', status: false });
        }
        return res.status(200).json({ success: true, data: results, status: true });
    })
})

router.delete('/kelas/:id', async (req, res) => {
    const { id } = req.params;
    const query = 'DELETE FROM tb_kelas WHERE id_kelas = ?';
    connection.query(query, [ id], (error, results) => {
        if (error) {
            console.log('error delete kelas', error);
            return res.status(500).json({ message: 'Internal server error', status: false });
        }
        return res.status(200).json({ success: true, data: results, status: true });
    })
})
// end kelas

// begin siswa
router.get('/siswa', async (req, res) => {
    res.sendFile(__dirname + '/ui/admin/siswa.html');
})


router.post('/siswa', async (req, res) => {
    const { nis, nama, kelas , nama_kelas, angkatan , nik , tempat_lahir , tanggal_lahir , jenis_kelamin , agama , orang_tua1 , orang_tua2 , no_hp_orang_tua , alamat } = req.body;
    const { gambar } = req.files;
    console.log(gambar)
    console.log("nama", nama)
    console.log("nis", nis)
    console.log("kelas", kelas)
    console.log("nama_kelas", nama_kelas)
    console.log("angkatan", angkatan)
    console.log("nik", nik)
    console.log("tempat_lahir", tempat_lahir)
    console.log("tanggal_lahir", tanggal_lahir)
    console.log("enis_kelamin", jenis_kelamin)
    console.log("agama", agama)
    console.log("orang_tua1", orang_tua1)
    console.log("orang_tua2", orang_tua2)
    console.log("no_hp_orang_tua", no_hp_orang_tua)
    console.log("alamat", alamat)
})




module.exports = router