<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<style>.cmp-form{max-width:1050px;margin:auto}.cmp-box{background:#fff;border:1px solid #e5e7eb;border-radius:12px;padding:20px;margin-bottom:16px}.order-result{display:block;width:100%;text-align:left;border:0;border-bottom:1px solid #eee;background:#fff;padding:10px}.search-results{position:absolute;z-index:20;background:#fff;width:100%;box-shadow:0 8px 20px #0002;max-height:260px;overflow:auto}</style>
<div class="container-fluid cmp-form"><a href="<?=site_url('komplain')?>"><i class="fa fa-arrow-left"></i> Kembali</a><h3 class="mt-2">Buat Komplain</h3><form id="cmpCreate" enctype="multipart/form-data"><div class="cmp-box"><h5>Informasi transaksi</h5><?php if(!$order):?><div class="form-group position-relative"><label>Cari invoice/order</label><input id="orderSearch" class="form-control" autocomplete="off"><div id="orderResults" class="search-results d-none"></div></div><div id="orderInfo" class="text-muted">Pilih order untuk memuat pelanggan, SPK, produk, dan qty.</div><?php else:?><div><b><?=html_escape($order['id_transaksi'])?></b> · <?=html_escape($order['customer'])?> · <?=date('d/m/Y',strtotime($order['tgl_trx']))?></div><?php endif;?><input type="hidden" name="transaction_id" id="transactionId" value="<?=$order?(int)$order['id_invoice']:''?>"><div id="itemArea" class="mt-3"><?php if($items):foreach($items as $it):?><div class="form-row align-items-end border-top pt-2"><div class="form-group col-md-5"><label><input type="checkbox" class="item-check" data-id="<?=$it['id_rincianinvoice']?>"> <?=html_escape($it['product_name'])?></label><div class="small text-muted">SPK <?=html_escape($it['no_spk']?:'-')?> · Qty order <?=number_format($it['jumlah'],0,',','.')?></div></div><div class="form-group col-md-3"><label>Qty bermasalah</label><input disabled class="form-control item-field" data-name="complaint_qty" data-id="<?=$it['id_rincianinvoice']?>" type="number" min="0.01" max="<?=$it['jumlah']?>" step="0.01"></div><div class="form-group col-md-4"><label>Masalah item</label><input disabled class="form-control item-field" data-name="issue_description" data-id="<?=$it['id_rincianinvoice']?>"></div></div><?php endforeach;endif;?></div></div>
<div class="cmp-box"><h5>Informasi komplain</h5><div class="form-row"><div class="form-group col-md-4"><label>Tanggal *</label><input type="date" name="complaint_date" value="<?=date('Y-m-d')?>" class="form-control" required></div><div class="form-group col-md-5"><label>Jenis *</label><select name="complaint_type" class="form-control" required><?php foreach($types as $t):?><option><?=html_escape($t)?></option><?php endforeach;?></select></div><div class="form-group col-md-3"><label>Prioritas</label><select name="priority" class="form-control"><?php foreach(['LOW','NORMAL','HIGH','URGENT'] as $p):?><option <?=$p==='NORMAL'?'selected':''?>><?=$p?></option><?php endforeach;?></select></div></div><div class="form-group"><label>Deskripsi *</label><textarea name="description" class="form-control" rows="4" required></textarea></div><div class="form-row"><div class="form-group col-md-6"><label>Permintaan pelanggan</label><textarea name="customer_request" class="form-control" rows="2"></textarea></div><div class="form-group col-md-6"><label>Catatan internal</label><textarea name="internal_note" class="form-control" rows="2"></textarea></div></div><div class="form-group"><label>Foto/bukti (gambar otomatis menjadi WEBP; PDF tetap PDF; maks. 5 MB/file)</label><input type="file" name="evidence[]" multiple accept=".jpg,.jpeg,.png,.webp,.pdf" class="form-control-file"></div><button class="btn btn-primary" type="submit"><i class="fa fa-save mr-1"></i>Simpan Komplain</button></div></form></div>
<script>(function(){function bindItems(){$('.item-check').off('change').on('change',function(){var id=$(this).data('id'),on=this.checked;$('.item-field[data-id="'+id+'"]').prop('disabled',!on).each(function(){this.name=on?'items['+id+']['+$(this).data('name')+']':''})})}bindItems();var timer;$('#orderSearch').on('input',function(){var q=this.value;clearTimeout(timer);if(q.length<2)return;timer=setTimeout(function(){$.getJSON(base_url+'komplain/search-order',{q:q},function(x){var h='';$.each(x.rows,function(_,r){h+='<button type="button" class="order-result" data-id="'+r.id_invoice+'"><b>'+r.id_transaksi+'</b> · '+r.customer+'</button>'});$('#orderResults').html(h).removeClass('d-none')})},250)});$(document).on('click','.order-result',function(){var id=$(this).data('id');$('#transactionId').val(id);$('#orderResults').addClass('d-none');$.getJSON(base_url+'komplain/order-items/'+id,function(x){var h='';$.each(x.rows,function(_,r){h+='<div class="form-row border-top pt-2"><div class="col-md-5"><label><input type="checkbox" class="item-check" data-id="'+r.id_rincianinvoice+'"> '+$('<div>').text(r.product_name).html()+'</label><div class="small text-muted">SPK '+(r.no_spk||'-')+' · Qty '+r.jumlah+'</div></div><div class="col-md-3"><input disabled type="number" min=".01" max="'+r.jumlah+'" step=".01" class="form-control item-field" data-name="complaint_qty" data-id="'+r.id_rincianinvoice+'"></div><div class="col-md-4"><input disabled class="form-control item-field" data-name="issue_description" data-id="'+r.id_rincianinvoice+'"></div></div>'});$('#itemArea').html(h);bindItems()})});$('#cmpCreate').on('submit',function(e){e.preventDefault();var b=$(this).find('button[type=submit]').prop('disabled',true);$.ajax({url:base_url+'komplain/store',method:'POST',data:new FormData(this),processData:false,contentType:false}).done(function(x){location.href=x.redirect}).fail(function(x){showNotif('bottom-right','Komplain',(x.responseJSON&&x.responseJSON.msg)||'Gagal menyimpan.','error')}).always(function(){b.prop('disabled',false)})})})();</script>
