<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>

<div class="page-header">
  <div>
    <h1><i class="bi bi-grid me-2"></i><?= esc($facility['name']) ?> — Units</h1>
    <nav aria-label="breadcrumb"><ol class="breadcrumb">
      <li class="breadcrumb-item"><a href="<?= base_url('facilities') ?>">Facilities</a></li>
      <li class="breadcrumb-item"><a href="<?= base_url('facilities/view/'.$facility['id']) ?>"><?= esc($facility['name']) ?></a></li>
      <li class="breadcrumb-item active">Units</li>
    </ol></nav>
  </div>
  <a href="<?= base_url('facilities/'.$facility['id'].'/units/create') ?>" class="btn btn-fm-primary btn-sm">
    <i class="bi bi-plus me-1"></i>Add Unit
  </a>
</div>

<!-- KPI Strip -->
<div class="row g-3 mb-3">
  <div class="col-6 col-md-3"><div class="kpi-card kpi-primary"><div class="d-flex align-items-center gap-3"><div class="kpi-icon"><i class="bi bi-grid"></i></div><div><div class="kpi-label">Total Units</div><div class="kpi-value"><?= $kpi['total'] ?></div></div></div></div></div>
  <div class="col-6 col-md-3"><div class="kpi-card kpi-green"><div class="d-flex align-items-center gap-3"><div class="kpi-icon"><i class="bi bi-person-check"></i></div><div><div class="kpi-label">Occupied</div><div class="kpi-value"><?= $kpi['occupied'] ?></div><div class="kpi-sub"><?= $kpi['occupancy_pct'] ?>% rate</div></div></div></div></div>
  <div class="col-6 col-md-3"><div class="kpi-card kpi-teal"><div class="d-flex align-items-center gap-3"><div class="kpi-icon"><i class="bi bi-house"></i></div><div><div class="kpi-label">Vacant</div><div class="kpi-value"><?= $kpi['vacant'] ?></div></div></div></div></div>
  <div class="col-6 col-md-3"><div class="kpi-card kpi-orange"><div class="d-flex align-items-center gap-3"><div class="kpi-icon"><i class="bi bi-tools"></i></div><div><div class="kpi-label">Maintenance</div><div class="kpi-value"><?= $kpi['maintenance'] ?></div></div></div></div></div>
</div>

<!-- Filter -->
<div class="fm-card mb-3">
  <div class="fm-card-body py-2">
    <form method="get" class="d-flex gap-2 flex-wrap align-items-center">
      <div class="small fw-semibold text-muted me-1">Filter:</div>
      <?php foreach([''=>'All',  'occupied'=>'Occupied','vacant'=>'Vacant','maintenance'=>'Maintenance'] as $v=>$l): ?>
      <a href="?<?= $v?'status='.$v:'' ?>" class="btn btn-sm <?= $statusFilter===$v?'btn-fm-primary':'btn-outline-secondary' ?>"><?= $l ?></a>
      <?php endforeach; ?>
    </form>
  </div>
</div>

<!-- Units Grid -->
<?php if(empty($units)): ?>
<div class="fm-card"><div class="fm-card-body text-center py-5">
  <i class="bi bi-grid fs-1 text-muted d-block mb-3"></i>
  <p class="text-muted">No units found. <a href="<?= base_url('facilities/'.$facility['id'].'/units/create') ?>">Add the first unit</a>.</p>
</div></div>
<?php else: ?>
<div class="row g-3">
<?php foreach($units as $u):
  $statusColor = ['occupied'=>'success','vacant'=>'teal','maintenance'=>'orange'][$u['status']] ?? 'secondary';
  $expiring = $u['contract_end'] && strtotime($u['contract_end']) < strtotime('+30 days') && $u['status']==='occupied';
?>
<div class="col-md-4 col-lg-3">
  <div class="fm-card h-100 <?= $expiring?'border border-warning':'' ?>">
    <div class="fm-card-body">
      <!-- Unit Header -->
      <div class="d-flex justify-content-between align-items-start mb-2">
        <div>
          <div class="fw-bold" style="font-size:1.1rem">Unit <?= esc($u['unit_number']) ?></div>
          <?php if($u['floor']): ?><div class="x-small text-muted">Floor <?= esc($u['floor']) ?></div><?php endif; ?>
          <?php if($u['unit_type']): ?><div class="x-small text-muted"><?= esc(ucfirst($u['unit_type'])) ?></div><?php endif; ?>
        </div>
        <span class="fm-badge badge-status-<?= esc($u['status']) ?>"><?= ucfirst($u['status']) ?></span>
      </div>

      <!-- Tenant Info -->
      <?php if($u['status']==='occupied' && $u['tenant_name']): ?>
      <div class="fm-form-section mb-2 py-2 px-2">
        <div class="x-small text-muted mb-1">TENANT</div>
        <div class="small fw-semibold"><i class="bi bi-person me-1 text-primary"></i><?= esc($u['tenant_name']) ?></div>
        <?php if($u['tenant_mobile']): ?><div class="x-small text-muted"><i class="bi bi-telephone me-1"></i><?= esc($u['tenant_mobile']) ?></div><?php endif; ?>
        <?php if($u['contract_end']): ?>
        <?php $days = (int)ceil((strtotime($u['contract_end'])-time())/86400); ?>
        <div class="x-small <?= $days<30?'text-danger fw-bold':($days<60?'text-warning':'text-muted') ?>">
          <i class="bi bi-calendar me-1"></i>Exp: <?= date('d M Y',strtotime($u['contract_end'])) ?>
          <?= $days<30?"(⚠️ $days days)":'' ?>
        </div>
        <?php endif; ?>
      </div>
      <?php elseif($u['owner_name']): ?>
      <div class="small text-muted mb-2"><i class="bi bi-person me-1"></i><?= esc($u['owner_name']) ?></div>
      <?php endif; ?>

      <!-- Rent -->
      <?php if($u['rent_amount']): ?>
      <div class="small mb-2"><span class="text-muted">Rent:</span> <strong><?= $currency ?> <?= number_format($u['rent_amount'],0) ?>/mo</strong></div>
      <?php endif; ?>

      <?php if($expiring): ?>
      <div class="alert alert-warning py-1 px-2 mb-2" style="font-size:.72rem"><i class="bi bi-exclamation-triangle me-1"></i>Contract expiring soon</div>
      <?php endif; ?>

      <!-- Actions -->
      <div class="d-flex gap-1 mt-auto pt-2 flex-wrap">
        <a href="<?= base_url('units/view/'.$u['id']) ?>" class="btn btn-fm-primary btn-sm flex-fill"><i class="bi bi-eye me-1"></i>View</a>
        <a href="<?= base_url('units/edit/'.$u['id']) ?>" class="btn btn-fm-outline btn-sm"><i class="bi bi-pencil"></i></a>
        <?php if($u['status']==='vacant'): ?>
        <a href="<?= base_url('units/checklist/'.$u['id'].'/move_in') ?>" class="btn btn-sm btn-success" title="Move-In"><i class="bi bi-box-arrow-in-right"></i></a>
        <?php elseif($u['status']==='occupied'): ?>
        <a href="<?= base_url('units/checklist/'.$u['id'].'/move_out') ?>" class="btn btn-sm btn-warning" title="Move-Out"><i class="bi bi-box-arrow-right"></i></a>
        <?php endif; ?>
        <a href="<?= base_url('workorders/create?facility_id='.$facility['id'].'&unit_id='.$u['id']) ?>" class="btn btn-sm btn-outline-secondary" title="New Work Order"><i class="bi bi-tools"></i></a>
      </div>
    </div>
  </div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>

<?= $this->endSection() ?>
