<?php
    header('Access-Control-Allow-Origin: '.$config->get('site_url'));
    header('Content-Type: application/json');
    header('Access-Control-Allow-Methods: POST');
    header('Access-Control-Allow-Headers: Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With');
    
    $response;
    if($_SERVER["REQUEST_METHOD"] == "POST") {
        if(isset($_POST['csrf_token']) && $csrf_generator->validate($_POST['csrf_token'])) {
            $results = array();
            $status_sale = "1,3,4,5,6,7,8,11";
            $status_cancelled = "9,10,12";
            $date_start = isset($_POST['date_start']) && !empty($_POST['date_start']) ? date("Y-m-d", strtotime($_POST['date_start'])) : date("Y-m-d", strtotime("-1 month"));
            $date_end = isset($_POST['date_end']) && !empty($_POST['date_end']) ? date("Y-m-d", strtotime($_POST['date_end'])) : date("Y-m-d");

            $date1 = new DateTime($date_start);
            $date2 = new DateTime($date_end);
            $interval = $date2->diff($date1);
            $days = $interval->days;
            $grand_total = 0;

            for($i = $days; $i >= 0; $i--) {
                $date       = date("Y-m-d", strtotime("-$i day", strtotime($date_end)));
                $stmt  	= $db->query("SELECT SUM(order_total) AS 'total' FROM ".$db->prefix."orders WHERE status IN ({$status_sale}) AND CAST(date_added AS DATE) = :date", array(':date'=>$date));
                if($stmt->num_rows > 0){
                    $total      = $stmt->row["total"];
                    $results['sale']['labels'][] = $date;
                    $results['sale']['data'][] = $total;
                    $grand_total += $total;
                }else{
                    $results['sale']['labels'][] = $date;
                    $results['sale']['data'][] = 0;
                }
            }


            for($i = $days; $i >= 0; $i--) {
                $date       = date("Y-m-d", strtotime("-$i day", strtotime($date_end)));
                $stmt  	= $db->query("SELECT SUM(order_total) AS 'total' FROM ".$db->prefix."orders WHERE status IN ({$status_cancelled}) AND CAST(date_added AS DATE) = :date", array(':date'=>$date));
                if($stmt->num_rows > 0){
                    $total      = $stmt->row["total"];
                    $results['cancelled']['labels'][] = $date;
                    $results['cancelled']['data'][] = $total;
                }else{
                    $results['cancelled']['labels'][] = $date;
                    $results['cancelled']['data'][] = 0;
                }
            }
            $results['grand_total'] = $utils->moneyFormatter($grand_total) . " TL";
            $response = array('status'=>true, 'result'=>$results);
        } else {
            $response = array('status'=>false, 'message'=>'Hatalı CSRF token. Lütfen daha sonra tekrar deneyiniz.');
        }
    } else {
        $response = array('status'=>false, 'message'=>'Hatalı istek. Lütfen daha sonra tekrar deneyiniz.');
    }

    echo json_encode($response);