<?php
require_once __DIR__ . '/config/config.php';
require_once __DIR__ . '/vendor/autoload.php';

use Dompdf\Dompdf;
use Dompdf\Options;

if(empty($_POST['id_kw'])){
    die("Tidak ada kwitansi dipilih");
}

$pajak = $_POST['pajak'] ?? 'off';
$ids   = implode(",", array_map('intval', $_POST['id_kw']));
$q = $conn->query("SELECT * FROM kwitansi WHERE id_kw IN ($ids)");


/* ================= TERBILANG ================= */
function terbilang($x){
    $x = abs((int)$x);

    $bil = array(
        "",
        "satu",
        "dua",
        "tiga",
        "empat",
        "lima",
        "enam",
        "tujuh",
        "delapan",
        "sembilan",
        "sepuluh",
        "sebelas"
    );

    if ($x < 12)
        return " " . $bil[$x];
    elseif ($x < 20)
        return terbilang($x - 10) . " belas";
    elseif ($x < 100)
        return terbilang($x / 10) . " puluh" . terbilang($x % 10);
    elseif ($x < 200)
        return " seratus" . terbilang($x - 100);
    elseif ($x < 1000)
        return terbilang($x / 100) . " ratus" . terbilang($x % 100);
    elseif ($x < 2000)
        return " seribu" . terbilang($x - 1000);
    elseif ($x < 1000000)
        return terbilang($x / 1000) . " ribu" . terbilang($x % 1000);
    elseif ($x < 1000000000)
        return terbilang($x / 1000000) . " juta" . terbilang($x % 1000000);
    elseif ($x < 1000000000000)
        return terbilang($x / 1000000000) . " miliar" . terbilang($x % 1000000000);
    else
        return "terlalu besar";
}

function tgl_indo($tgl){
    $b = [1=>"Januari","Februari","Maret","April","Mei","Juni","Juli","Agustus","September","Oktober","November","Desember"];
    $p = explode("-", $tgl);
    return $p[2]." ".$b[(int)$p[1]]." ".$p[0];
}

/* ================= HTML ================= */
$html = "
<style>
body{ font-family:'Times New Roman'; font-size:12px; }
.kwitansi{
    page-break-after: always;
    page-break-inside: avoid;
}
.kwitansi:last-child{
    page-break-after: auto;
}
h3{ text-align:center; margin:0; }
.line{ border-bottom:2px solid #000; margin:6px 0 10px; }
.tbl{ width:100%; border-collapse:collapse; }
.tbl td{ padding:4px; vertical-align:top; }
.box{ border:1px solid #000; padding:5px; }

/* ===== TTD ===== */
.ttd-bos{
    width:100%;
    border-collapse:collapse;
    margin-top:10px;
    border-top:2px solid #000;
    border-bottom:2px solid #000;
    page-break-inside: avoid;
}
.ttd-bos td{
    border-left:1px solid #000;
    border-right:1px solid #000;
    text-align:center;
    padding:6px;
}
.ttd-judul{ height:55px; }
.ttd-jarak{ height:90px; }
.nama{ font-weight:bold; text-decoration:underline; }
.ttd-garis-tengah td{ border-bottom:1px solid #000; padding:0; height:0; }
</style>
";

while($d = $q->fetch_assoc()){

    $nilai = $d['pengeluaran'];
    if($pajak == 'on'){
        $ppn   = $nilai * 0.12;
        $total = $nilai + $ppn;
    } else {
        $ppn = 0;
        $total = $nilai;
    }

$html .= "
<div class='kwitansi'>
<h3>BUKTI PENGELUARAN</h3>
<div class='line'></div>

<table class='tbl'>
<tr>
<td width='20%'>Nomor Bukti</td><td>: {$d['nobukti']}</td>
<td width='20%'>Tanggal</td><td>: ".tgl_indo($d['tanggal'])."</td>
</tr>
</table>
<div class='line'></div>

<table class='tbl'>
<tr><td width='30%'>Sudah Terima Dari</td><td>: <b>BENDAHARA SD NEGERI 1 TLOGOSARI</b></td></tr>
<tr><td>Banyaknya Uang</td><td>: <b>Rp ".number_format($total,0,',','.')."</b></td></tr>
<tr><td>Terbilang</td><td class='box'>".ucwords(trim(terbilang($total)))." rupiah</td></tr>
</table>
";
$html .= "
<table class='tbl'>
<tr>
  <td width='25%'>Untuk Pembayaran</td>
  <td width='56%'>
    {$d['kode_kegiatan']}<br>
    {$d['kode_rekening']}<br><br>
    {$d['uraian']} &nbsp;&nbsp; {$d['jumb']} {$d['satuanb']}
  </td>
</tr>
</table>

<br>
";



if($pajak == 'on'){
$html .= "
<table class='tbl'>
<tr><td width='30%'>Nilai Sebelum Pajak</td><td>: Rp ".number_format($nilai,0,',','.')."</td></tr>
<tr><td>PPN 12%</td><td>: Rp ".number_format($ppn,0,',','.')."</td></tr>
<tr><td><b>Total</b></td><td>: <b>Rp ".number_format($total,0,',','.')."</b></td></tr>
</table>
";
}

$html .= "
<table class='ttd-bos'>
<tr>
<td class='ttd-judul'>Menyetujui,<br>Kepala Sekolah<br>SD NEGERI 1 TLOGOSARI</td>
<td class='ttd-judul'>Dibayar lunas tgl,<br>".tgl_indo($d['tanggal'])."<br>Oleh Bendahara BOS</td>
<td class='ttd-judul'><br>Yang Menerima</td>
</tr>

<tr class='ttd-garis-tengah'><td colspan='3'></td></tr>

<tr>
<td class='ttd-jarak'></td>
<td class='ttd-jarak'></td>
<td class='ttd-jarak'></td>
</tr>

<tr>
<td><span class='nama'>SUPRIYADI, S.E., S.Pd</span><br>NIP. 19700101 200501 1 025</td>
<td><span class='nama'>NUR HASAN, S.Pd.SD</span><br>NIP. 19800408 200701 1 006</td>
<td><span class='nama'>".strtoupper($d['yang_menerima'])."</span></td>
</tr>
</table>
</div>
";
}

$pdf = new Dompdf(new Options(['isRemoteEnabled'=>true]));
$pdf->loadHtml($html);
$pdf->setPaper('A4','portrait');
$pdf->render();
$pdf->stream("KWITANSI_BANYAK.pdf", ["Attachment"=>false]);
exit;
