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

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

if (isset($_GET['id'])) {
    $id = $_GET['id'];

    try {
        $conn->beginTransaction();

        $sql1 = "DELETE FROM cash_deposit WHERE bank_id = :id";
        $stmt1 = $conn->prepare($sql1);
        $stmt1->bindParam(':id', $id);
        $stmt1->execute();

        $sql2 = "DELETE FROM bank_debit_amount WHERE bank_id = :id";
        $stmt2 = $conn->prepare($sql2);
        $stmt2->bindParam(':id', $id);
        $stmt2->execute();

        $sql3 = "DELETE FROM expense WHERE bank_id = :id";
        $stmt3 = $conn->prepare($sql3);
        $stmt3->bindParam(':id', $id);
        $stmt3->execute();

        $sql4 = "DELETE FROM banks WHERE id = :id";
        $stmt4 = $conn->prepare($sql4);
        $stmt4->bindParam(':id', $id);
        $stmt4->execute();

        $conn->commit();

        header('Location: ../list_banks.php');
        exit();

    } catch (PDOException $e) {
        $conn->rollBack();
        echo "Error: " . $e->getMessage();
    }
} else {
    echo "No bank ID provided!";
}
?>
