<?php
$rows = $rows ?? [];
$counts = $counts ?? [];
$q = $q ?? '';
$status = $status ?? 'active';
$accountId = (int) ($accountId ?? 0);
$accounts = $accounts ?? [];
$page = (int) ($page ?? 1);
$pages = (int) ($pages ?? 1);
$canEdit = can_edit('equipment');
$summaries = [
    ['Active', $counts['active'] ?? 0, 'bi-cpu-fill', '#0f766e', '#f0fdfa'],
    ['Retired', $counts['retired'] ?? 0, 'bi-archive-fill', '#6b7280', '#f3f4f6'],
    ['All', $counts['all'] ?? 0, 'bi-collection', '#2563eb', '#eff6ff'],
    ['Sites', $counts['sites'] ?? 0, 'bi-buildings', '#ea580c', '#fff7ed'],
];
?>
<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">Equipment</h1>
      <p class="page-sub mb-0">Machines at the customer site — model, serial, location, and who you service.</p>
    </div>
    <?php if ($canEdit && $accounts !== []): ?>
      <a class="btn btn-primary" href="#eq-create"><i class="bi bi-plus-lg"></i> New machine</a>
    <?php endif; ?>
  </div>

  <div class="task-summary mb-3">
    <?php foreach ($summaries 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="eq-q">Search</label>
      <input class="form-control" id="eq-q" name="q" value="<?= e($q) ?>" placeholder="Search name, model, serial, account, or location" autocomplete="off">
    </div>
    <div>
      <label class="form-label" for="eq-status">Status</label>
      <select class="form-select" id="eq-status" name="status">
        <option value="all" <?= $status === 'all' ? 'selected' : '' ?>>All</option>
        <option value="active" <?= $status === 'active' ? 'selected' : '' ?>>Active</option>
        <option value="retired" <?= $status === 'retired' ? 'selected' : '' ?>>Retired</option>
      </select>
    </div>
    <div>
      <label class="form-label" for="eq-account"><?= e(noun('account', 'Account')) ?></label>
      <select class="form-select" id="eq-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 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-cpu"></i></div>
          <h2>No equipment</h2>
          <p>Try another filter, or add a machine so the customer can see it on the Customer Portal.</p>
        </div>
      <?php else: foreach ($rows as $r):
          $st = equipment_status_label((string) ($r['status'] ?? 'active'));
          $model = trim((string) ($r['model'] ?? ''));
          $serial = trim((string) ($r['serial_no'] ?? ''));
          $loc = trim((string) ($r['location'] ?? ''));
          $installed = !empty($r['installed_at']) ? dt_fmt($r['installed_at'], 'M j, Y') : '';
      ?>
        <div class="svc-visit svc-tone-<?= e($st['tone']) ?>">
          <div class="svc-visit-main">
            <div class="svc-visit-icon"><i class="bi <?= $st['tone'] === 'retired' ? 'bi-archive' : 'bi-cpu' ?>"></i></div>
            <div class="min-w-0 flex-grow-1">
              <div class="svc-visit-name"><?= e((string) ($r['name'] ?? 'Machine')) ?></div>
              <div class="svc-visit-sub">
                <?php if (!empty($r['account_name'])): ?>
                  <a href="<?= e(url('accounts/' . (int) $r['account_id'])) ?>"><i class="bi bi-buildings"></i> <?= e((string) $r['account_name']) ?></a>
                <?php endif; ?>
                <?php if ($model !== ''): ?>
                  <span><i class="bi bi-tag"></i> <?= e($model) ?></span>
                <?php endif; ?>
                <?php if ($serial !== ''): ?>
                  <span><i class="bi bi-upc"></i> <?= e($serial) ?></span>
                <?php endif; ?>
                <?php if ($loc !== ''): ?>
                  <span><i class="bi bi-geo"></i> <?= e($loc) ?></span>
                <?php endif; ?>
                <?php if ($installed !== '' && $installed !== '—'): ?>
                  <span><i class="bi bi-calendar3"></i> <?= e($installed) ?></span>
                <?php endif; ?>
              </div>
            </div>
            <div class="task-meta">
              <span class="due-pill <?= e($st['class']) ?>"><?= e($st['text']) ?></span>
            </div>
          </div>
        </div>
      <?php endforeach; endif; ?>
      <?php \App\Core\View::partial('pager', compact('page', 'pages')); ?>
    </div>
    <?php if ($canEdit): ?>
    <div class="col-lg-5" id="eq-create">
      <form class="svc-req ajax-form" action="<?= e(url('equipment')) ?>" method="post">
        <?= csrf_field() ?>
        <div class="svc-req-head">
          <span class="svc-req-icon"><i class="bi bi-cpu"></i></span>
          <div>
            <div class="svc-req-kicker">New machine</div>
            <h2>On the customer site</h2>
            <p>Customers can see these machines on the Customer Portal.</p>
          </div>
        </div>
        <div class="svc-req-body">
          <label class="form-label" for="eq-new-account"><?= e(noun('account', 'Account')) ?> *</label>
          <select class="form-select mb-2" id="eq-new-account" name="account_id" required>
            <option value="">Select a site</option>
            <?php foreach ($accounts as $acc): ?>
              <option value="<?= (int) $acc['id'] ?>"><?= e($acc['name']) ?></option>
            <?php endforeach; ?>
          </select>
          <label class="form-label" for="eq-name">Name *</label>
          <input class="form-control mb-2" id="eq-name" name="name" required placeholder="Lobby copier, floor 2…">
          <label class="form-label" for="eq-model">Model</label>
          <input class="form-control mb-2" id="eq-model" name="model">
          <label class="form-label" for="eq-serial">Serial</label>
          <input class="form-control mb-2" id="eq-serial" name="serial_no">
          <label class="form-label" for="eq-loc">Location</label>
          <input class="form-control mb-2" id="eq-loc" name="location" placeholder="3rd floor copy room">
          <label class="form-label" for="eq-installed">Installed</label>
          <input class="form-control mb-2" id="eq-installed" type="date" name="installed_at">
          <div class="svc-req-foot">
            <div class="svc-wait-chips">
              <span class="lst-chip">On this list</span>
              <span class="lst-chip">Visible to customer</span>
            </div>
            <button class="btn btn-primary" type="submit"><i class="bi bi-check2"></i> Save equipment</button>
          </div>
        </div>
      </form>
    </div>
    <?php endif; ?>
  </div>
</div>
