<?php
require('includes/fpdf.php');
require('includes/config.php');

class PDF extends FPDF {
    // Fungsi untuk watermark separa lutsinar
    function RotatedImage($file,$x,$y,$w,$h,$angle){
        $this->Rotate($angle,$x+$w/2,$y+$h/2);
        $this->Image($file,$x,$y,$w,$h);
        $this->Rotate(0);
    }

    function SetAlpha($alpha){
        $extgstates = [];
        $extgstates['ca'] = $alpha; // fill
        $extgstates['CA'] = $alpha; // stroke
        $this->extgstates = $extgstates;
    }
}

$pelajar_id = isset($_GET['pelajar_id']) ? (int)$_GET['pelajar_id'] : 0;
$aktiviti_id = isset($_GET['aktiviti_id']) ? (int)$_GET['aktiviti_id'] : 0;
if(!$pelajar_id || !$aktiviti_id) die("Parameter tidak lengkap.");

// Data pelajar & aktiviti
$pelajar = $conn->query("SELECT nama, no_ic FROM pelajar WHERE id=$pelajar_id")->fetch_assoc();
$aktiviti = $conn->query("SELECT nama_aktiviti, tarikh_mula, tarikh_tamat, tempat FROM aktiviti WHERE id=$aktiviti_id")->fetch_assoc();
$tarikh = $aktiviti['tarikh_mula']==$aktiviti['tarikh_tamat'] 
          ? date('d/m/Y',strtotime($aktiviti['tarikh_mula'])) 
          : date('d/m/Y',strtotime($aktiviti['tarikh_mula'])).' - '.date('d/m/Y',strtotime($aktiviti['tarikh_tamat']));

$pdf = new PDF('L','mm','A4');
$pdf->AddPage();

// Background warna lembut
$pdf->SetFillColor(245,245,255);
$pdf->Rect(0,0,297,210,'F');

// Frame luar & dalam
$pdf->SetDrawColor(59,10,119);
$pdf->SetLineWidth(2);
$pdf->Rect(10,10,277,190);
$pdf->SetLineWidth(0.5);
$pdf->Rect(12,12,273,186);

// Logo watermark separa lutsinar di tengah
//$pdf->SetAlpha(0.03); // 10% opacity
//$pdf->Image('images/logo_kolej.png',70,40,160,0);
//$pdf->SetAlpha(2); // reset alpha

// Logo atas kiri & kanan
$pdf->Image('images/logo_hq.png',20,15,59);
$pdf->Image('images/logo_kolej.png',222,15,55);

// Tajuk
$pdf->SetFont('Times','B',36);
$pdf->SetTextColor(59,10,119);
$pdf->Ln(20);
$pdf->Cell(0,20,'SIJIL PENGHARGAAN',0,1,'C');

// Nama pelajar
$pdf->SetFont('Times','B',22);
$pdf->SetTextColor(0,0,0);
$pdf->Cell(0,12,'Diberikan Kepada:',0,1,'C');
$pdf->Cell(0,12,$pelajar['nama'],0,1,'C');

// No.KP
$pdf->SetFont('Times','B',22);
$pdf->Cell(0,8,'No.KP: '.$pelajar['no_ic'],0,1,'C');

// Aktiviti
$pdf->Ln(5);
$pdf->SetFont('Times','B',20);
$pdf->Cell(0,10,'Kerana Telah Menyertai:',0,1,'C');
$pdf->Ln(2);
$pdf->SetFont('Times','B',24);
$pdf->Cell(0,10,$aktiviti['nama_aktiviti'],0,1,'C');
$pdf->Ln(2);
$pdf->SetFont('Times','B',16);
$pdf->Cell(0,8,'Tarikh : '.$tarikh,0,1,'C');
$pdf->Cell(0,8,'Bertempat di: '.$aktiviti['tempat'],0,1,'C');

// Tandatangan pengarah
$ttd_width = 35;
$ttd_x = ($pdf->GetPageWidth()-$ttd_width)/2;
$ttd_y = 145;
$pdf->Image('images/ttd_pengarah.png',$ttd_x,$ttd_y,$ttd_width);

// PENGARAH & KOLEJ
$pdf->SetY($ttd_y + 25);
$pdf->SetFont('Times','B',14);
$pdf->Cell(0,7,'PENGARAH',0,1,'C');
$pdf->Cell(0,7,'KOLEJ KOMUNITI SEBERANG JAYA',0,1,'C');

// Output PDF
$pdf->Output('I','Sijil_'.$pelajar['nama'].'.pdf');
?>
