<?php
require_once __DIR__ . '/config/db.php';
$pid=(int)($_GET['pledge_id']??$_GET['id']??0);
$pledge=null;$payments=[];$items=[];$err='';
if(!function_exists('tk')){function tk($n){return '৳'.number_format((float)$n,2);}}
if(!function_exists('e2')){function e2($s){return htmlspecialchars((string)$s,ENT_QUOTES,'UTF-8');}}
if($pid<=0){$err='URL এ pledge_id আসেনি';}
else{
 try{
  $st=$pdo->prepare("SELECT p.*,COALESCE(pa.name,'') AS party_name,COALESCE(pa.phone,'') AS party_phone,COALESCE(sl.name,'') AS location_name FROM pawn_pledges p LEFT JOIN parties pa ON pa.id=p.party_id LEFT JOIN stock_locations sl ON sl.id=p.location_id WHERE p.id=? LIMIT 1");
  $st->execute([$pid]);$pledge=$st->fetch(PDO::FETCH_ASSOC);
  if(!$pledge){$err='বন্ধক পাওয়া যায়নি';}
  else{
   $ps=$pdo->prepare("SELECT pp.*,COALESCE(ca.account_name,'') AS cash_account_name FROM pawn_payments pp LEFT JOIN cash_accounts ca ON ca.id=pp.cash_account_id WHERE pp.pledge_id=? ORDER BY pp.payment_date DESC,pp.id DESC");
   $ps->execute([$pid]);$payments=$ps->fetchAll(PDO::FETCH_ASSOC);
   $pi=$pdo->prepare("SELECT * FROM pledge_items WHERE pledge_id=? ORDER BY id ASC");
   $pi->execute([$pid]);$items=$pi->fetchAll(PDO::FETCH_ASSOC);
  }
 }catch(Throwable $ex){$err='সার্ভার সমস্যা: '.$ex->getMessage();}
}
$st2=strtoupper((string)($pledge['status']??'OPEN'));
$hld=strtoupper((string)($pledge['holding_status']??'SHOWROOM'));
$out=(float)($pledge['outstanding_principal']??0);
$loan=(float)($pledge['loan_amount']??0);
$gval=(float)($pledge['gold_value']??0);
$irate=(float)($pledge['interest_rate']??0);
$prec=max(0,$loan-$out);
$idue=(float)($pledge['current_interest_due']??0);
$tdue=$out+$idue;
$safe=$gval-$tdue;
$ifrom=$pledge['interest_from_date']??$pledge['pledge_date']??'';
$cpay=($pledge&&$out>0&&$st2!=='CLOSED');
$clock=($pledge&&$st2!=='CLOSED'&&$hld!=='LOCKER');
$can_edit=($pledge&&$st2!=='CLOSED');
$tc=0;$ta=0;$tp=0;$ti=0;
foreach($payments as $p){
 $a=(float)($p['amount']??0);$i=(float)($p['interest_paid']??0);$pr=(float)($p['principal_paid']??0);
 if($pr<=0&&$i<=0&&$a>0)$pr=$a;
 $tc++;$ta+=$a;$tp+=$pr;$ti+=$i;
}
// বাংলা স্ট্যাটাস ও হোল্ডিং
$st_bn = ['OPEN'=>'চালু','CLOSED'=>'ফেরত','DEFAULTED'=>'বাতিল'][$st2] ?? $st2;
$hld_bn = ['SHOWROOM'=>'শোরুম','LOCKER'=>'লকার'][$hld] ?? $hld;
// ভরি হিসাব
function toVori($g){
 $BHARI=11.664;$ANA=$BHARI/16;$ROTI=$BHARI/96;
 $v=floor($g/$BHARI);$r=$g-$v*$BHARI;
 $a=floor($r/$ANA);$ro=floor(($r-$a*$ANA)/$ROTI);
 return $v.'ভ '.$a.'আ '.$ro.'র';
}
?>
<!DOCTYPE html>
<html lang="bn">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>বন্ধক বিবরণ — বধুবরণ জুয়েলার্স</title>
<style>
*{box-sizing:border-box;margin:0;padding:0;-webkit-tap-highlight-color:transparent}
:root{
  --bg:#080F1E;--card:#0F1A30;--card2:#060E1C;
  --border:#1A2D50;--gold:#F5B800;--gold2:#D4780A;
  --blue:#4F8EF7;--green:#10B981;--red:#EF4444;
  --purple:#A78BFA;--text:#E8F0FE;--sub:#5C7DA8;
}
body{background:var(--bg);color:var(--text);font-family:'Segoe UI',Arial,sans-serif;max-width:480px;margin:0 auto;padding-bottom:40px;font-size:14px}

/* HEADER */
.topbar{background:linear-gradient(135deg,#0D1829,#162240);border-bottom:2px solid var(--gold);padding:12px 16px;display:flex;align-items:center;gap:10px;position:sticky;top:0;z-index:99;box-shadow:0 2px 20px rgba(0,0,0,.5)}
.back-a{background:rgba(245,184,0,.12);border:1px solid rgba(245,184,0,.3);color:var(--gold);padding:8px 12px;border-radius:10px;text-decoration:none;font-size:13px;font-weight:700}
.topbar h1{font-size:15px;font-weight:800;color:var(--text);flex:1}

.wrap{padding:14px}

/* ERROR BOX */
.err-box{background:rgba(239,68,68,.1);border:1px solid rgba(239,68,68,.3);color:var(--red);padding:14px;border-radius:12px;font-weight:600}

/* CARD */
.box{background:var(--card);border:1px solid var(--border);border-radius:16px;padding:14px;margin-bottom:12px}

/* CUSTOMER HEADER */
.cust-head{display:flex;justify-content:space-between;align-items:flex-start;gap:10px;margin-bottom:14px;flex-wrap:wrap}
.cust-name{font-size:20px;font-weight:800;color:var(--text);margin-bottom:4px}
.cust-info{font-size:12px;color:var(--sub);margin-top:2px}
.badges{display:flex;gap:6px;flex-wrap:wrap;align-items:center}
.badge{display:inline-block;padding:4px 10px;border-radius:20px;font-size:11px;font-weight:800;white-space:nowrap}
.bg-code{background:rgba(245,184,0,.12);color:var(--gold);border:1px solid rgba(245,184,0,.25);font-size:13px}
.bg-open{background:rgba(245,184,0,.15);color:var(--gold);border:1px solid rgba(245,184,0,.25)}
.bg-closed{background:rgba(16,185,129,.15);color:var(--green);border:1px solid rgba(16,185,129,.25)}
.bg-showroom{background:rgba(79,142,247,.15);color:var(--blue);border:1px solid rgba(79,142,247,.25)}
.bg-locker{background:rgba(167,139,250,.15);color:var(--purple);border:1px solid rgba(167,139,250,.25)}

/* INFO GRID */
.ig{display:grid;grid-template-columns:1fr 1fr;gap:8px}
.iitem{background:var(--card2);border:1px solid var(--border);border-radius:11px;padding:11px}
.iitem .lbl{font-size:11px;color:var(--sub);margin-bottom:4px;font-weight:600}
.iitem .val{font-size:15px;font-weight:800;color:var(--text)}
.iitem .hint{font-size:10px;color:var(--sub);margin-top:2px}
.v-gold{color:var(--gold)!important}
.v-green{color:var(--green)!important}
.v-red{color:var(--red)!important}
.v-blue{color:var(--blue)!important}
.full{grid-column:1/-1}

/* SECTION TITLE */
.stitle{font-size:12px;font-weight:800;color:var(--gold);text-transform:uppercase;letter-spacing:.5px;margin-bottom:12px;display:flex;align-items:center;gap:6px}
.stitle::after{content:'';flex:1;height:1px;background:linear-gradient(90deg,var(--border),transparent)}

/* ACTIONS */
.actions{display:flex;gap:8px;flex-wrap:wrap;margin-top:14px}
.btn{display:inline-block;padding:10px 14px;border-radius:10px;font-size:13px;font-weight:700;cursor:pointer;border:none;text-decoration:none;text-align:center;font-family:inherit}
.btn-bk{background:rgba(255,255,255,.06);color:var(--sub);border:1px solid var(--border)}
.btn-py{background:linear-gradient(135deg,#16a34a,#0f7c3a);color:#fff;box-shadow:0 3px 10px rgba(22,163,74,.25)}
.btn-lk{background:linear-gradient(135deg,#1565c0,#1e40af);color:#fff;box-shadow:0 3px 10px rgba(21,101,192,.25)}
.btn-cls{background:linear-gradient(135deg,#b45309,#92400e);color:#fff;box-shadow:0 3px 10px rgba(180,83,9,.25)}
.btn-edit{background:linear-gradient(135deg,#7c3aed,#5b21b6);color:#fff;box-shadow:0 3px 10px rgba(124,58,237,.25)}
.btn-print{background:linear-gradient(135deg,#0f766e,#0d6a62);color:#fff;box-shadow:0 3px 10px rgba(15,118,110,.25)}

/* ITEM CARD */
.item-row{background:var(--card2);border:1px solid var(--border);border-radius:12px;padding:12px;margin-bottom:10px}
.item-row-head{display:flex;justify-content:space-between;align-items:center;margin-bottom:10px;gap:8px;flex-wrap:wrap}
.item-name{font-size:14px;font-weight:800;color:var(--gold)}
.item-badges{display:flex;gap:5px;flex-wrap:wrap}
.item-badge{display:inline-block;padding:3px 9px;border-radius:15px;font-size:10px;font-weight:800}
.ib-karat{background:rgba(245,184,0,.15);color:var(--gold);border:1px solid rgba(245,184,0,.25)}
.ib-qty{background:rgba(16,185,129,.15);color:var(--green);border:1px solid rgba(16,185,129,.25)}
.item-grid{display:grid;grid-template-columns:1fr 1fr;gap:7px}
.item-cell{background:var(--card);border:1px solid var(--border);border-radius:9px;padding:9px}
.item-cell .k{font-size:10px;color:var(--sub);margin-bottom:3px;font-weight:600}
.item-cell .v{font-size:13px;font-weight:800}
.item-rem{font-size:11px;color:var(--sub);margin-top:8px;font-style:italic;padding:6px 8px;background:rgba(255,255,255,.03);border-radius:6px}

/* PAYMENT CARD */
.pay-card{background:var(--card2);border:1px solid var(--border);border-radius:12px;padding:12px;margin-bottom:10px;border-left:3px solid var(--green)}
.pay-top{display:flex;justify-content:space-between;align-items:flex-start;gap:8px;margin-bottom:8px}
.pay-amt{font-size:20px;font-weight:900;color:var(--green)}
.pay-date{font-size:12px;color:var(--sub);margin-top:3px}
.pay-recv-btn{display:inline-block;margin-top:5px;font-size:11px;background:linear-gradient(135deg,#0f766e,#0d6a62);color:#fff;padding:5px 10px;border-radius:7px;text-decoration:none;font-weight:700}
.pay-meta{font-size:12px;color:var(--sub);line-height:1.7;margin-top:6px}
.pay-split{display:grid;grid-template-columns:1fr 1fr;gap:7px;margin-top:10px}
.sb{background:var(--card);border:1px solid var(--border);border-radius:9px;padding:9px}
.sb .lbl{font-size:10px;color:var(--sub);margin-bottom:3px;font-weight:600}
.sb .val{font-size:14px;font-weight:800}

/* EDIT MODAL */
.modal{display:none;position:fixed;inset:0;background:rgba(0,0,0,.82);z-index:9999;overflow-y:auto;padding:12px;align-items:center;justify-content:center}
.modal.show{display:flex}
.mbox{width:100%;max-width:420px;background:var(--card);border:1px solid rgba(124,58,237,.3);border-radius:18px;overflow:hidden;margin:auto;position:relative}
.mbox::before{content:'';position:absolute;top:0;left:20%;right:20%;height:2px;background:linear-gradient(90deg,transparent,#7c3aed,transparent)}
.mhead{background:rgba(124,58,237,.08);border-bottom:1px solid var(--border);padding:14px 16px;display:flex;justify-content:space-between;align-items:center}
.mht{font-size:16px;font-weight:800;color:var(--purple)}
.mcl{width:32px;height:32px;border-radius:8px;background:rgba(239,68,68,.1);border:1px solid rgba(239,68,68,.2);color:var(--red);cursor:pointer;font-size:15px;padding:0}
.mcl:hover{background:var(--red);color:#fff}
.mbody{padding:16px;max-height:75vh;overflow-y:auto}
.mf{display:flex;flex-direction:column;gap:5px;margin-bottom:13px}
.mf label{font-size:11px;color:var(--sub);font-weight:700;text-transform:uppercase;letter-spacing:.3px}
.mf input,.mf textarea{background:var(--card2);border:1px solid var(--border);color:var(--text);font-size:14px;padding:10px 12px;border-radius:9px;width:100%;outline:none;font-family:inherit}
.mf input:focus,.mf textarea:focus{border-color:var(--purple)}
.mf textarea{resize:none;height:70px}
.mf .hint{font-size:10px;color:var(--sub);margin-top:2px}
.msub{font-size:11px;font-weight:800;color:var(--purple);text-transform:uppercase;letter-spacing:.6px;padding-bottom:6px;border-bottom:1px dashed var(--border);margin:8px 0 12px}
.mfoot{padding:12px 16px;border-top:1px solid var(--border);display:flex;gap:8px;justify-content:flex-end;background:var(--card2)}
.btn-save{background:linear-gradient(135deg,#7c3aed,#5b21b6);color:#fff;border:none;padding:10px 22px;border-radius:10px;font-size:14px;font-weight:800;cursor:pointer;font-family:inherit;box-shadow:0 3px 10px rgba(124,58,237,.3)}
.btn-cancel{background:rgba(255,255,255,.06);color:var(--sub);border:1px solid var(--border);padding:10px 18px;border-radius:10px;font-size:14px;font-weight:600;cursor:pointer;font-family:inherit}
.alert{padding:11px 13px;border-radius:9px;font-size:12px;font-weight:700;margin-bottom:12px;display:none}
.alert.err{background:rgba(239,68,68,.1);border:1px solid rgba(239,68,68,.25);color:var(--red)}
.alert.ok{background:rgba(16,185,129,.1);border:1px solid rgba(16,185,129,.25);color:var(--green)}
.warn-box{background:rgba(124,58,237,.08);border:1px solid rgba(124,58,237,.25);border-radius:9px;padding:10px;font-size:12px;color:var(--purple);line-height:1.5;font-weight:500}

.empty{text-align:center;padding:20px;color:var(--sub);font-size:13px}
</style>
</head>
<body>

<div class="topbar">
  <a class="back-a" href="/pawn.php">← ফিরে যান</a>
  <h1>🔒 বন্ধক বিবরণ</h1>
</div>

<div class="wrap">
<?php if($err): ?>
  <div class="err-box">⚠ <?php echo e2($err); ?></div>
<?php else: ?>

  <!-- মূল তথ্য -->
  <div class="box">
    <div class="cust-head">
      <div>
        <div class="cust-name"><?php echo e2($pledge['party_name']); ?></div>
        <div class="cust-info">📞 <?php echo e2($pledge['party_phone']?:'-'); ?></div>
        <div class="cust-info">📅 <?php echo e2($pledge['pledge_date']); ?> | ID #<?php echo $pid; ?></div>
      </div>
      <div class="badges">
        <span class="badge bg-code"><?php echo e2($pledge['pledge_code']); ?></span>
        <span class="badge <?php echo $st2==='CLOSED'?'bg-closed':'bg-open'; ?>"><?php echo $st2==='CLOSED'?'✅ ':'🏷 '; ?><?php echo e2($st_bn); ?></span>
        <span class="badge <?php echo $hld==='LOCKER'?'bg-locker':'bg-showroom'; ?>"><?php echo $hld==='LOCKER'?'🔐 ':'🏪 '; ?><?php echo e2($hld_bn); ?></span>
      </div>
    </div>

    <div class="ig">
      <div class="iitem"><div class="lbl">বন্ধকে দেওয়া ঋণ</div><div class="val v-red"><?php echo tk($loan); ?></div></div>
      <div class="iitem"><div class="lbl">বাকি আসল</div><div class="val v-red"><?php echo tk($out); ?></div></div>
      <div class="iitem"><div class="lbl">বকেয়া লকার ভাড়া</div><div class="val v-gold"><?php echo tk($idue); ?></div></div>
      <div class="iitem"><div class="lbl">এখন মোট পাওনা</div><div class="val"><?php echo tk($tdue); ?></div></div>
      <div class="iitem"><div class="lbl">ফেরত আসা আসল</div><div class="val v-green"><?php echo tk($prec); ?></div></div>
      <div class="iitem"><div class="lbl">নিরাপত্তা মার্জিন</div><div class="val <?php echo $safe>=0?'v-green':'v-red'; ?>"><?php echo tk($safe); ?></div></div>
      <div class="iitem"><div class="lbl">লকার ভাড়ার হার</div><div class="val"><?php echo number_format($irate,2); ?>% / মাস</div></div>
      <div class="iitem"><div class="lbl">ভাড়া শুরু তারিখ</div><div class="val" style="font-size:13px"><?php echo e2($ifrom?:'-'); ?></div></div>
      <?php if(!empty($pledge['location_note'])): ?>
      <div class="iitem full">
        <div class="lbl">📍 লোকেশন / কার কাছে</div>
        <div class="val v-blue"><?php echo e2($pledge['location_note']); ?></div>
      </div>
      <?php endif; ?>
      <div class="iitem"><div class="lbl">Gross ওজন</div><div class="val"><?php echo number_format((float)$pledge['gross_weight'],3); ?> গ্রাম</div><div class="hint"><?php echo toVori((float)$pledge['gross_weight']); ?></div></div>
      <div class="iitem"><div class="lbl">নিট ওজন</div><div class="val"><?php echo number_format((float)$pledge['pure_weight'],3); ?> গ্রাম</div><div class="hint"><?php echo toVori((float)$pledge['pure_weight']); ?></div></div>
      <div class="iitem"><div class="lbl">প্রকৃত বিশুদ্ধতা</div><div class="val"><?php echo number_format((float)$pledge['actual_purity'],2); ?>%</div></div>
      <div class="iitem"><div class="lbl">সোনার মূল্য</div><div class="val v-gold"><?php echo tk($gval); ?></div></div>
      <?php if($pledge['manual_barcode']): ?>
      <div class="iitem full"><div class="lbl">🏷️ বারকোড</div><div class="val v-gold"><?php echo e2($pledge['manual_barcode']); ?></div></div>
      <?php endif; ?>
      <div class="iitem full"><div class="lbl">📝 নোট</div><div class="val" style="font-size:13px;font-weight:500;color:var(--sub)"><?php echo e2($pledge['note']?:'-'); ?></div></div>
    </div>

    <div class="actions">
      <a class="btn btn-bk" href="/pawn.php">◀ ফিরে</a>
      <?php if($can_edit): ?>
        <button type="button" class="btn btn-edit" id="btnEdit">✏️ এডিট</button>
      <?php endif; ?>
      <?php if($cpay): ?>
        <a class="btn btn-py" href="/pledge_payment.php?pledge_id=<?php echo $pid; ?>">💵 পেমেন্ট নিন</a>
      <?php endif; ?>
      <?php if($clock): ?>
        <button type="button" class="btn btn-lk">🔐 লকারে পাঠান</button>
      <?php endif; ?>
      <?php if($cpay): ?>
        <button type="button" class="btn btn-cls">🔓 বন্ধক ফেরত</button>
      <?php endif; ?>
      <a class="btn btn-print" href="/receipt.php?pledge_id=<?php echo $pid; ?>&type=pledge" target="_blank">🖨️ বন্ধক রশিদ</a>
      <?php if($hld==='LOCKER'): ?>
      <a class="btn btn-print" href="/receipt.php?pledge_id=<?php echo $pid; ?>&type=locker" target="_blank">🖨️ লকার রশিদ</a>
      <?php endif; ?>
      <?php if($st2==='CLOSED'): ?>
      <a class="btn btn-print" href="/receipt.php?pledge_id=<?php echo $pid; ?>&type=return" target="_blank">🖨️ ফেরত রশিদ</a>
      <?php endif; ?>
    </div>
  </div>

  <!-- আইটেম লিস্ট -->
  <?php if(!empty($items)): ?>
  <div class="box">
    <div class="stitle">🪙 বন্ধকৃত আইটেম (<?php echo count($items); ?>টি)</div>
    <?php foreach($items as $idx => $it):
      $ig=(float)($it['gross_weight']??0);
      $in=(float)($it['net_weight']??0);
      $iv=(float)($it['estimated_value']??0);
      $qty=(int)($it['quantity']??1);
    ?>
    <div class="item-row">
      <div class="item-row-head">
        <div class="item-name"><?php echo ($idx+1).'. '.e2($it['item_name']); ?></div>
        <div class="item-badges">
          <?php if($it['purity']): ?>
          <span class="item-badge ib-karat"><?php echo e2($it['purity']); ?></span>
          <?php endif; ?>
          <span class="item-badge ib-qty"><?php echo $qty; ?> পিস</span>
        </div>
      </div>
      <div class="item-grid">
        <div class="item-cell">
          <div class="k">Gross ওজন</div>
          <div class="v"><?php echo number_format($ig,3); ?> গ্রাম</div>
          <div style="font-size:10px;color:var(--sub);margin-top:2px"><?php echo toVori($ig); ?></div>
        </div>
        <div class="item-cell">
          <div class="k">নিট ওজন</div>
          <div class="v"><?php echo number_format($in,3); ?> গ্রাম</div>
          <div style="font-size:10px;color:var(--sub);margin-top:2px"><?php echo toVori($in); ?></div>
        </div>
        <div class="item-cell" style="grid-column:1/-1">
          <div class="k">আনুমানিক মূল্য</div>
          <div class="v v-gold"><?php echo tk($iv); ?></div>
        </div>
      </div>
      <?php if($it['remarks']): ?>
      <div class="item-rem">📝 <?php echo e2($it['remarks']); ?></div>
      <?php endif; ?>
    </div>
    <?php endforeach; ?>
  </div>
  <?php endif; ?>

  <!-- পেমেন্ট সারাংশ -->
  <div class="box">
    <div class="stitle">💳 পেমেন্ট সারাংশ</div>
    <div class="ig">
      <div class="iitem"><div class="lbl">মোট কয়বার</div><div class="val"><?php echo $tc; ?></div></div>
      <div class="iitem"><div class="lbl">মোট জমা</div><div class="val v-green"><?php echo tk($ta); ?></div></div>
      <div class="iitem"><div class="lbl">আসল জমা</div><div class="val v-green"><?php echo tk($tp); ?></div></div>
      <div class="iitem"><div class="lbl">লকার ভাড়া জমা</div><div class="val v-gold"><?php echo tk($ti); ?></div></div>
    </div>
  </div>

  <!-- পেমেন্ট হিস্ট্রি -->
  <div class="box">
    <div class="stitle">📋 পেমেন্ট হিস্ট্রি</div>
    <?php if(!$payments): ?>
      <div class="empty">এখনো কোনো পেমেন্ট নেই</div>
    <?php else: foreach($payments as $p):
      $a=(float)($p['amount']??0);$i=(float)($p['interest_paid']??0);$pr=(float)($p['principal_paid']??0);
      if($pr<=0&&$i<=0&&$a>0)$pr=$a;
    ?>
      <div class="pay-card">
        <div class="pay-top">
          <div class="pay-amt"><?php echo tk($a); ?></div>
          <div style="text-align:right">
            <div class="pay-date">📅 <?php echo e2($p['payment_date']??''); ?></div>
            <a href="/receipt.php?pledge_id=<?php echo $pid; ?>&type=payment&payment_id=<?php echo $p['id']; ?>" target="_blank" class="pay-recv-btn">🖨️ রশিদ</a>
          </div>
        </div>
        <div class="pay-meta">🏦 <?php echo e2($p['cash_account_name']?:'-'); ?><br>📝 <?php echo e2($p['note']?:'-'); ?></div>
        <div class="pay-split">
          <div class="sb"><div class="lbl">আসল</div><div class="val v-green"><?php echo tk($pr); ?></div></div>
          <div class="sb"><div class="lbl">লকার ভাড়া</div><div class="val v-gold"><?php echo tk($i); ?></div></div>
        </div>
      </div>
    <?php endforeach; endif; ?>
  </div>

<?php endif; ?>
</div>

<!-- এডিট MODAL -->
<?php if($can_edit): ?>
<div class="modal" id="modalEdit">
<div class="mbox">
  <div class="mhead">
    <div class="mht">✏️ বন্ধক এডিট</div>
    <button class="mcl" id="closeEdit">✕</button>
  </div>
  <div class="mbody">
    <div class="alert" id="editAlert"></div>

    <div class="msub">💰 মূল তথ্য</div>

    <div class="mf">
      <label>📅 বন্ধকের তারিখ *</label>
      <input type="date" id="eDate" value="<?php echo e2($pledge['pledge_date']); ?>" min="2021-01-01">
      <div class="hint">২০২১ সালের জানুয়ারি থেকে পরিবর্তন করা যাবে</div>
    </div>
    <div class="mf">
      <label>💵 ঋণের পরিমাণ (৳) *</label>
      <input type="number" id="eLoan" value="<?php echo $loan; ?>" min="1" step="1">
      <div class="hint">বর্তমান বাকি: <?php echo tk($out); ?></div>
    </div>
    <div class="mf">
      <label>📊 লকার ভাড়ার হার (% / মাস) *</label>
      <input type="number" id="eRate" value="<?php echo $irate; ?>" min="0" step="0.5">
    </div>

    <div class="msub">📍 অতিরিক্ত তথ্য</div>

    <div class="mf">
      <label>📍 লকার নাম / লোকেশন</label>
      <input type="text" id="eLocation" value="<?php echo e2($pledge['location_note']??''); ?>" placeholder="যেমন: লকার-১, শোরুম কাউন্টার-২">
      <div class="hint">কোন লকারে বা কোথায় রাখা আছে</div>
    </div>
    <div class="mf">
      <label>🏷️ বারকোড</label>
      <input type="text" id="eBarcode" value="<?php echo e2($pledge['manual_barcode']??''); ?>" placeholder="ঐচ্ছিক — বারকোড নম্বর">
    </div>
    <div class="mf">
      <label>📝 নোট</label>
      <textarea id="eNote" placeholder="বিশেষ তথ্য, পাথর আছে, ভাঙা..."><?php echo e2($pledge['note']??''); ?></textarea>
    </div>

    <div class="warn-box">⚠️ ঋণের পরিমাণ পরিবর্তন করলে বাকি আসলও আপডেট হবে।</div>
  </div>
  <div class="mfoot">
    <button class="btn-cancel" id="cancelEdit">বাতিল</button>
    <button class="btn-save" id="saveEdit">✔ সেভ করুন</button>
  </div>
</div>
</div>
<?php endif; ?>

<?php include __DIR__ . '/pledge_view_modals.php'; ?>
<input type="hidden" id="hdnPid" value="<?php echo (int)$pid; ?>">

<script>
var btnEdit=document.getElementById('btnEdit');
var modalEdit=document.getElementById('modalEdit');
if(btnEdit){ btnEdit.onclick=function(){modalEdit.classList.add('show');}; }
document.getElementById('closeEdit')&&(document.getElementById('closeEdit').onclick=function(){modalEdit.classList.remove('show');});
document.getElementById('cancelEdit')&&(document.getElementById('cancelEdit').onclick=function(){modalEdit.classList.remove('show');});
var saveEdit=document.getElementById('saveEdit');
if(saveEdit){
  saveEdit.onclick=function(){
    var date=document.getElementById('eDate').value;
    var loan=parseFloat(document.getElementById('eLoan').value||'0');
    var rate=parseFloat(document.getElementById('eRate').value||'0');
    var location_note=document.getElementById('eLocation').value.trim();
    var manual_barcode=document.getElementById('eBarcode').value.trim();
    var note=document.getElementById('eNote').value.trim();
    var al=document.getElementById('editAlert');
    function showA(msg,type){al.textContent=msg;al.className='alert '+type;al.style.display='block';if(type==='ok')setTimeout(function(){al.style.display='none';},2500);}
    if(!date) return showA('তারিখ দিন','err');
    if(loan<=0) return showA('ঋণের পরিমাণ দিন','err');
    saveEdit.disabled=true;saveEdit.textContent='সেভ হচ্ছে...';
    fetch('/api/pawn/pledge_update.php',{method:'POST',headers:{'Content-Type':'application/json'},credentials:'same-origin',
      body:JSON.stringify({
        pledge_id:<?php echo $pid;?>,
        pledge_date:date,
        loan_amount:loan,
        interest_rate:rate,
        location_note:location_note,
        manual_barcode:manual_barcode,
        note:note
      })})
    .then(function(r){return r.json();})
    .then(function(d){
      if(d.success){showA('✅ সেভ হয়েছে!','ok');setTimeout(function(){location.reload();},1500);}
      else showA('❌ '+(d.message||'সমস্যা'),'err');
      saveEdit.disabled=false;saveEdit.textContent='✔ সেভ করুন';
    }).catch(function(){showA('❌ সংযোগ সমস্যা','err');saveEdit.disabled=false;saveEdit.textContent='✔ সেভ করুন';});
  };
}
</script>
<script src="/ui/js/pledge_view.js?v=<?php echo time(); ?>"></script>
</body>
</html>