<?php
include "../config.php";
$data = array();
$state_arr = array();
$dealer_arr = array();
if (!empty($_POST["id"])) {
    $id = $_POST['id'];
    $time = $_POST['timeX'];
	$timeZ = "('".implode("','",$time)."')";
	$columnS=array("Q9g","Region","State","Main_dealer_name");
    $mq=query_marge($time,$columnS);
    
	$id= explode(",",$id);
    $region_arr = '('.'"' . implode('", "', $id) . '"'.')';
    $query = "SELECT DISTINCT `State` FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND `Region`IN $region_arr";
    $result = mysqli_query($conn,$query);
    if (mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            array_push($state_arr,$row['State']);
        }
    }

    $data['state'] = $state_arr;

    $query2 = "SELECT DISTINCT `Main_dealer_name` FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND `Region` IN $region_arr";
    $result2 = mysqli_query($conn, $query2);
    if (mysqli_num_rows($result2) > 0) {
        while ($row = mysqli_fetch_assoc($result2)) {
            array_push($dealer_arr,$row['Main_dealer_name']);
        }
    }
    $data['main_dealer'] = $dealer_arr;
}

echo json_encode($data);

?>