<?php use App\Helpers\Helper; $isEdit = isset($property) && $property; ?>
<div class="d-flex justify-content-between align-items-center mb-4">
  <h1 class="admin-page-title"><?= $isEdit ? 'Edit Property' : 'Add Property' ?></h1>
  <a href="<?= APP_URL ?>/admin/properties" 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/properties/'.$property['id'] : APP_URL.'/admin/properties' ?>" 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">Basic Information</div>
        <div class="row g-3">
          <div class="col-md-6"><label class="form-label">Property Name *</label><input type="text" name="name" class="form-control" required value="<?= Helper::e($property['name']??'') ?>"></div>
          <div class="col-md-3"><label class="form-label">Property Code</label><input type="text" name="property_code" class="form-control" placeholder="ZBR-001" value="<?= Helper::e($property['property_code']??'') ?>"></div>
          <div class="col-md-3"><label class="form-label">Type</label>
            <select name="property_type_id" class="form-select">
              <option value="">— Select —</option>
              <?php foreach ($types as $t): ?><option value="<?= $t['id'] ?>" <?= ($property['property_type_id']??'')==$t['id']?'selected':'' ?>><?= Helper::e($t['name']) ?></option><?php endforeach; ?>
            </select>
          </div>
          <div class="col-md-6"><label class="form-label">Owner</label>
            <select name="owner_id" class="form-select">
              <option value="">— Select Owner —</option>
              <?php foreach ($owners as $o): ?><option value="<?= $o['id'] ?>" <?= ($property['owner_id']??'')==$o['id']?'selected':'' ?>><?= Helper::e($o['name']) ?></option><?php endforeach; ?>
            </select>
          </div>
          <div class="col-md-6"><label class="form-label">Manager</label>
            <select name="manager_id" class="form-select">
              <option value="">— Select Manager —</option>
              <?php foreach ($managers as $m): ?><option value="<?= $m['id'] ?>" <?= ($property['manager_id']??'')==$m['id']?'selected':'' ?>><?= Helper::e($m['name']) ?></option><?php endforeach; ?>
            </select>
          </div>
          <div class="col-12"><label class="form-label">Description</label><textarea name="description" class="form-control" rows="4"><?= Helper::e($property['description']??'') ?></textarea></div>
          <div class="col-md-3"><label class="form-label">Total Floors</label><input type="number" name="total_floors" class="form-control" value="<?= $property['total_floors']??1 ?>" min="1"></div>
          <div class="col-md-3"><label class="form-label">Year Built</label><input type="number" name="year_built" class="form-control" value="<?= $property['year_built']??'' ?>" placeholder="e.g. 2020"></div>
          <div class="col-md-3"><label class="form-label">Status</label>
            <select name="status" class="form-select">
              <?php foreach (['active'=>'Active','inactive'=>'Inactive','under_construction'=>'Under Construction'] as $v=>$l): ?><option value="<?= $v ?>" <?= ($property['status']??'active')===$v?'selected':'' ?>><?= $l ?></option><?php endforeach; ?>
            </select>
          </div>
          <div class="col-md-3 d-flex align-items-end pb-2">
            <div class="form-check"><input class="form-check-input" type="checkbox" name="is_featured" <?= ($property['is_featured']??0)?'checked':'' ?>><label class="form-check-label">Featured</label></div>
          </div>
        </div>
      </div>
      <div class="form-card mb-4">
        <div class="form-section-title">Location</div>
        <div class="row g-3">
          <div class="col-md-6"><label class="form-label">Address</label><input type="text" name="address" class="form-control" value="<?= Helper::e($property['address']??'') ?>"></div>
          <div class="col-md-6"><label class="form-label">Area / Neighbourhood</label><input type="text" name="area" class="form-control" placeholder="e.g. Masaki" value="<?= Helper::e($property['area']??'') ?>"></div>
          <div class="col-md-4"><label class="form-label">Region</label>
            <select name="region_id" id="region_id" class="form-select">
              <option value="">Select Region</option>
              <?php foreach ($regions as $r): ?><option value="<?= $r['id'] ?>" <?= ($property['region_id']??'')==$r['id']?'selected':'' ?>><?= Helper::e($r['name']) ?></option><?php endforeach; ?>
            </select>
          </div>
          <div class="col-md-4"><label class="form-label">Latitude</label><input type="text" name="latitude" class="form-control" placeholder="-6.792354" value="<?= Helper::e($property['latitude']??'') ?>"></div>
          <div class="col-md-4"><label class="form-label">Longitude</label><input type="text" name="longitude" class="form-control" placeholder="39.208328" value="<?= Helper::e($property['longitude']??'') ?>"></div>
        </div>
      </div>
      <div class="form-card mb-4">
        <div class="form-section-title">Amenities</div>
        <div class="row g-2">
          <?php foreach ($amenities as $a): ?>
          <div class="col-md-3 col-6">
            <div class="form-check">
              <input class="form-check-input" type="checkbox" name="amenities[]" value="<?= $a['id'] ?>" id="am<?= $a['id'] ?>"
                <?= isset($selAmenities) && in_array($a['id'],$selAmenities) ? 'checked' : '' ?>>
              <label class="form-check-label small" for="am<?= $a['id'] ?>"><?= Helper::e($a['name']) ?></label>
            </div>
          </div>
          <?php endforeach; ?>
        </div>
      </div>
      <div class="form-card">
        <div class="form-section-title">SEO (Optional)</div>
        <div class="row g-3">
          <div class="col-12"><label class="form-label">Meta Title</label><input type="text" name="meta_title" class="form-control" value="<?= Helper::e($property['meta_title']??'') ?>"></div>
          <div class="col-12"><label class="form-label">Meta Description</label><textarea name="meta_description" class="form-control" rows="2"><?= Helper::e($property['meta_description']??'') ?></textarea></div>
        </div>
      </div>
    </div>
    <div class="col-lg-4">
      <div class="form-card mb-4">
        <div class="form-section-title">Featured Image</div>
        <?php if (!empty($property['featured_image'])): ?>
        <img src="<?= APP_URL ?>/uploads/<?= Helper::e($property['featured_image']) ?>" class="img-fluid rounded-2 mb-3">
        <?php endif; ?>
        <input type="file" name="featured_image" class="form-control" accept="image/*">
        <div class="form-text">JPG, PNG, WebP. Max 10MB.</div>
      </div>
      <div class="form-card">
        <button type="submit" class="btn btn-primary w-100 py-3 fw-600">
          <i class="bi bi-save me-2"></i><?= $isEdit ? 'Update Property' : 'Save Property' ?>
        </button>
      </div>
    </div>
  </div>
</form>
