<?php
session_start();
if (empty($_SESSION['SESSION_USER']) && empty($_SESSION['SESSION_ID'])) {
    header('location:../../login/');
    exit;
} else {
    require_once '../../../library/sw-config.php';
    require_once '../../login/login_session.php';
    include('../../../library/sw-function.php');

    switch (@$_GET['action']) {

        /* -------  LOAD DATA PERMINTAAN PER USER ----------*/
        case 'permintaan':
            $error = [];

            // Validasi ID user (misalnya ID penanggung jawab)
            if (empty($_GET['nama'])) {
                $error[] = 'Nama penanggung jawab tidak boleh kosong';
            } else {
                $penanggung_jawab = mysqli_real_escape_string($connection, $_GET['nama']);
            }

            // Ambil bulan & tahun
            if (!empty($_POST['month']) && !empty($_POST['year'])) {
                $bulan = $_POST['month'];
                $tahun = $_POST['year'];
            } else {
                $bulan = date("m");
                $tahun = date("Y");
            }

            if (empty($error)) {
                echo '
                <div class="table-responsive">
                <table class="table table-bordered table-hover" id="swdatatable">
                    <thead>
                        <tr>
                            <th class="align-middle" width="20">No</th>
                            <th class="align-middle">Tanggal Permintaan</th>
                            <th class="align-middle">Nama Pengaju</th>
                            <th class="align-middle">Divisi Pengaju</th>
                            <th class="align-middle">Divisi Tujuan</th>
                            <th class="align-middle">Tanggal Dibutuhkan</th>
                            <th class="align-middle">Detail Permintaan</th>
                            <th class="align-middle">Status</th>
                        </tr>
                    </thead>
                    <tbody>';

                // Query ambil data permintaan berdasarkan penanggung jawab dan periode
                $query = "
                    SELECT * FROM permintaan 
                    WHERE penanggung_jawab='$penanggung_jawab' 
                    AND MONTH(tanggal_permintaan)='$bulan' 
                    AND YEAR(tanggal_permintaan)='$tahun'
                    ORDER BY tanggal_permintaan ASC";
                $result = $connection->query($query);

                if ($result && $result->num_rows > 0) {
                    $no = 1;
                    while ($row = $result->fetch_assoc()) {
                        $status = $row['status'];
                        $label = 'label-default';
                        if ($status == 'Selesai') $label = 'label-success';
                        elseif ($status == 'Sedang Dikerjakan') $label = 'label-warning';
                        elseif ($status == 'Menunggu') $label = 'label-info';
                        elseif ($status == 'Ditolak') $label = 'label-danger';

                        echo '
                        <tr>
                            <td class="text-center">' . $no++ . '</td>
                            <td>' . tgl_indo($row['tanggal_permintaan']) . '</td>
                            <td>' . htmlspecialchars($row['nama_pengaju']) . '</td>
                            <td>' . htmlspecialchars($row['divisi_pengaju']) . '</td>
                            <td>' . htmlspecialchars($row['divisi_tujuan']) . '</td>
                            <td>' . tgl_indo($row['tanggal_dibutuhkan']) . '</td>
                            <td>' . htmlspecialchars($row['detail_permintaan']) . '</td>
                            <td class="text-center"><span class="label ' . $label . '">' . htmlspecialchars($status) . '</span></td>
                        </tr>';
                    }
                } else {
                    echo '<tr><td colspan="8" class="text-center">Tidak ada data permintaan</td></tr>';
                }

                echo '
                    </tbody>
                </table>
                </div>';

                // Statistik status pekerjaan
                $stats = [
                    'Menunggu' => 0,
                    'Disetujui' => 0,
                    'Ditolak' => 0,
                    'Sedang Dikerjakan' => 0,
                    'Selesai' => 0
                ];

                $qStat = $connection->query("
                    SELECT status, COUNT(*) as jumlah 
                    FROM permintaan 
                    WHERE penanggung_jawab='$penanggung_jawab'
                    AND MONTH(tanggal_permintaan)='$bulan' 
                    AND YEAR(tanggal_permintaan)='$tahun'
                    GROUP BY status
                ");
                while ($s = $qStat->fetch_assoc()) {
                    $stats[$s['status']] = $s['jumlah'];
                }

                echo '
                <hr>
                <div class="row text-center">
                    <div class="col-md-2 col-sm-4"><p>Menunggu : <span class="label label-info">' . $stats['Menunggu'] . '</span></p></div>
                    <div class="col-md-2 col-sm-4"><p>Disetujui : <span class="label label-primary">' . $stats['Disetujui'] . '</span></p></div>
                    <div class="col-md-2 col-sm-4"><p>Ditolak : <span class="label label-danger">' . $stats['Ditolak'] . '</span></p></div>
                    <div class="col-md-3 col-sm-6"><p>Sedang Dikerjakan : <span class="label label-warning">' . $stats['Sedang Dikerjakan'] . '</span></p></div>
                    <div class="col-md-3 col-sm-6"><p>Selesai : <span class="label label-success">' . $stats['Selesai'] . '</span></p></div>
                </div>';

                // Script DataTable
                echo '
                <script>
                    $("#swdatatable").dataTable({
                        "iDisplayLength": 25,
                        "aLengthMenu": [[25, 50, -1], [25, 50, "All"]]
                    });
                </script>';
            } else {
                echo '<div class="alert alert-danger">Terjadi kesalahan: ' . implode(", ", $error) . '</div>';
            }
        break;
    }
}
?>
