<?php
// Add new account
if (isset($_POST['save_account_detail_btn'])) {
    $bank_name = inject_checker($connection, $_POST['bank_name']);
    $account_name = inject_checker($connection, $_POST['account_name']);
    $account_number = inject_checker($connection, $_POST['account_number']);
    $account_purpose = inject_checker($connection, $_POST['account_purpose']);

    if (empty($bank_name) || empty($account_name) || empty($account_number) || empty($account_purpose)) {
        $_SESSION['msg'] = "All fields are required.";
        $_SESSION['type'] = "danger";
    } else {
        $check = $connection->prepare("SELECT id FROM account_details WHERE account_number = ?");
        $check->bind_param("s", $account_number);
        $check->execute();
        $check->store_result();

        if ($check->num_rows > 0) {
            $_SESSION['msg'] = "Account number already exists.";
            $_SESSION['type'] = "warning";
        } else {
            $stmt = $connection->prepare("INSERT INTO account_details (bank_name, account_name, account_number, account_purpose) VALUES (?, ?, ?, ?)");
            $stmt->bind_param("ssss", $bank_name, $account_name, $account_number, $account_purpose);
            $_SESSION['msg'] = $stmt->execute() ? "Account saved successfully." : "Error saving account.";
            $_SESSION['type'] = $stmt->execute() ? "success" : "danger";
            $stmt->close();
        }
        $check->close();
    }
    header("Location: save_account_details");
    exit;
}

// Update account
if (isset($_POST['update_account_btn'])) {
    $account_id = $_POST['account_id'];
    $bank_name = inject_checker($connection, $_POST['bank_name']);
    $account_name = inject_checker($connection, $_POST['account_name']);
    $account_number = inject_checker($connection, $_POST['account_number']);
    $account_purpose = inject_checker($connection, $_POST['account_purpose']);

    $stmt = $connection->prepare("UPDATE account_details SET bank_name = ?, account_name = ?, account_number = ?, account_purpose = ? WHERE id = ?");
    $stmt->bind_param("ssssi", $bank_name, $account_name, $account_number, $account_purpose, $account_id);
    $_SESSION['msg'] = $stmt->execute() ? "Account updated successfully." : "Failed to update account.";
    $_SESSION['type'] = $stmt->execute() ? "success" : "danger";
    $stmt->close();

    header("Location: save_account_details");
    exit;
}

// Delete account
if (isset($_POST['delete_account_id'])) {
    $account_id = $_POST['delete_account_id'];
    $stmt = $connection->prepare("DELETE FROM account_details WHERE id = ?");
    $stmt->bind_param("i", $account_id);
    $_SESSION['msg'] = $stmt->execute() ? "Account deleted." : "Deletion failed.";
    $_SESSION['type'] = $stmt->execute() ? "success" : "danger";
    $stmt->close();

    header("Location: save_account_details");
    exit;
}
?>


<div class='page-title'>
    <div>
        <h1><i class='fa fa-laptop'></i> School Bank Account Details</h1>
        <p>Add and manage school bank accounts</p>
    </div>
</div>
<div class='card'>
    <form action='' method='POST'>
        <div class='panel panel-primary ch'>
            <div class='panel-heading'>					
                <h4 class='text-center'>Add New Bank Account Detail</h4>
            </div>
            <div class='panel-body'>
                <div class='row'>
                    <div class='col-md-5'>
                        <input name='bank_name' class='form-control' placeholder='Bank Name' required>
                    </div>
                    <div class='col-md-5'>
                        <input name='account_name' class='form-control' placeholder='Account Name' required>
                    </div>
                </div>
                <br>
                <div class='row'>
                    <div class='col-md-5'>
                        <input name='account_number' class='form-control' placeholder='Account Number' required>
                    </div>
                    <div class='col-md-5'>
                        <input name='account_purpose' class='form-control' placeholder='Account Purpose' required>
                    </div>
                </div>
                <br>
                <div class='row'>
                    <div class='col-md-2'>
                        <button name='save_account_detail_btn' class='btn btn-primary'>Save</button>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>

<?php
$accounts = $connection->query("SELECT * FROM account_details ORDER BY id DESC");
if ($accounts && $accounts->num_rows > 0):
?>
<div class="card mt-4">
    <div class="card-header bg-dark text-white text-center">Existing Bank Accounts</div>
    <div class="card-body">
        <table class="table table-bordered table-hover">
            <thead>
                <tr class="text-center">
                    <th>S/N</th>
                    <th>Bank</th>
                    <th>Account Name</th>
                    <th>Account Number</th>
                    <th>Purpose</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
            <?php 
            $sn = 1; 
            $accounts->data_seek(0); // Ensure pointer reset
            while ($acc = $accounts->fetch_assoc()): 
            ?>
                <tr class="text-center">
                    <td><?= $sn++ ?></td>
                    <td><?= htmlspecialchars($acc['bank_name']) ?></td>
                    <td><?= htmlspecialchars($acc['account_name']) ?></td>
                    <td><?= htmlspecialchars($acc['account_number']) ?></td>
                    <td><?= htmlspecialchars($acc['account_purpose']) ?></td>
                    <td>
                        <button class="btn btn-sm btn-warning" data-toggle="modal" data-target="#editModal<?= $acc['id'] ?>">Edit</button>
                        <form method="post" action="" style="display:inline-block;" onsubmit="return confirm('Delete this account?');">
                            <input type="hidden" name="delete_account_id" value="<?= $acc['id'] ?>">
                            <button class="btn btn-sm btn-danger">Delete</button>
                        </form>
                    </td>
                </tr>
                <!-- Edit Modal -->
                <div class="modal fade" id="editModal<?= $acc['id'] ?>" tabindex="-1">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <form method="post">
                                <input type="hidden" name="account_id" value="<?= $acc['id'] ?>">
                                <div class="modal-header">
                                    <h5 class="modal-title">Edit Account</h5>
                                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                                </div>
                                <div class="modal-body">
                                    <div class="form-group">
                                        <label>Bank Name</label>
                                        <input type="text" class="form-control" name="bank_name" value="<?= htmlspecialchars($acc['bank_name']) ?>" required>
                                    </div>
                                    <div class="form-group">
                                        <label>Account Name</label>
                                        <input type="text" class="form-control" name="account_name" value="<?= htmlspecialchars($acc['account_name']) ?>" required>
                                    </div>
                                    <div class="form-group">
                                        <label>Account Number</label>
                                        <input type="text" class="form-control" name="account_number" value="<?= htmlspecialchars($acc['account_number']) ?>" required>
                                    </div>
                                    <div class="form-group">
                                        <label>Account Purpose</label>
                                        <input type="text" class="form-control" name="account_purpose" value="<?= htmlspecialchars($acc['account_purpose']) ?>" required>
                                    </div>
                                </div>
                                <div class="modal-footer">
                                    <button type="submit" name="update_account_btn" class="btn btn-primary">Update</button>
                                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            <?php endwhile; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

<!-- Required Bootstrap & jQuery for modal -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.bundle.min.js"></script>
