    <?php

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

class Market_listings extends CI_Controller {
    

    public function __construct() {
        parent::__construct();
      
        if(!$this->session->userdata('logged_in')) {
            $this->session->set_flashdata('noaccess', 'Sorry, you are not logged in' );
            redirect('auth/login');
        }


    }

    public $data = array('subview' => 'Opps, forgot to set a subview');

    
        /**
	 * Display members marketplace engine list
	 * @$sort_by
	 * @$sort_order
	 */

    public function e_listings($sort_by = 'manufacturer', $sort_order = 'asc') {

        $this->data['efields'] = array(
            'manufacturer' => 'Manufacturer:',
            'engine_model' => 'Model:',
            'esn' => 'ESN:',
            'lister' => 'Lister:',
            'transaction' => 'Transaction:',
            'location' => 'Location:',
            'asking_price' => 'Asking Price:',
            'monthly_rent' => 'Monthly Rent:',
            'available_date' => 'Available Date:'
        );
        
        if ($this->ion_auth->logged_in() && $this->ion_auth->is_admin())
		{
        $per_pg = 15;
        $total = $this->market_listings_model->get_totalcount_admin();


        $config['base_url'] = base_url() . "epc_listings/market_listings/e_listings/$sort_by/$sort_order";
        $config['total_rows'] = $total;
        $config['per_page'] = $per_pg;
        $config['uri_segment'] = 6;
        $choice = $config['total_rows'] / $config['per_page'];
        $config['num_links'] = round($choice);


        $this->pagination->initialize($config);
        $page = ($this->uri->segment(6)) ? $this->uri->segment(6) : 0;
            
            $this->data['elistings'] = $this->market_listings_model->get_elistings_admin($config['per_page'], $page, $sort_by, $sort_order);
            $this->data['elinks'] = $this->pagination->create_links();

            if ($this->data['elinks'] != '') {
                $this->data['start'] = (($this->pagination->cur_page - 1) * $this->pagination->per_page) + 1;
                $this->data['end'] = ($this->pagination->cur_page * $this->pagination->per_page);

                if ($this->data['end'] > $this->pagination->total_rows) {
                    $this->data['end'] = $this->pagination->total_rows;
                }
            }

            $this->data['total'] = $this->market_listings_model->get_totalcount_admin();

            $this->data['subview'] = 'market_listings/e_listings';
            $this->data['sort_by'] = $sort_by;
            $this->data['sort_order'] = $sort_order;
            
            
	             //load the view passing $data values
                     $this->load->view("layouts/main_admin", $this->data);
		}
                else if ($this->ion_auth->logged_in() && !$this->ion_auth->is_admin())
		{
            
            $per_pg = 15;
        $total = $this->market_listings_model->get_totalcount();


        $config['base_url'] = base_url() . "epc_listings/market_listings/e_listings/$sort_by/$sort_order";
        $config['total_rows'] = $total;
        $config['per_page'] = $per_pg;
        $config['uri_segment'] = 6;
        $choice = $config['total_rows'] / $config['per_page'];
        $config['num_links'] = round($choice);


        $this->pagination->initialize($config);
        $page = ($this->uri->segment(6)) ? $this->uri->segment(6) : 0;
        
            $this->data['elistings'] = $this->market_listings_model->get_elistings($config['per_page'], $page, $sort_by, $sort_order);
            $this->data['elinks'] = $this->pagination->create_links();

            if ($this->data['elinks'] != '') {
                $this->data['start'] = (($this->pagination->cur_page - 1) * $this->pagination->per_page) + 1;
                $this->data['end'] = ($this->pagination->cur_page * $this->pagination->per_page);

                if ($this->data['end'] > $this->pagination->total_rows) {
                    $this->data['end'] = $this->pagination->total_rows;
                }
            }

            $this->data['ptotal'] = $this->market_listings_model->get_ptotalcount();
            $this->data['total'] = $this->market_listings_model->get_totalcount();

            $this->data['subview'] = 'market_listings/e_listings';
            $this->data['sort_by'] = $sort_by;
            $this->data['sort_order'] = $sort_order;
            //load the view passing $data values
            $this->load->view("layouts/main_logged_in", $this->data);
		}
    }
    
    
       /**
	* Display market place engine listing to user
	* @$listing_id
        */

    public function display_elisting($listing_id) {

        $this->data['this_elisting'] = $this->market_listings_model->get_elisting_data($listing_id);

        $this->data['subview'] = 'market_listings/display_elisting';
        
        if ($this->ion_auth->logged_in() && $this->ion_auth->is_admin())
		{
                     $this->data['total'] = $this->market_listings_model->get_totalcount_admin();
	             //load the view passing $data values
                     $this->load->view("layouts/main_admin", $this->data);
		}
                else if ($this->ion_auth->logged_in() && !$this->ion_auth->is_admin())
		{
                     $this->data['total'] = $this->market_listings_model->get_totalcount();
		     //load the view passing $data values
                     $this->load->view("layouts/main_logged_in", $this->data);
		}
        
//        $this->load->view("layouts/main", $this->data);
    }
    
    
       /**
	* Display market place engine listing search results to user
	* @$listing_id
        */

    public function display_selisting($listing_id) {

        $this->data['this_selisting'] = $this->market_listings_model->get_selisting_data($listing_id);

        $this->data['subview'] = 'market_listings/display_selisting';
        
        if ($this->ion_auth->logged_in() && $this->ion_auth->is_admin())
		{
                     $this->data['ftotal'] = $this->session->userdata('num_rows');
	             //load the view passing $data values
                     $this->load->view("layouts/main_admin", $this->data);
		}
                else if ($this->ion_auth->logged_in() && !$this->ion_auth->is_admin())
		{
                     $this->data['ftotal'] = $this->session->userdata('num_rows');
		     //load the view passing $data values
                     $this->load->view("layouts/main_logged_in", $this->data);
		}
        
//        $this->load->view("layouts/main", $this->data);
    }
    

        /**
	 * Display members marketplace engine parts list
	 * @$sort_by
	 * @$sort_order
	 */

    public function p_listings($sort_by = 'part_number', $sort_order = 'asc') {

        $this->data['pfields'] = array(
            'part_number' => 'Part Number:',
            'part_description' => 'Part Description:',
            'part_condition' => 'Part Condition:',
            'enginep_model' => 'Engine Model:',
            'tag' => 'Tag:',
            'quantity' => 'Quantity:',
            'lister' => 'Lister:',
            'transaction' => 'Transaction:',
            'location' => 'Location:',
            'asking_price' => 'Asking Price:',
            'monthly_rent' => 'Monthly Rent:',
            'available_date' => 'Available Date:'
        );
        
        if ($this->ion_auth->logged_in() && $this->ion_auth->is_admin())
		{
            
        $part_c = $this->session->userdata('column');
        $crit = $this->session->userdata('criteria');


        $this->data['ptotal'] = $this->market_listings_model->get_ptotalcount_admin();



        $num_results = $this->market_listings_model->search_plistings_count_admin($part_c, $crit);
        $this->data['p_results_total'] = $num_results['num_rows'];
       
        $this->session->set_userdata($num_results);
        $limit = 8;


        $config = array();
        $config['base_url'] = base_url() . "epc_listings/market_listings/p_listings/" . $part_c . "/" . $crit . "/$sort_by/$sort_order";
        $config['total_rows'] = $num_results['num_rows'];
        $config['per_page'] = $limit;
        $config['uri_segment'] = 8;
        $choice = $config['total_rows'] / $config['per_page'];
        $config['num_links'] = round($choice);

        $this->pagination->initialize($config);
        $ppage = ($this->uri->segment(8)) ? $this->uri->segment(8) : 0;


        $results = $this->market_listings_model->get_plistings_admin($part_c, $crit, $config['per_page'], $ppage, $sort_by, $sort_order);
        $this->data['plistings'] = $results['rows'];

        $this->data['plinks'] = $this->pagination->create_links();


        if ($this->data['plinks'] != '') {
            $this->data['pstart'] = (($this->pagination->cur_page - 1) * $this->pagination->per_page) + 1;
            $this->data['pend'] = ($this->pagination->cur_page * $this->pagination->per_page);

            if ($this->data['pend'] > $this->pagination->total_rows) {
                $this->data['pend'] = $this->pagination->total_rows;
            }
        }

        $this->data['subview'] = 'market_listings/p_listings';
        $this->data['sort_by'] = $sort_by;
        $this->data['sort_order'] = $sort_order;
	             //load the view passing $data values
                     $this->load->view("layouts/main_admin", $this->data);
		}
                else if ($this->ion_auth->logged_in() && !$this->ion_auth->is_admin())
		{
                    $part_c = $this->session->userdata('column');
        $crit = $this->session->userdata('criteria');


        $this->data['ptotal'] = $this->market_listings_model->get_ptotalcount();
        $this->data['total'] = $this->market_listings_model->get_totalcount();



        $num_results = $this->market_listings_model->search_plistings_count($part_c, $crit);
        $this->data['p_results_total'] = $num_results['num_rows'];
        $this->session->set_userdata($num_results);
        $limit = 8;


        $config = array();
        $config['base_url'] = base_url() . "epc_listings/market_listings/p_listings/" . $part_c . "/" . $crit . "/$sort_by/$sort_order";
        $config['total_rows'] = $num_results['num_rows'];
        $config['per_page'] = $limit;
        $config['uri_segment'] = 8;
        $choice = $config['total_rows'] / $config['per_page'];
        $config['num_links'] = round($choice);

        $this->pagination->initialize($config);
        $ppage = ($this->uri->segment(8)) ? $this->uri->segment(8) : 0;


        $results = $this->market_listings_model->get_plistings($part_c, $crit, $config['per_page'], $ppage, $sort_by, $sort_order);
        $this->data['plistings'] = $results['rows'];

        $this->data['plinks'] = $this->pagination->create_links();


        if ($this->data['plinks'] != '') {
            $this->data['pstart'] = (($this->pagination->cur_page - 1) * $this->pagination->per_page) + 1;
            $this->data['pend'] = ($this->pagination->cur_page * $this->pagination->per_page);

            if ($this->data['pend'] > $this->pagination->total_rows) {
                $this->data['pend'] = $this->pagination->total_rows;
            }
        }

        $this->data['subview'] = 'market_listings/p_listings';
        $this->data['sort_by'] = $sort_by;
        $this->data['sort_order'] = $sort_order;
                    
		     //load the view passing $data values
                     $this->load->view("layouts/main_logged_in", $this->data);
		}
    }
    

        /**
	 * Search for engine part list
	 * @$sort_by
	 * @$sort_order
	 */

    public function search_p_listings() {

        $this->form_validation->set_rules('column', 'Search By', 'callback__column_dropdown|xss_clean');
        $this->form_validation->set_rules('search_field', 'Criteria', 'trim|required|xss_clean');

        if ($this->form_validation->run() == FALSE) {
            $this->data['subview'] = 'market_listings/search_p_listings';
            
            if ($this->ion_auth->logged_in() && $this->ion_auth->is_admin())
		{
                    $this->data['ptotal'] = $this->market_listings_model->get_ptotalcount_admin();
                    $this->data['ftotal'] = 0;
	             //load the view passing $data values
                     $this->load->view("layouts/main_admin", $this->data);
		}
                else if ($this->ion_auth->logged_in() && !$this->ion_auth->is_admin())
		{
                    $this->data['ptotal'] = $this->market_listings_model->get_ptotalcount();
                    $this->data['ftotal'] = 0;
		     //load the view passing $data values
                     $this->load->view("layouts/main_logged_in", $this->data);
		}
        } 
        else 
            {

            $search_term = array(
                'column' => $this->input->post('column_dropdown'),
                'criteria' => $this->input->post('search_field')
            );

            $this->session->set_userdata($search_term);


            redirect('epc_listings/market_listings/p_listings');
            }
    }

    
       /**
	* Display market place engine parts listing to user
	* @$listing_id
        */

    public function display_plisting($listing_id) {

        $this->data['this_plisting'] = $this->market_listings_model->get_plisting_data($listing_id);

//        $this->data['total'] = $this->market_listings_model->get_totalcount();

        $this->data['subview'] = 'market_listings/display_plisting';
        
        if ($this->ion_auth->logged_in() && $this->ion_auth->is_admin())
		{
                    $this->data['ftotal'] = $this->session->userdata('num_rows');
	             //load the view passing $data values
                     $this->load->view("layouts/main_admin", $this->data);
		}
                else if ($this->ion_auth->logged_in() && !$this->ion_auth->is_admin())
		{
                    $this->data['ftotal'] = $this->session->userdata('num_rows');
		     //load the view passing $data values
                     $this->load->view("layouts/main_logged_in", $this->data);
		}
//        $this->load->view("layouts/main", $this->data);
    }
    
    
        /**
	 * Validates search engine part dropdown
	 */

    public function _column_dropdown() {

        if ($this->input->post('column_dropdown') == '--Select Column--') {
            $this->form_validation->set_message('_column_dropdown', 'The Search By field is required.');
            return FALSE;
        }


        $columns = array('--Select Column--', 'part_number', 'part_description');

        $select = $this->input->post('column_dropdown');

        if (in_array($select, $columns)) {
            return TRUE;
        } else {
            $this->form_validation->set_message('_column_dropdown', 'Invalid Column');
            return FALSE;
        }
    }
    
        /**
	 * Validates search engine dropdown
	 */

    public function _column_edropdown() {

        if ($this->input->post('column_dropdown') == '--Select Column--') {
            $this->form_validation->set_message('_column_edropdown', 'The Column field is required.');
            return FALSE;
        }


        $columns1 = array('--Select Column--', 'manufacturer', 'engine_model');

        $select1 = $this->input->post('column_dropdown');

        if (in_array($select1, $columns1)) {
            return TRUE;
        } else {
            $this->form_validation->set_message('_column_edropdown', 'Invalid Column');
            return FALSE;
        }
    }
    
    
    /**
     * Search for engine part list
     */

    public function search_e_listings() {
        
        $this->form_validation->set_rules('column', 'Search By', 'callback__column_edropdown|xss_clean');
        $this->form_validation->set_rules('search_field', 'Criteria', 'trim|required|xss_clean');

        if ($this->form_validation->run() == FALSE) {
            $this->data['subview'] = 'market_listings/search_e_listings';
            
            if ($this->ion_auth->logged_in() && $this->ion_auth->is_admin())
		{
                    $this->data['total'] = $this->market_listings_model->get_totalcount_admin();
                    $this->data['ftotal'] = 0;
	             //load the view passing $data values
                     $this->load->view("layouts/main_admin", $this->data);
		}
                else if ($this->ion_auth->logged_in() && !$this->ion_auth->is_admin())
		{
                    $this->data['total'] = $this->market_listings_model->get_totalcount();
                    $this->data['ftotal'] = 0;
		     //load the view passing $data values
                     $this->load->view("layouts/main_logged_in", $this->data);
		}
        } 
        
        else 
            {

            $search_term1 = array(
                'column' => $this->input->post('column_dropdown'),
                'criteria' => $this->input->post('search_field')
            );

            $this->session->set_userdata($search_term1);


            redirect('epc_listings/market_listings/se_listings');
            }

       
    }
    
    
    
        /**
	 * Display members marketplace engine list from search results
	 * @$sort_by
	 * @$sort_order
         */

    public function se_listings($sort_by = 'manufacturer', $sort_order = 'asc'){
        
        $this->data['sfields'] = array(
            'manufacturer' => 'Manufacturer:',
            'engine_model' => 'Model:',
            'esn' => 'ESN:',
            'lister' => 'Lister:',
            'transaction' => 'Transaction:',
            'location' => 'Location:',
            'asking_price' => 'Asking Price:',
            'monthly_rent' => 'Monthly Rent:',
            'available_date' => 'Available Date:'
        );
        
        if ($this->ion_auth->logged_in() && $this->ion_auth->is_admin())
		{
        $manuf = $this->session->userdata('column');
        $crite = $this->session->userdata('criteria');
        
        $this->data['total'] = $this->market_listings_model->get_totalcount_admin();
        
        $num_results = $this->market_listings_model->search_elistings_count_admin($manuf, $crite);
        $this->data['se_results_total'] = $num_results['num_rows'];
        
        $this->session->set_userdata($num_results);

        $limit = 8;
        
        $config = array();
        $config['base_url'] = base_url() . "epc_listings/market_listings/se_listings/" . $manuf . "/" . $crite . "/$sort_by/$sort_order";
        $config['total_rows'] = $num_results['num_rows'];
        $config['per_page'] = $limit;
        $config['uri_segment'] = 8;
        $choice = $config['total_rows'] / $config['per_page'];
        $config['num_links'] = round($choice);

        $this->pagination->initialize($config);
        $ppage = ($this->uri->segment(8)) ? $this->uri->segment(8) : 0;
        
        $results = $this->market_listings_model->get_selistings_admin($manuf, $crite, $config['per_page'], $ppage, $sort_by, $sort_order);
        $this->data['selistings'] = $results['rows'];

        $this->data['selinks'] = $this->pagination->create_links();
        
        if ($this->data['selinks'] != '') {
            $this->data['sestart'] = (($this->pagination->cur_page - 1) * $this->pagination->per_page) + 1;
            $this->data['seend'] = ($this->pagination->cur_page * $this->pagination->per_page);

            if ($this->data['seend'] > $this->pagination->total_rows) {
                $this->data['seend'] = $this->pagination->total_rows;
            }
        }

        $this->data['subview'] = 'market_listings/se_listings';
        $this->data['sort_by'] = $sort_by;
        $this->data['sort_order'] = $sort_order;
	             //load the view passing $data values
                     $this->load->view("layouts/main_admin", $this->data);
		}
                else if ($this->ion_auth->logged_in() && !$this->ion_auth->is_admin())
		{
                    
        $manuf = $this->session->userdata('column');
        $crite = $this->session->userdata('criteria');
        
        $this->data['total'] = $this->market_listings_model->get_totalcount();
        
        $num_results = $this->market_listings_model->search_elistings_count($manuf, $crite);
        $this->data['se_results_total'] = $num_results['num_rows'];
        
        $this->session->set_userdata($num_results);
        $limit = 8;
        
        $config = array();
        $config['base_url'] = base_url() . "epc_listings/market_listings/se_listings/" . $manuf . "/" . $crite . "/$sort_by/$sort_order";
        $config['total_rows'] = $num_results['num_rows'];
        $config['per_page'] = $limit;
        $config['uri_segment'] = 8;
        $choice = $config['total_rows'] / $config['per_page'];
        $config['num_links'] = round($choice);

        $this->pagination->initialize($config);
        $ppage = ($this->uri->segment(8)) ? $this->uri->segment(8) : 0;
        
        $results = $this->market_listings_model->get_selistings($manuf, $crite, $config['per_page'], $ppage, $sort_by, $sort_order);
        $this->data['selistings'] = $results['rows'];

        $this->data['selinks'] = $this->pagination->create_links();
        
        if ($this->data['selinks'] != '') {
            $this->data['sestart'] = (($this->pagination->cur_page - 1) * $this->pagination->per_page) + 1;
            $this->data['seend'] = ($this->pagination->cur_page * $this->pagination->per_page);

            if ($this->data['seend'] > $this->pagination->total_rows) {
                $this->data['seend'] = $this->pagination->total_rows;
            }
        }

        $this->data['subview'] = 'market_listings/se_listings';
        $this->data['sort_by'] = $sort_by;
        $this->data['sort_order'] = $sort_order;
		     //load the view passing $data values
                     $this->load->view("layouts/main_logged_in", $this->data);
		}
        
        
    }
    
    

}