<?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($tenant['first_name'].' '.$tenant['last_name']) ?></h1>
    <p class="text-muted small mb-0">Code: <strong><?= Helper::e($tenant['tenant_code']) ?></strong> | <?= Helper::e($tenant['phone']) ?></p>
  </div>
  <div class="d-flex gap-2">
    <a href="<?= APP_URL ?>/admin/tenants/<?= $tenant['id'] ?>/edit" class="btn btn-outline-primary btn-sm"><i class="bi bi-pencil me-1"></i>Edit</a>
    <a href="<?= APP_URL ?>/admin/tenants" 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">
  <div class="col-md-4"><div class="admin-stat-card"><div class="stat-icon-wrap green"><i class="bi bi-cash-stack"></i></div><div><div class="stat-val" style="font-size:1.1rem"><?= Helper::money($stats['total_paid']) ?></div><div class="stat-lbl">Total Paid</div></div></div></div>
  <div class="col-md-4"><div class="admin-stat-card"><div class="stat-icon-wrap <?= $stats['balance']>0?'red':'green' ?>"><i class="bi bi-exclamation-circle"></i></div><div><div class="stat-val" style="font-size:1.1rem;color:<?= $stats['balance']>0?'#dc2626':'#16a34a' ?>"><?= Helper::money($stats['balance']) ?></div><div class="stat-lbl">Outstanding Balance</div></div></div></div>
  <div class="col-md-4"><div class="admin-stat-card"><div class="stat-icon-wrap blue"><i class="bi bi-file-earmark-text"></i></div><div><div class="stat-val"><?= $stats['active_leases'] ?></div><div class="stat-lbl">Active Leases</div></div></div></div>
</div>

<div class="row g-4">
  <div class="col-lg-4">
    <div class="form-card mb-3">
      <div class="form-section-title">Personal Info</div>
      <div class="text-center mb-3">
        <?php if ($tenant['photo']): ?>
        <img src="<?= APP_URL ?>/uploads/avatars/<?= Helper::e($tenant['photo']) ?>" class="rounded-circle" width="80" height="80" style="object-fit:cover">
        <?php else: ?>
        <div class="rounded-circle bg-primary d-flex align-items-center justify-content-center text-white fw-800 mx-auto" style="width:80px;height:80px;font-size:2rem"><?= strtoupper(substr($tenant['first_name'],0,1)) ?></div>
        <?php endif; ?>
        <div class="mt-2"><?= Helper::statusBadge($tenant['status']) ?></div>
      </div>
      <div class="small">
        <?php foreach ([
          ['Gender','gender'],['DOB','date_of_birth'],['Nationality','nationality'],
          ['National ID','national_id'],['Occupation','occupation'],['Employer','employer'],
        ] as [$lbl,$key]): ?>
        <div class="d-flex justify-content-between mb-1 pb-1 border-bottom">
          <span class="text-muted"><?= $lbl ?>:</span>
          <span class="fw-600"><?= Helper::e($tenant[$key]??'—') ?></span>
        </div>
        <?php endforeach; ?>
      </div>
    </div>
    <div class="form-card">
      <div class="form-section-title">Emergency Contact</div>
      <div class="small">
        <div class="fw-600"><?= Helper::e($tenant['emergency_contact_name']??'—') ?></div>
        <div class="text-muted"><?= Helper::e($tenant['emergency_contact_phone']??'') ?></div>
        <div class="text-muted"><?= Helper::e($tenant['emergency_contact_relation']??'') ?></div>
      </div>
    </div>
  </div>
  <div class="col-lg-8">
    <!-- Active Leases -->
    <div class="data-card mb-3">
      <div class="data-card-header"><h6>Leases</h6><a href="<?= APP_URL ?>/admin/leases/create?tenant_id=<?= $tenant['id'] ?>" class="btn btn-sm btn-primary">New Lease</a></div>
      <div class="table-responsive">
        <table class="table table-sm mb-0">
          <thead><tr><th>Lease No</th><th>Property/Unit</th><th>Rent</th><th>Start</th><th>End</th><th>Status</th></tr></thead>
          <tbody>
            <?php foreach ($leases as $l): ?>
            <tr>
              <td class="fw-600 text-primary small"><a href="<?= APP_URL ?>/admin/leases/<?= $l['id'] ?>"><?= Helper::e($l['lease_number']) ?></a></td>
              <td class="small"><?= Helper::e($l['property_name']) ?> / <?= Helper::e($l['unit_number']) ?></td>
              <td><?= Helper::money($l['monthly_rent']) ?></td>
              <td class="small"><?= Helper::date($l['start_date']) ?></td>
              <td class="small"><?= Helper::date($l['end_date']) ?></td>
              <td><?= Helper::statusBadge($l['status']) ?></td>
            </tr>
            <?php endforeach; ?>
            <?php if (empty($leases)): ?><tr><td colspan="6" class="text-center text-muted py-3 small">No leases</td></tr><?php endif; ?>
          </tbody>
        </table>
      </div>
    </div>
    <!-- Recent Payments -->
    <div class="data-card mb-3">
      <div class="data-card-header"><h6>Recent Payments</h6><a href="<?= APP_URL ?>/admin/payments?tenant=<?= urlencode($tenant['first_name']) ?>" class="btn btn-sm btn-outline-primary">All</a></div>
      <div class="table-responsive">
        <table class="table table-sm mb-0">
          <thead><tr><th>Receipt</th><th>Amount</th><th>Method</th><th>Date</th><th>Status</th></tr></thead>
          <tbody>
            <?php foreach ($payments as $p): ?>
            <tr>
              <td class="fw-600 small"><?= Helper::e($p['receipt_number']??$p['payment_ref']) ?></td>
              <td class="text-success fw-600"><?= Helper::money($p['amount']) ?></td>
              <td class="small"><?= Helper::e($p['method_name']??'Cash') ?></td>
              <td class="small text-muted"><?= Helper::date($p['payment_date']) ?></td>
              <td><?= Helper::statusBadge($p['status']) ?></td>
            </tr>
            <?php endforeach; ?>
            <?php if (empty($payments)): ?><tr><td colspan="5" class="text-center text-muted py-2 small">No payments</td></tr><?php endif; ?>
          </tbody>
        </table>
      </div>
    </div>
    <!-- Maintenance -->
    <div class="data-card">
      <div class="data-card-header"><h6>Maintenance Requests</h6></div>
      <div class="table-responsive">
        <table class="table table-sm mb-0">
          <thead><tr><th>Ref</th><th>Issue</th><th>Priority</th><th>Status</th><th>Date</th></tr></thead>
          <tbody>
            <?php foreach ($maintenance as $m): ?>
            <tr><td class="small fw-600"><?= Helper::e($m['request_number']) ?></td><td class="small"><?= Helper::e($m['title']) ?></td><td><?= Helper::statusBadge($m['priority']) ?></td><td><?= Helper::statusBadge($m['status']) ?></td><td class="small text-muted"><?= Helper::timeAgo($m['created_at']) ?></td></tr>
            <?php endforeach; ?>
            <?php if (empty($maintenance)): ?><tr><td colspan="5" class="text-center text-muted py-2 small">No requests</td></tr><?php endif; ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>
