<?php
	// required headers
    header("Access-Control-Allow-Origin: *");
    header("Content-Type: application/json; charset=UTF-8");
    header("Access-Control-Allow-Methods: POST");
    header("Access-Control-Max-Age: 3600");
    header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

	// include database and object files
	include_once '../config/database.php';
	include_once '../objects/sales.php';
	// include_once '../objects/sales.php';
	date_default_timezone_set('Asia/Rangoon'); 
	session_start();
	// instantiate database
	$database = new Database();
	$db = $database->getConnection();
	 
	$sales = new Sales($db);
    $data = json_decode(file_get_contents("php://input"));
	// $sales = new Sales($db);

	$sales->city = 'ohn chaw';
    $sales->exp_type = $data->exp_type;
    $sales->df = date('Y-m-d');
    $sales->dt = date('Y-m-d',(strtotime ( '+10 day' , strtotime ( date('Y-m-d') ) ) ));

	$stmt = $sales->getExpireList();
	$num = $stmt->rowCount();
	$arr=array();
	$arr["records"]=array();

	if($num>0){

		while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
			extract($row);

            if($data->exp_type =="E" && $med<$sales->df && $med!=null){
                if(date_diff( date_create($sales->df),date_create( $med))->format("%R%a") <= 0){
                    $d = 1;
                    $item=array(
                        "id" => (int)$id,
                        "sales_date" => $sales_date,
                        "id_no" => $id_no,
                        "name" => $name,
                        "phone" => $phone,
                        "d" => date_diff( date_create($sales->df),date_create( $med))->format("%R%a"),
                        "end_date" => $med,
                        "exp_type" => $sales->exp_type,
                        "remark" => (($remark)?$remark:"")
                    );
                    array_push($arr["records"], $item);
                }
            }

            if($data->exp_type =="T" && $med==$sales->df && $med!=null){
                if(date_diff( date_create($sales->df),date_create( $med))->format("%R%a") == 0){
                    $d = 0;
                    $item=array(
                        "id" => (int)$id,
                        "sales_date" => $sales_date,
                        "id_no" => $id_no,
                        "name" => $name,
                        "phone" => $phone,
                        "d" => $d,
                        "end_date" => $med,
                        "remark" => (($remark)?$remark:"")
                    );
                    array_push($arr["records"], $item);
                }
            }

            
            if($data->exp_type =="O" && $med>$sales->df && $med!=null){
                if(date_diff( date_create($sales->df),date_create( $med))->format("%R%a") == 1){
                    $d = 1;
                    $item=array(
                        "id" => (int)$id,
                        "sales_date" => $sales_date,
                        "id_no" => $id_no,
                        "name" => $name,
                        "phone" => $phone,
                        "d" => $d,
                        "end_date" => $med,
                        "remark" => (($remark)?$remark:"")
                    );
                    array_push($arr["records"], $item);
                }else if(date_diff( date_create($sales->df),date_create( $med))->format("%a") == 3){
                    $d = 3;
                    $item=array(
                        "id" => (int)$id,
                        "sales_date" => $sales_date,
                        "id_no" => $id_no,
                        "name" => $name,
                        "phone" => $phone,
                        "d" => $d,
                        "end_date" => $med,
                        "remark" => (($remark)?$remark:"")
                    );
                    array_push($arr["records"], $item);
                }else if(date_diff( date_create($sales->df),date_create( $med))->format("%a") == 5){
                    $d = 5;
                    $item=array(
                        "id" => (int)$id,
                        "sales_date" => $sales_date,
                        "id_no" => $id_no,
                        "name" => $name,
                        "phone" => $phone,
                        "d" => $d,
                        "end_date" => $med,
                        "remark" => (($remark)?$remark:""),
                        "de" => date_diff( date_create($sales->df),date_create( $med))->format("%a")
                    );
                    array_push($arr["records"], $item);
                }
            }

            
            if($data->exp_type =="N" && ($med==null || $med=='')){


                $item=array(
                    "id" => (int)$id,
                    "sales_date" => $sales_date,
                    "id_no" => $id_no,
                    "invoice_no" => $invoice_no,
                    "name" => $name,
                    "phone" => $phone,
                    "d" => '',
                    "package" => $package,
                    "start_date" => (($start_date)?$start_date:""),
                    "end_date" => (($med)?$med:""),
                    "remark" => (($remark)?$remark:"")
                );
                array_push($arr["records"], $item);
            
        }
            // else if(date_diff( date_create($sales->df),date_create( $end_date))->format("%a") < 1){

            //     $item=array(
            //         "id" => (int)$id,
            //         "id_no" => $id_no,
            //         "name" => $name,
            //         "phone" => $phone,
            //         "d" => date_diff( date_create($sales->df),date_create( $end_date))->format("%a")
            //     );
            //     array_push($arr["records"], $item);
            // }


		}
	 

	}

	echo json_encode($arr);
?>