<?php
require_once __DIR__ . '/../init.php';
require_once PROJECT_ROOT . '/backend/controllers/common_function.php';
require_once PROJECT_ROOT . '/backend/controllers/db_select_helper.php';

class KetidakhadiranRequestController
{
    private $conn;
    private $table = "
        ketidakhadiran_request R
        LEFT JOIN anggota A ON R.IDAnggota = A.IDAnggota
        LEFT JOIN ketidakhadiran_tipe T ON R.IDTipeKetidakhadiran = T.IDTipeKetidakhadiran
    ";
    private $pk = "IDKetidakhadiranRequest";

    protected $displayedColumn = [
        'R.IDKetidakhadiranRequest',
        'R.IDPerusahaan',
        'R.KodeKetidakhadiranRequest',
        'R.CounterNomor',
        'R.IDAnggota',
        'A.KodeAnggota',
        'A.NamaAnggota',
        'R.Tanggal',
        'R.TanggalAkhir',
        'R.JumlahHari',
        'R.IDTipeKetidakhadiran',
        'T.TipeKetidakhadiran',
        'T.MaksimalHari',
        'R.Attachment',
        'R.Keterangan',
        'R.Approval',
        'R.ApprovalRemarks',
        'R.ApprovalBy',
        'R.ApprovalAt',
        'R.CreateBy',
        'R.CreateAt',
        'R.LastUpdateBy',
        'R.LastUpdateAt'
    ];

    public function __construct($conn)
    {
        $this->conn = $conn;
    }

    public function select($post)
    {
        return runSelect(
            $this->conn,
            $this->table,
            $this->pk,
            $this->displayedColumn,
            $post
        );
    }

    public function getSummary($post)
    {
        try {
            $conn = $this->conn;

            if (empty($post['IDAnggota'])) {
                throw new Exception("Parameter tidak lengkap");
            }

            $TanggalAwal = $post['TanggalAwal'];
            $TanggalAkhir = $post['TanggalAkhir'];
            $idAnggota = intval($post['IDAnggota']);

            $sql = "
            SELECT 
                COUNT(*) AS totalKetidakhadiranRequest,
                SUM(CASE WHEN kr.Approval = 0 THEN 1 ELSE 0 END) AS totalWaiting,
                SUM(CASE WHEN kr.Approval = 1 THEN 1 ELSE 0 END) AS totalApproved,
                SUM(CASE WHEN kr.Approval = 2 THEN 1 ELSE 0 END) AS totalRejected
            FROM ketidakhadiran_request kr
            INNER JOIN anggota a ON a.IDAnggota = kr.IDAnggota
            INNER JOIN ketidakhadiran_tipe kt ON kt.IDTipeKetidakhadiran = kr.IDTipeKetidakhadiran
            WHERE 
                kr.IDAnggota = ?
                AND DATE(kr.Tanggal) >= DATE(?)
                AND DATE(kr.TanggalAkhir) <= DATE(?)
        ";

            $stmt = $conn->prepare($sql);
            if (!$stmt) {
                throw new Exception("Prepare gagal: " . $conn->error);
            }

            $stmt->bind_param("iss", $idAnggota, $TanggalAwal, $TanggalAkhir);
            $stmt->execute();
 

            $result = $stmt->get_result();
            if (!$result) {
                throw new Exception("Eksekusi gagal: " . $stmt->error);
            }

            $row = $result->fetch_assoc();

            $stmt->close();

            return [
                "Status" => 1,
                "Keterangan" => "",
                "list_data_summary" => [
                    "totalKetidakhadiranRequest" => intval($row['totalKetidakhadiranRequest']),
                    "totalWaiting" => intval($row['totalWaiting']),
                    "totalApproved" => intval($row['totalApproved']),
                    "totalRejected" => intval($row['totalRejected']),
                ],
            ];


        } catch (Exception $e) {
            return [
                "Status" => 0,
                "Keterangan" => $e->getMessage(),
                "list_data_summary" => [],
            ];
        }
    }
}
?>