<?php 
include '../includes/config.php';

if (isset($_REQUEST['del'])) {
    $id = intval($_GET['del']);
    
    $conn->beginTransaction();

    try {
        $sql = "DELETE FROM users WHERE id = :id";
        $query = $conn->prepare($sql);
        $query->bindParam(':id', $id, PDO::PARAM_INT);
        $query->execute();

        $count = $query->rowCount();
        if ($count > 0) {
            $sql2 = "DELETE FROM user_rights WHERE user_id = :id";
            $query2 = $conn->prepare($sql2);
            $query2->bindParam(':id', $id, PDO::PARAM_INT);
            $query2->execute();

            $conn->commit();

            echo "<script>alert('Records deleted successfully from User and Rights tables.');</script>";
        } else {
            $conn->rollBack();
            echo "<script>alert('No records found to delete.');</script>";
        }
    } catch (Exception $e) {
        $conn->rollBack();
        echo "<script>alert('Error deleting records: " . $e->getMessage() . "');</script>";
    }

    echo "<script>window.location.href='../list_users.php'</script>";
}
?>
