<?php
/**
 * Created by PhpStorm.
 * User: neosoft
 * Date: 22/11/14
 * Time: 10:53 AM
 */

class Agent extends CI_Controller {

    /**
     * Function index
     * default functionality for the user.
     * Controllers default method.
     *
     * @author	Himanshu Charegaonkar
     * @access	public
     */
    public function index()
    {
        $this->load->view('welcome_message');
    }

    /**
     * Function agent_listing
     * Function to generate the list of agent
     * @author	Himanshu Charegaonkar
     * @access	public
     */
    public function agent_listing()
    {
        # 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('agent');
        $arrData['middle']   = 'agent/agent_listings';
        $arrData['arrData']   = $arrAgents;
        $this->load->view('admin/templates/template', $arrData);
    }


    /**
     * Function magazine_list
     * Add new agent.
     * @author	Himanshu Charegaonkar
     * @access	public
     */
    public function add_agent()
    {
        # 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');

        # set form fields rules.
        $this->load->helper(array('form', 'url'));
        $this->form_validation->set_rules('first_name', 'First Name', 'trim|required|max_length[100]|alpha|xss_clean');
        $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required|max_length[100]|alpha|xss_clean');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[subscriber.email]|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['first_name'] = $_POST['first_name'];
            $arrAgent['last_name'] = $_POST['last_name'];
            $arrAgent['email'] = $_POST['email'];

            # Save to the database.
            $intNewPromotionId = $this->agents->fnSavePromotion('agent', $arrAgent);

            $this->session->set_flashdata('success', 'New agent has been added successfuly');
            redirect(base_url().'index.php/agent/agent_listing');
        } else {

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

    /**
     * Function magazine_list
     * Listing of added magazines.
     * @author	Himanshu Charegaonkar
     * @access	public
     */
    public function edit_agent()
    {
        # 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['agent_id']);
        if ($intAgent == 0) {
            $strRedirectPath = base_url().'index.php/agent/agent_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('first_name', 'First Name', 'trim|required|max_length[100]|alpha|xss_clean');
        $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required|max_length[100]|alpha|xss_clean');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|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['first_name'] = $_POST['first_name'];
            $arrAgent['last_name'] = $_POST['last_name'];
            $arrAgent['email'] = $_POST['email'];

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

            $this->session->set_flashdata('success', 'Agent information has been added successfully');
            redirect(base_url().'index.php/agent/agent_listing');
        } else {

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