<?php

$judul = "Pengajuan Checklist";
$menu  = "pengajuan";

include 'layout/header.php';

// SIMPAN PENGAJUAN BARU
if(isset($_POST['simpan'])){

    $job_list = mysqli_real_escape_string($conn,$_POST['job_list']);
    $job_desc = mysqli_real_escape_string($conn,$_POST['job_desc']);
    $waktu    = mysqli_real_escape_string($conn,$_POST['waktu']);
    $jenis    = mysqli_real_escape_string($conn,$_POST['jenis']);

    $id_user = $_SESSION['id_user'];

    mysqli_query($conn,"
        INSERT INTO pengajuan_checklist
        (job_list, job_desc, waktu, jenis, diajukan_oleh, status)
        VALUES
        ('$job_list', '$job_desc', '$waktu', '$jenis', '$id_user', 'Pending')
    ");

    echo "<script>alert('Pengajuan checklist berhasil dikirim'); location='pengajuan_checklist.php';</script>";
}

// UPDATE PENGAJUAN (hanya jika masih Pending)
if(isset($_POST['update'])){

    $id_pengajuan = mysqli_real_escape_string($conn,$_POST['id_pengajuan']);
    $job_list     = mysqli_real_escape_string($conn,$_POST['job_list']);
    $job_desc     = mysqli_real_escape_string($conn,$_POST['job_desc']);
    $waktu        = mysqli_real_escape_string($conn,$_POST['waktu']);
    $jenis        = mysqli_real_escape_string($conn,$_POST['jenis']);
    $id_user      = $_SESSION['id_user'];

    // Validasi: hanya bisa edit milik sendiri dan masih Pending
    $cek = mysqli_query($conn,"
        SELECT * FROM pengajuan_checklist
        WHERE id_pengajuan='$id_pengajuan'
        AND diajukan_oleh='$id_user'
        AND status='Pending'
    ");

    if(mysqli_num_rows($cek) > 0){
        mysqli_query($conn,"
            UPDATE pengajuan_checklist
            SET job_list='$job_list', job_desc='$job_desc', waktu='$waktu', jenis='$jenis'
            WHERE id_pengajuan='$id_pengajuan'
        ");
        echo "<script>alert('Pengajuan berhasil diperbarui'); location='pengajuan_checklist.php';</script>";
    } else {
        echo "<script>alert('Tidak dapat mengedit pengajuan yang sudah diproses'); location='pengajuan_checklist.php';</script>";
    }
}

// HAPUS PENGAJUAN (hanya jika masih Pending)
if(isset($_GET['hapus'])){

    $id_pengajuan = mysqli_real_escape_string($conn,$_GET['hapus']);
    $id_user      = $_SESSION['id_user'];

    // Validasi: hanya bisa hapus milik sendiri dan masih Pending
    $cek = mysqli_query($conn,"
        SELECT * FROM pengajuan_checklist
        WHERE id_pengajuan='$id_pengajuan'
        AND diajukan_oleh='$id_user'
        AND status='Pending'
    ");

    if(mysqli_num_rows($cek) > 0){
        mysqli_query($conn,"DELETE FROM pengajuan_checklist WHERE id_pengajuan='$id_pengajuan'");
        echo "<script>alert('Pengajuan berhasil dihapus'); location='pengajuan_checklist.php';</script>";
    } else {
        echo "<script>alert('Tidak dapat menghapus pengajuan yang sudah diproses'); location='pengajuan_checklist.php';</script>";
    }
}

// AMBIL DATA UNTUK EDIT (isi form edit)
$data_edit = null;
if(isset($_GET['edit'])){
    $id_pengajuan = mysqli_real_escape_string($conn,$_GET['edit']);
    $id_user      = $_SESSION['id_user'];

    $q_edit = mysqli_query($conn,"
        SELECT * FROM pengajuan_checklist
        WHERE id_pengajuan='$id_pengajuan'
        AND diajukan_oleh='$id_user'
        AND status='Pending'
    ");
    $data_edit = mysqli_fetch_assoc($q_edit);
}

include 'layout/sidebar.php';
?>

<div class="container-fluid">

<!-- CARD FORM TAMBAH / EDIT -->
<div class="card shadow mb-4">

    <div class="card-header">
        <h5 class="mb-0">
            <?= $data_edit ? '✏️ Edit Pengajuan Checklist' : 'Pengajuan Checklist Baru' ?>
        </h5>
    </div>

    <div class="card-body">

        <form method="POST">

            <?php if($data_edit): ?>
                <!-- Hidden field untuk mode edit -->
                <input type="hidden" name="id_pengajuan" value="<?= $data_edit['id_pengajuan'] ?>">
            <?php endif; ?>

            <div class="mb-3">
                <label class="form-label">Job List</label>
                <input
                    type="text"
                    name="job_list"
                    class="form-control"
                    value="<?= $data_edit ? htmlspecialchars($data_edit['job_list']) : '' ?>"
                    required>
            </div>

            <div class="mb-3">
                <label class="form-label">Job Desc</label>
                <textarea
                    name="job_desc"
                    class="form-control"
                    rows="4"
                    required><?= $data_edit ? htmlspecialchars($data_edit['job_desc']) : '' ?></textarea>
            </div>

            <div class="row">

                <div class="col-md-6 mb-3">
                    <label class="form-label">Waktu</label>
                    <select name="waktu" class="form-select">
                        <?php
                        $waktu_list = ['Pagi','Siang','Sore','Malam'];
                        foreach($waktu_list as $w){
                            $selected = ($data_edit && $data_edit['waktu']==$w) ? 'selected' : '';
                            echo "<option value='$w' $selected>$w</option>";
                        }
                        ?>
                    </select>
                </div>

                <div class="col-md-6 mb-3">
                    <label class="form-label">Jenis</label>
                    <select name="jenis" class="form-select">
                        <?php
                        $jenis_list = ['Daily','Weekly','Monthly'];
                        foreach($jenis_list as $j){
                            $selected = ($data_edit && $data_edit['jenis']==$j) ? 'selected' : '';
                            echo "<option value='$j' $selected>$j</option>";
                        }
                        ?>
                    </select>
                </div>

            </div>

            <?php if($data_edit): ?>
                <button type="submit" name="update" class="btn btn-primary">
                    💾 Simpan Perubahan
                </button>
                <a href="pengajuan_checklist.php" class="btn btn-secondary">
                    Batal
                </a>
            <?php else: ?>
                <button type="submit" name="simpan" class="btn btn-success">
                    Kirim Pengajuan
                </button>
            <?php endif; ?>

        </form>

    </div>

</div>

<!-- CARD RIWAYAT PENGAJUAN -->
<div class="card shadow">

    <div class="card-header">
        <h5 class="mb-0">Riwayat Pengajuan</h5>
    </div>

    <div class="card-body">

        <table class="table table-bordered table-hover">

            <thead class="table-light">
            <tr>
                <th>No</th>
                <th>Job List</th>
                <th>Jenis</th>
                <th>Waktu</th>
                <th>Status</th>
                <th>Aksi</th>
            </tr>
            </thead>

            <tbody>
            <?php

            $no      = 1;
            $id_user = $_SESSION['id_user'];

            $q = mysqli_query($conn,"
                SELECT * FROM pengajuan_checklist
                WHERE diajukan_oleh='$id_user'
                ORDER BY id_pengajuan DESC
            ");

            while($row = mysqli_fetch_assoc($q)){
            ?>
            <tr>

                <td><?= $no++ ?></td>

                <td><?= htmlspecialchars($row['job_list']) ?></td>

                <td><?= $row['jenis'] ?></td>

                <td><?= $row['waktu'] ?></td>

                <td>
                    <?php if($row['status']=='Pending'){ ?>
                        <span class="badge bg-warning text-dark">Pending</span>
                    <?php }elseif($row['status']=='Approve'){ ?>
                        <span class="badge bg-success">Approve</span>
                    <?php }else{ ?>
                        <span class="badge bg-danger">Reject</span>
                    <?php } ?>
                </td>

                <td>
                    <?php if($row['status']=='Pending'){ ?>

                        <!-- Tombol Edit: hanya muncul jika Pending -->
                        <a href="pengajuan_checklist.php?edit=<?= $row['id_pengajuan'] ?>"
                           class="btn btn-sm btn-warning">
                            ✏️ Edit
                        </a>

                        <!-- Tombol Hapus: hanya muncul jika Pending -->
                        <a href="pengajuan_checklist.php?hapus=<?= $row['id_pengajuan'] ?>"
                           class="btn btn-sm btn-danger"
                           onclick="return confirm('Yakin ingin menghapus pengajuan ini?')">
                            🗑️ Hapus
                        </a>

                    <?php }else{ ?>

                        <!-- Sudah diproses, tidak bisa diubah -->
                        <span class="text-muted small">Tidak dapat diubah</span>

                    <?php } ?>
                </td>

            </tr>
            <?php } ?>
            </tbody>

        </table>

    </div>

</div>

</div>

<?php include 'layout/footer.php'; ?>