<?php
require("../../config/koneksi.php");
require("../../config/function.php");
require("../../config/crud.php");
cek_session_admin();
(isset($_GET['pg'])) ? $pg = $_GET['pg'] : $pg = '';
if ($pg == 'ubah') {
    $sttd = (isset($_POST['sttd'])) ? 1 : 0;
    $sstempel = (isset($_POST['sstempel'])) ? 1 : 0;
    $nilai = (isset($_POST['nilai'])) ? 1 : 0;
    $kelompok = (isset($_POST['kelompok'])) ? 1 : 0;
    $data = [
	    'tingkat' => $_POST['tingkat'],
        'nama_surat' => $_POST['nama'],
        'no_surat' => $_POST['no_surat'],
        'tgl_surat' => $_POST['tgl_surat'],
        'pembuka' => $_POST['pembuka'],
        'isi_surat' => $_POST['isi'],
        'penutup' => $_POST['penutup'],
       
        'sstempel' => $sstempel,
        'sttd' => $sttd,
        'nilai' => $nilai,
        'kelompok' => $kelompok

    ];
    $where = [
        'id_skl' => 1
    ];
    $exec = update($koneksi, 'skl', $data, $where);
    echo mysqli_error($koneksi);
    if ($exec) {
        $ektensi = ['jpg', 'png', 'JPG', 'PNG'];
        if ($_FILES['header']['name'] <> '') {
            $header = $_FILES['header']['name'];
            $temp = $_FILES['header']['tmp_name'];
            $ext = explode('.', $header);
            $ext = end($ext);
            if (in_array($ext, $ektensi)) {
                $dest = 'dist/img/header' . rand(1, 1000) . '.' . $ext;
                $upload = move_uploaded_file($temp, '../../' . $dest);
                if ($upload) {
                    $data2 = [
                        'header' => $dest
                    ];
                    $exec = update($koneksi, 'skl', $data2, $where);
                } else {
                    echo "gagal";
                }
            }
        }
        
        
    } else {
        echo "Gagal menyimpan";
    }
}
if ($pg == 'ambil_mapel') {
    $klp = $_POST['kodemap'];
    $sql = mysqli_query($koneksi, "SELECT * FROM mata_pelajaran WHERE kode_mapel='" . $klp . "' ");
   
   while ($data = mysqli_fetch_array($sql)) {
        echo "<option value='$data[nama_mapel]'>$data[nama_mapel]</option>";
    }
}
if ($pg == 'hapusmapel') {
   $id = $_POST['id'];
    delete($koneksi, 'mapel_ijazah', ['idmapel' => $id]);
}
if ($pg == 'ambil') {
    $klp = $_POST['kelompok'];
    $sql = mysqli_query($koneksi, "SELECT * FROM mapel_ijazah WHERE kelompok='" . $klp . "' ");
    echo "<option value=''>--Pilih Mapel--</option>";
   while ($data = mysqli_fetch_array($sql)) {
        echo "<option value='$data[kode]'>$data[namamapel]</option>";
    }
}