<?php
include 'includes/config.php';
session_start();

if (!isset($_SESSION['email'])) {
    header('Location: login.php');
    exit();
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Get the form data
    $id = $_POST['id'];
    $bank_name = $_POST['bank_name'];
    $start_bank_date = $_POST['start_bank_date'];
    $account_number = $_POST['account_number'];
    $start_bank_balance = $_POST['start_bank_balance'];
    $description = $_POST['description'];

    try {
        // Prepare the update query
        $sql = "UPDATE banks 
                SET bank_name = :bank_name, start_bank_date = :start_bank_date, account_number = :account_number, 
                    start_bank_balance = :start_bank_balance, description = :description 
                WHERE id = :id";

        $stmt = $conn->prepare($sql);

        // Bind the parameters
        $stmt->bindParam(':id', $id);
        $stmt->bindParam(':bank_name', $bank_name);
        $stmt->bindParam(':start_bank_date', $start_bank_date);
        $stmt->bindParam(':account_number', $account_number);
        $stmt->bindParam(':start_bank_balance', $start_bank_balance);
        $stmt->bindParam(':description', $description);

        // Execute the query
        $stmt->execute();

        // Redirect to the bank list page after updating
        header('Location: list_banks.php');
        exit();
    } catch (PDOException $e) {
        echo "Error: " . $e->getMessage();
    }
}
?>
