<?php
if (!function_exists('nwa_fmt')) { function nwa_fmt($x){ return number_format((int)$x,0,',','.'); } }
function nwa_bar($label,$val,$max,$tot,$cls){
$w = ($max>0)? round($val/$max*100) : 0;
$p = ($tot>0)? round($val/$tot*100) : 0;
echo '<div class="barline"><span class="name">'.htmlspecialchars($label).'</span>'
.'<span class="track"><span class="bar '.$cls.'" style="width:'.$w.'%"></span></span>'
.'<span class="num">'.nwa_fmt($val).' ('.$p.'%)</span></div>';
}
function nwa_top($arr,$max,$tot,$cls,$limit=8){
$i=0; foreach($arr as $k=>$v){ if($i>=$limit)break; nwa_bar($k,$v,$max,$tot,$cls); $i++; }
}
$cat_cls = array('NODA'=>'r','KUSUT'=>'a','BAU'=>'b','LAIN'=>'v');
$cat_ico = array('NODA'=>'🟤','KUSUT'=>'🌀','BAU'=>'👃','LAIN'=>'✏️');
$max_cat=1; foreach($by_cat as $v){ if($v>$max_cat)$max_cat=$v; }
$max_pos=1; foreach($by_pos as $v){ if($v>$max_pos)$max_pos=$v; }
$max_ket=1; foreach($by_ket as $v){ if($v>$max_ket)$max_ket=$v; }
$max_prod=1; foreach($by_prod as $v){ if($v>$max_prod)$max_prod=$v; }
$max_day=1; foreach($by_day as $v){ if($v>$max_day)$max_day=$v; }
$max_out=1; foreach($by_out as $v){ if($v>$max_out)$max_out=$v; }
$max_usr=1; foreach($by_user as $v){ if($v>$max_usr)$max_usr=$v; }
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Analisa NWS</title>
<style type="text/css">
:root{--ink:#16202e;--muted:#69748a;--line:#e3e8ef;--red:#d6453f;--redbg:#fdecea;--amber:#c9821a;--amberbg:#fdf3e2;
--blue:#2f6fb0;--bluebg:#eaf2fa;--violet:#6f42c1;--violetbg:#f1ecfb;--teal:#0f766e;--tealbg:#e3f3f0;--green:#1f9d63;}
*{box-sizing:border-box;}
body{margin:0;color:var(--ink);font-family:Arial,Helvetica,sans-serif;font-size:12px;background:linear-gradient(160deg,#eef6f2,#e2f0ea);}
h1,h2{font-family:'Trebuchet MS','Segoe UI',sans-serif;margin:0;}
.wrap{max-width:1280px;margin:0 auto;padding:18px;}
.toolbar{display:flex;flex-wrap:wrap;gap:8px;align-items:center;background:#fff;border:1px solid var(--line);border-radius:12px;padding:12px 14px;margin-bottom:16px;}
.toolbar button{border:0;border-radius:9px;padding:9px 15px;font-size:13px;font-weight:700;cursor:pointer;color:#fff;}
.b-print{background:var(--teal);} .b-close{background:var(--muted);margin-left:auto;}
.fform{display:flex;gap:6px;align-items:center;flex-wrap:wrap;}
.fform input,.fform select{padding:7px 9px;border:1px solid var(--line);border-radius:8px;font-size:12px;}
.fform button{background:var(--violet);}
.head{background:linear-gradient(120deg,#4a2a8a,#6f42c1);color:#fff;border-radius:14px;padding:20px 22px;margin-bottom:16px;}
.head h1{font-size:22px;} .head .sub{margin-top:4px;font-size:13px;opacity:.92;}
.head .meta{margin-top:8px;font-size:11px;opacity:.88;}
.kpis{display:grid;grid-template-columns:repeat(6,1fr);gap:12px;margin-bottom:16px;}
.kpi{background:#fff;border:1px solid var(--line);border-left:5px solid var(--violet);border-radius:12px;padding:12px 14px;}
.kpi .lbl{font-size:10px;text-transform:uppercase;letter-spacing:.5px;color:var(--muted);font-weight:700;min-height:24px;}
.kpi .val{font-family:'Trebuchet MS',sans-serif;font-size:22px;font-weight:800;margin-top:4px;}
.kpi .ext{font-size:10.5px;color:var(--muted);margin-top:2px;}
.kpi.red{border-left-color:var(--red);} .kpi.red .val{color:var(--red);}
.kpi.amber{border-left-color:var(--amber);} .kpi.amber .val{color:var(--amber);}
.kpi.blue{border-left-color:var(--blue);} .kpi.blue .val{color:var(--blue);}
.kpi.teal{border-left-color:var(--teal);} .kpi.teal .val{color:var(--teal);}
.card{background:#fff;border:1px solid var(--line);border-radius:12px;padding:14px 16px;margin-bottom:16px;}
.card h2{font-size:14px;margin-bottom:10px;}
.two{display:grid;grid-template-columns:1fr 1fr;gap:16px;}
.barline{display:flex;align-items:center;gap:8px;margin:5px 0;font-size:11px;}
.barline .name{flex:0 0 150px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;}
.barline .track{flex:1;height:12px;border-radius:6px;background:#eef1f5;overflow:hidden;}
.barline .bar{height:100%;border-radius:6px;}
.barline .bar.r{background:var(--red);} .barline .bar.a{background:var(--amber);} .barline .bar.b{background:var(--blue);}
.barline .bar.v{background:var(--violet);} .barline .bar.g{background:var(--green);} .barline .bar.t{background:var(--teal);}
.barline .num{flex:0 0 90px;text-align:right;color:var(--muted);font-weight:700;}
table{border-collapse:collapse;width:100%;font-size:11px;}
th,td{border:1px solid var(--line);padding:5px 7px;vertical-align:top;}
thead th{background:#f3f6fa;text-align:center;}
.pill{display:inline-block;padding:1px 8px;border-radius:20px;font-size:9.5px;font-weight:800;white-space:nowrap;}
.pill.r{background:var(--redbg);color:var(--red);} .pill.a{background:var(--amberbg);color:var(--amber);}
.pill.b{background:var(--bluebg);color:var(--blue);} .pill.v{background:var(--violetbg);color:var(--violet);}
.foto img{width:42px;height:42px;object-fit:cover;border-radius:7px;border:1px solid var(--line);}
.empty{color:var(--muted);text-align:center;padding:14px;}
@media (max-width:900px){ .kpis{grid-template-columns:repeat(2,1fr);} .two{grid-template-columns:1fr;} }
@media print{ .toolbar{display:none !important;} body{background:#fff;} .wrap{max-width:none;padding:0;}
@page{margin:8mm;} thead{display:table-header-group;} tbody tr{page-break-inside:avoid;}
.barline .bar,.pill,.kpi{-webkit-print-color-adjust:exact;print-color-adjust:exact;} }
</style>
</head>
<body>
<div class="wrap">
<div class="toolbar no-print">
<button type="button" class="b-print" onclick="window.print();">🖨 Cetak</button>
<form method="get" action="<?php echo app_url(); ?>barber/nws_analysis" class="fform">
<input type="date" name="date_1" value="<?php echo $q1; ?>"> s/d
<input type="date" name="date_2" value="<?php echo $q2; ?>">
<select name="idoutlet">
<option value="">Semua Outlet</option>
<?php foreach ($outlets as $o): ?>
<option value="<?php echo $o->idoutlet; ?>" <?php echo ($idoutlet==$o->idoutlet)?'selected':''; ?>><?php echo htmlspecialchars($o->outlet_name); ?></option>
<?php endforeach; ?>
</select>
<button type="submit">🔍 Filter</button>
</form>
<button type="button" class="b-close" onclick="window.close();">✕ Tutup</button>
</div>
<div class="head">
<h1>📊 ANALISA NWS — <?php echo strtoupper($outlet_title); ?></h1>
<div class="sub">Note We Are Sorry · bahan evaluasi manajemen untuk kualitas produksi</div>
<div class="meta">Periode <b><?php echo date('d/m/Y',strtotime($q1)).' s/d '.date('d/m/Y',strtotime($q2)); ?></b> · Dicetak: <?php echo $tanggal_cetak; ?></div>
</div>
<div class="kpis">
<div class="kpi"><div class="lbl">Total Catatan NWS</div><div class="val"><?php echo nwa_fmt($g['notes']); ?></div><div class="ext">semua jenis masalah</div></div>
<div class="kpi red"><div class="lbl">Bill Terdampak</div><div class="val"><?php echo nwa_fmt($g['bills']); ?></div><div class="ext"><?php echo $g['pct_bill']; ?>% dari <?php echo nwa_fmt($total_bill); ?> bill</div></div>
<div class="kpi amber"><div class="lbl">Pakaian Terdampak</div><div class="val"><?php echo nwa_fmt($g['nums']); ?></div><div class="ext">per num tercatatat</div></div>
<div class="kpi blue"><div class="lbl">Foto Bukti</div><div class="val"><?php echo nwa_fmt($g['photos']); ?></div><div class="ext">lampiran QC</div></div>
<div class="kpi teal"><div class="lbl">Total Bill Periode</div><div class="val"><?php echo nwa_fmt($total_bill); ?></div><div class="ext">semua transaksi masuk</div></div>
<div class="kpi"><div class="lbl">Rata-rata / Bill</div><div class="val"><?php echo ($g['bills']>0)? round($g['notes']/$g['bills'],1) : 0; ?></div><div class="ext">catatan per bill terdampak</div></div>
</div>
<div class="two">
<div class="card">
<h2>🎯 Sebaran per Jenis Masalah</h2>
<?php if($g['notes']==0){ echo '<div class="empty">🎉 Tidak ada catatan NWS pada periode ini.</div>'; } else {
foreach($by_cat as $k=>$v){ nwa_bar((isset($cat_ico[$k])?$cat_ico[$k].' ':'').$k, $v, $max_cat, $g['notes'], isset($cat_cls[$k])?$cat_cls[$k]:'v'); } } ?>
</div>
<div class="card">
<h2>📈 Tren per Tanggal Input</h2>
<?php if(count($by_day)==0){ echo '<div class="empty">-</div>'; } else { foreach($by_day as $dk=>$v){ nwa_bar(date('d/m',strtotime($dk)), $v, $max_day, $g['notes'], 't'); } } ?>
</div>
<div class="card">
<h2>📍 Posisi Terbanyak (Noda / Kusut)</h2>
<?php if(count($by_pos)==0){ echo '<div class="empty">Tidak ada posisi tercatat.</div>'; } else { nwa_top($by_pos,$max_pos,$g['notes'],'r'); } ?>
</div>
<div class="card">
<h2>🏷 Keterangan Terbanyak (Noda Tinta, dll)</h2>
<?php if(count($by_ket)==0){ echo '<div class="empty">Tidak ada keterangan tercatat.</div>'; } else { nwa_top($by_ket,$max_ket,$g['notes'],'a'); } ?>
</div>
<div class="card">
<h2>👕 Jenis Barang Paling Banyak Kena NWS</h2>
<?php if(count($by_prod)==0){ echo '<div class="empty">-</div>'; } else { nwa_top($by_prod,$max_prod,$g['notes'],'v',10); } ?>
</div>
<div class="card">
<h2>🏬 Per Outlet &amp; 👤 Per Penginput</h2>
<?php if(count($by_out)==0){ echo '<div class="empty">-</div>'; } else { nwa_top($by_out,$max_out,$g['notes'],'b'); echo '<div style="height:10px"></div>'; nwa_top($by_user,$max_usr,$g['notes'],'g'); } ?>
</div>
</div>
<div class="card">
<h2>📋 Rincian Catatan NWS (terbaru)</h2>
<?php if($g['notes']==0){ echo '<div class="empty">Tidak ada data.</div>'; } else { ?>
<div style="overflow-x:auto;">
<table>
<thead><tr><th>Tgl Input</th><th>Bill</th><th>Num</th><th>Jenis Barang</th><th>Masalah</th><th>Posisi</th><th>Keterangan</th><th>Penginput</th><th>Foto</th></tr></thead>
<tbody>
<?php $i=0; foreach($rows as $n){ if($i>=200)break; $i++;
$cc = isset($cat_cls[$n->kategori]) ? $cat_cls[$n->kategori] : 'v';
$ket_txt = strlen($n->keterangan) ? $n->keterangan : (strlen($n->note_manual) ? $n->note_manual : '-');
$ph = isset($photo_map[$n->idtransaction]) ? $photo_map[$n->idtransaction] : '';
?>
<tr>
<td><?php echo date('d/m H:i', strtotime($n->created)); ?></td>
<td><b><?php echo htmlspecialchars((string)$n->code); ?></b></td>
<td class="c"><?php echo (int)$n->num; ?></td>
<td><?php echo htmlspecialchars((string)$n->product_name); ?></td>
<td><span class="pill <?php echo $cc; ?>"><?php echo htmlspecialchars($n->kategori); ?></span></td>
<td><?php echo strlen($n->posisi)? htmlspecialchars($n->posisi) : '-'; ?></td>
<td><?php echo htmlspecialchars($ket_txt); ?></td>
<td><?php echo htmlspecialchars((string)$n->user_name); ?></td>
<td class="foto"><?php if($ph!=''){ echo '<img src="'.base_url().AZAPP.'assets/nws/'.$ph.'">'; } else { echo '-'; } ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<?php } ?>
</div>
</div>
</body>
</html>