<?php
include_once __DIR__."/config.php";
function getEstateData($city, $fDate, $toDate){
    $mysqli = get_mysqli_connection();
    $sql = "SELECT city, listdate, sum(nums) as num FROM estate_listing where listdate >= ? and listdate <= ? and city = ? group by listdate";
    $result = db_query($mysqli, $sql, [$fDate, $toDate, $city]);
    $data=array();
    if($result && $result->num_rows>0) {
        while($row = $result->fetch_assoc()){
            array_push($data,array("value"=>array($row['listdate'],$row['num'])));
        }
    }

    return $data;
}

function esfTradeDaily(){
    global $mysqli;
    $district = $_REQUEST['district'];
    $t_start = $_REQUEST['t_start'];
    $t_end = $_REQUEST['t_end'];
    if($_REQUEST['dm']=='Daily'){
        $sql = "SELECT distinct(ej.uuid) uid, date_format(tdate,'%Y-%m-%d') td, eje.val district, eje1.val qty, eje2.val area
            FROM estate_json ej
            left join estate_json_ext eje on ej.uuid=eje.uuid and eje.val = ?
            left join estate_json_ext eje1 on eje1.id=eje.id+3
            left join estate_json_ext eje2 on eje2.id=eje.id+4
            where ej.data_type='5' and ej.tdate >= ? and ej.tdate <= ?
            order by ej.tdate asc";
        $params = [$district, $t_start, $t_end];
    }
    if($_REQUEST['dm']=='Monthly'){
        $sql = "SELECT distinct(ej.uuid) uid, date_format(tdate,'%Y-%m') td, eje.val district, sum(eje1.val) qty, sum(eje2.val) area
            FROM estate_json ej
            left join estate_json_ext eje on ej.uuid=eje.uuid and eje.val = ?
            left join estate_json_ext eje1 on eje1.id=eje.id+3
            left join estate_json_ext eje2 on eje2.id=eje.id+4
            where ej.data_type='5'";
        $params = [$district];
        if($t_start) {
            $sql .= " and ej.tdate >= ?";
            $params[] = $t_start . '-01';
        }
        if($t_end) {
            $endDate = new DateTime($t_end);
            $endDate->modify('last day of this month');
            $sql .= " and ej.tdate <= ?";
            $params[] = $endDate->format('Y-m-d');
        }
        $sql .= " group by date_format(tdate,'%Y-%m') order by date_format(tdate,'%Y-%m') asc";
    }
    $result = db_query($mysqli, $sql, $params);
    $data = $result->fetch_all(MYSQLI_ASSOC);
    return $data;
}

function esfListDaily(){
    global $mysqli;
    $district = $_REQUEST['district'];
    $t_start = $_REQUEST['t_start'];
    $t_end = $_REQUEST['t_end'];
    if($_REQUEST['dm']=='Daily'){
        $sql = "SELECT distinct(ej.uuid) uid, date_format(tdate,'%Y-%m-%d') td, eje.val district, eje1.val qty, eje2.val price
            FROM estate_json ej
            left join estate_json_ext eje on ej.uuid=eje.uuid and eje.val = ?
            left join estate_json_ext eje1 on eje1.id=eje.id+3
            left join estate_json_ext eje2 on eje2.id=eje.id+5
            where ej.data_type='8' and ej.tdate >= ? and ej.tdate <= ?
            order by ej.tdate asc";
        $params = [$district, $t_start, $t_end];
    }
    if($_REQUEST['dm']=='Monthly'){
        $sql = "SELECT distinct(ej.uuid) uid, date_format(tdate,'%Y-%m') td, eje.val district, sum(eje1.val) qty, sum(eje2.val) price
            FROM estate_json ej
            left join estate_json_ext eje on ej.uuid=eje.uuid and eje.val = ?
            left join estate_json_ext eje1 on eje1.id=eje.id+3
            left join estate_json_ext eje2 on eje2.id=eje.id+5
            where ej.data_type='8'";
        $params = [$district];
        if($t_start) {
            $sql .= " and ej.tdate >= ?";
            $params[] = $t_start . '-01';
        }
        if($t_end) {
            $endDate = new DateTime($t_end);
            $endDate->modify('last day of this month');
            $sql .= " and ej.tdate <= ?";
            $params[] = $endDate->format('Y-m-d');
        }
        $sql .= " group by date_format(tdate,'%Y-%m') order by date_format(tdate,'%Y-%m') asc";
    }
    $result = db_query($mysqli, $sql, $params);
    $data = $result->fetch_all(MYSQLI_ASSOC);
    return $data;
}
?>