<?php /* customers/form.php */ $isEdit = isset($customer); ?>

<div class="d-flex justify-content-between align-items-center mb-3">
  <h5 class="fw-semibold mb-0">
    <i class="bi bi-person-<?= $isEdit ? 'gear' : 'plus' ?> me-2 text-primary"></i>
    <?= $isEdit ? 'Edit Customer' : 'Add Customer' ?>
  </h5>
  <a href="<?= APP_URL ?>/customers" class="btn btn-sm btn-outline-secondary">
    <i class="bi bi-arrow-left me-1"></i>Back
  </a>
</div>

<div class="row justify-content-center">
  <div class="col-md-7 col-lg-6">
    <div class="card">
      <div class="card-header bg-transparent fw-semibold small">
        <i class="bi bi-person-vcard me-1"></i>
        <?= $isEdit ? 'Customer Details' : 'New Customer' ?>
      </div>
      <div class="card-body">
        <form method="POST"
              action="<?= APP_URL ?>/customers/<?= $isEdit ? 'edit/'.$customer['id'] : 'create' ?>">
          <?= Auth::csrfField() ?>
          <div class="row g-3">

            <!-- Name -->
            <div class="col-12">
              <label class="form-label small fw-semibold">Customer Name *</label>
              <input type="text" name="customer_name" class="form-control" required
                     value="<?= e($customer['customer_name'] ?? '') ?>"
                     placeholder="Full name or business name">
            </div>

            <!-- Phone + Email -->
            <div class="col-md-6">
              <label class="form-label small fw-semibold">Phone</label>
              <div class="input-group">
                <span class="input-group-text"><i class="bi bi-telephone"></i></span>
                <input type="text" name="phone" class="form-control"
                       value="<?= e($customer['phone'] ?? '') ?>"
                       placeholder="07X XXX XXXX">
              </div>
            </div>

            <div class="col-md-6">
              <label class="form-label small fw-semibold">Email</label>
              <div class="input-group">
                <span class="input-group-text"><i class="bi bi-envelope"></i></span>
                <input type="email" name="email" class="form-control"
                       value="<?= e($customer['email'] ?? '') ?>"
                       placeholder="customer@email.com">
              </div>
            </div>

            <!-- Address -->
            <div class="col-12">
              <label class="form-label small fw-semibold">Address</label>
              <textarea name="address" class="form-control" rows="2"
                        placeholder="Street, City, Region…"><?= e($customer['address'] ?? '') ?></textarea>
            </div>

            <!-- Credit Limit -->
            <div class="col-md-6">
              <label class="form-label small fw-semibold">Credit Limit</label>
              <div class="input-group">
                <span class="input-group-text"><?= e(setting('currency_symbol','Rs.')) ?></span>
                <input type="number" name="credit_limit" id="credit-input"
                       class="form-control" step="0.01" min="0"
                       value="<?= e($customer['credit_limit'] ?? 0) ?>"
                       placeholder="0.00"
                       oninput="updateCreditDisplay()">
              </div>
              <small class="text-muted" id="credit-hint">
                0 = no credit limit
              </small>
            </div>

            <!-- Opening Balance (create only) -->
            <?php if (!$isEdit): ?>
            <div class="col-md-6">
              <label class="form-label small fw-semibold">Opening Balance</label>
              <div class="input-group">
                <span class="input-group-text"><?= e(setting('currency_symbol','Rs.')) ?></span>
                <input type="number" name="opening_balance" class="form-control"
                       step="0.01" min="0" value="0" placeholder="0.00">
              </div>
              <small class="text-muted">Outstanding amount from before system start.</small>
            </div>
            <?php endif; ?>

            <!-- Status (edit only) -->
            <?php if ($isEdit): ?>
            <div class="col-md-6">
              <label class="form-label small fw-semibold">Status</label>
              <select name="status" class="form-select">
                <option value="active"   <?= ($customer['status'] ?? '') === 'active'   ? 'selected' : '' ?>>✓ Active</option>
                <option value="inactive" <?= ($customer['status'] ?? '') === 'inactive' ? 'selected' : '' ?>>✗ Inactive</option>
              </select>
            </div>

            <!-- Current Balance (edit only, read-only info) -->
            <div class="col-md-6">
              <label class="form-label small fw-semibold">Current Balance</label>
              <div class="input-group">
                <span class="input-group-text"><?= e(setting('currency_symbol','Rs.')) ?></span>
                <input type="text" class="form-control bg-light text-danger fw-semibold"
                       value="<?= number_format((float)($customer['current_balance'] ?? 0), 2) ?>"
                       readonly>
              </div>
              <small class="text-muted">Outstanding dues — managed via payments.</small>
            </div>
            <?php endif; ?>

            <!-- Buttons -->
            <div class="col-12 d-flex gap-2">
              <button type="submit" class="btn btn-primary px-4 fw-semibold flex-grow-1">
                <i class="bi bi-check-circle me-1"></i>
                <?= $isEdit ? 'Update Customer' : 'Save Customer' ?>
              </button>
              <a href="<?= APP_URL ?>/customers" class="btn btn-outline-secondary px-4">
                Cancel
              </a>
            </div>

          </div>
        </form>
      </div>
    </div>
  </div>
</div>

<script>
function updateCreditDisplay() {
  const val  = parseFloat(document.getElementById('credit-input').value) || 0;
  const hint = document.getElementById('credit-hint');
  const sym  = '<?= e(setting("currency_symbol","Rs.")) ?>';
  if (val <= 0) {
    hint.textContent = '0 = no credit limit';
    hint.className   = 'text-muted small';
  } else {
    hint.textContent = 'Max credit allowed: ' + sym + ' ' + val.toLocaleString(undefined, {minimumFractionDigits:2});
    hint.className   = 'text-primary small fw-semibold';
  }
}
updateCreditDisplay();
</script>
