<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<style>.cmp-grid{display:grid;grid-template-columns:repeat(5,1fr);gap:12px}.cmp-card{background:#fff;border:1px solid #e5e7eb;border-radius:12px;padding:16px}.cmp-card small{color:#64748b;text-transform:uppercase;font-weight:700}.cmp-card strong{display:block;font-size:24px;color:#172033}.cmp-head{display:flex;justify-content:space-between;align-items:center;margin-bottom:18px}.cmp-filter{display:grid;grid-template-columns:2fr repeat(3,1fr) 105px;gap:8px}.cmp-loading{padding:55px;text-align:center;color:#64748b}.cmp-data-card{padding:0;overflow:hidden}.cmp-data-card.is-loading{opacity:.65;pointer-events:none}@media(max-width:1100px){.cmp-filter{grid-template-columns:1fr 1fr 1fr}}@media(max-width:900px){.cmp-grid{grid-template-columns:repeat(2,1fr)}.cmp-filter{grid-template-columns:1fr}.cmp-head{align-items:flex-start;flex-direction:column;gap:12px}}</style>
<div class="container-fluid"><div class="cmp-head"><div><h3 class="mb-1">Manajemen Komplain</h3><div class="text-muted">Komplain, rework, biaya, dan audit tetap terhubung ke order original.</div></div><button id="openComplaintModal" class="btn btn-primary" type="button"><i class="fa fa-plus mr-1"></i>Buat Komplain</button></div>
<div class="cmp-grid mb-3"><?php foreach(['open_count'=>'Komplain Baru','approved_count'=>'Approved','rework_count'=>'Rework','rejected_count'=>'Rejected'] as $k=>$label):?><div class="cmp-card"><small><?=$label?></small><strong><?=(int)($summary[$k]??0)?></strong></div><?php endforeach;?><div class="cmp-card"><small>Total Biaya</small><strong style="font-size:18px">Rp <?=number_format((float)($summary['total_cost']??0),0,',','.')?></strong></div></div>
<div class="cmp-card mb-3"><form id="complaintFilter" class="cmp-filter"><input id="complaintKeyword" class="form-control" name="q" placeholder="No. komplain, invoice, pelanggan"><select class="form-control" name="status"><option value="">Semua status</option><?php foreach($statuses as $s):?><option><?=html_escape($s)?></option><?php endforeach;?></select><select class="form-control" name="priority"><option value="">Semua prioritas</option><?php foreach(['LOW','NORMAL','HIGH','URGENT'] as $p):?><option><?=html_escape($p)?></option><?php endforeach;?></select><select class="form-control" name="complaint_type"><option value="">Semua jenis</option><?php foreach($types as $t):?><option><?=html_escape($t)?></option><?php endforeach;?></select><select class="form-control" name="limit" aria-label="Jumlah data per halaman"><option>10</option><option>20</option><option>50</option><option>100</option></select></form></div>
<div id="complaintData" class="cmp-card cmp-data-card"><div class="cmp-loading"><i class="fa fa-circle-o-notch fa-spin mr-2"></i>Memuat data komplain...</div></div></div>
<div class="modal fade cmp-create-modal" id="complaintCreateModal" tabindex="-1" role="dialog" aria-labelledby="complaintCreateTitle" aria-hidden="true"><div class="modal-dialog modal-dialog-centered" role="document"><div class="modal-content"><div class="modal-header"><div><span class="cmp-modal-kicker">Complaint Management</span><h4 class="modal-title" id="complaintCreateTitle">Buat Komplain Pelanggan</h4><p>Hubungkan laporan pelanggan dengan transaksi dan item yang tepat.</p></div><button type="button" class="close" data-dismiss="modal" aria-label="Tutup"><span aria-hidden="true">&times;</span></button></div><div id="complaintModalBody" class="modal-body"><div class="cmp-modal-loading"><i class="fa fa-circle-o-notch fa-spin"></i><span>Menyiapkan formulir...</span></div></div></div></div></div>
<script>(function(){var request,timer;window.loadComplaints=function(page){if(request)request.abort();var target=$('#complaintData').addClass('is-loading');request=$.ajax({url:base_url+'komplain/data/'+(page||0),method:'POST',data:$('#complaintFilter').serialize()}).done(function(html){target.html(html)}).fail(function(xhr,status){if(status!=='abort')target.html('<div class="cmp-loading text-danger">Data komplain gagal dimuat. Silakan coba lagi.</div>')}).always(function(){target.removeClass('is-loading')})};$('#complaintFilter').on('submit',function(e){e.preventDefault();loadComplaints(0)}).find('select').on('change',function(){loadComplaints(0)});$('#complaintKeyword').on('input',function(){clearTimeout(timer);timer=setTimeout(function(){loadComplaints(0)},350)});loadComplaints(0)})();</script>
<style>.cmp-create-modal .modal-dialog{max-width:1080px}.cmp-create-modal .modal-content{border:0;border-radius:20px;box-shadow:0 28px 80px rgba(15,23,42,.28);overflow:hidden}.cmp-create-modal .modal-header{padding:22px 26px 17px;border:0;background:linear-gradient(135deg,#f8faff,#fff)}.cmp-create-modal .modal-title{font-weight:750;color:#172033;margin:3px 0}.cmp-create-modal .modal-header p{margin:0;color:#697386}.cmp-create-modal .close{width:38px;height:38px;padding:0;margin:0;border-radius:50%;font-weight:400}.cmp-create-modal .close:hover{background:#e9eef8}.cmp-modal-kicker{font-size:11px;font-weight:800;letter-spacing:.1em;text-transform:uppercase;color:var(--app-theme-color,#4f46e5)}.cmp-create-modal .modal-body{padding:0;background:#f6f8fb;max-height:calc(100vh - 155px);overflow-y:auto}.cmp-modal-loading{min-height:320px;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:12px;color:#697386}.cmp-modal-loading i{font-size:28px;color:var(--app-theme-color,#4f46e5)}@media(max-width:576px){.cmp-create-modal .modal-dialog{margin:8px}.cmp-create-modal .modal-header{padding:18px}.cmp-create-modal .modal-body{max-height:calc(100vh - 125px)}}</style>
<script>(function(){var loaded=false,requestedInvoice=<?=json_encode(isset($prefillInvoiceToken)?$prefillInvoiceToken:'')?>;$('#openComplaintModal').on('click',function(){var modal=$('#complaintCreateModal');modal.modal('show');if(loaded)return;$.get(base_url+'komplain/modal-form',requestedInvoice?{invoice:requestedInvoice}:{}).done(function(html){$('#complaintModalBody').html(html);loaded=true}).fail(function(xhr){$('#complaintModalBody').html('<div class="alert alert-danger m-4">'+((xhr.responseJSON&&xhr.responseJSON.msg)||'Formulir komplain gagal dimuat.')+'</div>')})});$('#complaintCreateModal').on('hidden.bs.modal',function(){if(!loaded)$('#complaintModalBody').html('<div class="cmp-modal-loading"><i class="fa fa-circle-o-notch fa-spin"></i><span>Menyiapkan formulir...</span></div>')});if(requestedInvoice)$('#openComplaintModal').trigger('click');})();</script>
