<?php
defined('BASEPATH') or exit('No direct script access allowed');
function spk_pdf_value($source, array $keys, $default = '-')
{
    foreach ($keys as $key) {
        if (is_array($source) && isset($source[$key]) && trim((string)$source[$key]) !== '') return trim((string)$source[$key]);
        if (is_object($source) && isset($source->$key) && trim((string)$source->$key) !== '') return trim((string)$source->$key);
    }
    return $default;
}
function spk_pdf_image($path)
{
    $path = trim((string)$path);
    if ($path === '') return '';
    if (strpos($path, 'data:image/') === 0) return $path;
    if (preg_match('#^https?://#i', $path)) {
        $urlPath = (string) parse_url($path, PHP_URL_PATH);
        $path = $urlPath !== '' ? $urlPath : $path;
    }
    $relative = ltrim(str_replace('\\', '/', $path), '/');
    $absolute = FCPATH . $relative;
    if (!is_file($absolute)) return '';
    $mime = function_exists('mime_content_type') ? @mime_content_type($absolute) : 'image/jpeg';
    $content = @file_get_contents($absolute);
    return $content === false ? '' : 'data:' . $mime . ';base64,' . base64_encode($content);
}
$items = is_array($detail) ? $detail : [];
$first = !empty($items) ? $items[0] : [];
$customerName = spk_pdf_value($konsumen, ['nama', 'nama_lengkap', 'perusahaan'], '-');
$productName = spk_pdf_value($first, ['title', 'produk'], '-');
$materialName = spk_pdf_value($first, ['nbahan', 'bahan', 'nama_bahan'], '-');
$size = spk_pdf_value($first, ['ukuran'], '-');
$note = spk_pdf_value($first, ['keterangan', 'ket'], '-');
$qty = spk_pdf_value($first, ['jumlah'], '0');
$unit = spk_pdf_value($first, ['satuan'], '');
if ($unit !== '' && function_exists('get_satuan')) $unit = get_satuan($unit);
$qtyLabel = trim($qty . ' ' . $unit);
$invoiceRaw = spk_pdf_value($cetak, ['nomor_order', 'id_transaksi', 'id_invoice'], '-');
$invoiceNo = $invoiceRaw;
if (is_numeric($invoiceRaw) && function_exists('format_transaction_number')) {
    $invoiceNo = format_transaction_number($invoiceRaw);
}
$deadlineDate = spk_pdf_value($cetak, ['tgl_ambil', 'tgl_deadline'], '');
$deadlineTime = spk_pdf_value($cetak, ['jam_ambil'], '');
$deadlineLabel = trim(($deadlineDate !== '' && function_exists('tgl_indo') ? tgl_indo($deadlineDate) : $deadlineDate) . ' ' . $deadlineTime);
if ($deadlineLabel === '') $deadlineLabel = 'URGENT';
$finishingLabels = [];
$previewImages = [];
foreach ($items as $item) {
    $raw = spk_pdf_value($item, ['detail'], '');
    if ($raw === '') continue;
    $decoded = json_decode($raw);
    if (!$decoded || empty($decoded->data) || !is_array($decoded->data)) continue;
    foreach ($decoded->data as $finish) {
        $label = spk_pdf_value($finish, ['isi', 'title', 'name', 'keterangan'], '');
        if ($label !== '') $finishingLabels[] = $label;
        $image = spk_pdf_image(spk_pdf_value($finish, ['gambar', 'image', 'img'], ''));
        if ($image !== '') $previewImages[] = $image;
    }
}
$finishingLabels = array_values(array_unique($finishingLabels));
$previewImages = array_values(array_unique($previewImages));
$finishingText = empty($finishingLabels) ? '-' : implode(' / ', $finishingLabels);
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Surat Perintah Kerja #<?=htmlspecialchars($invoiceNo, ENT_QUOTES, 'UTF-8');?></title>
<style>
@page { size: A4 landscape; margin: 3mm; }
* { box-sizing: border-box; }
body { margin: 0; color: #2b0808; font-family: DejaVu Sans, Arial, sans-serif; font-size: 10px; }
.sheet { position: relative; width: 100%; height: 202mm; overflow: hidden; border: 2px solid #4b0707; page-break-inside: avoid; }
.left, .right { position: absolute; top: 0; height: 202mm; }
.left { left: 0; width: 45%; border-right: 2px solid #4b0707; }
.right { right: 0; width: 55%; }
.bar { height: 12mm; padding: 2mm 4mm; color: #fff; font-family: DejaVu Serif, serif; font-size: 19px; font-weight: bold; letter-spacing: .5px; text-align: center; }
.bar-dark { background: #410303; }
.bar-red { background: #d51018; font-size: 23px; }
.info-table { width: 100%; border-collapse: collapse; font-size: 10px; }
.info-table td { height: 7.5mm; padding: 1mm 3mm; border-right: 1px solid #5d1717; border-bottom: 1px solid #5d1717; }
.info-table .label { width: 44%; font-style: italic; font-weight: bold; font-size: 9.5px; white-space: nowrap; }
.request { height: 40mm; padding: 3mm 4mm 2mm; border-bottom: 1px solid #5d1717; }
.request h2, .timeline h2 { margin: 0 0 3mm; font-size: 18px; font-style: italic; }
.request-lines { line-height: 5.5mm; font-size: 9px; border-bottom: 1px dotted #6d4141; }
.timeline { padding: 3mm 4mm; }
.timeline h2 { color: #d51018; text-align: center; }
.timeline-table { width: 100%; border-collapse: separate; border-spacing: 0 1.5mm; }
.timeline-table td { width: 50%; padding-right: 5mm; font-size: 11px; font-weight: bold; }
.timeline-line { height: 6mm; margin-top: .5mm; border-left: 4px solid #111; border-bottom: 1px solid #111; }
.preview { height: 148mm; padding: 4mm; }
.preview-title { margin: 0; color: #d51018; font-size: 22px; font-style: italic; }
.summary { margin: 2mm 0 3mm; color: #111; font-size: 11px; font-style: italic; font-weight: bold; line-height: 1.45; text-transform: uppercase; }
.preview-area { width: 100%; height: 111mm; text-align: center; vertical-align: middle; }
.preview-area img { max-width: 100%; max-height: 108mm; }
.preview-empty { width: 100%; height: 100mm; padding-top: 42mm; border: 1px dashed #c9aaaa; color: #b78989; font-size: 14px; }
.sign { height: 37mm; padding: 4mm 6mm; text-align: center; font-family: cursive; font-size: 27px; font-style: italic; }
.sign small { display: block; margin-top: 4mm; font-family: DejaVu Sans, Arial, sans-serif; font-size: 9px; font-style: normal; text-transform: uppercase; }
.order-ref { position: absolute; right: 8mm; bottom: 5mm; color: #6e3333; font-size: 8px; }
</style>
</head>
<body>
<div class="sheet">
<div class="left">
    <div class="bar bar-dark">SURAT PERINTAH KERJA</div>
    <table class="info-table">
        <tr><td class="label">NAMA PELANGGAN</td><td><?=htmlspecialchars($customerName, ENT_QUOTES, 'UTF-8');?></td></tr>
        <tr><td class="label">NOMOR ORDER / TRANSAKSI</td><td><?=htmlspecialchars($invoiceNo, ENT_QUOTES, 'UTF-8');?></td></tr>
        <tr><td class="label">JENIS ORDER</td><td><?=htmlspecialchars($materialName, ENT_QUOTES, 'UTF-8');?></td></tr>
        <tr><td class="label">JUMLAH</td><td><?=htmlspecialchars($qtyLabel, ENT_QUOTES, 'UTF-8');?></td></tr>
        <tr><td class="label">WARNA</td><td>................................................................</td></tr>
        <tr><td class="label">UKURAN</td><td><?=htmlspecialchars($size, ENT_QUOTES, 'UTF-8');?></td></tr>
        <tr><td class="label">PACKING</td><td><?=htmlspecialchars($note, ENT_QUOTES, 'UTF-8');?></td></tr>
    </table>
    <div class="request">
        <h2>REQUEST</h2>
        <div class="request-lines">&nbsp;</div>
        <div class="request-lines">&nbsp;</div><div class="request-lines">&nbsp;</div><div class="request-lines">&nbsp;</div>
    </div>
    <div class="timeline">
        <h2>TIMELINE</h2>
        <table class="timeline-table">
            <tr><td>BAHAN CETAK<div class="timeline-line"></div></td><td>SETTING PLAT<div class="timeline-line"></div></td></tr>
            <tr><td>CETAK<div class="timeline-line"></div></td><td>PLONG/POTONG<div class="timeline-line"></div></td></tr>
            <tr><td>HOTPRINT/EMBOSSED/SABLON<div class="timeline-line"></div></td><td>LAMINASI<div class="timeline-line"></div></td></tr>
            <tr><td>PERLENGKAPAN<div class="timeline-line"></div></td><td>FINISHING<div class="timeline-line"></div></td></tr>
        </table>
    </div>
</div>
<div class="right">
    <div class="bar bar-red">DEADLINE : <?=htmlspecialchars(strtoupper($deadlineLabel), ENT_QUOTES, 'UTF-8');?></div>
    <div class="preview">
        <h1 class="preview-title">PREVIEW</h1>
        <div class="summary"><?=htmlspecialchars($productName, ENT_QUOTES, 'UTF-8');?> // BAHAN <?=htmlspecialchars($materialName, ENT_QUOTES, 'UTF-8');?> // <?=htmlspecialchars($finishingText, ENT_QUOTES, 'UTF-8');?> // UKURAN <?=htmlspecialchars($size, ENT_QUOTES, 'UTF-8');?></div>
        <div class="preview-area">
            <?php if (!empty($previewImages)): ?><img src="<?=$previewImages[0];?>" alt="Preview pekerjaan">
            <?php else: ?><div class="preview-empty">AREA PREVIEW DESAIN</div><?php endif; ?>
        </div>
    </div>
    <div class="sign">ACC / CATATAN PRODUKSI<small>Paraf penanggung jawab produksi</small></div>
</div>
</div>

</body>
</html>
