<?php use App\Helpers\Helper; ?>
<div class="d-flex justify-content-between align-items-center mb-4">
  <div>
    <h1 class="admin-page-title">Occupancy Report</h1>
    <p class="text-muted small">Occupancy Rate: <strong class="text-primary"><?= $summary['rate'] ?>%</strong></p>
  </div>
  <div class="d-flex gap-2">
    <button onclick="window.print()" class="btn btn-outline-primary btn-sm"><i class="bi bi-printer me-1"></i>Print</button>
    <a href="<?= APP_URL ?>/admin/reports" class="btn btn-outline-secondary btn-sm"><i class="bi bi-arrow-left me-1"></i>Back</a>
  </div>
</div>

<div class="row g-3 mb-4">
  <?php foreach ([
    ['label'=>'Total Units','value'=>$summary['total'],'color'=>'blue'],
    ['label'=>'Occupied','value'=>$summary['occupied'],'color'=>'red'],
    ['label'=>'Available','value'=>$summary['available'],'color'=>'green'],
    ['label'=>'Maintenance','value'=>$summary['maintenance'],'color'=>'orange'],
  ] as $s): ?>
  <div class="col-md-3"><div class="admin-stat-card"><div class="stat-icon-wrap <?= $s['color'] ?>"><i class="bi bi-door-open"></i></div><div><div class="stat-val"><?= $s['value'] ?></div><div class="stat-lbl"><?= $s['label'] ?></div></div></div></div>
  <?php endforeach; ?>
</div>

<!-- Occupancy Progress -->
<div class="form-card mb-4">
  <div class="d-flex justify-content-between mb-2">
    <span class="fw-600">Overall Occupancy</span>
    <span class="fw-700 text-primary"><?= $summary['rate'] ?>%</span>
  </div>
  <div class="occupancy-bar" style="height:16px"><div class="occupancy-bar-fill bg-primary" style="width:<?= $summary['rate'] ?>%"></div></div>
</div>

<!-- Filter -->
<div class="form-card mb-3">
  <form method="GET" class="d-flex gap-2">
    <select name="property_id" class="form-select form-select-sm" style="width:220px">
      <option value="">All Properties</option>
      <?php foreach ($properties as $p): ?><option value="<?= $p['id'] ?>" <?= $propId==$p['id']?'selected':'' ?>><?= Helper::e($p['name']) ?></option><?php endforeach; ?>
    </select>
    <button class="btn btn-sm btn-primary">Filter</button>
    <a href="<?= APP_URL ?>/admin/reports/occupancy" class="btn btn-sm btn-outline-secondary">Clear</a>
  </form>
</div>

<div class="data-card">
  <div class="table-responsive">
    <table class="table mb-0">
      <thead><tr><th>Unit</th><th>Building</th><th>Property</th><th>Type</th><th>Bed/Bath</th><th>Rent</th><th>Current Tenant</th><th>Status</th></tr></thead>
      <tbody>
        <?php foreach ($units as $u): ?>
        <tr>
          <td class="fw-600"><?= Helper::e($u['unit_number']) ?></td>
          <td class="small text-muted"><?= Helper::e($u['building_name']) ?></td>
          <td class="small"><?= Helper::e($u['property_name']) ?></td>
          <td><span class="badge bg-light text-dark"><?= Helper::e($u['unit_type']) ?></span></td>
          <td class="small"><?= $u['bedrooms'] ?>bd/<?= $u['bathrooms'] ?>ba</td>
          <td class="fw-600 text-primary"><?= Helper::money($u['monthly_rent']) ?></td>
          <td class="small"><?= Helper::e($u['current_tenant']??'—') ?></td>
          <td><?= Helper::statusBadge($u['status']) ?></td>
        </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
</div>
<style>@media print{.admin-topbar,.admin-sidebar,.form-card,.d-flex.mb-4{display:none!important}.admin-main{margin:0}}</style>
