<?php
require_once __DIR__.'/../includes/functions.php';
$admin = require_admin();
$adminTitle = 'User Service Carts';
$adminSubtitle = 'View user/seller carts for admin-charge services. No customer-to-seller checkout is allowed.';
if (function_exists('mj2919_cart_ensure_schema')) { mj2919_cart_ensure_schema(); }
$search = trim((string)($_GET['q'] ?? ''));
$status = trim((string)($_GET['status'] ?? 'cart')) ?: 'cart';
$page = max(1,(int)($_GET['page'] ?? 1)); $per=30; $off=($page-1)*$per;
$where = ['c.status=?']; $params = [$status];
if ($search !== '') { $where[]='(u.name LIKE ? OR u.email LIKE ? OR c.title LIKE ? OR c.description LIKE ?)'; $like='%'.$search.'%'; array_push($params,$like,$like,$like,$like); }
$sqlWhere = implode(' AND ', $where);
$total = 0; $rows=[]; $summary=['open_carts'=>0,'open_amount'=>0,'invoiced_today'=>0];
try {
    $summary = mj2919_admin_cart_summary();
    $r=one('SELECT COUNT(*) c FROM mj_admin_charge_cart c LEFT JOIN mj_users u ON u.id=c.user_id WHERE '.$sqlWhere,$params); $total=(int)($r['c']??0);
    $rows=all_rows('SELECT c.*, u.name user_name, u.email user_email FROM mj_admin_charge_cart c LEFT JOIN mj_users u ON u.id=c.user_id WHERE '.$sqlWhere.' ORDER BY c.id DESC LIMIT '.$per.' OFFSET '.$off,$params);
} catch(Throwable $e) { flash('error',$e->getMessage()); }
include __DIR__.'/_header.php';
?>
<div class="row mb-3">
  <div class="col-md-4"><div class="small-box bg-primary"><div class="inner"><h3><?=e($summary['open_carts']??0)?></h3><p>Users with open carts</p></div><div class="icon"><i class="fas fa-shopping-cart"></i></div></div></div>
  <div class="col-md-4"><div class="small-box bg-success"><div class="inner"><h3><?=money((float)($summary['open_amount']??0),'SGD')?></h3><p>Open cart amount</p></div><div class="icon"><i class="fas fa-coins"></i></div></div></div>
  <div class="col-md-4"><div class="small-box bg-info"><div class="inner"><h3><?=e($summary['invoiced_today']??0)?></h3><p>Items invoiced today</p></div><div class="icon"><i class="fas fa-file-invoice"></i></div></div></div>
</div>
<div class="alert alert-info"><b>Scope lock:</b> These carts are only for seller/user payments to admin: extra slots, deal promotions and marketplace admin services. Customer-to-seller payment remains disabled.</div>
<div class="card card-primary card-outline">
  <div class="card-header"><h3 class="card-title">Cart Items</h3></div>
  <div class="card-body">
    <form class="row mb-3" method="get">
      <div class="col-md-5"><input class="form-control" name="q" value="<?=e($search)?>" placeholder="Search user, email, cart title"></div>
      <div class="col-md-3"><select class="form-control" name="status"><option value="cart" <?=$status==='cart'?'selected':''?>>Open cart</option><option value="invoiced" <?=$status==='invoiced'?'selected':''?>>Invoiced</option><option value="cancelled" <?=$status==='cancelled'?'selected':''?>>Cancelled</option></select></div>
      <div class="col-md-2"><button class="btn btn-primary btn-block">Filter</button></div>
      <div class="col-md-2"><a class="btn btn-outline-secondary btn-block" href="cart-abandoned.php">Reset</a></div>
    </form>
    <?php if(!$rows): ?><div class="text-center text-muted py-5"><i class="fas fa-shopping-cart fa-3x mb-3"></i><h5>No cart items found</h5><p>Open service carts will appear here.</p></div><?php else: ?>
    <div class="table-responsive"><table class="table table-hover align-middle"><thead><tr><th>User</th><th>Cart Item</th><th>Qty</th><th class="text-right">Amount</th><th>Status</th><th>Invoice</th><th>Created</th><th>Action</th></tr></thead><tbody>
    <?php foreach($rows as $r): ?><tr>
      <td><b><?=e($r['user_name'] ?: 'User #'.$r['user_id'])?></b><br><small><?=e($r['user_email']??'')?></small></td>
      <td><b><?=e($r['title'])?></b><br><small><?=e($r['description'])?></small></td>
      <td><?=e($r['quantity'])?></td>
      <td class="text-right"><b><?=money((float)$r['total_amount'],(string)$r['currency_code'])?></b></td>
      <td><span class="badge badge-<?=($r['status']==='cart'?'warning':($r['status']==='invoiced'?'success':'secondary'))?>"><?=e(ucwords($r['status']))?></span></td>
      <td><?php if(!empty($r['invoice_id'])): ?><a href="payment-center.php?tab=invoices&q=<?=e($r['invoice_id'])?>">Invoice #<?=e($r['invoice_id'])?></a><?php else: ?><span class="text-muted">Not yet invoiced</span><?php endif; ?></td>
      <td><?=e($r['created_at'])?></td>
      <td><a class="btn btn-sm btn-outline-primary" href="user-view.php?id=<?=e($r['user_id'])?>">User 360</a></td>
    </tr><?php endforeach; ?>
    </tbody></table></div>
    <?php endif; ?>
  </div>
</div>
<?php include __DIR__.'/_footer.php'; ?>
