<?php

    require_once "config/connection.php";

    $arr = array();

    foreach ($_POST["chk_customer_id"] as $idx => $customer_id)
    {
        $sql = "DELETE FROM `customer` WHERE `customer_id` = " . $customer_id;

        if (!mysql_query($sql))
        {
            $arr["status"] = 0;
            $arr["message"] = mysql_error();
            die(json_encode($arr));
        }

        $sql = "SELECT c.case_id, `case_file_path` 
            	FROM `case` AS c LEFT JOIN `case_file` AS cf ON c.`case_id` = cf.`case_id`
            	WHERE `case_customer_id` = " . $customer_id;

        $tmprs = mysql_query($sql);
        while ($tmprow = mysql_fetch_array($tmprs))
        {
            $sql = "DELETE FROM `case_file`  WHERE `case_id` = " . $tmprow["case_id"];

            if (!mysql_query($sql))
            {
                $arr["status"] = 0;
                $arr["message"] = mysql_error();
                die(json_encode($arr));
            }

            $case_file_name = $tmprow["case_file_name"];
            if (!empty($case_file_name))
            {
                unlink(CASE_FILES_UPLOAD_DIR . "/" . $case_file_name);
            }
        }

        $sql = "DELETE FROM `case` WHERE `case_customer_id` = " . $customer_id;

        if (!mysql_query($sql))
        {
            $arr["status"] = 0;
            $arr["message"] = mysql_error();
            die(json_encode($arr));
        }
    }

    $arr["status"] = 1;
    $arr["message"] = "Client(s) supprimé(s) avec succès";
    echo json_encode($arr);
    