<?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($lease['lease_number']) ?></h1>
    <p class="text-muted small mb-0"><?= Helper::e($lease['tenant_name']) ?> — <?= Helper::e($lease['property_name']) ?> / Unit <?= Helper::e($lease['unit_number']) ?></p>
  </div>
  <div class="d-flex gap-2">
    <a href="<?= APP_URL ?>/admin/leases/<?= $lease['id'] ?>/pdf" class="btn btn-outline-secondary btn-sm" target="_blank"><i class="bi bi-file-pdf me-1"></i>PDF</a>
    <a href="<?= APP_URL ?>/admin/leases/<?= $lease['id'] ?>/edit" class="btn btn-outline-primary btn-sm"><i class="bi bi-pencil me-1"></i>Edit</a>
    <a href="<?= APP_URL ?>/admin/leases" class="btn btn-outline-secondary btn-sm"><i class="bi bi-arrow-left me-1"></i>Back</a>
  </div>
</div>

<div class="row g-4">
  <div class="col-lg-8">
    <div class="form-card mb-4">
      <div class="form-section-title">Lease Details</div>
      <div class="row g-3 small">
        <div class="col-md-4"><div class="text-muted">Tenant</div><div class="fw-700"><?= Helper::e($lease['tenant_name']) ?></div><div class="text-muted"><?= Helper::e($lease['phone']) ?></div></div>
        <div class="col-md-4"><div class="text-muted">National ID</div><div class="fw-600"><?= Helper::e($lease['national_id']??'—') ?></div></div>
        <div class="col-md-4"><div class="text-muted">Status</div><?= Helper::statusBadge($lease['status']) ?></div>
        <div class="col-md-4"><div class="text-muted">Property</div><div class="fw-600"><?= Helper::e($lease['property_name']) ?></div></div>
        <div class="col-md-4"><div class="text-muted">Unit</div><div class="fw-600"><?= Helper::e($lease['unit_number']) ?> (<?= Helper::e($lease['unit_type']) ?>)</div></div>
        <div class="col-md-4"><div class="text-muted">Floor Area</div><div class="fw-600"><?= $lease['floor_size'] ? $lease['floor_size'].' sqm' : '—' ?></div></div>
        <div class="col-md-3"><div class="text-muted">Start Date</div><div class="fw-600"><?= Helper::date($lease['start_date']) ?></div></div>
        <div class="col-md-3"><div class="text-muted">End Date</div><div class="fw-600 <?= strtotime($lease['end_date']??'')<time()&&$lease['status']==='active'?'text-danger':'' ?>"><?= Helper::date($lease['end_date']) ?></div></div>
        <div class="col-md-3"><div class="text-muted">Frequency</div><div class="fw-600"><?= ucfirst(str_replace('_',' ',$lease['payment_frequency'])) ?></div></div>
        <div class="col-md-3"><div class="text-muted">Due Day</div><div class="fw-600"><?= $lease['payment_due_day'] ?>th of month</div></div>
      </div>
    </div>
    <div class="form-card mb-4">
      <div class="form-section-title">Financial Terms</div>
      <div class="row g-3 small">
        <div class="col-md-3"><div class="text-muted">Monthly Rent</div><div class="fw-700 text-primary"><?= Helper::money($lease['monthly_rent']) ?></div></div>
        <div class="col-md-3"><div class="text-muted">Security Deposit</div><div class="fw-600"><?= Helper::money($lease['security_deposit']??0) ?></div></div>
        <div class="col-md-3"><div class="text-muted">Advance</div><div class="fw-600"><?= Helper::money($lease['advance_payment']??0) ?></div></div>
        <div class="col-md-3"><div class="text-muted">Discount</div><div class="fw-600 text-success"><?= Helper::money($lease['discount_amount']??0) ?></div></div>
        <div class="col-md-3"><div class="text-muted">Grace Period</div><div class="fw-600"><?= $lease['grace_period_days'] ?> days</div></div>
        <div class="col-md-3"><div class="text-muted">Late Fee</div><div class="fw-600"><?= $lease['late_fee_percent'] ?>%</div></div>
        <div class="col-md-3"><div class="text-muted">VAT</div><?= Helper::statusBadge($lease['vat_applicable']?'active':'inactive') ?></div>
        <div class="col-md-3"><div class="text-muted">Utilities Incl.</div><?= Helper::statusBadge($lease['utilities_included']?'active':'inactive') ?></div>
      </div>
    </div>
    <?php if ($lease['terms_conditions']): ?>
    <div class="form-card mb-4">
      <div class="form-section-title">Terms & Conditions</div>
      <div class="small text-muted"><?= nl2br(Helper::e($lease['terms_conditions'])) ?></div>
    </div>
    <?php endif; ?>
    <!-- Invoices -->
    <div class="data-card mb-4">
      <div class="data-card-header"><h6>Invoices</h6><a href="<?= APP_URL ?>/admin/invoices/generate" class="btn btn-sm btn-outline-primary">Generate</a></div>
      <div class="table-responsive">
        <table class="table table-sm mb-0">
          <thead><tr><th>Invoice No</th><th>Period</th><th>Total</th><th>Paid</th><th>Balance</th><th>Status</th><th></th></tr></thead>
          <tbody>
            <?php foreach ($invoices as $inv): ?>
            <tr>
              <td class="fw-600 text-primary small"><?= Helper::e($inv['invoice_number']) ?></td>
              <td class="small"><?= date('M Y', strtotime($inv['period_from'])) ?></td>
              <td><?= Helper::money($inv['total_amount']) ?></td>
              <td class="text-success"><?= Helper::money($inv['paid_amount']) ?></td>
              <td class="<?= ($inv['balance']??0)>0?'text-danger fw-700':'' ?>"><?= Helper::money($inv['balance']??$inv['total_amount']-$inv['paid_amount']) ?></td>
              <td><?= Helper::statusBadge($inv['status']) ?></td>
              <td><a href="<?= APP_URL ?>/admin/invoices/<?= $inv['id'] ?>/pdf" class="btn btn-xs btn-outline-secondary btn-sm" target="_blank"><i class="bi bi-file-pdf"></i></a></td>
            </tr>
            <?php endforeach; ?>
            <?php if(empty($invoices)): ?><tr><td colspan="7" class="text-center text-muted py-3 small">No invoices yet</td></tr><?php endif; ?>
          </tbody>
        </table>
      </div>
    </div>
    <!-- Payments -->
    <div class="data-card">
      <div class="data-card-header"><h6>Payments</h6><a href="<?= APP_URL ?>/admin/payments/create" class="btn btn-sm btn-primary">Record Payment</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 yet</td></tr><?php endif; ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>
  <div class="col-lg-4">
    <div class="form-card mb-3">
      <div class="form-section-title">Quick Actions</div>
      <div class="d-flex flex-column gap-2">
        <a href="<?= APP_URL ?>/admin/leases/<?= $lease['id'] ?>/pdf" class="quick-action" target="_blank"><i class="bi bi-file-pdf text-danger"></i>Download Contract PDF</a>
        <a href="<?= APP_URL ?>/admin/payments/create" class="quick-action"><i class="bi bi-credit-card"></i>Record Payment</a>
        <a href="<?= APP_URL ?>/admin/invoices?lease_id=<?= $lease['id'] ?>" class="quick-action"><i class="bi bi-receipt"></i>View Invoices</a>
        <a href="<?= APP_URL ?>/admin/leases/<?= $lease['id'] ?>/edit" class="quick-action"><i class="bi bi-pencil"></i>Edit Lease</a>
      </div>
    </div>
    <div class="form-card">
      <div class="form-section-title">Audit Info</div>
      <div class="small">
        <div class="text-muted mb-1">Created by: <strong><?= Helper::e($lease['created_by_name']??'System') ?></strong></div>
        <div class="text-muted">Created: <strong><?= Helper::datetime($lease['created_at']) ?></strong></div>
      </div>
    </div>
  </div>
</div>
