<?php
session_start();
require_once '../../../library/sw-config.php';
require_once '../../../library/sw-function.php';

// Cek session login
if (!isset($_SESSION['SESSION_USER'])) {
    header('location:../../login/');
    exit;
}

$pimpinan = ucfirst($_SESSION['SESSION_USER']);

// Simpan data permintaan
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['buat_permintaan'])) {
    $tanggal_pengajuan  = date('Y-m-d');
    $tanggal_dibutuhkan = $_POST['tanggal_dibutuhkan'];
    $divisi_tujuan      = $_POST['divisi_tujuan'];
    $detail_permintaan  = $_POST['detail_permintaan'];
    $penanggung_jawab   = $_POST['penanggung_jawab'];
    $progress           = 'Menunggu Dikerjakan';

    $sql = "INSERT INTO permintaan_kerja 
            (tanggal_pengajuan, tanggal_dibutuhkan, divisi_tujuan, detail_permintaan, penanggung_jawab, konfirmasi_pimpinan, progress, created_at) 
            VALUES (?,?,?,?,?,?,?,NOW())";
    $stmt = $connection->prepare($sql);
    $stmt->bind_param('sssssss',
        $tanggal_pengajuan,
        $tanggal_dibutuhkan,
        $divisi_tujuan,
        $detail_permintaan,
        $penanggung_jawab,
        $pimpinan,
        $progress
    );

    if($stmt->execute()){
        $connection->query("INSERT INTO notifikasi (pesan, kepada_divisi, dibuat_pada)
                            VALUES ('Permintaan baru dari $pimpinan', '$divisi_tujuan', NOW())");
        $msg = "<div class='alert alert-success m-2'>✅ Permintaan berhasil dikirim!</div>";
    } else {
        $msg = "<div class='alert alert-danger m-2'>❌ Gagal menyimpan data: ".$stmt->error."</div>";
    }
    $stmt->close();
}

include_once 'mod/sw-header.php';
?>

<div id="appCapsule">
    <div class="section mt-3 text-center">
        <h3>📋 Permintaan Kerja</h3>
        <p><?= tgl_ind(date('Y-m-d')) ?> - <span class="clock"></span></p>
        <?= isset($msg) ? $msg : '' ?>
    </div>

    <div class="section mt-2 mb-2">
        <div class="card">
            <div class="card-body text-center">
                <button id="btnBukaForm" class="btn btn-primary">+ Buat Permintaan</button>
            </div>
        </div>

        <div id="formPermintaan" style="display:none">
            <div class="card mt-3">
                <div class="card-body">
                    <form method="POST">

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

                        <div class="form-group boxed">
                            <div class="input-wrapper">
                                <label class="label">Divisi Tujuan</label>
                                <select name="divisi_tujuan" class="form-control custom-select" required>
                                    <option value="">Pilih</option>
                                    <?php
                                    $q = $connection->query("SELECT nama_jabatan FROM jabatan");
                                    while($r = $q->fetch_assoc()){
                                        echo "<option value='".$r['nama_jabatan']."'>".$r['nama_jabatan']."</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>

                        <div class="form-group boxed">
                            <div class="input-wrapper">
                                <label class="label">Detail Permintaan</label>
                                <textarea name="detail_permintaan" class="form-control" required></textarea>
                            </div>
                        </div>

                        <div class="form-group boxed">
                            <div class="input-wrapper">
                                <label class="label">Penanggung Jawab</label>
                                <input type="text" name="penanggung_jawab" class="form-control" required>
                            </div>
                        </div>

                        <button type="submit" name="buat_permintaan" class="btn btn-success btn-block">
                            ✅ Kirim Permintaan
                        </button>
                    </form>
                </div>
            </div>
        </div>

    </div>
</div>

<script>
document.getElementById('btnBukaForm').addEventListener('click', function(){
    document.getElementById('formPermintaan').style.display = 'block';
    this.style.display = 'none';
});
</script>

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