<?php
/* ============================================================================
   WBMCZ SELLER — VISITS & MONEY COLLECTED
   ----------------------------------------------------------------------------
   Everything a seller needs after an order is handed to them:

     To collect   the orders where a customer still owes something
     Reported     what they have told the office they took, and whether it has
                  been verified
     My dues      how much company money they are holding right now

   Recording a collection here does not move the order on its own — the office
   verifies it first. That is deliberate: it is what keeps both sides honest.
   ============================================================================ */
require_once __DIR__ . '/../config.php';
require_once __DIR__ . '/../db.php';
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/Language.php';
require_once __DIR__ . '/../includes/shares.php';
require_once __DIR__ . '/../includes/wallet.php';
require_once __DIR__ . '/../includes/purchase.php';

if (!isLoggedIn()) { redirect('../login.php'); }
$user_id = (int)$_SESSION['user_id'];
$role    = (string)($_SESSION['user_type'] ?? '');
if ($role !== 'seller' && $role !== 'admin') { redirect('../dashboard.php'); }

$lang         = new Language($db);
$current_lang = $lang->getCurrentLanguage();

buy_ensure_schema($db);
wallet_ensure_schema($db);

$company_name = getSetting('company_name', 'WBMCZ');
$company_logo = getSetting('company_logo', 'assets/images/logo.png');

$u    = $db->select("SELECT * FROM users WHERE id = ?", [$user_id]);
$user = !empty($u) ? $u[0] : ['name' => 'Seller', 'mobile' => ''];
$wal  = wallet_balances($db, $user_id);
$wallet_balance = $wal['main'];

$flash_ok  = $_SESSION['flash_success'] ?? ''; unset($_SESSION['flash_success']);
$flash_err = $_SESSION['flash_error']   ?? ''; unset($_SESSION['flash_error']);

/* ---------- actions ---------- */
if (isset($_POST['record'])) {
    $r = buy_record_collection($db, (int)$_POST['order_id'], [
        'amount'    => (float)($_POST['amount'] ?? 0),
        'mode'      => (string)($_POST['mode'] ?? 'cash'),
        'reference' => trim((string)($_POST['reference'] ?? '')),
        'note'      => trim((string)($_POST['note'] ?? '')),
        'seller_id' => $user_id,
    ]);
    $_SESSION[!empty($r['ok']) ? 'flash_success' : 'flash_error'] = !empty($r['ok'])
        ? 'Recorded. The office will verify it and it will then show in your dues.'
        : ($r['error'] ?? 'Could not record it.');
    redirect('collections.php' . (!empty($_POST['back']) ? $_POST['back'] : ''));
}

if (isset($_POST['visit'])) {
    $r = buy_mark_visit($db, (int)$_POST['order_id'], (string)($_POST['visit_status'] ?? 'scheduled'),
                        (string)($_POST['visit_at'] ?? ''), trim((string)($_POST['visit_note'] ?? '')));
    $_SESSION[!empty($r['ok']) ? 'flash_success' : 'flash_error'] = !empty($r['ok']) ? 'Visit updated.' : ($r['error'] ?? 'Could not save it.');
    redirect('collections.php' . (!empty($_POST['back']) ? $_POST['back'] : ''));
}

/* ---------- what to show ---------- */
$tab = (string)($_GET['tab'] ?? 'collect');
if (!in_array($tab, ['collect', 'reported', 'dues'], true)) { $tab = 'collect'; }
$q = trim((string)($_GET['q'] ?? ''));

$todo = buy_orders($db, ['seller_id' => $user_id, 'due' => 1, 'q' => $q, 'limit' => 100]);
$mine = buy_collections($db, ['seller_id' => $user_id, 'q' => $q, 'limit' => 100]);
$dues = buy_seller_dues($db, $user_id);
$hist = buy_settlements($db, ['seller_id' => $user_id, 'limit' => 50]);
$stats = buy_stats($db, ['seller_id' => $user_id]);

$backq = '?' . http_build_query(array_filter(['tab' => $tab, 'q' => $q]));
$unread_notifs = $db->select("SELECT * FROM notifications WHERE user_id = ? AND is_read = 0 ORDER BY created_at DESC LIMIT 5", [$user_id]);

function pr($n) { return '₹' . number_format((float)$n, 2); }
function dt($s) { return $s ? date('d M Y, g:i A', strtotime($s)) : '—'; }
?>
<!DOCTYPE html>
<html lang="<?php echo $current_lang; ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Visits &amp; Collections - <?php echo esc($company_name); ?></title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700&family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
    <style>
        body { font-family:'Inter',sans-serif; }
        .font-poppins { font-family:'Poppins',sans-serif; }
        [x-cloak] { display:none !important; }
        .s-card { background:#fff; border:1px solid #e8edf5; border-radius:18px; box-shadow:0 10px 30px -22px rgba(15,23,42,.55); }
        .kpi { background:#fff; border:1px solid #e8edf5; border-radius:16px; padding:15px; }
        .tabbtn { padding:9px 15px; border-radius:12px; font-size:12.5px; font-weight:800; white-space:nowrap; }
        .tabbtn.on { background:#0b1220; color:#fff; }
        .tabbtn.off { background:#fff; color:#64748b; border:1px solid #e5e7eb; }
        .tag { display:inline-flex; align-items:center; gap:5px; padding:3px 9px; border-radius:999px; font-size:10.5px; font-weight:800; }
        .t-amber { background:#fffbeb; color:#b45309; border:1px solid #fde68a; }
        .t-green { background:#ecfdf5; color:#047857; border:1px solid #a7f3d0; }
        .t-red   { background:#fef2f2; color:#b91c1c; border:1px solid #fecaca; }
        .t-slate { background:#f1f5f9; color:#475569; border:1px solid #e2e8f0; }
        .fld { width:100%; border:1.5px solid #e8edf5; border-radius:12px; padding:10px 12px; font-size:.85rem; }
        .fld:focus { outline:none; border-color:#C8901B; box-shadow:0 0 0 3px rgba(200,144,27,.12); }
        .lbl { font-size:.68rem; font-weight:800; text-transform:uppercase; letter-spacing:.05em; color:#94a3b8; margin-bottom:4px; display:block; }
        .mini { padding:6px 11px; border-radius:10px; font-size:11.5px; font-weight:800; white-space:nowrap; display:inline-flex; align-items:center; gap:5px; }
    </style>
</head>
<?php
require_once __DIR__ . '/../includes/theme.php';
$__scats = [];
try { $__scats = $db->select("SELECT * FROM service_categories WHERE status = 1 ORDER BY display_order ASC"); } catch (\Throwable $e) {}
theme_open([
    'panel' => 'seller', 'base' => '../', 'title' => 'Visits & Collections', 'active' => 'collections',
    'user' => $user, 'wallet' => $wallet_balance, 'notifs' => $unread_notifs,
    'logo' => $company_logo, 'brand' => $company_name,
    'menu' => theme_seller_menu($__scats),
    'state' => "box: null, cur: { id: 0, code: '', due: 0 }",
]);
?>
<main class="flex-1 overflow-y-auto p-4 md:p-6 lg:p-8">
  <div class="max-w-6xl mx-auto">

    <?php if ($flash_ok): ?><div class="mb-4 px-4 py-3 rounded-xl bg-emerald-50 border border-emerald-200 text-emerald-800 text-sm font-semibold"><i class="fas fa-circle-check mr-1"></i> <?php echo esc($flash_ok); ?></div><?php endif; ?>
    <?php if ($flash_err): ?><div class="mb-4 px-4 py-3 rounded-xl bg-rose-50 border border-rose-200 text-rose-800 text-sm font-semibold"><i class="fas fa-circle-exclamation mr-1"></i> <?php echo esc($flash_err); ?></div><?php endif; ?>

    <div class="grid grid-cols-2 lg:grid-cols-4 gap-3 mb-5">
        <div class="kpi">
            <p class="text-[10.5px] font-extrabold uppercase tracking-wider text-slate-400">To collect</p>
            <p class="text-2xl font-extrabold text-amber-600 mt-1"><?php echo pr($stats['due']); ?></p>
            <p class="text-[11px] text-slate-400"><?php echo (int)$stats['pending_visits']; ?> visit(s) pending</p>
        </div>
        <div class="kpi">
            <p class="text-[10.5px] font-extrabold uppercase tracking-wider text-slate-400">Waiting to verify</p>
            <p class="text-2xl font-extrabold text-indigo-600 mt-1"><?php echo pr($dues['pending']); ?></p>
            <p class="text-[11px] text-slate-400">reported, not verified yet</p>
        </div>
        <div class="kpi">
            <p class="text-[10.5px] font-extrabold uppercase tracking-wider text-slate-400">You are holding</p>
            <p class="text-2xl font-extrabold <?php echo $dues['holding'] > 0 ? 'text-rose-600' : 'text-slate-400'; ?> mt-1"><?php echo pr($dues['holding']); ?></p>
            <p class="text-[11px] text-slate-400">company money to hand in</p>
        </div>
        <div class="kpi">
            <p class="text-[10.5px] font-extrabold uppercase tracking-wider text-slate-400">Handed in</p>
            <p class="text-2xl font-extrabold text-emerald-600 mt-1"><?php echo pr($dues['handed_in']); ?></p>
            <p class="text-[11px] text-slate-400">all time</p>
        </div>
    </div>

    <div class="flex flex-wrap items-center gap-2 mb-4">
        <a href="collections.php?tab=collect"  class="tabbtn <?php echo $tab === 'collect' ? 'on' : 'off'; ?>"><i class="fas fa-person-walking mr-1"></i> To collect (<?php echo count($todo); ?>)</a>
        <a href="collections.php?tab=reported" class="tabbtn <?php echo $tab === 'reported' ? 'on' : 'off'; ?>"><i class="fas fa-receipt mr-1"></i> What I reported</a>
        <a href="collections.php?tab=dues"     class="tabbtn <?php echo $tab === 'dues' ? 'on' : 'off'; ?>"><i class="fas fa-scale-balanced mr-1"></i> My dues</a>
        <form method="GET" class="ml-auto flex items-center gap-2">
            <input type="hidden" name="tab" value="<?php echo esc($tab); ?>">
            <input name="q" value="<?php echo esc($q); ?>" placeholder="Order code or customer…" class="fld" style="width:230px">
            <button class="mini bg-slate-900 text-white"><i class="fas fa-magnifying-glass"></i></button>
        </form>
    </div>

    <?php if ($tab === 'collect'): ?>
    <!-- ============ orders where money is still owed ============ -->
    <?php if (empty($todo)): ?>
        <div class="s-card p-10 text-center">
            <div class="w-20 h-20 mx-auto rounded-full bg-slate-50 flex items-center justify-center text-3xl text-slate-300"><i class="fas fa-check"></i></div>
            <h2 class="font-poppins text-lg font-bold text-slate-800 mt-4">Nothing outstanding</h2>
            <p class="text-[13px] text-slate-500 mt-1">Every order you have been given is fully paid.</p>
        </div>
    <?php else: ?>
    <div class="space-y-3">
        <?php foreach ($todo as $o):
            $due  = (float)$o['due_amount'];
            $wait = 0.0;
            foreach ($mine as $c) { if ((int)$c['order_id'] === (int)$o['id'] && $c['status'] === 'pending') { $wait += (float)$c['amount']; } }
            $room = round($due - $wait, 2);
            $ph   = preg_replace('/\D/', '', (string)$o['customer_mobile']);
            if (strlen($ph) > 10) { $ph = substr($ph, -10); }
        ?>
        <div class="s-card p-4 sm:p-5">
            <div class="flex flex-wrap items-start gap-3">
                <div class="flex-1 min-w-[220px]">
                    <div class="flex flex-wrap items-center gap-2">
                        <a href="../order.php?id=<?php echo (int)$o['id']; ?>" class="font-poppins font-bold text-slate-900 hover:text-[#C8901B]"><?php echo esc($o['order_code']); ?></a>
                        <?php $m = buy_stage_meta($o['stage']); ?>
                        <span class="tag t-slate"><i class="fas <?php echo $m[1]; ?>"></i> <?php echo esc($m[0]); ?></span>
                        <?php if ((string)$o['visit_status'] === 'done'): ?><span class="tag t-green">Visited</span><?php endif; ?>
                        <?php if ($wait > 0): ?><span class="tag t-amber"><?php echo pr($wait); ?> waiting to verify</span><?php endif; ?>
                    </div>
                    <p class="text-[13px] font-bold text-slate-700 mt-1.5"><?php echo esc($o['service_name']); ?></p>
                    <p class="text-[12.5px] text-slate-500 mt-0.5">
                        <i class="fas fa-user text-slate-300"></i> <?php echo esc($o['customer_name']); ?>
                        <?php if ($o['customer_mobile']): ?> · <?php echo esc($o['customer_mobile']); ?><?php endif; ?>
                    </p>
                    <?php if (!empty($o['address'])): ?>
                        <p class="text-[12.5px] text-slate-500 mt-1 leading-snug"><i class="fas fa-location-dot text-slate-300"></i> <?php echo esc($o['address']); ?></p>
                    <?php endif; ?>
                    <?php if (!empty($o['note'])): ?>
                        <p class="text-[12px] text-slate-400 mt-1"><i class="fas fa-note-sticky"></i> <?php echo esc($o['note']); ?></p>
                    <?php endif; ?>
                </div>

                <div class="text-right">
                    <p class="text-[10.5px] font-extrabold uppercase tracking-wider text-slate-400">To take</p>
                    <p class="text-2xl font-extrabold text-amber-600"><?php echo pr($due); ?></p>
                    <p class="text-[11px] text-slate-400">of <?php echo pr($o['amount']); ?> · <?php echo pr($o['paid_amount']); ?> already paid</p>
                </div>
            </div>

            <div class="flex flex-wrap gap-2 mt-3 pt-3 border-t border-slate-100">
                <?php if ($ph !== ''): ?>
                    <a href="tel:<?php echo $ph; ?>" class="mini bg-slate-100 text-slate-700 hover:bg-slate-200"><i class="fas fa-phone"></i> Call</a>
                    <a href="https://wa.me/91<?php echo $ph; ?>?text=<?php echo rawurlencode('Namaskar, ' . $o['customer_name'] . '. Regarding your order ' . $o['order_code'] . ' — when may I come?'); ?>"
                       target="_blank" rel="noopener" class="mini bg-[#25d366] text-white hover:opacity-90"><i class="fab fa-whatsapp"></i> WhatsApp</a>
                <?php endif; ?>
                <?php if (!empty($o['address'])): ?>
                    <a href="https://maps.google.com/?q=<?php echo rawurlencode($o['address']); ?>" target="_blank" rel="noopener"
                       class="mini bg-slate-100 text-slate-700 hover:bg-slate-200"><i class="fas fa-map-location-dot"></i> Map</a>
                <?php endif; ?>

                <form method="POST" class="inline">
                    <input type="hidden" name="order_id" value="<?php echo (int)$o['id']; ?>">
                    <input type="hidden" name="visit_status" value="done">
                    <input type="hidden" name="back" value="<?php echo esc($backq); ?>">
                    <button name="visit" value="1" class="mini bg-white text-slate-600 border border-slate-200 hover:bg-slate-50"><i class="fas fa-house-circle-check"></i> Visited</button>
                </form>

                <?php if ($room > 0): ?>
                <button @click="box = 'take'; cur = { id: <?php echo (int)$o['id']; ?>, code: '<?php echo esc(addslashes($o['order_code'])); ?>', due: <?php echo $room; ?> }"
                        class="mini bg-gradient-to-r from-[#C8901B] to-[#e0a92c] text-[#14202f] ml-auto">
                    <i class="fas fa-sack-dollar"></i> I collected <?php echo pr($room); ?>
                </button>
                <?php else: ?>
                    <span class="mini bg-amber-50 text-amber-700 border border-amber-200 ml-auto"><i class="fas fa-clock"></i> With the office</span>
                <?php endif; ?>
            </div>
        </div>
        <?php endforeach; ?>
    </div>
    <?php endif; ?>
    <?php endif; ?>

    <?php if ($tab === 'reported'): ?>
    <!-- ============ what this seller has reported ============ -->
    <div class="s-card overflow-hidden">
        <div class="overflow-x-auto">
        <table class="w-full text-[12.5px]">
            <thead><tr class="bg-slate-50 text-slate-500 text-[10.5px] font-extrabold uppercase tracking-wider">
                <th class="text-left p-3">Order</th><th class="text-left p-3">Amount</th>
                <th class="text-left p-3">How</th><th class="text-left p-3">When</th><th class="text-left p-3">Office</th>
            </tr></thead>
            <tbody>
            <?php if (empty($mine)): ?>
                <tr><td colspan="5" class="p-12 text-center text-slate-400">You have not reported any collection yet.</td></tr>
            <?php endif; ?>
            <?php foreach ($mine as $c): ?>
                <tr class="border-t border-slate-100">
                    <td class="p-3">
                        <a href="../order.php?id=<?php echo (int)$c['order_id']; ?>" class="font-bold text-indigo-700 hover:underline"><?php echo esc($c['order_code']); ?></a>
                        <p class="text-slate-400 text-[11.5px]"><?php echo esc($c['customer_name'] ?: ''); ?></p>
                    </td>
                    <td class="p-3 font-extrabold text-slate-800"><?php echo pr($c['amount']); ?></td>
                    <td class="p-3">
                        <span class="tag <?php echo buy_mode_holds($c['mode']) ? 't-amber' : 't-green'; ?>"><?php echo esc(buy_mode_label($c['mode'])); ?></span>
                        <?php if (!empty($c['reference'])): ?><p class="text-[11px] text-slate-400 mt-1"><?php echo esc($c['reference']); ?></p><?php endif; ?>
                    </td>
                    <td class="p-3 text-slate-500"><?php echo dt($c['created_at']); ?></td>
                    <td class="p-3">
                        <span class="tag <?php echo $c['status'] === 'approved' ? 't-green' : ($c['status'] === 'rejected' ? 't-red' : 't-amber'); ?>">
                            <?php echo $c['status'] === 'approved' ? 'Verified' : ($c['status'] === 'rejected' ? 'Rejected' : 'Waiting'); ?>
                        </span>
                        <?php if (!empty($c['approved_by_name'])): ?>
                            <p class="text-[11px] text-slate-400 mt-1"><?php echo esc($c['approved_by_name']); ?></p>
                        <?php endif; ?>
                        <?php if ($c['status'] === 'rejected' && !empty($c['note'])): ?>
                            <p class="text-[11px] text-rose-600 mt-0.5 max-w-[220px]"><?php echo esc($c['note']); ?></p>
                        <?php endif; ?>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
        </div>
    </div>
    <?php endif; ?>

    <?php if ($tab === 'dues'): ?>
    <!-- ============ the running account with the office ============ -->
    <div class="grid grid-cols-1 lg:grid-cols-3 gap-5">
        <div class="lg:col-span-1 s-card p-5">
            <h3 class="font-poppins font-bold text-slate-800 mb-3">Where you stand</h3>
            <div class="space-y-0.5 text-[13px]">
                <div class="flex justify-between py-2 border-b border-dashed border-slate-100"><span class="text-slate-500">Collected for the company</span><span class="font-bold text-slate-800"><?php echo pr($dues['collected']); ?></span></div>
                <div class="flex justify-between py-2 border-b border-dashed border-slate-100"><span class="text-slate-500">Handed in</span><span class="font-bold text-emerald-600">− <?php echo pr($dues['handed_in']); ?></span></div>
                <div class="flex justify-between py-2 border-b border-dashed border-slate-100"><span class="text-slate-500">Paid to you by the office</span><span class="font-bold text-indigo-600">+ <?php echo pr($dues['paid_out']); ?></span></div>
                <div class="flex justify-between py-3"><span class="font-bold text-slate-800">You are holding</span><span class="text-xl font-extrabold <?php echo $dues['holding'] > 0 ? 'text-rose-600' : 'text-slate-400'; ?>"><?php echo pr($dues['holding']); ?></span></div>
            </div>
            <?php if ($dues['pending'] > 0): ?>
                <p class="text-[12px] text-amber-700 bg-amber-50 border border-amber-200 rounded-xl p-3 mt-2 leading-snug">
                    <?php echo pr($dues['pending']); ?> more is reported but not verified yet — it joins the figure above once the office signs it off.
                </p>
            <?php endif; ?>
            <?php if ($dues['company'] > 0): ?>
                <p class="text-[12px] text-slate-500 mt-2 leading-snug">
                    <?php echo pr($dues['company']); ?> of what you brought in went straight to the company account, so it never sat with you.
                </p>
            <?php endif; ?>
            <p class="text-[11.5px] text-slate-400 mt-3 leading-relaxed">
                This is company money in your hands, not your earnings. What you earn reaches your wallet on its own when
                an order is completed.
            </p>
        </div>

        <div class="lg:col-span-2 s-card overflow-hidden">
            <div class="px-5 py-4 border-b border-slate-100">
                <h3 class="font-poppins font-bold text-slate-800">Money handed over</h3>
                <p class="text-[12px] text-slate-500">Recorded by the office each time cash changes hands.</p>
            </div>
            <div class="overflow-x-auto">
            <table class="w-full text-[12.5px]">
                <thead><tr class="bg-slate-50 text-slate-500 text-[10.5px] font-extrabold uppercase tracking-wider">
                    <th class="text-left p-3">Direction</th><th class="text-left p-3">Amount</th>
                    <th class="text-left p-3">How</th><th class="text-left p-3">When</th><th class="text-left p-3">Recorded by</th>
                </tr></thead>
                <tbody>
                <?php if (empty($hist)): ?>
                    <tr><td colspan="5" class="p-12 text-center text-slate-400">Nothing has been handed over yet.</td></tr>
                <?php endif; ?>
                <?php foreach ($hist as $h): ?>
                    <tr class="border-t border-slate-100">
                        <td class="p-3">
                            <span class="tag <?php echo $h['direction'] === 'in' ? 't-green' : 't-slate'; ?>">
                                <i class="fas <?php echo $h['direction'] === 'in' ? 'fa-arrow-right' : 'fa-arrow-left'; ?>"></i>
                                <?php echo $h['direction'] === 'in' ? 'You handed in' : 'Office paid you'; ?>
                            </span>
                        </td>
                        <td class="p-3 font-extrabold text-slate-800"><?php echo pr($h['amount']); ?></td>
                        <td class="p-3 text-slate-500">
                            <?php echo esc(ucfirst($h['mode'])); ?>
                            <?php if (!empty($h['reference'])): ?><span class="text-slate-400"> · <?php echo esc($h['reference']); ?></span><?php endif; ?>
                        </td>
                        <td class="p-3 text-slate-500"><?php echo dt($h['created_at']); ?></td>
                        <td class="p-3 text-slate-500"><?php echo esc($h['created_by_name'] ?: '—'); ?></td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
            </div>
        </div>
    </div>
    <?php endif; ?>

  </div>
</main>

<!-- record what was taken -->
<div x-show="box === 'take'" x-transition.opacity style="display:none" class="fixed inset-0 z-[110] flex items-center justify-center p-4">
    <div class="absolute inset-0 bg-slate-900/60 backdrop-blur-sm" @click="box = null"></div>
    <div class="relative w-full max-w-md bg-white rounded-2xl shadow-2xl overflow-hidden">
        <div class="px-5 py-4 bg-gradient-to-r from-[#0b1220] to-[#1e293b]">
            <h3 class="text-white font-bold font-poppins">What did you take?</h3>
            <p class="text-white/70 text-[12px]" x-text="'Order ' + cur.code"></p>
        </div>
        <form method="POST" class="p-5 space-y-3">
            <input type="hidden" name="order_id" :value="cur.id">
            <input type="hidden" name="back" value="<?php echo esc($backq); ?>">
            <div>
                <label class="lbl">Amount</label>
                <input type="number" step="0.01" min="0.01" name="amount" class="fld" :max="cur.due" :value="cur.due" required>
                <p class="text-[11px] text-slate-400 mt-1" x-text="'Outstanding on this order: ₹' + Number(cur.due).toFixed(2)"></p>
            </div>
            <div>
                <label class="lbl">How did the customer pay?</label>
                <select name="mode" class="fld">
                    <?php foreach (buy_collection_modes() as $k => $m): ?>
                        <option value="<?php echo $k; ?>"><?php echo esc($m[0]); ?> — <?php echo esc($m[3]); ?></option>
                    <?php endforeach; ?>
                </select>
                <p class="text-[11px] text-slate-400 mt-1">Cash and money into your own account both count as yours to hand in. Anything that went to the company account does not.</p>
            </div>
            <div>
                <label class="lbl">Reference (optional)</label>
                <input name="reference" class="fld" maxlength="120" placeholder="UPI id, receipt number…">
            </div>
            <div>
                <label class="lbl">Note (optional)</label>
                <input name="note" class="fld" maxlength="200">
            </div>
            <div class="flex justify-end gap-2 pt-1">
                <button type="button" @click="box = null" class="px-4 py-2 text-sm font-semibold text-slate-600 hover:bg-slate-100 rounded-lg">Cancel</button>
                <button name="record" value="1" class="px-5 py-2 text-sm font-bold text-[#14202f] rounded-lg shadow bg-gradient-to-r from-[#C8901B] to-[#e0a92c]">Report it</button>
            </div>
        </form>
    </div>
</div>

<?php theme_close(); ?>
</html>
