<?php use App\Helpers\Helper; $isEdit = isset($tenant) && $tenant; ?>
<div class="d-flex justify-content-between align-items-center mb-4">
  <h1 class="admin-page-title"><?= $isEdit ? 'Edit Tenant' : 'Add Tenant' ?></h1>
  <a href="<?= APP_URL ?>/admin/tenants" class="btn btn-outline-secondary"><i class="bi bi-arrow-left me-1"></i>Back</a>
</div>
<form method="POST" action="<?= $isEdit ? APP_URL.'/admin/tenants/'.$tenant['id'] : APP_URL.'/admin/tenants' ?>" enctype="multipart/form-data">
  <?= Helper::csrf() ?>
  <div class="row g-4">
    <div class="col-lg-8">
      <div class="form-card mb-4">
        <div class="form-section-title">Personal Information</div>
        <div class="row g-3">
          <div class="col-md-4"><label class="form-label">First Name *</label><input type="text" name="first_name" class="form-control" required value="<?= Helper::e($tenant['first_name']??'') ?>"></div>
          <div class="col-md-4"><label class="form-label">Last Name *</label><input type="text" name="last_name" class="form-control" required value="<?= Helper::e($tenant['last_name']??'') ?>"></div>
          <div class="col-md-4"><label class="form-label">Gender</label>
            <select name="gender" class="form-select">
              <option value="">—</option>
              <?php foreach (['male'=>'Male','female'=>'Female','other'=>'Other'] as $v=>$l): ?><option value="<?= $v ?>" <?= ($tenant['gender']??'')===$v?'selected':'' ?>><?= $l ?></option><?php endforeach; ?>
            </select>
          </div>
          <div class="col-md-6"><label class="form-label">Email</label><input type="email" name="email" class="form-control" value="<?= Helper::e($tenant['email']??$tenant['user_email']??'') ?>"></div>
          <div class="col-md-3"><label class="form-label">Phone *</label><input type="tel" name="phone" class="form-control" required value="<?= Helper::e($tenant['phone']??'') ?>" placeholder="+255 7XX XXX XXX"></div>
          <div class="col-md-3"><label class="form-label">Alt Phone</label><input type="tel" name="phone_alt" class="form-control" value="<?= Helper::e($tenant['phone_alt']??'') ?>"></div>
          <div class="col-md-4"><label class="form-label">Date of Birth</label><input type="date" name="date_of_birth" class="form-control" value="<?= Helper::e($tenant['date_of_birth']??'') ?>"></div>
          <div class="col-md-4"><label class="form-label">Nationality</label><input type="text" name="nationality" class="form-control" value="<?= Helper::e($tenant['nationality']??'Tanzanian') ?>"></div>
          <div class="col-md-4"><label class="form-label">National ID</label><input type="text" name="national_id" class="form-control" value="<?= Helper::e($tenant['national_id']??'') ?>"></div>
          <div class="col-md-4"><label class="form-label">Passport No</label><input type="text" name="passport_number" class="form-control" value="<?= Helper::e($tenant['passport_number']??'') ?>"></div>
          <div class="col-md-4"><label class="form-label">Occupation</label><input type="text" name="occupation" class="form-control" value="<?= Helper::e($tenant['occupation']??'') ?>"></div>
          <div class="col-md-4"><label class="form-label">Employer</label><input type="text" name="employer" class="form-control" value="<?= Helper::e($tenant['employer']??'') ?>"></div>
          <div class="col-md-4"><label class="form-label">Monthly Income (TZS)</label><input type="number" name="monthly_income" class="form-control" value="<?= $tenant['monthly_income']??'' ?>"></div>
          <div class="col-md-4">
            <label class="form-label">Region</label>
            <select name="region_id" class="form-select">
              <option value="">— Select —</option>
              <?php foreach ($regions as $r): ?><option value="<?= $r['id'] ?>" <?= ($tenant['region_id']??'')==$r['id']?'selected':'' ?>><?= Helper::e($r['name']) ?></option><?php endforeach; ?>
            </select>
          </div>
          <div class="col-12"><label class="form-label">Home Address</label><textarea name="address" class="form-control" rows="2"><?= Helper::e($tenant['address']??'') ?></textarea></div>
        </div>
      </div>
      <div class="form-card mb-4">
        <div class="form-section-title">Emergency Contact</div>
        <div class="row g-3">
          <div class="col-md-4"><label class="form-label">Name</label><input type="text" name="emergency_contact_name" class="form-control" value="<?= Helper::e($tenant['emergency_contact_name']??'') ?>"></div>
          <div class="col-md-4"><label class="form-label">Phone</label><input type="tel" name="emergency_contact_phone" class="form-control" value="<?= Helper::e($tenant['emergency_contact_phone']??'') ?>"></div>
          <div class="col-md-4"><label class="form-label">Relationship</label><input type="text" name="emergency_contact_relation" class="form-control" value="<?= Helper::e($tenant['emergency_contact_relation']??'') ?>" placeholder="e.g. Spouse, Parent"></div>
        </div>
      </div>
      <div class="form-card">
        <div class="form-section-title">Notes</div>
        <textarea name="notes" class="form-control" rows="3"><?= Helper::e($tenant['notes']??'') ?></textarea>
      </div>
    </div>
    <div class="col-lg-4">
      <div class="form-card mb-4">
        <div class="form-section-title">Photo</div>
        <?php if (!empty($tenant['photo'])): ?><img src="<?= APP_URL ?>/uploads/avatars/<?= Helper::e($tenant['photo']) ?>" class="rounded-circle d-block mb-3 mx-auto" width="80" height="80" style="object-fit:cover"><?php endif; ?>
        <input type="file" name="photo" class="form-control" accept="image/*">
      </div>
      <div class="form-card mb-4">
        <div class="form-section-title">ID Document</div>
        <input type="file" name="id_document" class="form-control" accept="image/*,.pdf">
        <div class="form-text">National ID, Passport, or Driver's License. PDF or Image.</div>
      </div>
      <?php if ($isEdit): ?>
      <div class="form-card mb-4">
        <div class="form-section-title">Status</div>
        <select name="status" class="form-select">
          <?php foreach (['active','inactive','blacklisted'] as $s): ?><option value="<?= $s ?>" <?= ($tenant['status']??'active')===$s?'selected':'' ?>><?= ucfirst($s) ?></option><?php endforeach; ?>
        </select>
      </div>
      <?php endif; ?>
      <div class="form-card">
        <button type="submit" class="btn btn-primary w-100 py-3">
          <i class="bi bi-save me-2"></i><?= $isEdit ? 'Update Tenant' : 'Save Tenant' ?>
        </button>
      </div>
    </div>
  </div>
</form>
