<?php use App\Helpers\Helper; ?>
<div class="d-flex justify-content-between align-items-center mb-4">
  <div>
    <h1 class="admin-page-title mb-1">Edit Unit</h1>
    <p class="text-muted small mb-0">Unit <?= Helper::e($unit['unit_number']) ?> &mdash; <code><?= Helper::e($unit['unit_code']??'') ?></code></p>
  </div>
  <a href="<?= APP_URL ?>/admin/units" class="btn btn-outline-secondary btn-sm"><i class="bi bi-arrow-left me-1"></i>Back to Units</a>
</div>

<div class="row justify-content-center">
  <div class="col-lg-9">
    <form method="POST" action="<?= APP_URL ?>/admin/units/<?= $unit['id'] ?>">
      <?= Helper::csrf() ?>
      <div class="form-card mb-4">
        <div class="form-section-title">Location & Identification</div>
        <div class="row g-3">
          <div class="col-md-4">
            <label class="form-label fw-600">Building <span class="text-danger">*</span></label>
            <select name="building_id" class="form-select" required>
              <?php foreach ($buildings as $b): ?>
              <option value="<?= $b['id'] ?>" <?= $b['id']==$unit['building_id']?'selected':'' ?>><?= Helper::e($b['name']) ?></option>
              <?php endforeach; ?>
            </select>
          </div>
          <div class="col-md-4">
            <label class="form-label fw-600">Unit Number <span class="text-danger">*</span></label>
            <input type="text" name="unit_number" class="form-control" value="<?= Helper::e($unit['unit_number']) ?>" required>
          </div>
          <div class="col-md-2">
            <label class="form-label fw-600">Floor</label>
            <input type="number" name="floor" class="form-control" value="<?= $unit['floor'] ?>" min="0" max="50">
          </div>
          <div class="col-md-2">
            <label class="form-label fw-600">Status</label>
            <select name="status" class="form-select">
              <?php foreach (['available','occupied','maintenance','inactive'] as $s): ?>
              <option value="<?= $s ?>" <?= $unit['status']===$s?'selected':'' ?>><?= ucfirst($s) ?></option>
              <?php endforeach; ?>
            </select>
          </div>
        </div>
      </div>

      <div class="form-card mb-4">
        <div class="form-section-title">Unit Details</div>
        <div class="row g-3">
          <div class="col-md-3">
            <label class="form-label fw-600">Unit Type</label>
            <select name="unit_type" class="form-select">
              <?php foreach (['apartment','studio','villa','room','office','shop'] as $t): ?>
              <option value="<?= $t ?>" <?= ($unit['unit_type']??'')===$t?'selected':'' ?>><?= ucfirst($t) ?></option>
              <?php endforeach; ?>
            </select>
          </div>
          <div class="col-md-2">
            <label class="form-label fw-600">Bedrooms</label>
            <input type="number" name="bedrooms" class="form-control" value="<?= $unit['bedrooms'] ?>" min="0" max="10">
          </div>
          <div class="col-md-2">
            <label class="form-label fw-600">Bathrooms</label>
            <input type="number" name="bathrooms" class="form-control" value="<?= $unit['bathrooms'] ?>" min="1" max="10">
          </div>
          <div class="col-md-2">
            <label class="form-label fw-600">Size (m²)</label>
            <input type="number" name="floor_size" class="form-control" value="<?= $unit['floor_size']??'' ?>">
          </div>
          <div class="col-md-3">
            <label class="form-label fw-600">Rent Period</label>
            <select name="rent_period" id="edit_rent_period" class="form-select" onchange="toggleRentFields()">
              <option value="monthly" <?= ($unit['rent_period']??'monthly')==='monthly'?'selected':'' ?>>Monthly</option>
              <option value="weekly"  <?= ($unit['rent_period']??'')==='weekly'?'selected':'' ?>>Weekly</option>
              <option value="daily"   <?= ($unit['rent_period']??'')==='daily'?'selected':'' ?>>Daily</option>
            </select>
          </div>
          <div class="col-md-3" id="wrap_monthly" <?= ($unit['rent_period']??'monthly')!=='monthly'?'style="display:none"':'' ?>>
            <label class="form-label fw-600">Monthly Rent (TZS)</label>
            <input type="number" name="monthly_rent" class="form-control" value="<?= $unit['monthly_rent'] ?>">
          </div>
          <div class="col-md-3" id="wrap_weekly" <?= ($unit['rent_period']??'monthly')!=='weekly'?'style="display:none"':'' ?>>
            <label class="form-label fw-600">Weekly Rent (TZS)</label>
            <input type="number" name="weekly_rent" class="form-control" value="<?= $unit['weekly_rent']??'' ?>">
          </div>
          <div class="col-md-3" id="wrap_daily" <?= ($unit['rent_period']??'monthly')!=='daily'?'style="display:none"':'' ?>>
            <label class="form-label fw-600">Daily Rent (TZS)</label>
            <input type="number" name="daily_rent" class="form-control" value="<?= $unit['daily_rent']??'' ?>">
          </div>
          <div class="col-md-3">
            <label class="form-label fw-600">Security Deposit (TZS)</label>
            <input type="number" name="security_deposit" class="form-control" value="<?= $unit['security_deposit']??'' ?>">
          </div>
          <div class="col-12">
            <label class="form-label fw-600">Description</label>
            <textarea name="description" class="form-control" rows="2"><?= Helper::e($unit['description']??'') ?></textarea>
          </div>
        </div>
      </div>

      <div class="form-card mb-4">
        <div class="form-section-title">Amenities</div>
        <div class="d-flex gap-4 flex-wrap">
          <?php foreach ([
            ['is_furnished','Furnished'],['has_parking','Parking'],
            ['has_wifi','WiFi'],['has_ac','Air Conditioning'],['pets_allowed','Pets Allowed']
          ] as [$name,$label]): ?>
          <div class="form-check form-switch">
            <input class="form-check-input" type="checkbox" name="<?= $name ?>" id="<?= $name ?>" value="1" <?= !empty($unit[$name])?'checked':'' ?>>
            <label class="form-check-label fw-600" for="<?= $name ?>"><?= $label ?></label>
          </div>
          <?php endforeach; ?>
        </div>
      </div>

      <div class="d-flex gap-2">
        <button type="submit" class="btn btn-primary px-4"><i class="bi bi-save me-2"></i>Update Unit</button>
        <a href="<?= APP_URL ?>/admin/units" class="btn btn-outline-secondary">Cancel</a>
      </div>
    </form>
<script>
function toggleRentFields() {
  const period = document.getElementById('edit_rent_period').value;
  document.getElementById('wrap_monthly').style.display = period === 'monthly' ? '' : 'none';
  document.getElementById('wrap_weekly').style.display  = period === 'weekly'  ? '' : 'none';
  document.getElementById('wrap_daily').style.display   = period === 'daily'   ? '' : 'none';
}
</script>
  </div>
</div>
