<?php
require_once __DIR__ . '/../app/init.php';
require_once __DIR__ . '/../app/layout.php';
require_once __DIR__ . '/../fleet/_lib.php';
require_login();
$pdo=db(); $cid=(int)$_SESSION['company_id']; fleet_ensure_schema($pdo);
$customerId=(int)($_GET['customer_id']??0);
$cust=$pdo->prepare("SELECT * FROM customers WHERE id=? AND company_id=? LIMIT 1"); $cust->execute([$customerId,$cid]); $customer=$cust->fetch(PDO::FETCH_ASSOC);
if(!$customer){ die('Customer not found'); }
$entries=[];
if((float)($customer['opening_balance']??0) != 0){ $entries[]=['date'=>'', 'ref'=>'رصيد افتتاحي', 'memo'=>'رصيد افتتاحي', 'debit'=>(float)$customer['opening_balance'], 'credit'=>0]; }
if(fleet_table_exists($pdo,'customer_service_logs')){
  $st=$pdo->prepare("SELECT service_date d, CONCAT('SRV-',id) ref, notes memo, total amt, payment_method FROM customer_service_logs WHERE company_id=? AND customer_id=?"); $st->execute([$cid,$customerId]);
  foreach($st->fetchAll(PDO::FETCH_ASSOC) as $r){ $entries[]=['date'=>$r['d'],'ref'=>$r['ref'],'memo'=>$r['memo']?:'خدمة عميل','debit'=>(float)$r['amt'],'credit'=>0]; }
}
if(fleet_table_exists($pdo,'invoices')){
  $st=$pdo->prepare("SELECT invoice_date d, invoice_no ref, notes memo, total amt FROM invoices WHERE company_id=? AND customer_id=?"); $st->execute([$cid,$customerId]);
  foreach($st->fetchAll(PDO::FETCH_ASSOC) as $r){ $entries[]=['date'=>$r['d'],'ref'=>$r['ref'],'memo'=>$r['memo']?:'فاتورة','debit'=>(float)$r['amt'],'credit'=>0]; }
}
if(fleet_table_exists($pdo,'payments') && fleet_table_exists($pdo,'invoices')){
  $st=$pdo->prepare("SELECT p.pay_date d, CONCAT('PAY-',p.id) ref, p.notes memo, p.amount amt FROM payments p JOIN invoices i ON i.id=p.invoice_id WHERE i.company_id=? AND i.customer_id=?"); $st->execute([$cid,$customerId]);
  foreach($st->fetchAll(PDO::FETCH_ASSOC) as $r){ $entries[]=['date'=>$r['d'],'ref'=>$r['ref'],'memo'=>$r['memo']?:'تحصيل','debit'=>0,'credit'=>(float)$r['amt']]; }
}
if(fleet_table_exists($pdo,'vehicle_contracts')){
  $st=$pdo->prepare("SELECT start_date d, contract_no ref, notes memo, amount amt, payment_method FROM vehicle_contracts WHERE company_id=? AND customer_id=?"); $st->execute([$cid,$customerId]);
  foreach($st->fetchAll(PDO::FETCH_ASSOC) as $r){ if(($r['payment_method']??'CREDIT')==='CREDIT') $entries[]=['date'=>$r['d'],'ref'=>$r['ref'],'memo'=>$r['memo']?:'أمر تشغيل','debit'=>(float)$r['amt'],'credit'=>0]; }
}
usort($entries, fn($a,$b)=>strcmp(($a['date'] ?: '0000-00-00').$a['ref'], ($b['date'] ?: '0000-00-00').$b['ref']));
$balance=0; foreach($entries as &$e){ $balance += (float)$e['debit'] - (float)$e['credit']; $e['balance']=$balance; } unset($e);
render_layout('كشف حساب العميل','إدارة العملاء <span class="mx-1">/</span> كشف حساب عميل', function() use($customer,$entries,$balance){ ?>
<div class="container-fluid mt-3">
  <div class="d-flex justify-content-between align-items-center flex-wrap gap-2"><div><div class="fw-bold fs-4">كشف حساب العميل</div><div class="text-secondary small"><?= h($customer['full_name']) ?> — الرصيد الحالي: <strong><?= nf($balance) ?></strong></div></div><a class="btn btn-outline-dark" href="<?= url('/tourism/customers.php') ?>">عودة</a></div>
  <div class="card cardx mt-3"><div class="p-3"><div class="row g-2 small"><div class="col-md-4"><strong>الهاتف:</strong> <?= h($customer['phone']) ?></div><div class="col-md-4"><strong>البريد:</strong> <?= h($customer['email']) ?></div><div class="col-md-4"><strong>العنوان:</strong> <?= h($customer['address']) ?></div></div></div><div class="table-responsive px-3 pb-3"><table class="table align-middle mb-0"><thead><tr class="text-secondary small"><th>التاريخ</th><th>المرجع</th><th>البيان</th><th class="text-end">مدين</th><th class="text-end">دائن</th><th class="text-end">الرصيد</th></tr></thead><tbody><?php foreach($entries as $e): ?><tr><td><?= h($e['date'] ?: '-') ?></td><td><?= h($e['ref']) ?></td><td><?= h($e['memo']) ?></td><td class="text-end"><?= nf($e['debit']) ?></td><td class="text-end"><?= nf($e['credit']) ?></td><td class="text-end fw-semibold"><?= nf($e['balance']) ?></td></tr><?php endforeach; ?></tbody></table></div></div>
</div>
<?php }); ?>
