<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

require "../config.php";
require_once __DIR__ . '/dompdf/autoload.inc.php';

use Dompdf\Dompdf;
use Dompdf\Options;

// =====================
// VALIDASI INPUT
// =====================
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

$q = $conn->query("SELECT * FROM pengajuan WHERE id=$id");
$d = $q->fetch_assoc();

if (!$d) {
    die("Data tidak ditemukan");
}

// =====================
// FUNGSI AMAN
// =====================
function e($str){
    return htmlspecialchars($str ?? '', ENT_QUOTES, 'UTF-8');
}

// =====================
// SETTING DOMPDF
// =====================
$options = new Options();
$options->set('isHtml5ParserEnabled', true);
$options->set('isPhpEnabled', true);

$dompdf = new Dompdf($options);

// =====================
// AUTO DETECT PATH 🔥
// =====================
$root = $_SERVER['DOCUMENT_ROOT'];

$paths = [
    $root . "/petesa/uploads/",
    $root . "/petesa/",
    $root . "/uploads/",
];

// fungsi cari file
function findFile($filename, $paths){
    foreach ($paths as $p) {
        if (!empty($filename) && file_exists($p . $filename)) {
            return $p . $filename;
        }
    }
    return '';
}

// ambil file real
$file1 = findFile($d['file_surat'], $paths);
$file2 = findFile($d['file_identitas'], $paths);

// =====================
// BASE64 (PASTI MUNCUL)
// =====================
function getBase64($file){
    if ($file && file_exists($file)) {
        $type = pathinfo($file, PATHINFO_EXTENSION);
        return 'data:image/'.$type.';base64,'.base64_encode(file_get_contents($file));
    }
    return '';
}

$img1 = getBase64($file1);
$img2 = getBase64($file2);

// =====================
// HTML
// =====================
$html = '
<style>
@page { size:A4; margin:2cm; }
body { 
    font-family: Arial; 
    font-size: 12px; 
    text-align: center; 
}

h3, p {
    text-align: center;
}

table {
    width: 100%;
    border-collapse: collapse;
    margin: 10px auto;
}

td {
    border: 1px solid #000;
    padding: 6px;
    text-align: left; /* ⬅️ tetap kiri */
}

.img {
    display: block;
    margin: 10px auto;
    max-width: 90%;
    max-height: 400px;
}

.label {
    width: 30%;
    font-weight: bold;
}
.label { width:30%; font-weight:bold; }
</style>

<h3>DETAIL PERMOHONAN</h3>

<p style="text-align:center;"><b>Surat Permohonan</b></p>
';

// =====================
// GAMBAR 1
// =====================
if ($img1) {
    $html .= '<img class="img" src="'.$img1.'">';
} else {
    $html .= '<p style="text-align:center;color:red;">File tidak ditemukan</p>';
}

// =====================
// DATA
// =====================
$html .= '

<h3>DETAIL IDENTITAS</h3>

<table>
<tr><td class="label">Nama</td><td>'.e($d['nama']).'</td></tr>
<tr><td class="label">No HP</td><td>'.e($d['no_hp']).'</td></tr>
<tr><td class="label">Email</td><td>'.e($d['email']).'</td></tr>
<tr><td class="label">Instansi</td><td>'.e($d['instansi']).'</td></tr>
<tr><td class="label">Pekerjaan</td><td>'.e($d['pekerjaan']).'</td></tr>
<tr><td class="label">Alamat</td><td>'.e($d['alamat']).'</td></tr>
<tr><td class="label">Kota</td><td>'.e($d['kota']).'</td></tr>
<tr><td class="label">NIK</td><td>'.e($d['nik']).'</td></tr>
<tr><td class="label">Jenis</td><td>'.e($d['jenis_permohonan']).'</td></tr>
<tr><td class="label">Tujuan</td><td>'.e($d['tujuan']).'</td></tr>
<tr><td class="label">Tanggal</td><td>'.e($d['tanggal_permohonan']).'</td></tr>
</table>

<p style="text-align:center;"><b>File Identitas</b></p>
';

// =====================
// GAMBAR 2
// =====================
if ($img2) {
    $html .= '<img class="img" src="'.$img2.'">';
} else {
    $html .= '<p style="text-align:center;color:red;">File tidak ditemukan</p>';
}

// =====================
// OUTPUT
// =====================
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();

$dompdf->stream("Detail_Permohonan.pdf", ["Attachment" => true]);