<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>

<div class="row">
	<div class="col-md-12">
		<div class="panel panel-default">
			<div class="panel-heading">
				<b>📌 PMG — Pengajuan Marker Pengganti</b>
				<span class="pull-right">
					<a href="<?php echo $base_url; ?>status=0" class="btn btn-xs btn-warning">Menunggu</a>
					<a href="<?php echo $base_url; ?>status=1" class="btn btn-xs btn-success">Disetujui</a>
					<a href="<?php echo $base_url; ?>status=2" class="btn btn-xs btn-danger">Ditolak</a>
					<a href="<?php echo $base_url; ?>status=all" class="btn btn-xs btn-default">Semua</a>
				</span>
			</div>
			<div class="panel-body">
				<?php if (count($rows) == 0): ?>
					<div class="alert alert-info">Tidak ada pengajuan pada filter ini.</div>
				<?php else: ?>
				<div class="table-responsive">
				<table class="table table-bordered table-striped" style="font-size:12px;">
					<thead>
						<tr><th>Kode</th><th>Bill / Pelanggan</th><th>Alasan</th><th>Num</th><th>Foto</th><th>Pengaju & Waktu</th><th>Status</th><th>Aksi</th></tr>
					</thead>
					<tbody>
					<?php foreach ($rows as $r): ?>
						<tr>
							<td><b><?php echo htmlspecialchars($r->pmg_code); ?></b></td>
							<td><?php echo htmlspecialchars($r->bill_code); ?><br><small><?php echo htmlspecialchars($r->customer_name); ?> · <?php echo htmlspecialchars($r->outlet_name); ?></small></td>
							<td><?php echo ($r->reason == 'HILANG') ? 'Lepas saat cuci & hilang' : 'Tulisan tidak tampak 95%'; ?></td>
							<td><?php echo htmlspecialchars($r->nums_label); ?></td>
							<td>
								<?php if (count($r->photos) > 0): ?>
									<?php foreach ($r->photos as $i => $ph): ?>
										<a href="<?php echo $photo_base . $ph->photo; ?>" target="_blank">📷<?php echo $i + 1; ?></a>
									<?php endforeach; ?>
																<?php else: ?>
									<?php if ((int)$r->status === 1 && (int)$r->printed === 0): ?>
										<a class="btn btn-xs btn-info" target="_blank" href="<?php echo $print_base; ?><?php echo (int)$r->idpmg; ?>">🖨 Cetak</a>
									<?php elseif ((int)$r->printed === 1): ?>
										<small>Sudah dicetak<br><?php echo date('d/m/Y H:i', strtotime($r->printed_at)); ?></small>
									<?php else: ?>-<?php endif; ?>
								<?php endif; ?>
							</td>
							<td><small><?php echo htmlspecialchars($r->createdby); ?><br><?php echo date('d/m/Y H:i', strtotime($r->created)); ?></small></td>
							<td>
								<?php if ((int)$r->status === 0): ?><span class="label label-warning">MENUNGGU</span>
								<?php elseif ((int)$r->status === 1): ?><span class="label label-success">DISETUJUI</span><?php if ((int)$r->printed === 1): ?> <span class="label label-default">DICETAK</span><?php endif; ?>
								<?php else: ?><span class="label label-danger">DITOLAK</span><?php endif; ?>
								<br><small><?php if ($r->approved_at) echo date('d/m/Y H:i', strtotime($r->approved_at)) . ' · via ' . ($r->approved_via == 'telegram' ? 'Telegram' : 'form admin'); ?></small>
							</td>
							<td>
								<?php if ((int)$r->status === 0): ?>
									<form method="post" action="<?php echo $approve_url; ?>" style="display:inline-block;margin:2px;">
										<input type="hidden" name="id" value="<?php echo (int)$r->idpmg; ?>">
										<button class="btn btn-xs btn-success" type="submit">✅ Setujui</button>
									</form>
									<form method="post" action="<?php echo $reject_url; ?>" style="display:inline-block;margin:2px;">
										<input type="hidden" name="id" value="<?php echo (int)$r->idpmg; ?>">
										<button class="btn btn-xs btn-danger" type="submit">❌ Tolak</button>
									</form>
								<?php else: ?>-<?php endif; ?>
							</td>
						</tr>
					<?php endforeach; ?>
					</tbody>
				</table>
				</div>
				<?php endif; ?>
			</div>
		</div>
	</div>
</div>