<?php /* suppliers/form.php */ $isEdit = isset($supplier); ?>

<div class="d-flex justify-content-between align-items-center mb-3">
  <h5 class="fw-semibold mb-0">
    <i class="bi bi-truck me-2 text-primary"></i>
    <?= $isEdit ? 'Edit Supplier' : 'Add Supplier' ?>
  </h5>
  <a href="<?= APP_URL ?>/suppliers<?= $isEdit ? '/view/'.$supplier['id'] : '' ?>"
     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-8 col-lg-7">
    <form method="POST"
          action="<?= APP_URL ?>/suppliers/<?= $isEdit ? 'edit/'.$supplier['id'] : 'create' ?>">
      <?= Auth::csrfField() ?>

      <!-- Basic Info -->
      <div class="card mb-3">
        <div class="card-header bg-transparent fw-semibold small">
          <i class="bi bi-person-badge me-1"></i>Supplier Information
        </div>
        <div class="card-body">
          <div class="row g-3">
            <div class="col-12">
              <label class="form-label small fw-semibold">Supplier / Company Name *</label>
              <input type="text" name="supplier_name" class="form-control" required
                     placeholder="e.g. ABC Traders (Pvt) Ltd"
                     value="<?= e($supplier['supplier_name'] ?? '') ?>">
            </div>
            <div class="col-md-6">
              <label class="form-label small fw-semibold">Contact Person</label>
              <input type="text" name="contact_person" class="form-control"
                     placeholder="Sales representative name"
                     value="<?= e($supplier['contact_person'] ?? '') ?>">
            </div>
            <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"
                       placeholder="e.g. 0771234567"
                       value="<?= e($supplier['phone'] ?? '') ?>">
              </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"
                       placeholder="supplier@email.com"
                       value="<?= e($supplier['email'] ?? '') ?>">
              </div>
            </div>
            <?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">Existing payable balance before using this system.</small>
            </div>
            <?php endif; ?>
            <div class="col-12">
              <label class="form-label small fw-semibold">Address</label>
              <textarea name="address" class="form-control" rows="2"
                        placeholder="Street, city, postal code…"><?= e($supplier['address'] ?? '') ?></textarea>
            </div>
          </div>
        </div>
      </div>

      <?php if ($isEdit): ?>
      <!-- Status (edit only) -->
      <div class="card mb-3">
        <div class="card-header bg-transparent fw-semibold small">
          <i class="bi bi-toggle-on me-1"></i>Account Status
        </div>
        <div class="card-body">
          <div class="row g-3 align-items-center">
            <div class="col-md-4">
              <label class="form-label small fw-semibold">Status</label>
              <select name="status" class="form-select">
                <option value="active"   <?= ($supplier['status'] ?? '') === 'active'   ? 'selected' : '' ?>>Active</option>
                <option value="inactive" <?= ($supplier['status'] ?? '') === 'inactive' ? 'selected' : '' ?>>Inactive</option>
              </select>
            </div>
            <div class="col-md-8">
              <div class="small text-muted mt-3">
                <i class="bi bi-info-circle me-1"></i>
                Inactive suppliers will not appear in new purchase dropdowns.
              </div>
            </div>
          </div>
        </div>
      </div>
      <?php endif; ?>

      <!-- Actions -->
      <div class="d-flex gap-2">
        <button type="submit" class="btn btn-primary px-4 fw-semibold">
          <i class="bi bi-check-lg me-1"></i><?= $isEdit ? 'Update Supplier' : 'Save Supplier' ?>
        </button>
        <a href="<?= APP_URL ?>/suppliers<?= $isEdit ? '/view/'.$supplier['id'] : '' ?>"
           class="btn btn-outline-secondary">
          Cancel
        </a>
      </div>

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