<?php

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

class Coupon_controller extends CI_Controller {

    public function __construct() {
        parent::__construct();
        // Your own constructor code
        
        $this->load->model('coupon_model');
        $this->load->model('service_model');
        $this->load->model('business_model');
        if ($this->session->userdata('role') == '') {
            redirect('logout');
        }
    }

    
    public function verify(){
        $coupon = $this->input->post('coupon');
        
        $data['coupondetails'] = $this->coupon_model->verify($coupon);
        $data['couponservices']=[];
        if($data['coupondetails']!==null){
			if(isset($data['coupondetails']->coupon_type)){
				if($data['coupondetails']->coupon_type == 'service'){
					
					$services = $this->coupon_model->coupon_services($data['coupondetails']->coupon_category_id);
					
					foreach($services as $service){
						array_push($data['couponservices'], $service['service_id']);
					}
				}
			}
		}
        echo(json_encode($data));
    }
    
    public function coupon_category(){
    
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            //storemanager,hr,reception...user serial
            checkroles(0, 0, 0);

            $data['nav'] = 'my_business';
            $data['subnav'] = 'discount';

            $data['business'] = $this->business_model->get_business_details();
            
            $data['coupon_category'] = $this->coupon_model->get_coupon_categories();

           

            $this->load->view('includes/header', $data);
            $this->load->view('coupons/coupon_category');
            $this->load->view('includes/footer');
        }
        
    }
    
    public function add_category(){
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            //storemanager,hr,reception...user serial
            checkroles(0, 0, 0);

            $data['nav'] = 'my_business';
            $data['subnav'] = 'discount';

            $data['business'] = $this->business_model->get_business_details();
            
            $this->load->view('includes/header', $data);
            $this->load->view('coupons/coupon_category_add');
            $this->load->view('includes/footer');
        }
    }
    
    public function edit_category($categoryid=null){
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            //storemanager,hr,reception...user serial
            checkroles(0, 0, 0);

            $data['nav'] = 'my_business';
            $data['subnav'] = 'discount';

            if(null==$categoryid){
                $categoryid=$this->input->post('category_id');
            }
            
            $data['business'] = $this->business_model->get_business_details();
            $data['coupon_category'] = $this->coupon_model->get_coupon_category($categoryid);
            if($data['coupon_category']->coupon_type == 'service'){
                $data['services'] = $this->service_model->get_service_list();
            }
            
            $data['category_services'] = $this->coupon_model->coupon_services($categoryid);
            
             $data['coupon_listing'] = $this->coupon_model->get_coupon_listing($categoryid);
            
            $this->load->view('includes/header', $data);
            $this->load->view('coupons/coupon_category_edit');
            $this->load->view('includes/footer');
        }
    }

    public function update_services(){

        $this->coupon_model->remove_coupon_services($this->input->post('category_id'));
        
        $services = $this->input->post('services');
//        /var_dump($services); exit();
        $count=0;
        foreach($services as $service){
            $data = array(
                'coupon_category_id' => $this->input->post('category_id'),
                'service_id' => $service
            );

            $result = $this->coupon_model->insert_coupon_services($data);
            $count++;
        }
        
        $this->session->set_flashdata('Success', $count.' Services Added!');
        
        redirect('edit_category/'. $this->input->post('category_id'));
        
    }

    public function create_coupon_category(){
    
        $data = array(
            'coupon_category' => $this->input->post('coupon_category'),
            'coupon_category_desc' => $this->input->post('coupon_category_desc'),
            'business_id' => $this->session->userdata('businessid'),
            'coupon_type' => $this->input->post('coupon_type'),
            'discount_perc' => $this->input->post('discount_perc'),
            'coupon_category_prefix' => $this->input->post('coupon_category_prefix'),
            'created_by' => $this->session->userdata('username'),
            'created_on' => date('Y-m-d H:i:s')
        );
        
        $result = $this->coupon_model->insert_category($data);
        
        redirect('coupon_category');
        
    }
    
    
    public function generate_sequence(){
        
        $togenerate = $this->input->post('seq');
        $starting = $this->input->post('starting');
        $category_id=$this->input->post('cateogry_id');
        $category=$this->input->post('cateogry');
        $category_prefix = $this->input->post('category_prefix');
        $validity = $this->input->post('validity');
        
        $count=0;
        for($x=0; $x< $togenerate; $x++){
            
            $randstring =  $category_prefix .'-'. $x.str_pad(rand(1,90000),5,'0', STR_PAD_LEFT);
            $goahead=false;
            while($goahead==false){
                $alreadyused = $this->coupon_model->verify($randstring);
                if(null==$alreadyused){
                    $goahead=true;
                } else {
                    $randstring =  $category_prefix .'-'. $x.str_pad(rand(1,90000),5,'0', STR_PAD_LEFT);                    
                }
            }
            
            $data=array(
                'coupon_category_id' => $category_id,
                'coupon_number' => $randstring,
                'coupon_text' => $category,
                'coupon_validity' => $validity,
                'coupon_status' => 'open',
                'created_by' => $this->session->userdata('username'),
                'created_on' => date('Y-m-d H:i:s')
            );
            $result = $this->coupon_model->generate_sequence($data);
            $count++;
        }
        
        $this->session->set_flashdata('Success', $count.' coupons created!');
        
        redirect('edit_category/'. $category_id);
    }

}
