<?php use App\Helpers\Helper; ?>
<div class="d-flex justify-content-between align-items-center mb-4">
  <h1 class="admin-page-title">Record Payment</h1>
  <a href="<?= APP_URL ?>/admin/payments" class="btn btn-outline-secondary"><i class="bi bi-arrow-left me-1"></i>Back</a>
</div>

<div class="row g-4">
  <div class="col-lg-8">
    <div class="form-card">
      <form method="POST" action="<?= APP_URL ?>/admin/payments">
        <?= Helper::csrf() ?>
        <div class="form-section-title">Payment Details</div>
        <div class="row g-3">
          <div class="col-md-6">
            <label class="form-label">Tenant *</label>
            <select name="tenant_id" class="form-select" required id="tenantSelect">
              <option value="">Select Tenant</option>
              <?php foreach ($tenants as $t): ?>
              <option value="<?= $t['id'] ?>"><?= Helper::e($t['name']) ?></option>
              <?php endforeach; ?>
            </select>
          </div>
          <div class="col-md-6">
            <label class="form-label">Unit</label>
            <select name="unit_id" class="form-select">
              <option value="">Select Unit</option>
              <?php foreach ($units as $u): ?>
              <option value="<?= $u['id'] ?>"><?= Helper::e($u['name']) ?></option>
              <?php endforeach; ?>
            </select>
          </div>
          <div class="col-md-6">
            <label class="form-label">Invoice (Optional)</label>
            <select name="invoice_id" class="form-select">
              <option value="">— No specific invoice —</option>
              <?php foreach ($invoices as $inv): ?>
              <option value="<?= $inv['id'] ?>"><?= Helper::e($inv['label']) ?></option>
              <?php endforeach; ?>
            </select>
          </div>
          <div class="col-md-6">
            <label class="form-label">Payment For *</label>
            <select name="payment_for" class="form-select" required>
              <?php foreach (['rent'=>'Rent','deposit'=>'Security Deposit','booking_fee'=>'Booking Fee','maintenance'=>'Maintenance','penalty'=>'Penalty','utility'=>'Utility','other'=>'Other'] as $val=>$lbl): ?>
              <option value="<?= $val ?>"><?= $lbl ?></option>
              <?php endforeach; ?>
            </select>
          </div>
          <div class="col-md-6">
            <label class="form-label">Amount (TZS) *</label>
            <input type="number" name="amount" class="form-control" placeholder="0" min="1" required>
          </div>
          <div class="col-md-6">
            <label class="form-label">Payment Method *</label>
            <select name="payment_method_id" class="form-select" required>
              <option value="">Select Method</option>
              <?php foreach ($methods as $m): ?>
              <option value="<?= $m['id'] ?>"><?= Helper::e($m['name']) ?> (<?= ucfirst($m['type']) ?>)</option>
              <?php endforeach; ?>
            </select>
          </div>
          <div class="col-md-6">
            <label class="form-label">Payment Date *</label>
            <input type="date" name="payment_date" class="form-control" value="<?= date('Y-m-d') ?>" required>
          </div>
          <div class="col-md-6">
            <label class="form-label">Transaction ID / Reference</label>
            <input type="text" name="transaction_id" class="form-control" placeholder="e.g. MP123456789">
          </div>
          <div class="col-md-4">
            <label class="form-label">Billing Period *</label>
            <select name="billing_period" id="billing_period" class="form-select" onchange="updatePeriodDates()">
              <option value="monthly">Monthly</option>
              <option value="weekly">Weekly</option>
              <option value="daily">Daily</option>
            </select>
          </div>
          <div class="col-md-4">
            <label class="form-label">Period From</label>
            <input type="date" name="period_from" id="period_from" class="form-control" onchange="updatePeriodTo()">
          </div>
          <div class="col-md-4">
            <label class="form-label">Period To</label>
            <input type="date" name="period_to" id="period_to" class="form-control">
          </div>
          <div class="col-12">
            <label class="form-label">Notes</label>
            <textarea name="notes" class="form-control" rows="2" placeholder="Optional notes..."></textarea>
          </div>
        </div>
        <div class="mt-4 d-flex gap-2">
          <button type="submit" class="btn btn-primary px-5"><i class="bi bi-check-lg me-2"></i>Save Payment</button>
          <a href="<?= APP_URL ?>/admin/payments" class="btn btn-outline-secondary">Cancel</a>
        </div>
      </form>
    </div>
  </div>
  <div class="col-lg-4">
    <div class="form-card mb-3">
      <div class="form-section-title">Billing Period Guide</div>
      <div class="d-flex flex-column gap-2">
        <div class="d-flex align-items-center gap-2 p-2 rounded-2" style="background:#f0fdf4;border:1px solid #bbf7d0">
          <span class="badge bg-success">Monthly</span>
          <span class="small text-muted">Period covers one calendar month</span>
        </div>
        <div class="d-flex align-items-center gap-2 p-2 rounded-2" style="background:#fffbeb;border:1px solid #fde68a">
          <span class="badge bg-warning text-dark">Weekly</span>
          <span class="small text-muted">Period covers 7 days</span>
        </div>
        <div class="d-flex align-items-center gap-2 p-2 rounded-2" style="background:#fef2f2;border:1px solid #fecaca">
          <span class="badge bg-danger">Daily</span>
          <span class="small text-muted">Period covers a single day</span>
        </div>
      </div>
    </div>
    <div class="form-card">
      <div class="form-section-title">Payment Methods</div>
      <?php foreach ($methods as $m): ?>
      <div class="d-flex align-items-center gap-2 mb-2 p-2 rounded-2" style="background:#f8fafc;border:1px solid #e2e8f0">
        <i class="bi bi-<?= $m['type']==='mobile_money'?'phone':'credit-card' ?> text-primary"></i>
        <div>
          <div class="small fw-600"><?= Helper::e($m['name']) ?></div>
          <div class="text-muted" style="font-size:0.72rem"><?= ucfirst(str_replace('_',' ',$m['type'])) ?></div>
        </div>
      </div>
      <?php endforeach; ?>
    </div>
  </div>
</div>

<script>
function updatePeriodTo() {
  const from = document.getElementById('period_from').value;
  const period = document.getElementById('billing_period').value;
  if (!from) return;
  const d = new Date(from);
  if (period === 'daily') {
    document.getElementById('period_to').value = from;
  } else if (period === 'weekly') {
    d.setDate(d.getDate() + 6);
    document.getElementById('period_to').value = d.toISOString().split('T')[0];
  } else {
    // monthly - last day of same month
    const lastDay = new Date(d.getFullYear(), d.getMonth() + 1, 0);
    document.getElementById('period_to').value = lastDay.toISOString().split('T')[0];
  }
}
function updatePeriodDates() {
  updatePeriodTo();
}
// Set default period_from to today on load
document.addEventListener('DOMContentLoaded', function() {
  const pf = document.getElementById('period_from');
  if (!pf.value) {
    const today = new Date();
    pf.value = today.toISOString().split('T')[0];
    updatePeriodTo();
  }
});
</script>
