<?php

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

class Packages_controller extends CI_Controller {

    public function __construct() {

        parent::__construct();

        // Your own constructor code

        $this->load->model('service_model');
        $this->load->model('product_model');
        $this->load->model('customer_model');
        $this->load->model('packages_model');

        if ($this->session->userdata('role') == '') {

            redirect('logout');
        }
    }

    public function list_package_types() {
        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, 0);

            $data['nav'] = 'my_business';
            $data['subnav'] = 'package_types';
            $data['packages_type'] = $this->packages_model->get_package_types();
            $this->load->model('business_model');
            $data['businesses'] = $this->business_model->get_all_businesses();

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

    public function add_package_type() {
        $result = $this->packages_model->add_package_type();
        echo('success|' . $result);
    }

    public function update_packages_type() {
        $result = $this->packages_model->update_packages_type();
        echo('success|' . $result);
    }

    public function package_type_image() {
        if ($this->input->post('id_package_type_image', TRUE) && $this->input->post('id_package_type_image', TRUE) != "") {
            $image = "";
            if ($_FILES['package_type_image']['name'] && $_FILES['package_type_image']['name'] != "") {
                $image = $this->image_upload('images/servicetype', 'package_type_image', 'gif|jpg|png');
                if ($image == FALSE) {
                    $this->session->set_flashdata('errorimage', 'The image dimension must be 128 X 128');
                    redirect(base_url('package/types'));
                } else {
                    $image = $image;
                }
            } else {
                $image = ($this->input->post('org_image', TRUE) != "" ? $this->input->post('org_image', TRUE) : NULL);
            }

            $result = $this->packages_model->package_type_image($image);
            if ($result) {
                redirect(base_url('package/types'));
            }
        }
    }

    public function list_package_category($package_type_id) {
        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, 0);

            $data['nav'] = 'my_business';
            $data['subnav'] = 'package_types';
            $data['packages_category'] = $this->packages_model->get_package_category($package_type_id);

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

    public function add_package_category() {
        $result = $this->packages_model->add_package_category();
        echo('success|' . $result);
    }

    public function update_package_category() {
        $result = $this->packages_model->update_package_category();
        echo('success|' . $result);
    }

    public function package_category_image() {
        if ($this->input->post('id_package_cat_image', TRUE) && $this->input->post('id_package_cat_image', TRUE) != "") {
            $image = "";
            if ($_FILES['package_cat_image']['name'] && $_FILES['package_cat_image']['name'] != "") {
                $image = $this->image_upload('images/category', 'package_cat_image', 'gif|jpg|png');
                if ($image == FALSE) {
                    $this->session->set_flashdata('errorimage', 'The image dimension must be 128 X 128');
                    redirect(base_url('package/category' . '/' . $this->input->post('id_package_type_image')));
                } else {
                    $image = $image;
                }
            } else {
                $image = ($this->input->post('org_image', TRUE) != "" ? $this->input->post('org_image', TRUE) : NULL);
            }

            $result = $this->packages_model->package_category_image($image);
            if ($result) {
                redirect(base_url('package/category' . '/' . $this->input->post('id_package_type_image')));
            }
        }
    }

    public function list_package_service($package_type_id) {
        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, 0);

            $data['nav'] = 'my_business';
            $data['subnav'] = 'package_types';
            $data['packages_service'] = $this->packages_model->get_package_service($package_type_id);
            
            $data['packages_service_options']=array();
           
            foreach($data['packages_service'] as $packageservice){
               
                //$options = $this->packages_model->get_package_service_options($packageservice['id_package_services']);
                if(isset($packageservice['id_package_services'])){
                    array_push($data['packages_service_options'], $this->packages_model->get_package_service_options($packageservice['id_package_services'])); 
                }
            }
//            echo '<pre>';
//            print_r($data['packages_service_options']);
//            exit();
            
            $data['business_service'] = $this->packages_model->get_business_service($data['packages_service'][0]['business_id']);
            $data['package_category'] = $this->packages_model->get_package_list_category();
            

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

    public function add_package_service() {
        $result = $this->packages_model->add_package_service();
        echo('success|' . $result);
    }

    public function update_package_services() {
        $result = $this->packages_model->update_package_services();
        echo('success|' . $result);
    }

    public function delete_package_service() {
        $result = $this->packages_model->delete_package_service();
        echo('success|' . $result);
    }

    function image_upload($folder = NULL, $image = NULL, $type = NULL, $width = NULL, $height = NULL, $encrypt = NULL) {
        $config['upload_path'] = 'assets/' . $folder . '/';
        $config['allowed_types'] = $type;
        $config['max_size'] = '2048';

        if ($width != NULL && $height != NULL) {
            $config['max_width'] = $width;
            $config['max_height'] = $height;
        }
        if ($encrypt != NULL) {
            $config['encrypt_name'] = TRUE;
        }

        $this->load->library('upload', $config);
        $this->upload->initialize($config);

        if (!$this->upload->do_upload($image)) {
            return FALSE;
        } else {
            return $this->upload->data('file_name');
        }
    }

    public function order_function() {
        $id = $this->input->post('id');
        $orderid = $this->input->post('orderid');
        $type = $this->input->post('type');

        $result = $this->packages_model->update_order_function($id, $orderid, $type);
        echo('success|' . $result);
        die;
    }
    
    public function edit_package_type(){
        $data = $this->packages_model->edit_package_type();
        echo (json_encode($data));
        die;
    }
    
    public function edit_package_category(){
        $data = $this->packages_model->edit_package_category();
        echo (json_encode($data));
        die;
    }
    
    public function edit_package_service(){
        $data = $this->packages_model->edit_package_service();
        echo (json_encode($data));
        die;
    }

    public function duplicate_package_category(){
        
        $data = $this->packages_model->duplicate_package_service();
        echo ('success|'.$data);
       
    }
    
    public function duplicate_package_type(){
        
        $data = $this->packages_model->duplicate_package_cat_services();
        echo ('success|'.$data);
       
    }
    
    public function get_a_package_service(){
        $idpackageservices=$this->input->post('id_package_services');
        $data = $this->packages_model->get_a_package_service($idpackageservices);
        echo (json_encode($data));
       
    }
    
    public function get_package_service_options($serviceid){
        
        $data = $this->packages_model->get_package_service_options();
        echo ($data);
       
    }
    
    public function get_package_cat_options(){
        
        $ptype = $this->input->post('package_type_id');
        
        $data['category'] = $this->service_model->get_package_category($ptype);
        $data['services'] =array();
        foreach($data['category'] as $category){
            array_push($data['services'], $this->packages_model->get_package_services($category->id_package_category));
        }
        //print_r($data['services']);
        $data['options'] =array();
        foreach($data['services'] as $services){
            foreach($services as $service){
                array_push($data['options'], $this->packages_model->get_package_services(0,$service['id_package_services']));
            }
        }
        echo (json_encode($data));
        die;
        
    }
    
    public function add_package_service_options(){
        
        $category=$this->input->post('category');
        $idpackageservices=$this->input->post('id_package_services');
        $idoptionalservice=$this->input->post('optionsservicesselect');
        $servicename = $this->input->post('optionsservice');
        $dealrate = $this->input->post('optionsdealprice');
        $optionserviceqty = $this->input->post('optionserviceqty');
        
        $data = array(
           'package_service_id' => $idpackageservices,
           'package_service_name' =>  $servicename,
           'package_service_rate' => $dealrate,
           'service_id' => $idoptionalservice,
           'package_service_qty' => $optionserviceqty
        );
        
        
        $this->db->select('ifnull(max(package_option_number),0) as optional', false);
        $this->db->where('option_for', $idpackageservices);
        $query = $this->db->get('package_services');
        $row = $query->row();
        
        $option = $row->optional + 1;
        
        $data1 = array(
           'service_id' => $idoptionalservice,
           'package_category_id' =>  $category,
           'package_services_active' => 'Yes',
           'service_rate' => $dealrate,
           'package_service_optional' => 'Yes',
           'package_option_number' => $option,
           'option_for' => $idpackageservices,
           'package_service_qty' => $optionserviceqty
                
        );
        
        $result = $this->packages_model->add_package_service_options($idpackageservices, $data, $data1);
        
        redirect(base_url('package/service/'.$category));
       
    }
    
    public function update_package_service_options($serviceid){
        
        $data = $this->packages_model->update_package_service_options();
        echo ('success|'.$data);
       
    }
    
    public function delete_package_service_options($optionid){
        
        $data = $this->packages_model->delete_package_service_options();
        
        
        
        echo ('success|'.$data);
       
    }
    

    public function copy_to_branches(){

        $branch_id = $this->input->post('business_id');

        $package_id = $this->input->post('package_id');
        ///select details of the package from headoffice
        $sql = "select * from package_type where id_package_type = ".$package_id;
       
        $query = $this->db->query($sql);
        $packagedata = $query->result_array();
        //print_r($packagedata); exit();
        if(null == $branch_id){
            //get all branches
            $sqlbranches = "select * from business where business.common_products = 'Yes' and business.ho='No';";
            $query1 = $this->db->query($sqlbranches);

            $branches = $query1->result();

            foreach($branches as $branch){
                //insert the packagetype  
                $data = array(
                    "service_type" => $packagedata[0]['service_type'],
                    "business_id" => $branch[0]['id_business'],
                    "order_id" => $packagedata[0]['order_id'],
                    "service_type_image" => $packagedata[0]['service_type_image'],
                    "service_type_active" => $packagedata[0]['service_type_active'],
                    "expiry_date" => $packagedata[0]['expiry_date']
                );
                $this->db->insert('package_type', $data);
                $package_type_id = $this->db->insert_id();

                foreach($packagedata as $package){
                    //get package categories
                    $sql = "select * from package_category where package_type_id = ".$package['id_package_type'];
                    $query = $this->db->query($sql);
                    $categoriesdata = $query->result_array();

                    foreach($categoriesdata as $category){
                        //insert package category
                        $data = array(
                            "package_type_id" => $package_type_id,
                            "business_id" => $branch['id_business'],
                            "order_id" => $category['order_id'],
                            "service_category" => $category['service_category'],
                            "service_category_active" => $category['service_category_active'],
                            "service_category_image" => $category['service_category_image']
                        );
                        $this->db->insert('package_category', $data);
                        $package_category_id = $this->db->insert_id();

                        //get package category services
                        $sql = "select * from package_services where package_category_id = ".$category['id_package_category'];
                        $query = $this->db->query($sql);
                        $servicesdata = $query->result_array();

                        foreach($servicesdata as $service){
                            $sql = "select * from business_services where id_business_services = ".$service['service_id'];
                            $q=$this->db->query($sql);
                            $service_selected = $q->row();
                            if($service_selected->ref_number!=null && $service_selected->ref_number!=""){
                                $sql = "select id_business_services, service_name from business_services where business_id = ".$branch_id." and business_services.ref_number=".$service_selected->ref_number.";";
                                $q = $this->db->query($sql);
                                $serviceid = $q->row();
    
                                //insert into package service
                                $data = array(
                                    "service_id" => $serviceid->id_business_services,
                                    "package_category_id" => $package_category_id,
                                    "package_services_active" => $service['package_services_active'],
                                    "service_rate" => $service['service_rate'],
                                    "package_service_optional" => $service['package_service_optional'],
                                    "package_option_number" => $service['package_option_number'],
                                    "option_for" => $service['option_for'],
                                    "package_service_qty" => $service['package_service_qty']                            
                                );
                                $this->db->insert('package_services', $data);
                                $package_service_id = $this->db->insert_id();
    
                                //get service options
                                $sql = "select * from package_service_options where package_service_id = ".$service['id_package_services'];
                                $query = $this->db->query($sql);
                                $serviceoptiondata = $query->result_array();
    
                                foreach($serviceoptiondata as $serviceoption){
    
                                    $data =array(
                                        "package_service_id" => $package_service_id,
                                        "service_id" => $serviceid->id_business_services,
                                        "package_service_name" => $serviceid->service_name,
                                        "package_service_rate" => $serviceoption['package_service_rate'],
                                        "package_service_qty" => $serviceoption['package_service_qty']
                                    );
                                    $this->db->insert('package_service_options', $data);
                                }
    
                            }
                        }

                    }

                }

            }
        } else {
            //insert the packagetype  
            $new_date = new DateTime('now');
            $date = $new_date->modify('+1 years');
            $expiry_date = $date->format('Y-m-d');

            if($packagedata[0]['expiry_date'] != null && $packagedata[0]['expiry_date'] != ""){$expiry_date=$packagedata[0]['expiry_date'];}   

            $data = array(
                "service_type" => $packagedata[0]['service_type'],
                "business_id" => $branch_id,
                "order_id" => $packagedata[0]['order_id'],
                "service_type_image" => $packagedata[0]['service_type_image'],
                "service_type_active" => $packagedata[0]['service_type_active'],
                "expiry_date" => $expiry_date
            );
            $this->db->insert('package_type', $data);
            $package_type_id = $this->db->insert_id();

            foreach($packagedata as $package){
                //get package categories
                $sql = "select * from package_category where package_type_id = ".$package['id_package_type'];
                $query = $this->db->query($sql);
                $categoriesdata = $query->result_array();

                foreach($categoriesdata as $category){
                    //insert package category
                    $data = array(
                        "package_type_id" => $package_type_id,
                        "business_id" => $branch_id,
                        "order_id" => $category['order_id'],
                        "service_category" => $category['service_category'],
                        "service_category_active" => $category['service_category_active'],
                        "service_category_image" => $category['service_category_image']
                    );
                    $this->db->insert('package_category', $data);
                    $package_category_id = $this->db->insert_id();

                    //get package category services
                    $sql = "select * from package_services where package_category_id = ".$category['id_package_category'];
                    $query = $this->db->query($sql);
                    $servicesdata = $query->result_array();

                    foreach($servicesdata as $service){
                        $sql = "select * from business_services where id_business_services = ".$service['service_id'];
                        $q=$this->db->query($sql);
                        $service_selected = $q->row();
                        if($service_selected->ref_number!=null && $service_selected->ref_number!=""){
                            $sql = "select id_business_services, service_name from business_services where business_id = ".$branch_id." and business_services.ref_number=".$service_selected->ref_number.";";
                            $q = $this->db->query($sql);
                            $serviceid = $q->row();

                            //insert into package service
                            $data = array(
                                "service_id" => $serviceid->id_business_services,
                                "package_category_id" => $package_category_id,
                                "package_services_active" => $service['package_services_active'],
                                "service_rate" => $service['service_rate'],
                                "package_service_optional" => $service['package_service_optional'],
                                "package_option_number" => $service['package_option_number'],
                                "option_for" => $service['option_for'],
                                "package_service_qty" => $service['package_service_qty']                            
                            );
                            $this->db->insert('package_services', $data);
                            $package_service_id = $this->db->insert_id();

                            //get service options
                            $sql = "select * from package_service_options where package_service_id = ".$service['id_package_services'];
                            $query = $this->db->query($sql);
                            $serviceoptiondata = $query->result_array();

                            foreach($serviceoptiondata as $serviceoption){

                                $data =array(
                                    "package_service_id" => $package_service_id,
                                    "service_id" => $serviceid->id_business_services,
                                    "package_service_name" => $serviceid->service_name,
                                    "package_service_rate" => $serviceoption['package_service_rate'],
                                    "package_service_qty" => $serviceoption['package_service_qty']
                                );
                                $this->db->insert('package_service_options', $data);
                            }

                        }
                    }
                }

            }
        }
    }
}


