<?php use App\Helpers\Helper; ?>
<div class="mb-4">
  <h1 class="admin-page-title">Welcome, <?= Helper::e($tenant['first_name']) ?>!</h1>
  <p class="text-muted small">Tenant Code: <strong><?= Helper::e($tenant['tenant_code']) ?></strong></p>
</div>

<div class="row g-3 mb-4">
  <div class="col-md-3">
    <div class="admin-stat-card">
      <div class="stat-icon-wrap <?= $balance>0?'red':'green' ?>"><i class="bi bi-cash-stack"></i></div>
      <div><div class="stat-val" style="font-size:1.3rem;color:<?= $balance>0?'#dc2626':'#16a34a' ?>"><?= Helper::money($balance) ?></div><div class="stat-lbl"><?= $balance>0?'Outstanding Balance':'Fully Paid' ?></div></div>
    </div>
  </div>
  <?php if ($lease): ?>
  <div class="col-md-3">
    <div class="admin-stat-card">
      <div class="stat-icon-wrap blue"><i class="bi bi-door-closed"></i></div>
      <div><div class="stat-val"><?= Helper::e($lease['unit_number']) ?></div><div class="stat-lbl"><?= Helper::e($lease['property_name']) ?></div></div>
    </div>
  </div>
  <div class="col-md-3">
    <div class="admin-stat-card">
      <div class="stat-icon-wrap orange"><i class="bi bi-calendar"></i></div>
      <div><div class="stat-val" style="font-size:1.1rem"><?= Helper::date($lease['end_date']) ?></div><div class="stat-lbl">Lease Expires</div></div>
    </div>
  </div>
  <div class="col-md-3">
    <div class="admin-stat-card">
      <div class="stat-icon-wrap purple"><i class="bi bi-cash"></i></div>
      <div><div class="stat-val" style="font-size:1.3rem"><?= Helper::money($lease['monthly_rent']) ?></div><div class="stat-lbl">Monthly Rent</div></div>
    </div>
  </div>
  <?php else: ?>
  <div class="col-md-9"><div class="alert alert-info mb-0"><i class="bi bi-info-circle me-2"></i>No active lease found. Contact management.</div></div>
  <?php endif; ?>
</div>

<?php if ($lease): ?>
<div class="row g-3 mb-4">
  <div class="col-md-6">
    <div class="form-card">
      <div class="form-section-title">Active Lease</div>
      <div class="row g-2 small">
        <div class="col-6"><span class="text-muted">Lease No:</span> <strong><?= Helper::e($lease['lease_number']??'—') ?></strong></div>
        <div class="col-6"><span class="text-muted">Start:</span> <strong><?= Helper::date($lease['start_date']) ?></strong></div>
        <div class="col-6"><span class="text-muted">End:</span> <strong><?= Helper::date($lease['end_date']) ?></strong></div>
        <div class="col-6"><span class="text-muted">Deposit:</span> <strong><?= Helper::money($lease['security_deposit']??0) ?></strong></div>
        <div class="col-6"><span class="text-muted">Due Day:</span> <strong><?= $lease['payment_due_day'] ?>th of month</strong></div>
        <div class="col-6"><span class="text-muted">Property:</span> <strong><?= Helper::e($lease['property_name']) ?></strong></div>
      </div>
      <a href="<?= APP_URL ?>/tenant/payments" class="btn btn-primary btn-sm mt-3 w-100"><i class="bi bi-credit-card me-2"></i>Make Payment</a>
    </div>
  </div>
  <div class="col-md-6">
    <div class="form-card">
      <div class="form-section-title">Quick Actions</div>
      <div class="d-flex flex-column gap-2">
        <a href="<?= APP_URL ?>/tenant/payments" class="quick-action"><i class="bi bi-credit-card"></i>View & Pay Invoices</a>
        <a href="<?= APP_URL ?>/tenant/maintenance" class="quick-action"><i class="bi bi-tools"></i>Submit Maintenance Request</a>
        <a href="<?= APP_URL ?>/tenant/profile" class="quick-action"><i class="bi bi-person-gear"></i>Update Profile</a>
        <a href="<?= APP_URL ?>/residences" class="quick-action"><i class="bi bi-buildings"></i>Browse Properties</a>
      </div>
    </div>
  </div>
</div>
<?php endif; ?>

<!-- Recent Payments -->
<div class="data-card">
  <div class="data-card-header">
    <h6>Recent Payments</h6>
    <a href="<?= APP_URL ?>/tenant/payments" class="btn btn-sm btn-outline-primary">All Payments</a>
  </div>
  <div class="table-responsive">
    <table class="table mb-0">
      <thead><tr><th>Receipt</th><th>Amount</th><th>For</th><th>Method</th><th>Date</th><th>Status</th><th></th></tr></thead>
      <tbody>
        <?php foreach ($recentPayments as $p): ?>
        <tr>
          <td class="fw-600 text-primary small"><?= Helper::e($p['receipt_number']??$p['payment_ref']) ?></td>
          <td class="fw-700 text-success"><?= Helper::money($p['amount']) ?></td>
          <td><span class="badge bg-light text-dark"><?= ucfirst(str_replace('_',' ',$p['payment_for'])) ?></span></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>
          <td><a href="<?= APP_URL ?>/tenant/receipt/<?= $p['id'] ?>" class="btn btn-xs btn-outline-secondary btn-sm"><i class="bi bi-download"></i></a></td>
        </tr>
        <?php endforeach; ?>
        <?php if (empty($recentPayments)): ?><tr><td colspan="7" class="text-center text-muted py-3 small">No payments yet</td></tr><?php endif; ?>
      </tbody>
    </table>
  </div>
</div>
