<?php
$judul = "Persetujuan Checklist";
$menu  = "persetujuan";

include 'layout/header.php';
include 'layout/sidebar.php';

// --- LOGIKA PHP ---
if(isset($_GET['approve'])){

    $id = (int)$_GET['approve'];

    $q = mysqli_query($conn,"
        SELECT
            p.*,
            u.role
        FROM pengajuan_checklist p
        LEFT JOIN users u
            ON p.diajukan_oleh = u.id_user
        WHERE p.id_pengajuan='$id'
    ");

    $row = mysqli_fetch_assoc($q);

    if($row){

        mysqli_query($conn,"
            INSERT INTO checklist_master
            (
                job_list,
                job_desc,
                waktu,
                jenis,
                jabatan,
                status
            )
            VALUES
            (
                '".$row['job_list']."',
                '".$row['job_desc']."',
                '".$row['waktu']."',
                '".$row['jenis']."',
                '".$row['role']."',
                'aktif'
            )
        ");

        $id_checklist = mysqli_insert_id($conn);

        mysqli_query($conn,"
            INSERT INTO checklist_pic
            (
                id_checklist,
                jabatan
            )
            VALUES
            (
                '$id_checklist',
                '".$row['role']."'
            )
        ");

        mysqli_query($conn,"
            UPDATE pengajuan_checklist
            SET status='Approve'
            WHERE id_pengajuan='$id'
        ");

        echo "
        <script>
            alert('Checklist disetujui');
            location='persetujuan_checklist.php';
        </script>";
        exit;
    }
}

if(isset($_GET['reject'])){

    $id = (int)$_GET['reject'];

    // Update status pengajuan
    mysqli_query($conn,"
        UPDATE pengajuan_checklist
        SET status='Reject'
        WHERE id_pengajuan='$id'
    ");

    // Simpan log reject (opsional)
    $user = $_SESSION['nama'] ?? 'Direktur';

    mysqli_query($conn,"
        INSERT INTO log_approval
        (
            id_pengajuan,
            aksi,
            user,
            waktu
        )
        VALUES
        (
            '$id',
            'Reject',
            '$user',
            NOW()
        )
    ");

    echo "
    <script>
        alert('Checklist berhasil ditolak');
        location='persetujuan_checklist.php';
    </script>";
    exit;
}

$data = mysqli_query($conn,"SELECT p.*, u.nama FROM pengajuan_checklist p LEFT JOIN users u ON p.diajukan_oleh=u.id_user ORDER BY p.id_pengajuan DESC");
?>

<style>

.page-header{
    display:flex;
    justify-content:space-between;
    align-items:center;
    margin-bottom:20px;
}

.page-title{
    font-size:1.25rem;
    font-weight:700;
    color:var(--text-dark);
}

.card-content{
    background:#fff;
    border:1px solid var(--green-100);
    border-radius:16px;
    overflow:hidden;
    box-shadow:var(--shadow-sm);
}

.table-responsive{
    width:100%;
    overflow-x:auto;
}

.table{
    width:100%;
    border-collapse:collapse;
    min-width:1000px;
}

.table thead{
    background:var(--green-50);
}

.table th{
    padding:14px 16px;
    font-size:.72rem;
    text-transform:uppercase;
    letter-spacing:.05em;
    color:var(--green-700);
    border-bottom:1px solid var(--green-100);
    white-space:nowrap;
}

.table td{
    padding:14px 16px;
    border-bottom:1px solid #eefcf2;
    color:var(--text-dark);
    vertical-align:middle;
}

.table tbody tr:hover{
    background:#f9fffb;
}

.badge{
    display:inline-block;
    padding:4px 10px;
    border-radius:999px;
    font-size:.72rem;
    font-weight:700;
}

.badge-pending{
    background:#fef3c7;
    color:#92400e;
}

.badge-approve{
    background:#dcfce7;
    color:#166534;
}

.badge-reject{
    background:#fee2e2;
    color:#991b1b;
}

.btn-action{
    display:inline-block;
    padding:7px 12px;
    border-radius:8px;
    text-decoration:none;
    font-size:.75rem;
    font-weight:600;
    margin-right:5px;
    transition:.2s;
}

.btn-approve{
    background:#16a34a;
    color:#fff;
}

.btn-approve:hover{
    background:#15803d;
}

.btn-reject{
    background:#dc2626;
    color:#fff;
}

.btn-reject:hover{
    background:#b91c1c;
}

.status-done{
    color:var(--text-muted);
    font-size:.8rem;
}

.empty-data{
    text-align:center;
    padding:30px;
    color:var(--text-muted);
}

</style>

<div class="page-header">
    <div class="page-title">
        📋 Persetujuan Checklist
    </div>
</div>

<div class="card-content">

    <div class="table-responsive">

        <table class="table">

            <thead>
                <tr>
                    <th>No</th>
                    <th>Job List</th>
                    <th>Job Desc</th>
                    <th>Jenis</th>
                    <th>Waktu</th>
                    <th>Diajukan Oleh</th>
                    <th>Status</th>
                    <th>Aksi</th>
                </tr>
            </thead>

            <tbody>

            <?php if(mysqli_num_rows($data) > 0): ?>

                <?php $no=1; while($row=mysqli_fetch_assoc($data)): ?>

                <tr>

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

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

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

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

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

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

                    <td>

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

                            <span class="badge badge-pending">
                                Pending
                            </span>

                        <?php elseif($row['status']=='Approve'): ?>

                            <span class="badge badge-approve">
                                Approved
                            </span>

                        <?php else: ?>

                            <span class="badge badge-reject">
                                Rejected
                            </span>

                        <?php endif; ?>

                    </td>

                    <td>

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

                            <a href="?approve=<?= $row['id_pengajuan'] ?>"
                               class="btn-action btn-approve"
                               onclick="return confirm('Setujui checklist ini?')">
                               Approve
                            </a>

                            <a href="?reject=<?= $row['id_pengajuan'] ?>"
                               class="btn-action btn-reject"
                               onclick="return confirm('Tolak checklist ini?')">
                               Reject
                            </a>

                        <?php else: ?>

                            <span class="status-done">
                                Sudah Diproses
                            </span>

                        <?php endif; ?>

                    </td>

                </tr>

                <?php endwhile; ?>

            <?php else: ?>

                <tr>
                    <td colspan="8" class="empty-data">
                        Belum ada pengajuan checklist
                    </td>
                </tr>

            <?php endif; ?>

            </tbody>

        </table>

    </div>

</div>

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