<?php 
if ($mod == ''){
    header('location:../404');
    echo 'kosong';
}else{
    include_once 'mod/sw-header.php';

    if(!isset($_COOKIE['COOKIES_MEMBER']) && !isset($_COOKIE['COOKIES_COOKIES'])){
        setcookie('COOKIES_MEMBER', '', 0, '/');
        setcookie('COOKIES_COOKIES', '', 0, '/');
        setcookie("COOKIES_MEMBER", "", time()-$expired_cookie);
        setcookie("COOKIES_COOKIES", "", time()-$expired_cookie);
        session_destroy();
        header("location:./index");
    }else{

        // 🚀 Ambil nama pimpinan dari user login
        $pimpinan = ucfirst($row_user['employees_name']);  

        // 🚀 Proses Simpan Supervisi
        if ($_SERVER["REQUEST_METHOD"] == "POST") {
            $tanggal     = $_POST['tanggal'];
            $karyawan    = $_POST['karyawan'];
            $jabatan     = $_POST['jabatan'];
            $substansi   = $_POST['substansi'];
            $lokasi      = ($_POST['lokasi'] == 'Lainnya') ? $_POST['lokasi_manual'] : $_POST['lokasi'];
            $waktu_mulai = $_POST['waktu_mulai'];
            $waktu_akhir = $_POST['waktu_akhir'];
            $perihal_1   = $_POST['perihal_1'];
            $perihal_2   = $_POST['perihal_2'];
            $perihal_3   = $_POST['perihal_3'];
            $catatan     = $_POST['catatan'];
            $status      = $_POST['status'];

            // Upload Foto
            $foto = "";
            if(isset($_FILES['foto']) && $_FILES['foto']['error'] == 0){
                $target = "uploads/".basename($_FILES['foto']['name']);
                if(move_uploaded_file($_FILES['foto']['tmp_name'], $target)){
                    $foto = $_FILES['foto']['name'];
                }
            }

            // Query Simpan
            $sql = "INSERT INTO supervisi 
                    (tanggal, pimpinan, karyawan, jabatan, substansi, lokasi, waktu_mulai, waktu_akhir, perihal_1, perihal_2, perihal_3, catatan, status, foto, created_at) 
                    VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,NOW())";
            $stmt = $connection->prepare($sql);
            if(!$stmt){
                echo "<div class='alert alert-danger m-3'>❌ Query Error: ".$connection->error."</div>";
            }else{
                $stmt->bind_param("ssssssssssssss", 
                    $tanggal, $pimpinan, $karyawan, $jabatan, $substansi, 
                    $lokasi, $waktu_mulai, $waktu_akhir, 
                    $perihal_1, $perihal_2, $perihal_3, 
                    $catatan, $status, $foto
                );
                if($stmt->execute()){
                    echo '<div class="alert alert-success m-3">✅ Data supervisi berhasil disimpan</div>';
                    echo '<meta http-equiv="refresh" content="2;url=?mod=supervisi">';
                }else{
                    echo '<div class="alert alert-danger m-3">❌ Gagal menyimpan data: '.$stmt->error.'</div>';
                }
                $stmt->close();
            }
        }

        // 🚀 Form Input Supervisi
        echo '
        <div id="appCapsule">
            <div class="section mt-3 text-center">
                <h3>📋 Formulir Supervisi</h3>
                <p>'.tgl_ind($date).' - <span class="clock"></span></p>
            </div>

            <div class="section mt-2 mb-2">
                <div class="card">
                    <div class="card-body">
                        <form action="" method="POST" enctype="multipart/form-data">

                            <!-- Tanggal -->
                            <div class="form-group boxed">
                                <div class="input-wrapper">
                                    <label class="label">Tanggal Pertemuan</label>
                                    <input type="date" name="tanggal" class="form-control" required>
                                </div>
                            </div>

                            <!-- Nama Pimpinan (otomatis dari login) -->
                            <div class="form-group boxed">
                                <div class="input-wrapper">
                                    <label class="label">Nama Pimpinan</label>
                                    <input type="text" class="form-control" value="'.$pimpinan.'" readonly>
                                    <input type="hidden" name="pimpinan" value="'.$pimpinan.'">
                                </div>
                            </div>

                            <!-- Nama Karyawan -->
                            <div class="form-group boxed">
                                <div class="input-wrapper">
                                    <label class="label">Nama Karyawan yang Disupervisi</label>
                                    <input type="text" name="karyawan" class="form-control" required>
                                </div>
                            </div>

                            <!-- Jabatan -->
                            <div class="form-group boxed">
                                <div class="input-wrapper">
                                    <label class="label">Jabatan</label>
                                    <input type="text" name="jabatan" class="form-control" required>
                                </div>
                            </div>

                            <!-- Substansi Pertemuan -->
                            <div class="form-group boxed">
                                <div class="input-wrapper">
                                    <label class="label">Substansi Pertemuan</label>
                                    <select class="form-control custom-select" name="substansi" required>
                                        <option value="">Pilih</option>
                                        <option value="Laporan Perkembangan/Kegiatan">Laporan Perkembangan/Kegiatan</option>
                                        <option value="Laporan Kendala yang Dihadapi">Laporan Kendala yang Dihadapi</option>
                                        <option value="Evaluasi Pelanggaran SOP">Evaluasi Pelanggaran SOP</option>
                                        <option value="Evaluasi Penurunan Kinerja">Evaluasi Penurunan Kinerja</option>
                                    </select>
                                </div>
                            </div>

                            <!-- Lokasi Pertemuan -->
                            <div class="form-group boxed">
                                <div class="input-wrapper">
                                    <label class="label">Lokasi Pertemuan</label>
                                    <select class="form-control custom-select" name="lokasi" required>
                                        <option value="">Pilih</option>
                                        <option value="PDK">PDK</option>
                                        <option value="Lampasi">Lampasi</option>
                                        <option value="Harau Putra">Harau Putra</option>
                                        <option value="Harau Putri">Harau Putri</option>
                                        <option value="Global Putra">Global Putra</option>
                                        <option value="Global Putri">Global Putri</option>
                                        <option value="Lainnya">Lainnya (Ketik manual)</option>
                                    </select>
                                    <input type="text" name="lokasi_manual" class="form-control mt-2" placeholder="Ketik lokasi lain">
                                </div>
                            </div>

                            <!-- Waktu -->
                            <div class="form-group boxed">
                                <div class="input-wrapper">
                                    <label class="label">Waktu Mulai</label>
                                    <input type="time" name="waktu_mulai" class="form-control" required>
                                </div>
                            </div>
                            <div class="form-group boxed">
                                <div class="input-wrapper">
                                    <label class="label">Waktu Akhir</label>
                                    <input type="time" name="waktu_akhir" class="form-control" required>
                                </div>
                            </div>

                            <!-- Perihal -->
                            <div class="form-group boxed">
                                <div class="input-wrapper">
                                    <label class="label">Perihal 1</label>
                                    <input type="text" name="perihal_1" class="form-control">
                                </div>
                            </div>
                            <div class="form-group boxed">
                                <div class="input-wrapper">
                                    <label class="label">Perihal 2</label>
                                    <input type="text" name="perihal_2" class="form-control">
                                </div>
                            </div>
                            <div class="form-group boxed">
                                <div class="input-wrapper">
                                    <label class="label">Perihal 3</label>
                                    <input type="text" name="perihal_3" class="form-control">
                                </div>
                            </div>

                            <!-- Catatan -->
                            <div class="form-group boxed">
                                <div class="input-wrapper">
                                    <label class="label">Catatan Pertemuan</label>
                                    <textarea name="catatan" class="form-control"></textarea>
                                </div>
                            </div>

                            <!-- Status -->
                            <div class="form-group boxed">
                                <div class="input-wrapper">
                                    <label class="label">Status</label>
                                    <select class="form-control custom-select" name="status" required>
                                        <option value="Pending">Pending</option>
                                        <option value="Selesai">Selesai</option>
                                        <option value="Konsultasi">Konsultasi</option>
                                    </select>
                                </div>
                            </div>

                            <!-- Foto -->
                            <div class="form-group boxed">
                                <div class="input-wrapper">
                                    <label class="label">Upload Foto</label>
                                    <input type="file" name="foto" class="form-control">
                                </div>
                            </div>

                            <button type="submit" class="btn btn-primary btn-block">✅ Simpan</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
        ';
    }

    include_once 'mod/sw-footer.php';
}
?>
