<style type="text/css">
.brb-ready-badge{margin-top:6px;display:inline-flex;align-items:center;gap:4px;font-size:9px;font-weight:800;
color:#157347;background:#d4edda;border:1px solid #b6dfc4;border-radius:7px;padding:3px 6px;line-height:1.2;white-space:nowrap;}
.brb-ready-badge .dot{width:6px;height:6px;border-radius:50%;background:#1f9d63;box-shadow:0 0 0 0 rgba(31,157,99,.6);animation:brbReadyPulse 1.6s infinite;}
@keyframes brbReadyPulse{0%{box-shadow:0 0 0 0 rgba(31,157,99,.55)}70%{box-shadow:0 0 0 5px rgba(31,157,99,0)}100%{box-shadow:0 0 0 0 rgba(31,157,99,0)}}
tr.brb-ready-row td{background:#f1faf4;}
.pu-badge{display:inline-block;margin-top:4px;font-size:9px;font-weight:800;color:#b3261e;background:#fdecea;border:1px solid #f5c6c2;border-radius:7px;padding:2px 6px;line-height:1.2;white-space:nowrap;}
.pu-note-done{display:inline-block;font-size:9px;font-weight:800;color:#157347;background:#d4edda;border:1px solid #b6dfc4;border-radius:7px;padding:2px 6px;white-space:nowrap;}
.pu-note-proses{display:inline-block;font-size:9px;font-weight:800;color:#856404;background:#fff3cd;border:1px solid #ffeeba;border-radius:7px;padding:2px 6px;white-space:nowrap;}
.section-pu-ready{background:#fff8e6;border:1px solid #f0dcb4;border-radius:10px;padding:12px;margin-bottom:16px;}
.section-pu-ready h4{margin:0 0 10px;font-size:14px;color:#8a5a12;display:flex;align-items:center;gap:8px;}
</style>
<form id="form_dstb" class="form-horizontal">
<?php
if (strlen($this->session->userdata('idoutlet')) == 0) {
?>
<table cellspacing="3">
<tr>
<td>
<div style="width:400px;">
<select class="form-control" name="idoutlet" id="idoutlet">
<option value="">Pilih Outlet</option>
<?php
foreach ($outlet->result() as $key => $value) {
$sel = '';
if ($this->input->get('idoutlet') == $value->idoutlet) { $sel = 'selected'; }
echo "<option ".$sel." value='".$value->idoutlet."'>".$value->outlet_name."</option>";
}
?>
</select>
</div>
</td>
<td style="padding-left:5px;padding-right:5px;">
<?php echo $duedate;?>
</td>
<td>
<button class="btn btn-info" type="button" id="btn_get_data"><i class="fa fa-refresh"></i> Filter</button>
</td>
</tr>
</table>
<hr>
<?php
}
?>
<?php if (empty($sudah_filter)) { ?>
<div class="alert alert-info" style="margin-top:10px;">
<i class="fa fa-info-circle"></i> Silakan pilih <b>Outlet</b> dan <b>Tanggal</b>, lalu klik <b>Filter</b> untuk memunculkan daftar bill serta form <b>Driver</b> &amp; <b>Catatan DSTB</b>.
</div>
<?php } ?>

<?php
// ============================================================
//  SECTION 1: BARANG PU SIAP KIRIM (status=1 di dstb_clean_pu)
// ============================================================
if (isset($arr_pu_ready) && count($arr_pu_ready) > 0) {
?>
<div class="section-pu-ready">
<h4><i class="fa fa-shopping-basket"></i> Barang PU Wajib Keluar (sesuai filter)</h4>
<div class="alert alert-warning" style="font-size:11px;padding:6px 10px;">
Daftar barang PU yang harus keluar pada tanggal ini. Baris <b>SUDAH SELESAI</b> = sudah ada sinyal selesai dari Barber. Pengiriman tetap <b>wajib centang</b> barang yang akan dikirim.
</div>
<table class="table table-bordered table-condensed" id="table_pu_ready">
<tr>
<th width="10px">Checklist</th>
<th>No Bill</th>
<th>Nama Pelanggan</th>
<th>Nama Barang</th>
<th>Alasan PU</th>
<th>Est. Selesai</th>
<th>Status</th>
<th>Foto</th>
<th>Catatan</th>
</tr>
<?php
foreach ($arr_pu_ready as $key => $value) {
?>
<tr>
<td align="center">
<input type="checkbox" class="chk-pu-item" name="chk_pu[]" value="<?php echo $value['iddstb_clean_pu'];?>">
</td>
<td><?php echo $value['bill_code'];?></td>
<td><?php echo $value['customer_name'];?></td>
<td><?php echo $value['product_name'];?></td>
<td><?php echo $value['reason'];?></td>
<td><?php echo Date('d-m-Y', strtotime($value['est_finish_date']));?></td>
<td align="center">
<?php if (isset($value['status']) && (int)$value['status'] === 1) { ?>
<span class="pu-note-done">✔ Sudah Selesai</span>
<?php } else { ?>
<span class="pu-note-proses">Proses</span>
<?php } ?>
</td>
<td align="center">
<?php
if (strlen($value['photo_proof']) > 0) {
?>
<a href="<?php echo base_url().AZAPP.'assets/pu_proof/'.$value['photo_proof'];?>" target="_blank"><button class="btn btn-info btn-xs" type="button"><i class="fa fa-camera"></i> Lihat</button></a>
<?php
}
else {
echo '-';
}
?>
</td>
<td><?php echo $value['trx_note'];?></td>
</tr>
<?php
}
?>
</table>
</div>
<?php
}
?>

<?php
// ============================================================
//  SECTION 2: BILL DENGAN PU AKTIF (tombol Edit + label "X pcs PU")
// ============================================================
if (isset($arr_pu_bills) && count($arr_pu_bills) > 0) {
?>
<div class="alert alert-danger" style="font-size:11px;padding:6px 10px;">
<i class="fa fa-exclamation-triangle"></i> Bill di bawah ini memiliki barang PU yang belum dikirim. Klik <b>Edit</b> untuk mengubah atau membatalkan PU.
</div>
<table class="table table-bordered table-condensed" id="table_pu_bills" style="background:#fff5f5;">
<tr>
<th width="10px">Checklist</th>
<th width="100px">Komplit</th>
<th>No Bill</th>
<th>Nama</th>
<th>Status</th>
<th>Sisa PCS</th>
<th>Tgl Masuk</th>
<th>Tgl Selesai</th>
<th>Detail</th>
<th>Catatan Bill</th>
</tr>
<?php
foreach ($arr_pu_bills as $key => $value) {
?>
<tr>
<td align="center">
<?php if (empty($value['already_shipped'])) { ?>
<input type="checkbox" class="chk-bill" name="chk[]" value="<?php echo $value['idtransaction_group'];?>">
<?php } else { ?>
<span class="label label-success">Terkirim</span>
<?php } ?>
</td>
<td align="center">
<?php if (!empty($value['shipped_dstb_code'])) { ?>
<span class="label label-success" style="white-space:normal;line-height:1.4;"><?php echo $value['shipped_dstb_code'];?></span>
<br>
<span class="pu-badge"><?php echo $value['pu_count'];?> pcs PU</span>
<?php } else { ?>
<button class="btn btn-warning btn-xs btn-edit-pu" type="button" data-id="<?php echo $value['idtransaction_group'];?>" data-code="<?php echo $value['code'];?>"><i class="fa fa-pencil"></i> Edit</button>
<br>
<span class="pu-badge"><?php echo $value['pu_count'];?> pcs PU</span>
<?php } ?>
</td>
<td><?php echo $value['code'];?></td>
<td><?php echo $value['customer_name'];?></td>
<td><?php echo $value['dstb_status'];?></td>
<td><b><?php echo $value['sisa_pcs'];?></b> <small>(total <?php echo $value['total_pcs'];?> − PU <?php echo $value['pu_count'];?>)</small></td>
<td><?php echo Date('d-m-Y', strtotime($value['date']));?></td>
<td><?php echo Date('d-m-Y', strtotime($value['duedate']));?></td>
<td align="center" width="10px"><button class="btn btn-info btn-xs btn-detail-bill" type="button" data-code="<?php echo $value['code'];?>" data-id="<?php echo $value['idtransaction_group'];?>"><i class="fa fa-list"></i> Detail Baju</button></td>
<td><?php echo $value['note'];?></td>
</tr>
<?php
}
?>
</table>
<br>
<?php
}
?>

<?php
// ============================================================
//  SECTION 3: BILL BIASA (SIAP KIRIM) - kolom Komplit + tombol Sebagian
// ============================================================
?>
<table class="table table-bordered table-condensed" id="table_pu_bills" style="background:#fff5f5;">
<tr>
<th width="10px">Checklist</th>
<th width="100px">Komplit / No. DSTB</th>
<th>Pending</th>
<th>No Bill</th>
<th>Nama</th>
<th>Status</th>
<th>Jumlah PCS</th>
<th>Tgl Masuk</th>
<th>Tgl Selesai</th>
<th>Detail</th>
<th>Catatan Bill</th>
</tr>
<?php
foreach ($arr_data as $key => $value) {
$is_ready = !empty($value['ready']);
?>
<tr class="<?php echo $is_ready ? 'brb-ready-row' : ''; ?>">
<td align="center">
<input type="checkbox" class="chk-bill" name="chk[]" value="<?php echo $value['idtransaction_group'];?>">
<?php if ($is_ready) { ?>
<div class="brb-ready-badge"><span class="dot"></span> âœ” Sudah Siap Dikirim</div>
<?php } ?>
</td>
<td align="center">
<button class="btn btn-default btn-xs btn-sebagian" type="button" data-id="<?php echo $value['idtransaction_group'];?>" data-code="<?php echo $value['code'];?>"><i class="fa fa-filter"></i> Sebagian</button>
</td>
<td>
<?php
if (strtotime($value['duedate']) <= strtotime(Date('Y-m-d'))) {
?>
<button class="btn btn-xs btn-warning btn-pending" type="button" data-id="<?php echo $value['idtransaction_group'];?>"><i class='fa fa-align-left'></i> Pending</button>
<?php
}
?>
</td>
<td><?php echo $value['code'];?></td>
<td><?php echo $value['customer_name'];?></td>
<td><?php echo $value['dstb_status'];?></td>
<td><?php echo $value['total_pcs'];?></td>
<td><?php echo Date('d-m-Y', strtotime($value['date']));?></td>
<td><?php echo Date('d-m-Y', strtotime($value['duedate']));?></td>
<td align="center" width="10px"><button class="btn btn-info btn-xs btn-detail-bill" type="button" data-code="<?php echo $value['code'];?>" data-id="<?php echo $value['idtransaction_group'];?>"><i class="fa fa-list"></i> Detail Baju</button></td>
<td><?php echo $value['note'];?></td>
</tr>
<?php
}
?>
</table>
<!-- ===== AREA INI TERSEMBUNYI SAMPAI FILTER DIKLIK ===== -->
<div id="box_dstb_action" <?php echo (!empty($sudah_filter)) ? '' : 'style="display:none"'; ?>>
<hr>
<div class="form-group hide">
<label class="control-label col-md-1">Tanggal</label>
<div class="col-md-4"><?php echo $dstb_date;?></div>
</div>
<div class="form-group">
<label class="control-label col-md-1">Driver <red>*</red></label>
<div class="col-md-4">
<select class="form-control" name="iddriver" id="iddriver">
<option value="">~ Pilih Driver ~</option>
<?php
foreach ($drivers->result() as $d) {
echo "<option value='".$d->iduser."'>".htmlspecialchars($d->name)."</option>";
}
?>
</select>
<?php if (!empty($sudah_filter) && $drivers->num_rows() == 0) { ?>
<span class="help-block" style="color:#c0392b;"><i class="fa fa-exclamation-triangle"></i> Tidak ada driver yang terdaftar untuk outlet ini. Silakan daftarkan driver pada outlet ini terlebih dahulu.</span>
<?php } ?>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-1">Catatan DSTB</label>
<div class="col-md-4">
<textarea class="form-control" name="dstb_note" id="dstb_note" rows="4"></textarea>
</div>
</div>
</form>
<hr>
<a href="<?php echo app_url();?>dstb_clean"><button class="btn btn-default" type="button"><i class="fa fa-arrow-left"></i> Kembali</button></a>&nbsp;
<button class="btn btn-primary btn-save-dstb" type="button"><i class="fa fa-save"></i> Simpan DSTB</button>
</div>