<?php
$customerName = '-';
$customerPhone = '-';
if ($customer) {
    $isCompany = isset($customer->tampil) && (int)$customer->tampil === 1;
    $customerName = $isCompany && !empty($customer->perusahaan) ? $customer->perusahaan : $customer->nama;
    $customerPhone = $isCompany && !empty($customer->no_telp) ? $customer->no_telp : $customer->no_hp;
}
$formatDate = static function ($value) {
    if (empty($value) || strpos((string)$value, '0000-00-00') === 0) return '-';
    $time = strtotime($value);
    return $time ? date('d/m/Y', $time) : '-';
};
$formatTime = static function ($value) {
    if (empty($value) || strpos((string)$value, '00:00:00') !== false) return '';
    $time = strtotime($value);
    return $time ? date('H:i', $time).' WIB' : '';
};
?>
<style>
.spk-info-grid{display:grid;grid-template-columns:1.35fr repeat(3,1fr);gap:10px;margin-bottom:18px}.spk-info-card{background:#f7f9fc;border:1px solid #e8ecf2;border-radius:12px;padding:13px 14px;min-width:0}.spk-info-card.primary{background:linear-gradient(135deg,#315fbd,#4e73df);border:0;color:#fff}.spk-info-label{display:block;font-size:10px;font-weight:700;letter-spacing:.07em;text-transform:uppercase;color:#8490a3;margin-bottom:5px}.primary .spk-info-label{color:rgba(255,255,255,.72)}.spk-info-value{font-size:14px;font-weight:700;color:#263248;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}.primary .spk-info-value{color:#fff}.spk-info-sub{display:block;font-size:11px;color:#7f899a;margin-top:3px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}.primary .spk-info-sub{color:rgba(255,255,255,.78)}.spk-form-table{border:1px solid #e8ecf2;border-radius:12px;overflow:hidden}.spk-form-table table{margin:0}.spk-form-table thead th{border:0;background:#f3f6fa;padding:11px 12px;color:#697586;font-size:10px;letter-spacing:.055em;text-transform:uppercase;white-space:nowrap}.spk-form-table tbody td{border-color:#edf0f4;padding:13px 12px;vertical-align:middle;color:#344054}.spk-form-table tbody tr:hover{background:#fafcff}.spk-row-number{width:28px;height:28px;border-radius:8px;background:#edf2ff;color:#3f63c4;display:inline-flex;align-items:center;justify-content:center;font-weight:700}.spk-no{font-weight:700;color:#263b70;white-space:nowrap}.spk-material{font-weight:600}.spk-material small{display:block;font-weight:400;color:#7d8798;margin-top:3px;max-width:330px}.spk-qty{font-weight:700}@media(max-width:768px){.spk-info-grid{grid-template-columns:1fr 1fr}.spk-info-card.primary{grid-column:1/-1}.spk-form-table{overflow-x:auto}.spk-form-table table{min-width:760px}}@media(max-width:480px){.spk-info-grid{grid-template-columns:1fr}.spk-info-card.primary{grid-column:auto}}
 .spk-item-action{border:1px solid #a9bdf7;background:#f3f6ff;color:#315fbd;border-radius:7px;padding:6px 10px;font-size:12px;font-weight:700;white-space:nowrap}.spk-item-action:hover{background:#315fbd;border-color:#315fbd;color:#fff}
 @media(max-width:768px){.spk-form-table table{min-width:880px}}
</style>
<input type="hidden" name="id_invoive" value="<?=html_escape($token);?>">
<input type="hidden" name="invoice_id" value="<?=(int)$row->id_invoice;?>">
<input type="hidden" name="no_order" value="<?=html_escape($row->id_transaksi);?>">
<div class="spk-info-grid">
  <div class="spk-info-card primary"><span class="spk-info-label">Pelanggan</span><div class="spk-info-value"><i class="fa fa-user-o mr-1"></i><?=html_escape($customerName);?></div><small class="spk-info-sub"><?=html_escape($customerPhone);?></small></div>
  <div class="spk-info-card"><span class="spk-info-label">No. Order</span><div class="spk-info-value"><?=html_escape($row->id_transaksi);?></div><small class="spk-info-sub">Referensi transaksi</small></div>
  <div class="spk-info-card"><span class="spk-info-label">Tanggal Order</span><div class="spk-info-value"><i class="fa fa-calendar-o mr-1 text-primary"></i><?=$formatDate($row->tgl_trx);?></div><small class="spk-info-sub"><?=$formatTime(isset($row->jam_order) ? $row->jam_order : '');?></small></div>
  <div class="spk-info-card"><span class="spk-info-label">Tanggal Deadline</span><div class="spk-info-value"><i class="fa fa-flag-checkered mr-1 text-danger"></i><?=$formatDate($row->tgl_ambil);?></div><small class="spk-info-sub"><?=$formatTime($row->tgl_ambil);?></small></div>
</div>
<div class="spk-form-table"><table class="table"><thead><tr><th class="text-center">No.</th><th>No. SPK</th><th class="text-center">Qty</th><th>Produk</th><th>Nama Bahan / Pesanan</th><th>Ukuran</th><th class="text-center">Aksi</th></tr></thead><tbody>
<?php $number=1; foreach ($detail as $val): if (jkategori($val['id_produk']) == 9) continue; $spkNumber=!empty($val['no_spk'])?$val['no_spk']:'SPK-'.$val['id_rincianinvoice'].tgl_spk($val['tgl_trx']); ?>
<tr><td class="text-center"><span class="spk-row-number"><?=$number;?></span></td><td><span class="spk-no"><?=html_escape($spkNumber);?></span><input type="hidden" name="no_spk[]" value="<?=html_escape($spkNumber);?>"><input type="hidden" name="id[]" value="<?=(int)$val['id_rincianinvoice'];?>"></td><td class="text-center spk-qty"><?=html_escape($val['jumlah']);?></td><td><?=html_escape($val['title']);?></td><td><div class="spk-material"><?=html_escape($val['nbahan']);?><?php if(!empty($val['keterangan'])): ?><small><?=html_escape($val['keterangan']);?></small><?php endif;?></div></td><td><?=html_escape($val['ukuran']);?></td><td class="text-center"><button type="button" class="spk-item-action open-spk-item-editor" data-detail-id="<?=(int)$val['id_rincianinvoice'];?>"><i class="fa fa-pencil-square-o mr-1"></i>Buat SPK</button></td></tr>
<?php $number++; endforeach; ?>
</tbody></table></div>
