<?php
session_start();
require('../fpdf/fpdf.php');
include '../config/database.php';

$id = $_GET['id'];
$db = new Database(); $conn = $db->getConnection();
$stmt = $conn->prepare("SELECT j.*, g.nama_gereja FROM jemaat j LEFT JOIN gereja g ON j.gereja_id=g.id WHERE j.id=?");
$stmt->execute([$id]);
$d = $stmt->fetch(PDO::FETCH_ASSOC);

if(!$d) die('Data jemaat tidak ditemukan');

class PDF extends FPDF {
    public $data;
    
    function SetData($data){
        $this->data = $data;
    }
    
    function Card(){
        // Posisi kartu di tengah A4
        $x = 62; // margin kiri biar center
        $y = 100; // margin atas biar center
        $w = 86; // Lebar KTP
        $h = 54; // Tinggi KTP
        
        // Border luar kartu
        $this->SetDrawColor(0,80,180);
        $this->SetLineWidth(0.6);
        $this->Rect($x,$y,$w,$h);
        
        // Header biru
        $this->SetFillColor(0,80,180);
        $this->Rect($x,$y,$w,14,'F');
        
        // Kotak putih logo
        $this->SetFillColor(255,255,255);
        $this->Rect($x+3,$y+2,12,10,'F');
        $this->Image('../assets/img/logo_gbi.png',$x+3.5,$y+2.2,11);
        
        // Judul
        $this->SetTextColor(255,255,255);
        $this->SetFont('Arial','B',9);
        $this->SetXY($x+16,$y+2.5);
        $this->Cell($w-18,5,'KARTU ANGGOTA JEMAAT',0,1,'C');
        $this->SetFont('Arial','B',9);
        $this->SetX($x+16);
        $this->Cell($w-18,5,'GBI '.strtoupper($this->data['nama_gereja']),0,1,'C');
        
        // Isi data
        $this->SetTextColor(0,0,0);
        $this->SetFont('Arial','',9);
        $yData = $y+17;
        
        $this->SetXY($x+3,$yData);
        $this->Cell(22,5,'No. Anggota',0,0); $this->Cell(3,5,':',0,0); $this->Cell(55,5,$this->data['no_anggota'],0,1);
        $this->SetX($x+3);
        $this->Cell(22,5,'Nama',0,0); $this->Cell(3,5,':',0,0); $this->SetFont('Arial','B',9); $this->Cell(55,5,$this->data['nama_lengkap'],0,1);
        $this->SetFont('Arial','',9); $this->SetX($x+3);
        $this->Cell(22,5,'Tgl Lahir',0,0); $this->Cell(3,5,':',0,0); $this->Cell(55,5,date('d-m-Y',strtotime($this->data['tgl_lahir'])),0,1);
        $this->SetX($x+3);
        $this->Cell(22,5,'Alamat',0,0); $this->Cell(3,5,':',0,0); $this->MultiCell(55,5,substr($this->data['alamat'],0,80),0,1);
        $this->SetX($x+3);
        $this->Cell(22,5,'Baptis',0,0); $this->Cell(3,5,':',0,0); $this->Cell(55,5,$this->data['status_baptis'],0,1);
        
        // TTD
        $this->SetXY($x+3,$y+42);
        $this->SetFont('Arial','',8);
        $this->Cell(40,4,'Gembala,',0,1,'C');
        $this->SetXY($x+3,$y+48);
        $this->SetFont('Arial','BU',8);
        $this->Cell(40,4,$_SESSION['nama'],0,1,'C');
        
        // Footer
        $this->SetXY($x+45,$y+48);
        $this->SetFont('Arial','I',6);
        $this->SetTextColor(120,120,120);
        $this->Cell(38,4,'Berlaku selama menjadi jemaat',0,1,'R');
        
        // Garis potong
        $this->SetDrawColor(200,200,200);
        $this->SetLineWidth(0.1);
        $this->Line($x-5,$y,$x-2,$y); // kiri atas
        $this->Line($x+$w+2,$y,$x+$w+5,$y); // kanan atas
        $this->Line($x-5,$y+$h,$x-2,$y+$h); // kiri bawah
        $this->Line($x+$w+2,$y+$h,$x+$w+5,$y+$h); // kanan bawah
        $this->Line($x,$y-5,$x,$y-2); // atas kiri
        $this->Line($x+$w,$y-5,$x+$w,$y-2); // atas kanan
        $this->Line($x,$y+$h+2,$x,$y+$h+5); // bawah kiri
        $this->Line($x+$w,$y+$h+2,$x+$w,$y+$h+5); // bawah kanan
    }
}

$pdf = new PDF('P','mm','A4'); // Pake A4 biar gede
$pdf->SetData($d);
$pdf->AddPage();
$pdf->Card();

// Instruksi print
$pdf->SetFont('Arial','',9);
$pdf->SetXY(62,160);
$pdf->Cell(86,5,'Gunting sesuai garis potong abu-abu',0,1,'C');
$pdf->SetX(62);
$pdf->Cell(86,5,'Ukuran kartu: 86 x 54 mm - Standar KTP',0,1,'C');

$pdf->Output('I','Kartu_Anggota_'.$d['no_anggota'].'.pdf');
?>