<?php

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

class Listings_model extends CI_Model {

    public function get_totalcount() {
        $sta = 'A';
        $user_id = $this->ion_auth->get_user_id();
        
        $this->db->where('status', $sta);
        $this->db->where('id =', $user_id);
        
        $num = $this->db->count_all_results('engines_list');
        return $num;
    }

    public function get_listings($limit, $start, $sort_by, $sort_order) {

        $sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
        $sort_columns = array('manufacturer', 'engine_model', 'esn', 'lister', 'transaction', 'location', 'asking_price', 'monthly_rent', 'available_date');
        $sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'manufacturer';
        $st = 'D';
        $user_id = $this->ion_auth->get_user_id();

        $this->db->limit($limit, $start);
        $query = $this->db->order_by($sort_by, $sort_order)->where('status !=', $st)->where('id =', $user_id)->get('engines_list');

        if ($query->num_rows() > 0) {
            return $query->result();
        }
        return false;
    }

    public function add_listing($data) {

        $insert = $this->db->insert('engines_list', $data);
        return $insert;
    }

    public function edit_listing($listing_id, $info) {

        $sel = $this->input->post('inlinecheck');
        $sel2 = $this->input->post('inlinecheck1');


        if ($info['file_name'] != '') {
            $data = array(
                'manufacturer' => $this->input->post('company_dropdown'),
                'engine_model' => $this->input->post('engine_model'),
                'esn' => $this->input->post('esn'),
                'tsn' => $this->input->post('tsn'),
                'csn' => $this->input->post('csn'),
                'tso' => $this->input->post('tso'),
                'cso' => $this->input->post('cso'),
                'first_limiter' => $this->input->post('first_limiter'),
                'limiting_module' => $this->input->post('limiting_module'),
                'last_operator' => $this->input->post('last_operator'),
                'location' => $this->input->post('location'),
                'lister' => $this->input->post('inlineradio'),
                'transaction' => implode("/", $sel),
                'asking_price' => $this->input->post('asking_price'),
                'monthly_rent' => $this->input->post('monthly_rent'),
                'available_date' => $this->input->post('available_date'),
                'mini_pack_name' => $info['file_name'],
                'mini_pack_path' => $info['full_path']
                    
            );
        } else {
            
            if($sel2 == '1') {
                
                
                $id4 = $this->ion_auth->get_user_id();
                $user4 = $this->ion_auth->user($id4)->row();
                $em4 = $user4->email;
                
                $mini_p_f = $info['file_name'];
                delete_files('./members/'.$em4.'/engine_listings/'.$mini_p_f.'/');
                
            $data = array(
                'manufacturer' => $this->input->post('company_dropdown'),
                'engine_model' => $this->input->post('engine_model'),
                'esn' => $this->input->post('esn'),
                'tsn' => $this->input->post('tsn'),
                'csn' => $this->input->post('csn'),
                'tso' => $this->input->post('tso'),
                'cso' => $this->input->post('cso'),
                'first_limiter' => $this->input->post('first_limiter'),
                'limiting_module' => $this->input->post('limiting_module'),
                'last_operator' => $this->input->post('last_operator'),
                'location' => $this->input->post('location'),
                'lister' => $this->input->post('inlineradio'),
                'transaction' => implode("/", $sel),
                'asking_price' => $this->input->post('asking_price'),
                'monthly_rent' => $this->input->post('monthly_rent'),
                'available_date' => $this->input->post('available_date'),
                'mini_pack_name' => '',
                'mini_pack_path' => '',
                'mini_pack_delete' => $sel2
                
            );
            }
            
            else {
                
                $data = array(
                'manufacturer' => $this->input->post('company_dropdown'),
                'engine_model' => $this->input->post('engine_model'),
                'esn' => $this->input->post('esn'),
                'tsn' => $this->input->post('tsn'),
                'csn' => $this->input->post('csn'),
                'tso' => $this->input->post('tso'),
                'cso' => $this->input->post('cso'),
                'first_limiter' => $this->input->post('first_limiter'),
                'limiting_module' => $this->input->post('limiting_module'),
                'last_operator' => $this->input->post('last_operator'),
                'location' => $this->input->post('location'),
                'lister' => $this->input->post('inlineradio'),
                'transaction' => implode("/", $sel),
                'asking_price' => $this->input->post('asking_price'),
                'monthly_rent' => $this->input->post('monthly_rent'),
                'available_date' => $this->input->post('available_date'),
                'mini_pack_delete' => $sel2
                
            );
                
            }
        }

        $this->db->where('list_id', $listing_id);
        $this->db->update('engines_list', $data);
        return TRUE;
    }

    public function get_listing_data($listing_id) {
        $this->db->where('list_id', $listing_id);
        $query = $this->db->get('engines_list');
        return $query->row();
    }

    public function delete_listing($listing_id) {

        $data = array(
            'status' => 'D'
        );

        $this->db->where('list_id', $listing_id);
        $this->db->update('engines_list', $data);
        return TRUE;
    }

    public function get_ptotalcount() {
        $sta = 'A';
        $user_id = $this->ion_auth->get_user_id();
        
        $this->db->where('status', $sta);
        $this->db->where('id =', $user_id);
        
        $num = $this->db->count_all_results('engine_parts_list');
        return $num;
    }

    public function get_plistings($limit, $pstart, $sort_by, $sort_order) {

        $sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
        $sort_columns = array('part_number', 'part_description', 'part_condition', 'enginep_model', 'tag', 'quantity', 'lister', 'transaction', 'asking_price', 'monthly_rent', 'available_date');
        $sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'part_number';
        $st = 'D';
        $user_id = $this->ion_auth->get_user_id();

        $this->db->limit($limit, $pstart);
        $query = $this->db->order_by($sort_by, $sort_order)->where('status !=', $st)->where('id =', $user_id)->get('engine_parts_list');

        if ($query->num_rows() > 0) {
            return $query->result();
        }
        return false;
    }

    public function add_plisting($data) {

        $insert = $this->db->insert('engine_parts_list', $data);
        return $insert;
    }

    public function get_plisting_data($listing_id) {
        $this->db->where('list_id', $listing_id);
        $query = $this->db->get('engine_parts_list');
        return $query->row();
    }

    public function delete_plisting($listing_id) {

        $data = array(
            'status' => 'D'
        );

        $this->db->where('list_id', $listing_id);
        $this->db->update('engine_parts_list', $data);
        return;
    }

    public function edit_plisting($listing_id, $info1) {
        
        //$this->data['upload_data1'] = $info;
        
        $sel1 = $this->input->post('inlinecheck');
        $sel3 = $this->input->post('inlinecheck1');
        
        if ($info1['file_name'] != '') {
        
            $data = array(
                'part_number' => $this->input->post('part_number'),
                'part_description' => $this->input->post('part_description'),
                'part_condition' => $this->input->post('part_condition_dropdown'),
                'tag' => $this->input->post('tag_dropdown'),
                'quantity' => $this->input->post('quantity'),
                'lister' => $this->input->post('inlineradio'),
                'transaction' => implode("/", $sel1),
                'asking_price' => $this->input->post('asking_price'),
                'monthly_rent' => $this->input->post('monthly_rent'),
                'available_date' => $this->input->post('available_date'),
                'record_name' => $info1['file_name'],
                'record_path' => $info1['full_path']
            );
        }
        else
        {
            
            if($sel3 == '1') {
                
                
                $id5 = $this->ion_auth->get_user_id();
                $user5 = $this->ion_auth->user($id5)->row();
                $em5 = $user5->email;
                
                $record_fol = $info1['file_name'];
                delete_files('./members/'.$em5.'/engine_listings/'.$record_fol.'/');
                
                $data = array(
                'part_number' => $this->input->post('part_number'),
                'part_description' => $this->input->post('part_description'),
                'part_condition' => $this->input->post('part_condition_dropdown'),
                'tag' => $this->input->post('tag_dropdown'),
                'quantity' => $this->input->post('quantity'),
                'lister' => $this->input->post('inlineradio'),
                'transaction' => implode("/", $sel1),
                'asking_price' => $this->input->post('asking_price'),
                'monthly_rent' => $this->input->post('monthly_rent'),
                'available_date' => $this->input->post('available_date'),
                'record_name' => '',
                'record_path' => '',
                'record_pack_delete' => $sel3    
            );
            }
            else {
                
                $data = array(
                'part_number' => $this->input->post('part_number'),
                'part_description' => $this->input->post('part_description'),
                'part_condition' => $this->input->post('part_condition_dropdown'),
                'tag' => $this->input->post('tag_dropdown'),
                'quantity' => $this->input->post('quantity'),
                'lister' => $this->input->post('inlineradio'),
                'transaction' => implode("/", $sel1),
                'asking_price' => $this->input->post('asking_price'),
                'monthly_rent' => $this->input->post('monthly_rent'),
                'available_date' => $this->input->post('available_date'),
                'record_pack_delete' => $sel3    
            );
                
            }
            
        }

        $this->db->where('list_id', $listing_id);
        $this->db->update('engine_parts_list', $data);
        return TRUE;
    }

}
