<?php
require_once('tcpdf/tcpdf.php');
require_once('includes/config.php');
session_start();

// Semak admin login
if (!isset($_SESSION['id_admin'])) {
    header("Location: login_admin.php");
    exit();
}

// Ambil maklumat admin login
$id_admin = $_SESSION['id_admin'];
$admin_query = mysqli_query($conn, "SELECT nama, jawatan FROM admin WHERE id = '$id_admin'");
$admin = mysqli_fetch_assoc($admin_query);
$nama_admin = strtoupper($admin['nama']);
$jawatan_admin = strtoupper($admin['jawatan']);

// Dapatkan data merit pelajar
$sql = "
SELECT p.nama, p.no_pendaftaran, p.program, 
       SUM(CASE WHEN py.status_kehadiran = 'Hadir' THEN 5 ELSE 0 END) AS merit
FROM penyertaan py
JOIN pelajar p ON py.pelajar_id = p.id
GROUP BY p.id
ORDER BY merit DESC
";
$result = mysqli_query($conn, $sql);

// Buat PDF
class MYPDF extends TCPDF {
    public function Header() {
        // Logo HQ kiri
        $this->Image('images/logo_hq.png', 15, 8, 42);
        // Logo Kolej kanan
        $this->Image('images/logo_kolej.png', 165, 8, 32);
        $this->Ln(20);
        $this->SetFont('helvetica', 'B', 14);
        $this->Cell(0, 8, 'LAPORAN MARKAH MERIT PELAJAR', 0, 1, 'C');
        $this->Ln(5);
    }
    public function Footer() {
        $this->SetY(-15);
        $this->SetFont('helvetica', 'I', 8);
        $this->Cell(0, 10, 'Halaman '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, 0, 'C');
    }
}

$pdf = new MYPDF();
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('e-SPEKTRA');
$pdf->SetTitle('Laporan Markah Merit Pelajar');
$pdf->SetMargins(15, 35, 15);
$pdf->SetAutoPageBreak(TRUE, 20);
$pdf->AddPage();
$pdf->SetFont('helvetica', '', 10);

// Lebar setiap column
$w = array(10, 55, 35, 60, 15); // Bil, Nama, No Pendaftaran, Program, Merit

// Header Table
$pdf->SetFillColor(200, 200, 200);
$pdf->SetFont('helvetica', 'B', 10);
$headers = array('Bil', 'Nama', 'No. Pendaftaran', 'Program', 'Merit');
for ($i = 0; $i < count($headers); $i++) {
    $pdf->MultiCell($w[$i], 8, $headers[$i], 1, 'C', 1, 0, '', '', true);
}
$pdf->Ln();

// Isi Table
$pdf->SetFont('helvetica', '', 10);
$bil = 1;
while ($row = mysqli_fetch_assoc($result)) {
    // Tentukan tinggi maksimum untuk baris ni
    $nb1 = $pdf->getNumLines($bil, $w[0]);
    $nb2 = $pdf->getNumLines($row['nama'], $w[1]);
    $nb3 = $pdf->getNumLines($row['no_pendaftaran'], $w[2]);
    $nb4 = $pdf->getNumLines($row['program'], $w[3]);
    $nb5 = $pdf->getNumLines($row['merit'], $w[4]);
    $h = max($nb1, $nb2, $nb3, $nb4, $nb5) * 6; // Tinggi baris

    $pdf->MultiCell($w[0], $h, $bil, 1, 'C', 0, 0, '', '', true);
    $pdf->MultiCell($w[1], $h, strtoupper($row['nama']), 1, 'L', 0, 0, '', '', true);
    $pdf->MultiCell($w[2], $h, $row['no_pendaftaran'], 1, 'C', 0, 0, '', '', true);
    $pdf->MultiCell($w[3], $h, strtoupper($row['program']), 1, 'L', 0, 0, '', '', true);
    $pdf->MultiCell($w[4], $h, $row['merit'], 1, 'C', 0, 1, '', '', true);

    $bil++;
}

// Ruang kosong sebelum tandatangan
$pdf->Ln(10);
$pdf->SetFont('helvetica', '', 10);
$pdf->Cell(0, 6, 'Disediakan Oleh:', 0, 1);
$pdf->Ln(15);

$pdf->SetFont('helvetica', 'B', 11);
$pdf->Cell(0, 6, $nama_admin, 0, 1);
$pdf->SetFont('helvetica', '', 10);
$pdf->Cell(0, 6, $jawatan_admin, 0, 1);
$pdf->Cell(0, 6, 'Tarikh: '.date('d/m/Y'), 0, 1);

$pdf->Output('laporan_merit.pdf', 'I');
?>
