<?php
session_start();
require("php_con.php");
if(!isset($_COOKIE['user_id'])){
    echo "<script>window.location='logout';</script>";
    exit();
}

$cs_id = $_GET['id'] ?? null;
if ($cs_id) {
    // Fetch customer details to get photo paths
    $stmt = $connation->prepare("SELECT cs_photo, cs_id_photo FROM customer_details WHERE cs_id = ?");
    $stmt->bind_param("i", $cs_id);
    $stmt->execute();
    $result = $stmt->get_result();
    $customer = $result->fetch_assoc();

    // Delete customer photos if they exist
    if ($customer['cs_photo'] && file_exists($customer['cs_photo'])) {
        unlink($customer['cs_photo']);
    }
    if ($customer['cs_id_photo'] && file_exists($customer['cs_id_photo'])) {
        unlink($customer['cs_id_photo']);
    }

    // Delete customer record
    $stmt = $connation->prepare("DELETE FROM customer_details WHERE cs_id = ?");
    $stmt->bind_param("i", $cs_id);
    if ($stmt->execute()) {
        echo "<script>alert('Customer and associated photos deleted successfully.'); window.location='view_customers.php';</script>";
    } else {
        echo "<script>alert('Error deleting customer: " . $stmt->error . "'); window.location='view_customers.php';</script>";
    }
} else {
    echo "<script>alert('No customer ID provided.'); window.location='view_customers.php';</script>";
}
?>