<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Order_controller extends CI_Controller {

    public function __construct() {
        parent::__construct();
        // Your own constructor code
        $this->load->model('order_model');
        $this->load->model('product_model');
        
        if($this->session->userdata('role')==''){
            redirect('logout');
        }
    }
    
    function abandonedcarts(){
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            //storemanager,hr,reception...user serial
            checkroles(1,0,1);

            $data['nav'] = 'invoice';
            $data['subnav'] = 'abandonedcarts';

            $status= $this->input->post('order_status', TRUE);
            $data['status'] = $status;

            $daterange = $this->input->post('date_range', TRUE);
            $data['date_range'] = $daterange;
            $data['start_date'] = '';
            $data['end_date'] = '';
            if(null != $daterange || $daterange != ''){
                // 02/25/2025 - 03/12/2025
                $mydate = explode(' - ', $daterange);
                $sdate = $mydate[0];
                $edate = $mydate[1];

                $sdate = explode('/', $sdate);
                $data['start_date'] = $sdate[2].'-'.$sdate[0].'-'.$sdate[1];

                $edate = explode('/', $edate);
                $data['end_date'] = $edate[2].'-'.$edate[0].'-'.$edate[1];
            }


            $data['orders'] = $this->order_model->get_open_orders($status, $data['start_date'], $data['end_date']);

            // echo "<pre>";
            // print_r($data['orders']); exit;

            $this->load->view('includes/header', $data);
            $this->load->view('open_order_list');
            $this->load->view('includes/footer');
        }
    }

    public function get_orders() {
        //get the posted values
        
        $data = $this->order_model->get_orders();
        echo(json_encode($data));
        
    }
        
    public function getOrderbyid($qtype=0){
        $orderid=$this->input->post('id_customer_order', TRUE);
        if($qtype==0){
            $data = $this->order_model->getopenorderbyid($orderid);
        } else {
            $data = $this->order_model->getorderbyid($orderid);
        }
        echo(json_encode($data));
    }
    
     public function getOrderbyCid(){
       $customer_id = $this->input->post('customerid', TRUE);
       $data = $this->order_model->getorderbycid($customer_id);
       echo(json_encode($data));
    }
    
    public function addorders(){
        
        $result=$this->order_model->add_order();
        echo "success|".$result;
    }
    
    public function updateorder(){
        
        $order_id = $this->input->post('orderid', TRUE);
        $order = $this->order_model->check_order_status($order_id);
        if($order->order_status === 'invoiced'){
            echo "invoiced"; exit;
        }
        
        $result=$this->order_model->update_order();
        echo $result;
    }
    
    public function getlast4orders($customerid){
        
        $data=$this->order_model->getlast4orders($customerid);
        echo(json_encode($data));
    }


    public function product_orders(){
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            //storemanager,hr,reception...user serial
            checkroles(1,0,1);

            $data['nav'] = 'invoice';
            $data['subnav'] = 'product_orders';

            $status= $this->input->post('order_status', TRUE);
            $data['status'] = $status;

            $daterange = $this->input->post('date_range', TRUE);
            $data['date_range'] = $daterange;
            $data['start_date'] = '';
            $data['end_date'] = '';
            if(null != $daterange || $daterange != ''){
                // 02/25/2025 - 03/12/2025
                $mydate = explode(' - ', $daterange);
                $sdate = $mydate[0];
                $edate = $mydate[1];

                $sdate = explode('/', $sdate);
                $data['start_date'] = $sdate[2].'-'.$sdate[0].'-'.$sdate[1];

                $edate = explode('/', $edate);
                $data['end_date'] = $edate[2].'-'.$edate[0].'-'.$edate[1];
            } else {

                $data['start_date'] = date('Y-m-d', strtotime('-1 month'));
                $data['end_date'] = date('Y-m-d');
            }


            $data['orders'] = $this->order_model->get_product_orders($status, $data['start_date'], $data['end_date']);

            //  echo "<pre>";
            //  print_r($data['orders']); exit;

            $this->load->view('includes/header', $data);
            $this->load->view('product_order_list');
            $this->load->view('includes/footer');
        }
    }

    
}