<?php
include 'db.php';
include 'layout.php';

$id = $_GET['id'] ?? 0;

// Update logic
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $bank_name      = $_POST['bank_name'];
    $account_title  = $_POST['account_title'];
    $account_number = $_POST['account_number'];
    $iban           = $_POST['iban'];
    $branch_name    = $_POST['branch_name'];
    $branch_code    = $_POST['branch_code'];
    $swift_code     = $_POST['swift_code'];
    $address        = $_POST['address'];
    $is_active      = isset($_POST['is_active']) ? 1 : 0;

    $stmt = $conn->prepare("UPDATE bank_accounts SET 
        bank_name=?, account_title=?, account_number=?, iban=?, branch_name=?, 
        branch_code=?, swift_code=?, address=?, is_active=? 
        WHERE id=?");
    $stmt->bind_param("ssssssssii", $bank_name, $account_title, $account_number, $iban, $branch_name, $branch_code, $swift_code, $address, $is_active, $id);
    $stmt->execute();

    echo "<script>
        Swal.fire({
            icon: 'success',
            title: 'Updated!',
            text: 'Bank account updated successfully.',
            timer: 2000,
            showConfirmButton: false
        }).then(() => {
            window.location.href = 'bank_account_list.php';
        });
    </script>";
    exit;
}

// Fetch bank record
$res = $conn->query("SELECT * FROM bank_accounts WHERE id = $id");
$bank = $res->fetch_assoc();
?>

<div class="main-wrapper">
  <div class="page-wrapper">
    <div class="content">
      <div class="page-header">
        <div class="page-title">
          <h4>Edit Bank Account</h4>
        </div>
      </div>

      <div class="card">
        <div class="card-body">
          <form method="POST">
            <div class="mb-2"><label>Bank Name</label>
              <input type="text" name="bank_name" value="<?= $bank['bank_name'] ?>" class="form-control" required>
            </div>

            <div class="mb-2"><label>Account Title</label>
              <input type="text" name="account_title" value="<?= $bank['account_title'] ?>" class="form-control" required>
            </div>

            <div class="mb-2"><label>Account Number</label>
              <input type="text" name="account_number" value="<?= $bank['account_number'] ?>" class="form-control" required>
            </div>

            <div class="mb-2"><label>IBAN</label>
              <input type="text" name="iban" value="<?= $bank['iban'] ?>" class="form-control">
            </div>

            <div class="mb-2"><label>Branch Name</label>
              <input type="text" name="branch_name" value="<?= $bank['branch_name'] ?>" class="form-control">
            </div>

            <div class="mb-2"><label>Branch Code</label>
              <input type="text" name="branch_code" value="<?= $bank['branch_code'] ?>" class="form-control">
            </div>

            <div class="mb-2"><label>SWIFT Code</label>
              <input type="text" name="swift_code" value="<?= $bank['swift_code'] ?>" class="form-control">
            </div>

            <div class="mb-2"><label>Address</label>
              <textarea name="address" class="form-control"><?= $bank['address'] ?></textarea>
            </div>

            <div class="form-check mb-2">
              <input class="form-check-input" type="checkbox" name="is_active" <?= $bank['is_active'] ? 'checked' : '' ?>>
              <label class="form-check-label">Active</label>
            </div>

            <button type="submit" class="btn btn-primary">Update Bank Account</button>
          </form>
        </div>
      </div>
    </div>
  </div>
</div>
