<?php

class Vouchers extends CI_Controller {

   
    public function index()
    {
        # Check whether the session is set or not.
        if(! $this->session->userdata('username') ) {
            $strRedirectpath = base_url().'index.php/users/login';
            redirect($strRedirectpath);
        }

        #Load required models and libraries.
        $this->load->model('agents');
        #$this->load->library('pagination');
        #$this->load->library('Backend_pagination');
        //$arrAgents = $this->agents->fnGetAllAgents('currency');

        $arrData['vouchers'] = $this->db->get('vouchers')->result_array();


        $arrData['middle']   = 'vouchers/vouchers_listings';
        //$arrData['arrData']   = $arrAgents;
        $this->load->view('admin/templates/template', $arrData);
    }

    public function add_vouchers()
    {
        # Check whether the session is set or not.
        if(! $this->session->userdata('username') ) {
            $strRedirectpath = base_url().'index.php/users/login';
            redirect($strRedirectpath);
        }

        # Load the required model.
        $this->load->model('agents');

        $voucher_id = $this->input->post('voucher_id');
        $arrData['voucher_id'] = $voucher_id;

        $get_id = $this->input->get('voucher_id');

        if(!empty($get_id)) {
            $responce = $this->db->get_where('vouchers', array('id' => base64_decode($get_id)))->result_array();
            $arrData['voucher_name'] = $responce[0]['voucher_name'];
            $arrData['voucher_amount'] = $responce[0]['voucher_amount'];
            $arrData['valid_until'] = date('d-M-Y', strtotime($responce[0]['valid_until']));
        }else {
            $arrData['voucher_name'] = '';
            $arrData['voucher_amount'] = '';
            $arrData['valid_until'] = '';
        }

        # set form fields rules.
        $this->load->helper(array('form', 'url'));
        $this->form_validation->set_rules('voucher_name', 'Voucher Name', 'trim|required|max_length[100]|xss_clean');
        $this->form_validation->set_rules('voucher_amount', 'Voucher Amount', 'trim|required|max_length[100]|xss_clean|numeric');
        $this->form_validation->set_rules('valid_until', 'Valid Until', 'trim|required|max_length[100]|xss_clean');


        # Check whether the validation turns true.
        if ($this->form_validation->run() == TRUE)
        {
            #get the values of all fields.
            $cols = [];
            $cols['voucher_name'] = $this->input->post('voucher_name');
            $cols['voucher_amount'] = $this->input->post('voucher_amount');
            $cols['valid_until'] = date('Y-m-d', strtotime($this->input->post('valid_until')));

            # Save to the database.
            

            if(!empty($voucher_id)) {

                $this->db->where(array('id' => base64_decode($voucher_id)));
                $this->db->update('vouchers', $cols);

                $this->agents->fnUpdatePromotion('vouchers', $cols, $voucher_id);    
                $this->session->set_flashdata('success', 'successfully updated');
            }else {
                $intNewPromotionId = $this->agents->fnSavePromotion('vouchers', $cols);
                $this->session->set_flashdata('success', 'New voucher has been added successfully');
            }
            

            
            redirect(base_url().'index.php/vouchers');
        } else {

            /** if validation errors found then redirect  **/
            $strValidationErrors = validation_errors();
            if ($strValidationErrors != "" ) {
                $this->session->set_flashdata('error', $strValidationErrors);
                $strRedirectpath = base_url().'index.php/vouchers/';
                redirect($strRedirectpath);
            } else {
                $arrData['middle']   = 'vouchers/add_vouchers';
                $strValidationErrors = validation_errors();
                $this->session->set_flashdata('error', $strValidationErrors);
                echo $this->load->view('vouchers/add_vouchers', $arrData, true);exit;
            }
        }
    }

    /**
     * Function magazine_list
     * Listting of added magezines.
     * @author	Himanshu Charegaonkar
     * @access	public
     */
    public function edit_currency()
    {
        # Check whether the session is set or not.
        if(! $this->session->userdata('username') ) {
            $strRedirectPath = base_url().'index.php/users/login';
            redirect($strRedirectPath);
        }

        # now check whether we receive the valid agent id.
        $intAgent = (int) base64_decode($_REQUEST['lang_id']);
        if ($intAgent == 0) {
            $strRedirectPath = base_url().'index.php/currency/currency_listing';
            redirect($strRedirectPath);
        }

        # Load the required model.
        $this->load->model('agents');

        # set form fields rules.
        $this->load->helper(array('form', 'url'));
        $this->form_validation->set_rules('name', 'Name', 'trim|required|max_length[100]|alpha|xss_clean');

        # Check whether the validation turns true.
        if ($this->form_validation->run() == TRUE)
        {
            $arrPostedData = $_POST;

            #get the values of all fields.
            $arrAgent = array();
            $arrAgent['name'] = $_POST['name'];

            # Update to the database.
            $this->agents->fnUpdatePromotion('currency', $arrAgent, $intAgent);

            $this->session->set_flashdata('success', 'Currency information has been updated successfully');
            redirect(base_url().'index.php/currency/currency_listing');
        } else {

            /** if validation errors found then redirect  **/
            $strValidationErrors = validation_errors();
            if ($strValidationErrors != "" ) {
                $this->session->set_flashdata('error', $strValidationErrors);
                $strRedirectPath = base_url().'index.php/currency/currency_listing/';
                redirect($strRedirectPath);
            } else {
                $arrDetailedInformation = $this->agents->fnGetAllAgents('currency', $intAgent);
                $arrData['middle']   = 'currency/edit_currency';
                $arrData['arrData']   = $arrDetailedInformation[0];
                $arrData['strAgentId']   = $_REQUEST['lang_id'];
                $strValidationErrors = validation_errors();
                $this->session->set_flashdata('error', $strValidationErrors);
                echo $this->load->view('currency/edit_currency', $arrData, true);exit;
            }
        }
    }

    #check voucher
    public function checkVoucher()
    {
        $voucher_name = $_GET['voucher_name'];
        $responce = $this->db->get_where('vouchers', array('voucher_name' => $voucher_name))->result_array();
        echo count($responce);
    }
} 