<?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->dd =  $data->dd;

    $sales->city =  $data->city;
    $sales->mbps =  isset($data->mbps)?$data->mbps:"";
    $sales->months =  isset($data->months)?$data->months:"";
    $sales->id_no =  isset($data->id_no)?$data->id_no:"";
    $sales->exp_type = $data->exp_type;
    $sales->remark = isset($data->remark)?$data->remark:"";
    $sales->df = date('Y-m-d');
    $sales->dt = date('Y-m-d',(strtotime ( '+10 day' , strtotime ( date('Y-m-d') ) ) ));

    if((isset($data->check_type)?$data->check_type:"")==1){
        $sales->df =  $data->df;
        $sales->dt =  $data->dt;
        $stmt = $sales->getExpireListByDate();
        $num = $stmt->rowCount();
        $arr=array();
        $arr["records"]=array();

        if($num>0){

            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
                extract($row);
                        $item=array(
                            "id" => (int)$id,
                            "sales_date" => $sales_date,
                            "id_no" => $id_no,
                            "invoice_no" => $invoice_no,
                            "name" => $name,
                            "phone" => $phone,
                            "address" => $address,
                            "d" => date_diff( date_create(date('Y-m-d')),date_create( $med))->format("%R%a"),
                            "package" => $package,
                            "price" => (int)$price,
                            "start_date" => $start_date,
                            "end_date" => $med,
                            "exp_type" => $sales->exp_type,
                            "remark" => (($remark)?$remark:""),
                            "remark_date" => (($rmd)?$rmd:"")
                        );
                        array_push($arr["records"], $item);           
            }       
        }
    }else{
        $sales->exp_type =  $data->exp_type;
        $stmt = $sales->getExpireList(date('Y-m-d'));
        $num = $stmt->rowCount();
        $arr=array();
        $arr["records"]=array();

        if($num>0){

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

                        $item=array(
                            "id" => (int)$id,
                            "sales_date" => $sales_date,
                            "id_no" => $id_no,
                            "invoice_no" => $invoice_no,
                            "name" => $name,
                            "phone" => $phone,
                            "address" => $address,
                            "d" => date_diff( date_create(date('Y-m-d')),date_create( $med))->format("%R%a"),
                            "package" => $package,
                            "price" => (int)$price,
                            "start_date" => $start_date,
                            "end_date" => $med,
                            "exp_type" => $sales->exp_type,
                            "remark" => (($remark)?$remark:""),
                            "remark_date" => (($rmd)?$rmd:"")
                        );
                        array_push($arr["records"], $item);
                    
            }    

        }
    }


    

    echo json_encode($arr);
?>