<?php
include('../config/function.php'); // Include your database connection file

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

    // Check if the contractID exists
    $checkContractQuery = "SELECT * FROM contracts WHERE contractID = '$contractID'";
    $checkContractResult = mysqli_query($conn, $checkContractQuery);

    if ($checkContractResult && mysqli_num_rows($checkContractResult) > 0) {
        // Delete the associated project first
        $deleteProjectQuery = "DELETE FROM projects WHERE contractID = '$contractID'";
        $resultProject = mysqli_query($conn, $deleteProjectQuery);

        // Delete the contract
        $deleteContractQuery = "DELETE FROM contracts WHERE contractID = '$contractID'";
        $resultContract = mysqli_query($conn, $deleteContractQuery);

        if ($resultContract && $resultProject) {
            $_SESSION['status'] = 'Contract and associated Project deleted successfully.';
        } else {
            $_SESSION['status'] = 'Error deleting Contract and Project. Please try again.';
        }
    } else {
        $_SESSION['status'] = 'Contract not found.';
    }

    header('Location: contracts.php');
    exit();
} else {
    // If contractID is not provided, redirect to contracts.php
    header('Location: contracts.php');
    exit();
}
?>