<?php /* finance/account_edit.php — Admin Only */
$sym = setting('currency_symbol','Rs.');
$isBank = $account['account_type'] === 'bank';
?>
<style>
.ae-hero{background:#0f172a;border-radius:14px;padding:20px 26px;color:#fff;
  margin-bottom:18px;border:1px solid #1e293b;box-shadow:0 4px 20px rgba(0,0,0,.15)}
.ae-hero .page-title{font-size:1.2rem;font-weight:800;display:flex;align-items:center;gap:10px;margin-bottom:4px}
.ae-hero .page-meta{font-size:.78rem;color:#94a3b8}
.hero-btn{border-radius:10px;font-weight:600;font-size:.8rem;padding:8px 16px;
  display:inline-flex;align-items:center;gap:6px;transition:all .15s;border:none;cursor:pointer}
</style>

<!-- Hero -->
<div class="ae-hero">
  <div class="d-flex justify-content-between align-items-center flex-wrap gap-3">
    <div>
      <div class="page-title">
        <i class="bi <?=$isBank?'bi-bank':'bi-cash-stack'?>"
           style="color:<?=$isBank?'#93c5fd':'#6ee7b7'?>"></i>
        Edit Account
        <span style="background:rgba(255,255,255,.12);font-size:.65rem;font-weight:600;
                     padding:2px 10px;border-radius:20px;color:#fbbf24">
          Admin Only
        </span>
      </div>
      <div class="page-meta"><?=e($account['account_name'])?></div>
    </div>
    <a href="<?=APP_URL?>/finance/accounts" class="hero-btn"
       style="background:rgba(255,255,255,.1);color:#e2e8f0">
      <i class="bi bi-arrow-left"></i>Cancel
    </a>
  </div>
</div>

<?= flashMessages() ?>

<div class="row justify-content-center">
  <div class="col-lg-6">
    <div class="card">
      <div class="card-header bg-dark text-white fw-semibold small">
        <i class="bi bi-pencil me-1"></i>Edit: <?=e($account['account_name'])?>
      </div>
      <div class="card-body">
        <form method="POST" action="<?=APP_URL?>/finance/accounts/edit/<?=$account['id']?>">
          <?=Auth::csrfField()?>

          <!-- Current balance notice (read-only) -->
          <div class="alert alert-info d-flex align-items-center gap-2 py-2 mb-4">
            <i class="bi bi-info-circle-fill"></i>
            <span class="small">
              <strong>Current Balance:</strong>
              <?=$sym?><?=number_format($account['current_balance'],2)?>
              — Balance can only be changed via Deposit / Withdraw transactions in the Ledger.
            </span>
          </div>

          <div class="mb-3">
            <label class="form-label fw-semibold small">Account Name *</label>
            <input type="text" name="account_name" class="form-control" required
                   value="<?=e($account['account_name'])?>">
          </div>

          <div class="mb-3">
            <label class="form-label fw-semibold small">Account Type</label>
            <select name="account_type" class="form-select" id="accTypeEdit"
                    onchange="toggleBankFieldsEdit(this.value)">
              <option value="cash" <?=$account['account_type']==='cash'?'selected':''?>>Cash</option>
              <option value="bank" <?=$account['account_type']==='bank'?'selected':''?>>Bank</option>
            </select>
          </div>

          <div id="bankFieldsEdit" style="display:<?=$isBank?'block':'none'?>">
            <div class="row g-3 mb-3">
              <div class="col-6">
                <label class="form-label fw-semibold small">Bank Name</label>
                <input type="text" name="bank_name" class="form-control"
                       value="<?=e($account['bank_name']??'')?>">
              </div>
              <div class="col-6">
                <label class="form-label fw-semibold small">Account Number</label>
                <input type="text" name="account_number" class="form-control"
                       value="<?=e($account['account_number']??'')?>">
              </div>
            </div>
            <div class="mb-3">
              <label class="form-label fw-semibold small">Branch</label>
              <input type="text" name="branch" class="form-control"
                     value="<?=e($account['branch']??'')?>">
            </div>
          </div>

          <div class="mb-4">
            <label class="form-label fw-semibold small">Status</label>
            <select name="status" class="form-select">
              <option value="active" <?=$account['status']==='active'?'selected':''?>>Active</option>
              <option value="inactive" <?=$account['status']==='inactive'?'selected':''?>>Inactive</option>
            </select>
            <div class="form-text">Inactive accounts won't appear in POS payment mapping.</div>
          </div>

          <div class="d-flex gap-2 justify-content-between">
            <a href="<?=APP_URL?>/finance/accounts" class="btn btn-outline-secondary">
              <i class="bi bi-x me-1"></i>Cancel
            </a>
            <button type="submit" class="btn btn-warning fw-semibold px-4">
              <i class="bi bi-check-circle me-1"></i>Save Changes
            </button>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>

<script>
function toggleBankFieldsEdit(v) {
  document.getElementById('bankFieldsEdit').style.display = v==='bank' ? 'block' : 'none';
}
</script>