<?php

namespace App\Controllers\Panel\App\Settings\BackupRestore;

use App\Controllers\Panel\BC;
use CodeIgniter\API\ResponseTrait;

class DataMaster extends BC
{
    use ResponseTrait;
    public function __construct()
    {
        parent::__construct();
    }
    public function index()
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $db = db_connect();
        $db->transStart();
        if ($this->request->getVar('siswa')) {
            $db->query("DELETE FROM m_siswa");
            $db->query("ALTER TABLE m_siswa AUTO_INCREMENT = 1");
        }
        if ($this->request->getVar('mapel')) {
            $db->query("DELETE FROM m_mapel");
            $db->query("ALTER TABLE m_mapel AUTO_INCREMENT = 1");
        }
        if ($this->request->getVar('kelas')) {
            $db->query("DELETE FROM m_kelas");
            $db->query("ALTER TABLE m_kelas AUTO_INCREMENT = 1");
        }
        if ($this->request->getVar('tingkat')) {
            $db->query("DELETE FROM m_tingkat");
            $db->query("ALTER TABLE m_tingkat AUTO_INCREMENT = 1");
        }
        if ($this->request->getVar('jurusan')) {
            $db->query("DELETE FROM m_jurusan");
            $db->query("ALTER TABLE m_jurusan AUTO_INCREMENT = 1");
        }
        if ($this->request->getVar('ruang')) {
            $db->query("DELETE FROM m_ruangujian");
            $db->query("ALTER TABLE m_ruangujian AUTO_INCREMENT = 1");
        }
        if ($this->request->getVar('jenis_ujian')) {
            $db->query("DELETE FROM m_jenisujian");
            $db->query("ALTER TABLE m_jenisujian AUTO_INCREMENT = 1");
        }
        $db->transComplete();
        if ($db->transStatus() === FALSE) return $this->fail(['csrf' => csrf_hash(), 'message' => "Data Master Gagal Di Hapus"], 500);

        try {
        } catch (\Throwable $th) {
        }
        return $this->respond(['message' => "Data Master Berhasil Di Hapus"]);
    }
}
