<?php
require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../classes/Branch.php';
requireRole(['branch_admin']);
$pageTitle = 'Request Penarikan';

$branchId = currentBranchId();
$branch = Branch::find($branchId);

$message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!verifyCsrf($_POST['csrf_token'] ?? '')) {
        $message = '<div class="alert alert-danger">Token tidak valid</div>';
    } else {
        $result = Branch::requestWithdrawal(
            $branchId,
            (float) $_POST['amount'],
            $_POST['bank_name'],
            $_POST['account_number'],
            $_POST['account_holder'],
            $_POST['notes'] ?? ''
        );
        if ($result['success']) {
            logActivity(currentUserId(), 'request_withdrawal', "Request penarikan " . formatRupiah($_POST['amount']));
            redirect('/branch/withdrawal.php', 
                "Request penarikan berhasil diajukan. Dapat diproses setelah: " . $result['can_approve_at']);
        } else {
            $message = '<div class="alert alert-danger">' . e($result['error']) . '</div>';
        }
    }
}

$available = $branch['balance'] - $branch['pending_withdrawal'];

require __DIR__ . '/../includes/header.php';
require __DIR__ . '/../includes/sidebar.php';
?>
<div class="main-content">
  <h3>Request Penarikan Dana</h3>
  <?= $message ?>
  <?= flashMessage() ?>

  <div class="alert alert-info">
    <strong>Saldo Available:</strong> <?= formatRupiah($available) ?><br>
    <small>⏰ Request penarikan akan menunggu 1x24 jam sebelum dapat diproses oleh pusat.</small>
  </div>

  <div class="card">
    <div class="card-body">
      <form method="POST">
        <?= csrfField() ?>
        <div class="mb-3">
          <label class="form-label">Jumlah Penarikan *</label>
          <input type="number" name="amount" class="form-control" required min="<?= getSetting('min_withdrawal', 100000) ?>" max="<?= $available ?>">
          <small class="text-muted">Min: <?= formatRupiah(getSetting('min_withdrawal', 100000)) ?></small>
        </div>
        <div class="row">
          <div class="col-md-4 mb-3">
            <label class="form-label">Nama Bank *</label>
            <input type="text" name="bank_name" class="form-control" required value="<?= e($branch['bank_name']) ?>">
          </div>
          <div class="col-md-4 mb-3">
            <label class="form-label">No. Rekening *</label>
            <input type="text" name="account_number" class="form-control" required value="<?= e($branch['bank_account']) ?>">
          </div>
          <div class="col-md-4 mb-3">
            <label class="form-label">Atas Nama *</label>
            <input type="text" name="account_holder" class="form-control" required value="<?= e($branch['bank_holder']) ?>">
          </div>
        </div>
        <div class="mb-3">
          <label class="form-label">Catatan</label>
          <textarea name="notes" class="form-control" rows="2"></textarea>
        </div>
        <button class="btn btn-primary">Ajukan Request</button>
        <a href="/branch/index.php" class="btn btn-secondary">Batal</a>
      </form>
    </div>
  </div>
</div>
<?php require __DIR__ . '/../includes/footer.php'; ?>