<?php $this->start('body'); ?>
<?php $banks = $this->data['banks']; $f = $this->data['form']; ?>

<div class="d-flex align-items-center justify-content-between mb-3">
  <h5 class="mb-0 fw-semibold"><i class="fas fa-file-import me-2 text-muted"></i>Import Bank Statement</h5>
  <a href="<?= PROOT; ?>BankRecon" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
</div>

<?= $this->raw('fb_msg'); ?>

<div class="row g-3">
  <div class="col-lg-7">
    <form method="POST" action="<?= PROOT; ?>BankRecon/save" enctype="multipart/form-data" class="card border-0 shadow-sm">
      <?= csrf_field(); ?>
      <div class="card-body">
        <div class="row g-3">
          <div class="col-md-6">
            <label class="form-label">Bank account <span class="text-danger">*</span></label>
            <select name="bank_account_id" class="form-select" required>
              <option value="">— pick —</option>
              <?php foreach ($banks as $b): ?>
                <option value="<?= (int)$b['c0d']; ?>" <?= ((int)($f['bank_account_id'] ?? 0) === (int)$b['c0d']) ? 'selected' : ''; ?>>
                  <?= htmlspecialchars($b['account_code']); ?> · <?= htmlspecialchars($b['account_name']); ?>
                </option>
              <?php endforeach; ?>
            </select>
          </div>
          <div class="col-md-3">
            <label class="form-label">Statement no</label>
            <input type="text" name="reference" class="form-control" value="<?= htmlspecialchars($f['reference'] ?? ''); ?>">
          </div>
          <div class="col-md-3">
            <label class="form-label">Opening balance</label>
            <input type="number" step="0.01" name="opening_balance" class="form-control" value="<?= htmlspecialchars($f['opening_balance'] ?? '0.00'); ?>">
          </div>

          <div class="col-md-3">
            <label class="form-label">From</label>
            <input type="date" name="statement_from" class="form-control" value="<?= htmlspecialchars($f['statement_from'] ?? ''); ?>">
            <div class="form-text small">Auto-detected if blank</div>
          </div>
          <div class="col-md-3">
            <label class="form-label">To</label>
            <input type="date" name="statement_to" class="form-control" value="<?= htmlspecialchars($f['statement_to'] ?? ''); ?>">
          </div>
          <div class="col-md-3">
            <label class="form-label">Closing balance</label>
            <input type="number" step="0.01" name="closing_balance" class="form-control" value="<?= htmlspecialchars($f['closing_balance'] ?? '0.00'); ?>">
          </div>
          <div class="col-md-3">
            <label class="form-label">Notes</label>
            <input type="text" name="notes" class="form-control" value="<?= htmlspecialchars($f['notes'] ?? ''); ?>">
          </div>

          <div class="col-12">
            <label class="form-label">CSV file <span class="text-danger">*</span></label>
            <input type="file" name="csv" class="form-control" accept=".csv,.tsv,.txt" required>
          </div>
        </div>
      </div>
      <div class="card-footer bg-white d-flex justify-content-end gap-2 py-3">
        <a href="<?= PROOT; ?>BankRecon" class="btn btn-outline-secondary">Cancel</a>
        <button class="btn btn-primary"><i class="fas fa-upload me-1"></i>Import</button>
      </div>
    </form>
  </div>

  <div class="col-lg-5">
    <div class="card border-0 shadow-sm">
      <div class="card-body">
        <h6 class="text-uppercase text-muted small mb-3">Expected CSV format</h6>
        <p class="small mb-2">The parser is order-tolerant — it reads the header row and maps common column names. Any subset of these works:</p>
        <table class="table table-sm small">
          <thead class="table-light">
            <tr><th>Column</th><th>Accepts</th></tr>
          </thead>
          <tbody>
            <tr><td><strong>Date</strong> <span class="text-danger">*</span></td><td class="font-monospace small">Date, TxnDate, ValueDate, PostingDate</td></tr>
            <tr><td>Description</td><td class="font-monospace small">Description, Narrative, Details, Particulars</td></tr>
            <tr><td>Reference</td><td class="font-monospace small">Reference, TxnRef, ChequeNo</td></tr>
            <tr><td><strong>Amount</strong> <span class="text-danger">*</span></td><td class="font-monospace small">Amount &nbsp;<em>or</em>&nbsp; MoneyIn + MoneyOut</td></tr>
            <tr><td>Balance</td><td class="font-monospace small">Balance, RunningBalance</td></tr>
          </tbody>
        </table>
        <p class="small mb-1"><strong>Dates:</strong> <span class="font-monospace">YYYY-MM-DD</span>, <span class="font-monospace">DD/MM/YYYY</span>, and <span class="font-monospace">01 Aug 2026</span> all parse.</p>
        <p class="small mb-1"><strong>Amounts:</strong> if you use a single Amount column, use signed values (+deposit / -withdrawal). Parenthesised values like <span class="font-monospace">(1,234.56)</span> are treated as negative.</p>
        <p class="small mb-0 text-muted">Not sure? Import a few lines first — the parser skips rows it can't understand and reports the count.</p>
      </div>
    </div>
  </div>
</div>
<?php $this->end(); ?>
