<?php
$rows = $rows ?? [];
$counts = $counts ?? [];
$q = $q ?? '';
$status = $status ?? 'draft';
$assigned = (int) ($assigned ?? 0);
$showAssigned = (bool) ($showAssigned ?? false);
$accountId = (int) ($accountId ?? 0);
$created = $created ?? 'all';
$accounts = $accounts ?? [];
$owners = $owners ?? [];
$page = (int) ($page ?? 1);
$pages = (int) ($pages ?? 1);
$statusLabels = [
    'draft' => 'Draft',
    'pending_approval' => 'Waiting',
    'approved' => 'Approved',
    'rejected' => 'Rejected',
    'sent' => 'Sent',
    'signed' => 'Document Signed',
    'void' => 'Void',
];
?>
<div class="svc-page">
  <div class="d-flex flex-wrap justify-content-between align-items-start gap-3 mb-3">
    <div>
      <h1 class="page-title">Quotes</h1>
      <p class="page-sub mb-0">Turn a deal into a quote. A manager must say yes before you send it to the customer.</p>
    </div>
    <?php if (can_edit('quotes') && $accounts !== []): ?>
      <a class="btn btn-primary" href="#quote-create"><i class="bi bi-plus-lg"></i> New quote</a>
    <?php endif; ?>
  </div>
  <div class="task-summary is-five mb-3">
    <?php foreach ([['Draft', $counts['draft'] ?? 0, 'bi-pencil-fill', '#2563eb', '#eff6ff'], ['Waiting', $counts['pending_approval'] ?? 0, 'bi-hourglass-split', '#ea580c', '#fff7ed'], ['Approved', $counts['approved'] ?? 0, 'bi-check-circle-fill', '#16a34a', '#f0fdf4'], ['Sent', $counts['sent'] ?? 0, 'bi-send-fill', '#0f766e', '#f0fdfa'], ['Document Signed', $counts['signed'] ?? 0, 'bi-pen-fill', '#7c3aed', '#f5f3ff']] as $s): ?>
      <div class="task-summary-card" style="--task-accent: <?= e($s[3]) ?>; --task-tint: <?= e($s[4]) ?>">
        <div class="task-summary-icon"><i class="bi <?= e($s[2]) ?>"></i></div>
        <div><div class="task-summary-n"><?= (int) $s[1] ?></div><div class="task-summary-l"><?= e($s[0]) ?></div></div>
      </div>
    <?php endforeach; ?>
  </div>

  <form class="task-filters opp-filters card-soft p-3 mb-3" method="get">
    <div class="task-filter-search">
      <label class="form-label" for="quo-q">Search</label>
      <input class="form-control" id="quo-q" name="q" value="<?= e($q) ?>" placeholder="Search title, quote number, or account" autocomplete="off">
    </div>
    <?php if ($showAssigned): ?>
    <div>
      <label class="form-label" for="quo-assigned">Assigned to</label>
      <select class="form-select" id="quo-assigned" name="assigned">
        <option value="0" <?= $assigned === 0 ? 'selected' : '' ?>>All</option>
        <?php foreach ($owners as $o): ?>
          <option value="<?= (int) $o['id'] ?>" <?= $assigned === (int) $o['id'] ? 'selected' : '' ?>><?= e(trim($o['first_name'] . ' ' . $o['last_name'])) ?></option>
        <?php endforeach; ?>
      </select>
    </div>
    <?php endif; ?>
    <div>
      <label class="form-label" for="quo-account"><?= e(noun('account', 'Account')) ?></label>
      <select class="form-select" id="quo-account" name="account">
        <option value="0" <?= $accountId === 0 ? 'selected' : '' ?>>All</option>
        <?php foreach ($accounts as $acc): ?>
          <option value="<?= (int) $acc['id'] ?>" <?= $accountId === (int) $acc['id'] ? 'selected' : '' ?>><?= e($acc['name']) ?></option>
        <?php endforeach; ?>
      </select>
    </div>
    <div>
      <label class="form-label" for="quo-created">Created</label>
      <select class="form-select" id="quo-created" name="created">
        <option value="all" <?= $created === 'all' ? 'selected' : '' ?>>All</option>
        <option value="today" <?= $created === 'today' ? 'selected' : '' ?>>Today</option>
        <option value="week" <?= $created === 'week' ? 'selected' : '' ?>>This week</option>
        <option value="month" <?= $created === 'month' ? 'selected' : '' ?>>This month</option>
      </select>
    </div>
    <div>
      <label class="form-label" for="quo-status">Status</label>
      <select class="form-select" id="quo-status" name="status">
        <option value="all" <?= $status === 'all' ? 'selected' : '' ?>>All</option>
        <option value="draft" <?= $status === 'draft' ? 'selected' : '' ?>>Draft</option>
        <option value="pending_approval" <?= $status === 'pending_approval' ? 'selected' : '' ?>>Waiting</option>
        <option value="approved" <?= $status === 'approved' ? 'selected' : '' ?>>Approved</option>
        <option value="sent" <?= $status === 'sent' ? 'selected' : '' ?>>Sent</option>
        <option value="signed" <?= $status === 'signed' ? 'selected' : '' ?>>Document Signed</option>
        <option value="rejected" <?= $status === 'rejected' ? 'selected' : '' ?>>Rejected</option>
        <option value="void" <?= $status === 'void' ? 'selected' : '' ?>>Void</option>
      </select>
    </div>
    <div class="task-filter-go">
      <button class="btn btn-primary" type="submit"><i class="bi bi-search"></i> Search</button>
    </div>
  </form>

  <div class="row g-3">
    <div class="col-lg-7">
      <?php if (!$rows): ?>
        <div class="card-soft task-empty"><div class="task-empty-icon"><i class="bi bi-file-earmark-richtext"></i></div><h2>No quotes</h2><p>Try another filter, or create one from a customer or a won deal.</p></div>
      <?php else: foreach ($rows as $r):
          $st = commerce_status_label((string) $r['status'], $statusLabels[$r['status']] ?? null);
      ?>
        <div class="svc-visit svc-tone-<?= e($st['tone']) ?>">
          <a class="svc-visit-main" href="<?= e(url('quotes/' . $r['id'])) ?>">
            <div class="svc-visit-icon"><i class="bi bi-file-earmark-richtext"></i></div>
            <div class="min-w-0 flex-grow-1">
              <div class="svc-visit-name"><?= e($r['title']) ?></div>
              <div class="svc-visit-sub">
                <span><i class="bi bi-hash"></i> <?= e($r['number']) ?></span>
                <?php if (!empty($r['account_name'])): ?><span><i class="bi bi-buildings"></i> <?= e($r['account_name']) ?></span><?php endif; ?>
                <?php if (!empty($r['owner_name'])): ?><span><i class="bi bi-person"></i> <?= e($r['owner_name']) ?></span><?php endif; ?>
                <?php if (!empty($r['file_name'])): ?><span><i class="bi bi-file-earmark-pdf"></i> PDF</span><?php endif; ?>
              </div>
            </div>
            <div class="task-meta">
              <div class="svc-visit-amt"><strong><?= e(money_fmt($r['amount'])) ?></strong><span>Amount</span></div>
              <span class="due-pill <?= e($st['class']) ?>"><?= e($st['text']) ?></span>
            </div>
          </a>
        </div>
      <?php endforeach; endif; ?>
      <?php \App\Core\View::partial('pager', compact('page', 'pages')); ?>
    </div>
    <?php if (can_edit('quotes')): ?>
    <div class="col-lg-5" id="quote-create">
      <form class="svc-req ajax-form" action="<?= e(url('quotes')) ?>" method="post" enctype="multipart/form-data">
        <?= csrf_field() ?>
        <div class="svc-req-head">
          <span class="svc-req-icon"><i class="bi bi-file-earmark-richtext"></i></span>
          <div><div class="svc-req-kicker">New quote</div><h2>From this customer</h2><p>Upload the quote PDF now. A manager must approve it before you send it.</p></div>
        </div>
        <div class="svc-req-body">
          <label class="form-label"><?= e(noun('account', 'Account')) ?> *</label>
          <select class="form-select mb-2" name="account_id" required>
            <option value="">Select</option>
            <?php foreach ($accounts as $acc): ?><option value="<?= (int) $acc['id'] ?>"><?= e($acc['name']) ?></option><?php endforeach; ?>
          </select>
          <label class="form-label">Title *</label>
          <input class="form-control mb-2" name="title" required maxlength="180">
          <label class="form-label">Amount</label>
          <input class="form-control mb-2" name="amount" type="number" step="0.01" min="0">
          <label class="form-label">What is included</label>
          <textarea class="form-control mb-2" name="body" rows="3"></textarea>
          <label class="form-label">Quote PDF *</label>
          <input class="form-control mb-3" type="file" name="pdf" accept="application/pdf,.pdf" required>
          <div class="svc-req-foot">
            <div class="svc-wait-chips"><span class="lst-chip">Needs approval</span></div>
            <button class="btn btn-primary" type="submit"><i class="bi bi-check2"></i> Save draft</button>
          </div>
        </div>
      </form>
    </div>
    <?php endif; ?>
  </div>
</div>
