<?php use App\Helpers\Helper; ?>
<div class="d-flex justify-content-between align-items-center mb-4">
  <div>
    <h1 class="admin-page-title"><?= Helper::e($property['name']) ?></h1>
    <p class="text-muted small mb-0"><i class="bi bi-geo-alt me-1"></i><?= Helper::e($property['address']??'') ?></p>
  </div>
  <div class="d-flex gap-2">
    <a href="<?= APP_URL ?>/admin/properties/<?= $property['id'] ?>/edit" class="btn btn-outline-primary btn-sm"><i class="bi bi-pencil me-1"></i>Edit</a>
    <a href="<?= APP_URL ?>/admin/properties" class="btn btn-outline-secondary btn-sm"><i class="bi bi-arrow-left me-1"></i>Back</a>
  </div>
</div>

<!-- Stats -->
<div class="row g-3 mb-4">
  <?php foreach ([
    ['label'=>'Total Units','value'=>$stats['total_units'],'color'=>'blue','icon'=>'bi-door-open'],
    ['label'=>'Occupied','value'=>$stats['occupied'],'color'=>'red','icon'=>'bi-person-check'],
    ['label'=>'Available','value'=>$stats['available'],'color'=>'green','icon'=>'bi-check-circle'],
    ['label'=>'Monthly Income','value'=>Helper::money($stats['monthly_income']),'color'=>'purple','icon'=>'bi-cash-stack'],
  ] as $s): ?>
  <div class="col-md-3">
    <div class="admin-stat-card">
      <div class="stat-icon-wrap <?= $s['color'] ?>"><i class="bi <?= $s['icon'] ?>"></i></div>
      <div><div class="stat-val" style="font-size:<?= strlen((string)$s['value'])>10?'1.1rem':'1.5rem' ?>"><?= $s['value'] ?></div><div class="stat-lbl"><?= $s['label'] ?></div></div>
    </div>
  </div>
  <?php endforeach; ?>
</div>

<div class="row g-4">
  <div class="col-lg-8">
    <!-- Buildings & Units -->
    <?php foreach ($buildings as $bld): ?>
    <div class="data-card mb-3">
      <div class="data-card-header">
        <h6><i class="bi bi-building me-2"></i><?= Helper::e($bld['name']) ?> (<?= $bld['floors'] ?> floor<?= $bld['floors']>1?'s':'' ?>)</h6>
        <a href="<?= APP_URL ?>/admin/units?building_id=<?= $bld['id'] ?>" class="btn btn-sm btn-outline-primary">Manage Units</a>
      </div>
      <div class="table-responsive">
        <table class="table table-sm mb-0">
          <thead><tr><th>Unit</th><th>Type</th><th>Bed/Bath</th><th>Rent</th><th>Status</th><th>Tenant</th></tr></thead>
          <tbody>
            <?php foreach (array_filter($units, fn($u)=>$u['building_id']==$bld['id']) as $unit): ?>
            <tr>
              <td class="fw-600"><?= Helper::e($unit['unit_number']) ?></td>
              <td><span class="badge bg-light text-dark"><?= Helper::e($unit['unit_type']) ?></span></td>
              <td class="small"><?= $unit['bedrooms'] ?>bd / <?= $unit['bathrooms'] ?>ba</td>
              <td class="fw-600 text-primary"><?= Helper::money($unit['monthly_rent']) ?></td>
              <td><?= Helper::statusBadge($unit['status']) ?></td>
              <td class="small text-muted"><?= Helper::e($unit['current_tenant']??'—') ?></td>
            </tr>
            <?php endforeach; ?>
          </tbody>
        </table>
      </div>
    </div>
    <?php endforeach; ?>
    <?php if (empty($buildings)): ?>
    <div class="form-card text-center py-4 text-muted">
      <i class="bi bi-building fs-1 d-block mb-2"></i>
      No buildings added yet. <a href="<?= APP_URL ?>/admin/buildings">Add Building</a>
    </div>
    <?php endif; ?>
  </div>
  <div class="col-lg-4">
    <div class="form-card mb-3">
      <div class="form-section-title">Property Info</div>
      <div class="small">
        <div class="d-flex justify-content-between mb-2"><span class="text-muted">Code:</span><span class="fw-600"><?= Helper::e($property['property_code']??'—') ?></span></div>
        <div class="d-flex justify-content-between mb-2"><span class="text-muted">Type:</span><span class="fw-600"><?= Helper::e($property['type_name']??'—') ?></span></div>
        <div class="d-flex justify-content-between mb-2"><span class="text-muted">Floors:</span><span class="fw-600"><?= $property['total_floors'] ?></span></div>
        <div class="d-flex justify-content-between mb-2"><span class="text-muted">Region:</span><span class="fw-600"><?= Helper::e($property['region_name']??'—') ?></span></div>
        <div class="d-flex justify-content-between mb-2"><span class="text-muted">Area:</span><span class="fw-600"><?= Helper::e($property['area']??'—') ?></span></div>
        <div class="d-flex justify-content-between mb-2"><span class="text-muted">Featured:</span><?= Helper::statusBadge($property['is_featured']?'active':'inactive') ?></div>
        <div class="d-flex justify-content-between mb-2"><span class="text-muted">Status:</span><?= Helper::statusBadge($property['status']) ?></div>
      </div>
    </div>
    <?php if (!empty($amenities)): ?>
    <div class="form-card mb-3">
      <div class="form-section-title">Amenities</div>
      <div class="d-flex flex-wrap gap-2">
        <?php foreach ($amenities as $a): ?>
        <span class="amenity-pill"><i class="bi bi-check text-success"></i><?= Helper::e($a['name']) ?></span>
        <?php endforeach; ?>
      </div>
    </div>
    <?php endif; ?>
    <div class="d-flex gap-2">
      <a href="<?= APP_URL ?>/admin/units/create?property_id=<?= $property['id'] ?>" class="btn btn-primary flex-fill"><i class="bi bi-plus me-1"></i>Add Unit</a>
      <a href="<?= APP_URL ?>/admin/buildings" class="btn btn-outline-primary flex-fill"><i class="bi bi-building me-1"></i>Buildings</a>
    </div>
  </div>
</div>
