<?php
$judul = "Edit Checklist";
$menu  = "checklist";

include 'layout/header.php';

$id = (int)($_GET['id'] ?? 0);

if($id == 0){
    echo "<script>alert('ID tidak valid'); window.location='checklist.php';</script>";
    exit;
}

$data = mysqli_fetch_assoc(mysqli_query($conn,"
    SELECT * FROM checklist_master WHERE id_checklist='$id'
"));

if(!$data){
    echo "<script>alert('Data tidak ditemukan'); window.location='checklist.php';</script>";
    exit;
}

// Ambil jabatan yang sudah dipilih
$q_pic = mysqli_query($conn,"SELECT jabatan FROM checklist_pic WHERE id_checklist='$id'");
$selected_jabatan = [];
while($p = mysqli_fetch_assoc($q_pic)){
    $selected_jabatan[] = $p['jabatan'];
}

// Daftar semua jabatan yang tersedia
$semua_jabatan = [
    'Store_Manager',
    'Kadiv_gudang',
    'kadiv_ops',
    'kadiv_cs',
    'kadiv_kasir',
];

if(isset($_POST['update'])){

    $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']);

    // Gabungkan jabatan menjadi string
    $jabatan_list = '';

    if(isset($_POST['role']) && is_array($_POST['role'])){
        $jabatan_list = implode(',', $_POST['role']);
    }

    // Update checklist master
    mysqli_query($conn,"
        UPDATE checklist_master
        SET
            job_list = '$job_list',
            job_desc = '$job_desc',
            waktu    = '$waktu',
            jenis    = '$jenis',
            jabatan  = '$jabatan_list'
        WHERE id_checklist = '$id'
    ");

    // Hapus PIC lama
    mysqli_query($conn,"
        DELETE FROM checklist_pic
        WHERE id_checklist='$id'
    ");

    // Simpan PIC baru
    if(isset($_POST['role']) && is_array($_POST['role'])){

        foreach($_POST['role'] as $role){

            $role = mysqli_real_escape_string($conn,$role);

            mysqli_query($conn,"
                INSERT INTO checklist_pic
                (
                    id_checklist,
                    jabatan
                )
                VALUES
                (
                    '$id',
                    '$role'
                )
            ");

        }

    }

    echo "
    <script>
        alert('Checklist berhasil diupdate');
        window.location='checklist.php';
    </script>
    ";
    exit;
}
?>

<div class="content">
    <div class="topbar d-flex align-items-center px-4">
        <h5 class="mb-0 fw-bold">Edit Checklist</h5>
    </div>

    <div class="container-fluid p-4">

        <div class="row justify-content-center">
            <div class="col-lg-7">

                <div class="card border-0 shadow-sm rounded-3">
                    <div class="card-header bg-white border-bottom py-3 px-4">
                        <div class="d-flex align-items-center gap-2">
                            <span style="font-size:20px;">✏️</span>
                            <div>
                                <h6 class="mb-0 fw-bold">Edit Checklist</h6>
                                <small class="text-muted">ID #<?= $id ?></small>
                            </div>
                        </div>
                    </div>

                    <div class="card-body p-4">
                        <form method="POST">

                            <!-- Job List -->
                            <div class="mb-3">
                                <label class="form-label fw-semibold">Job List <span class="text-danger">*</span></label>
                                <input type="text" name="job_list" class="form-control"
                                       value="<?= htmlspecialchars($data['job_list']) ?>"
                                       placeholder="Nama checklist..." required>
                            </div>

                            <!-- Job Desc -->
                            <div class="mb-3">
                                <label class="form-label fw-semibold">Job Desc</label>
                                <textarea name="job_desc" class="form-control" rows="3"
                                          placeholder="Deskripsi pekerjaan (opsional)..."><?= htmlspecialchars($data['job_desc'] ?? '') ?></textarea>
                            </div>

                            <!-- Waktu & Jenis -->
                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label class="form-label fw-semibold">Waktu</label>
                                    <input type="text" name="waktu" class="form-control"
                                           value="<?= htmlspecialchars($data['waktu'] ?? '') ?>"
                                           placeholder="cth: 08:00 - 09:00">
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label class="form-label fw-semibold">Jenis <span class="text-danger">*</span></label>
                                    <select name="jenis" class="form-select" required>
                                        <option value="">-- Pilih Jenis --</option>
                                        <?php
                                        $jenis_list = ['daily','weekly','monthly'];
                                        foreach($jenis_list as $j):
                                        ?>
                                        <option value="<?= $j ?>"
                                            <?= (strtolower($data['jenis']) == $j) ? 'selected' : '' ?>>
                                            <?= ucfirst($j) ?>
                                        </option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                            </div>

                            <!-- PIC / Jabatan -->
                            <div class="mb-4">
                                <label class="form-label fw-semibold">PIC (Jabatan yang Bertanggung Jawab)</label>
                                <div class="border rounded-3 p-3" style="background:#f8f9fa;">
                                    <?php foreach($semua_jabatan as $jab): ?>
                                    <div class="form-check mb-1">
                                        <input class="form-check-input" type="checkbox"
                                               name="role[]"
                                               value="<?= $jab ?>"
                                               id="jab_<?= $jab ?>"
                                               <?= in_array($jab, $selected_jabatan) ? 'checked' : '' ?>>
                                        <label class="form-check-label" for="jab_<?= $jab ?>">
                                            <?= htmlspecialchars($jab) ?>
                                        </label>
                                    </div>
                                    <?php endforeach; ?>
                                </div>
                            </div>

                            <!-- Tombol -->
                            <div class="d-flex gap-2">
                                <button type="submit" name="update" class="btn btn-success px-4 fw-semibold">
                                    💾 Simpan Perubahan
                                </button>
                                <a href="checklist.php" class="btn btn-outline-secondary px-4">
                                    ✕ Batal
                                </a>
                            </div>

                        </form>
                    </div>
                </div>

            </div>
        </div>

    </div>
</div>

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