<?php
ob_start();
date_default_timezone_set("Asia/Manila");

$action = $_GET['action'];
// include 'admin_class.php';
session_start();
ini_set('display_errors', 1);
Class Action {
    private $db;

    public function __construct() {
        ob_start();
    include 'db_connect.php';
    
    $this->db = $conn;
    }
    function __destruct() {
        $this->db->close();
        ob_end_flush();
    }

    
    function save_parcel(){
        extract($_POST);
        foreach($price as $k => $v){
            $data = "";
            foreach($_POST as $key => $val){
                if(!in_array($key, array('id','weight','height','width','length','price')) && !is_numeric($key)){
                    if(empty($data)){
                        $data .= " $key='$val' ";
                    }else{
                        $data .= ", $key='$val' ";
                    }
                }
            }
            if(!isset($type)){
                $data .= ", type='2' ";
            }
                $data .= ", height='{$height[$k]}' ";
                $data .= ", width='{$width[$k]}' ";
                $data .= ", length='{$length[$k]}' ";
                $data .= ", weight='{$weight[$k]}' ";
                $price[$k] = str_replace(',', '', $price[$k]);
                $data .= ", price='{$price[$k]}' ";
            if(empty($id)){
                $i = 0;
                while($i == 0){
                    $ref = sprintf("%'012d",mt_rand(0, 999999999999));
                    $chk = $this->db->query("SELECT * FROM parcels where reference_number = '$ref'")->num_rows;
                    if($chk <= 0){
                        $i = 1;
                    }
                }
                $data .= ", reference_number='$ref' ";
                if($save[] = $this->db->query("INSERT INTO parcels set $data"))
                    $ids[]= $this->db->insert_id;
            }else{
                if($save[] = $this->db->query("UPDATE parcels set $data where id = $id"))
                    $ids[] = $id;
            }
        }
        if(isset($save) && isset($ids)){
            // return json_encode(array('ids'=>$ids,'status'=>1));
            return 1;
        }
    }
    function delete_parcel(){
        extract($_POST);
        $delete = $this->db->query("DELETE FROM parcels where id = $id");
        if($delete){
            return 1;
        }
    }
    function update_parcel(){
        extract($_POST);
        $update = $this->db->query("UPDATE parcels set status= $status where id = $id");
        $save = $this->db->query("INSERT INTO parcel_tracks set status= $status , parcel_id = $id");
        if($update && $save)
            return 1;  
    }
    function get_parcel_heistory(){
        extract($_POST);
        $data = array();
        $parcel = $this->db->query("SELECT * FROM parcels where reference_number = '$ref_no' or sender_contact='$ref_no'");
        if($parcel->num_rows <=0){
            return 2;
        }else{
            $parcel = $parcel->fetch_array();
            $data[] = array('status'=>'Item accepted by Courier','date_created'=>date("M d, Y h:i A",strtotime($parcel['date_created'])));
            $history = $this->db->query("SELECT * FROM parcel_tracks where parcel_id = {$parcel['id']}");
            $status_arr = array("Item Accepted by Courier","Collected","Shipped","In-Transit","Arrived At Destination","Out for Delivery","Ready to Pickup","Delivered","Picked-up","Unsuccessfull Delivery Attempt");
            while($row = $history->fetch_assoc()){
                $row['date_created'] = date("M d, Y h:i A",strtotime($row['date_created']));
                $row['status'] = $status_arr[$row['status']];
                $data[] = $row;
            }
            return json_encode($data);
        }
    }
     function get_parcel_history(){
        extract($_POST);
        $data = array();
        $parcel = $this->db->query("SELECT * FROM parcels where reference_number = '$mobile_no' or sender_contact='$mobile_no'");
        if($parcel->num_rows <=0){
            return 2;
        }else{
            $parcel = $parcel->fetch_array();
            $data[] = array('status'=>'Item accepted by Courier','date_created'=>date("M d, Y h:i A",strtotime($parcel['date_created'])));
            $history = $this->db->query("SELECT * FROM parcel_tracks where parcel_id = {$parcel['id']}");
            $status_arr = array("Item Accepted by Courier","Collected","Shipped","In-Transit","Arrived At Destination","Out for Delivery","Ready to Pickup","Delivered","Picked-up","Unsuccessfull Delivery Attempt");
            while($row = $history->fetch_assoc()){
                $row['date_created'] = date("M d, Y h:i A",strtotime($row['date_created']));
                $row['status'] = $status_arr[$row['status']];
                $data[] = $row;
            }
            return json_encode($data);
        }
    }
    function get_report(){
        extract($_POST);
        $data = array();
        $get = $this->db->query("SELECT * FROM parcels where date(date_created) BETWEEN '$date_from' and '$date_to' ".($status != 'all' ? " and status = $status " : "")." order by unix_timestamp(date_created) asc");
        $status_arr = array("Item Accepted by Courier","Collected","Shipped","In-Transit","Arrived At Destination","Out for Delivery","Ready to Pickup","Delivered","Picked-up","Unsuccessfull Delivery Attempt");
        while($row=$get->fetch_assoc()){
            $row['sender_name'] = ucwords($row['sender_name']);
            $row['recipient_name'] = ucwords($row['recipient_name']);
            $row['date_created'] = date("M d, Y",strtotime($row['date_created']));
            $row['status'] = $status_arr[$row['status']];
            $row['price'] = number_format($row['price'],2);
            $data[] = $row;
        }
        return json_encode($data);
    }
}

$crud = new Action();

if($action == 'save_parcel'){
    $save = $crud->save_parcel();
    if($save)
        echo $save;
}
if($action == 'delete_parcel'){
    $save = $crud->delete_parcel();
    if($save)
        echo $save;
}
if($action == 'update_parcel'){
    $save = $crud->update_parcel();
    if($save)
        echo $save;
}
if($action == 'get_parcel_heistory'){
    $get = $crud->get_parcel_heistory();
    if($get)
        echo $get;
}
if($action == 'get_parcel_history'){
    $get = $crud->get_parcel_history();
    if($get)
        echo $get;
}

if($action == 'get_report'){
    $get = $crud->get_report();
    if($get)
        echo $get;
}
ob_end_flush();
?>
