<?php
require_once __DIR__ . '/../../admin/_nav.php';
require_once VIEWS . '/accountant/_nav.php';
$pageTitle  = 'Cash Accounts';
$_role = \Core\Session::get('user')['role'] ?? '';
$sidebarNav = $_role === 'accountant' ? accountantNav() : adminNav();
$u = function($p) { return \Core\View::url($p); };
$typeColors = ['bank'=>'#3b82f6','cash'=>'#10b981','mpesa'=>'#f59e0b','cheque'=>'#8b5cf6'];
$typeIcons  = ['bank'=>'bi-bank','cash'=>'bi-cash','mpesa'=>'bi-phone','cheque'=>'bi-file-earmark-text'];
$totalBalance = array_sum(array_column($accounts,'current_balance'));
?>
<div class="page-header">
  <div><h1><i class="bi bi-bank"></i> Cash Accounts</h1><p>Bank, cash & M-Pesa accounts</p></div>
  <div style="display:flex;gap:8px">
    <a href="<?= $u('admin/fees/cashbook') ?>" class="btn btn-secondary"><i class="bi bi-journal-text"></i> Cashbook</a>
    <button onclick="openModal('addAccount')" class="btn btn-primary"><i class="bi bi-plus-lg"></i> Add Account</button>
  </div>
</div>

<!-- Total Balance -->
<div class="card" style="margin-bottom:20px;">
  <div class="card-body" style="padding:20px;display:flex;align-items:center;justify-content:space-between">
    <div>
      <div style="font-size:12px;color:var(--gray);text-transform:uppercase;font-weight:600;letter-spacing:.5px">Total Cash & Bank Balance</div>
      <div style="font-size:32px;font-weight:700;color:<?= $totalBalance>=0?'var(--success)':'var(--danger)' ?>;margin-top:4px">KES <?= number_format($totalBalance,2) ?></div>
    </div>
    <div style="font-size:48px;color:var(--primary);opacity:.2"><i class="bi bi-bank"></i></div>
  </div>
</div>

<!-- Account Cards -->
<div class="grid g3" style="margin-bottom:20px">
  <?php foreach($accounts as $a):
    $col = $typeColors[$a['type']]??'#6b7280';
    $ico = $typeIcons[$a['type']]??'bi-wallet2'; ?>
  <div class="card" style="border-top:4px solid <?= $col ?>">
    <div class="card-body" style="padding:20px">
      <div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:12px">
        <div style="width:44px;height:44px;background:<?= $col ?>22;border-radius:10px;display:flex;align-items:center;justify-content:center;color:<?= $col ?>;font-size:20px">
          <i class="bi <?= $ico ?>"></i>
        </div>
        <span class="badge" style="background:<?= $col ?>22;color:<?= $col ?>"><?= strtoupper($a['type']) ?></span>
      </div>
      <div style="font-weight:600;font-size:15px"><?= htmlspecialchars($a['name']) ?></div>
      <?php if($a['account_no']): ?><div style="font-size:12px;color:var(--gray);margin-top:2px"><?= htmlspecialchars($a['account_no']) ?><?= $a['bank_name']?' — '.htmlspecialchars($a['bank_name']):'' ?></div><?php endif; ?>
      <div style="margin-top:12px;padding-top:12px;border-top:1px solid var(--border)">
        <div style="font-size:11px;color:var(--gray)">Current Balance</div>
        <div style="font-size:22px;font-weight:700;color:<?= (float)$a['current_balance']>=0?'var(--success)':'var(--danger)' ?>">KES <?= number_format($a['current_balance'],2) ?></div>
        <div style="font-size:11px;color:var(--gray);margin-top:2px">Opening: KES <?= number_format($a['opening_balance'],2) ?></div>
      </div>
      <div style="margin-top:12px">
        <a href="<?= $u('admin/fees/cashbook?account_id='.$a['id']) ?>" class="btn btn-sm btn-secondary" style="width:100%;justify-content:center"><i class="bi bi-journal-text"></i> View Cashbook</a>
      </div>
    </div>
  </div>
  <?php endforeach; ?>
</div>

<?php if(empty($accounts)): ?>
<div class="card"><div class="card-body" style="text-align:center;padding:60px;color:var(--gray)">
  <i class="bi bi-bank" style="font-size:48px"></i>
  <p style="margin-top:12px">No accounts set up yet. Add your bank, cash, or M-Pesa accounts.</p>
  <button onclick="openModal('addAccount')" class="btn btn-primary" style="margin-top:12px"><i class="bi bi-plus-lg"></i> Add Account</button>
</div></div>
<?php endif; ?>

<!-- Add Account Modal -->
<div class="modal" id="addAccount"><div class="modal-box">
  <div class="card-header"><h5>Add Cash Account</h5><button onclick="closeModal('addAccount')" style="background:none;border:none;cursor:pointer;font-size:20px">&times;</button></div>
  <div class="card-body"><form method="POST" action="<?= $u('admin/fees/accounts/store') ?>">
    <?= \Core\View::csrf() ?>
    <div class="grid g2">
      <div class="form-group" style="grid-column:span 2"><label>Account Name *</label><input type="text" name="name" required placeholder="e.g. Equity Bank Main Account"></div>
      <div class="form-group"><label>Account Type *</label>
        <select name="type" required>
          <option value="bank">Bank Account</option>
          <option value="cash">Petty Cash</option>
          <option value="mpesa">M-Pesa</option>
          <option value="cheque">Cheque Account</option>
        </select>
      </div>
      <div class="form-group"><label>Opening Balance (KES)</label><input type="number" name="opening_balance" min="0" step="0.01" value="0"></div>
      <div class="form-group"><label>Account Number</label><input type="text" name="account_no" placeholder="e.g. 1234567890"></div>
      <div class="form-group"><label>Bank Name</label><input type="text" name="bank_name" placeholder="e.g. Equity Bank"></div>
    </div>
    <button type="submit" class="btn btn-primary"><i class="bi bi-save"></i> Add Account</button>
  </form></div>
</div></div>

