<?php
session_start();
include '../config/koneksi.php';

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

// Load library FPDF
require('../fpdf/fpdf.php');

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

// Judul
$pdf->SetFont('Arial','B',18);
$pdf->Cell(0,10,'DATA PENDAFTAR SPMB SMKN 1 CIKAUM',0,1,'C');

$pdf->SetFont('Arial','',12);
$pdf->Cell(0,8,'Tahun Ajaran 2026 / 2027',0,1,'C');

$pdf->Ln(5);

// Header tabel
$pdf->SetFont('Arial','B',10);

$pdf->SetFillColor(13,110,253);
$pdf->SetTextColor(255,255,255);

$pdf->Cell(10,10,'No',1,0,'C',true);
$pdf->Cell(40,10,'Nama',1,0,'C',true);
$pdf->Cell(30,10,'NISN',1,0,'C',true);
$pdf->Cell(40,10,'Asal Sekolah',1,0,'C',true);
$pdf->Cell(25,10,'Jurusan',1,0,'C',true);
$pdf->Cell(35,10,'No HP',1,0,'C',true);
$pdf->Cell(70,10,'Alamat',1,0,'C',true);
$pdf->Cell(35,10,'Tanggal',1,1,'C',true);

// Isi tabel
$pdf->SetFont('Arial','',9);

$pdf->SetTextColor(0,0,0);

$data = mysqli_query($conn,"SELECT * FROM pendaftar ORDER BY id DESC");

$no = 1;

while($d = mysqli_fetch_array($data)){

    $pdf->Cell(10,10,$no++,1,0,'C');

    $pdf->Cell(40,10,$d['nama'],1,0);

    $pdf->Cell(30,10,$d['nisn'],1,0);

    $pdf->Cell(40,10,$d['asal_sekolah'],1,0);

    $pdf->Cell(25,10,$d['jurusan'],1,0,'C');

    $pdf->Cell(35,10,$d['no_hp'],1,0);

    $pdf->Cell(70,10,substr($d['alamat'],0,40),1,0);

    $pdf->Cell(35,10,
    date('d-m-Y',strtotime($d['tanggal_daftar'])),
    1,1,'C');

}

// Footer
$pdf->Ln(10);

$pdf->SetFont('Arial','I',10);

$pdf->Cell(0,10,
'Dicetak pada: '.date('d-m-Y H:i:s'),
0,1,'R');

// Output PDF
$pdf->Output('I','Data_Pendaftar_SPMB.pdf');
?>