<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Asset_type extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
    }


    public function index()
    {
        //If currency not set retrieve from DB
        if (!$this->session->has_userdata('currency')) {
            $q1 = $this->db->query("SELECT a.currency_name,a.currency,a.currency_code,a.exchange_rate,a.symbol,b.currency_placement FROM db_currency a,db_sitesettings b WHERE a.id=b.currency_id AND b.id=1");
            $currency = $q1->row()->currency;
            $currency_placement = $q1->row()->currency_placement;
            $currency_code = $q1->row()->currency_code;
            $exchange_rate = $q1->row()->exchange_rate;
            $this->session->set_userdata(array('currency'  => $currency, 'currency_placement'  => $currency_placement, 'currency_code'  => $currency_code, 'exchange_rate' => $exchange_rate));
        }
        //end 
        $query = $this->db->select('site_name,version,language_id,timezone,time_format,date_format')->where('id', 1)->get('db_sitesettings');
        date_default_timezone_set(trim($query->row()->timezone));
        $time_format = (trim($query->row()->time_format) == '24') ? date("h:i:s") : date("h:i:s a");
        $date_view_format = trim($query->row()->date_format);
        $this->session->set_userdata(array('view_date'  => $date_view_format));
        $this->session->set_userdata(array('view_time'  => $query->row()->time_format));


        //CHECK LANGUAGE IN SESSION ELSE FROM DB
        if (!$this->session->has_userdata('language') && $this->session->has_userdata('logged_in')) {
            $this->load->model('language_model');
            $this->language_model->set($query->row()->language_id);
        }
        if ($this->session->has_userdata('logged_in')) {
            $this->lang->load($this->session->userdata('language'), $this->session->userdata('language'));
        }
        //End

        $data = array(
            'theme_link'    => base_url() . 'theme/',
            'base_url'      => base_url(),
            'SITE_TITLE'    => $query->row()->site_name,
            'VERSION'       => $query->row()->version,
            'CURRENCY'       => $this->session->userdata('currency'),
            'CURRENCY_PLACE' => $this->session->userdata('curre
			ncy_placement'),
            'CURRENCY_CODE' => $this->session->userdata('currency_code'),
            'EXHCHANGE_RATE' => $this->session->userdata('exchange_rate'),
            'CUR_DATE'      => date("Y-m-d"),
            'VIEW_DATE'      => $date_view_format,
            'CUR_TIME'      => $time_format,
            'SYSTEM_IP'     => $_SERVER['REMOTE_ADDR'],
            'SYSTEM_NAME'   => gethostbyaddr($_SERVER['REMOTE_ADDR']),
            'CUR_USERNAME'  => $this->session->userdata('inv_username'),
            'CUR_USERID'    => $this->session->userdata('inv_userid'),
        ); 
        $data['asset_types'] = $this->get_asset_types();

        $data['page_title'] = 'Asset Type';
        $this->load->view('asset-type-list', $data);
    }
    public function get_asset_types()
    {
        $query = $this->db->get('db_asset_type');
        return $query->result();
    }

    public function ajax_list()
    {
        $query = $this->db->get('db_asset_type');
        $list =  $query->result();
        $data = array();
        $no = $_POST['start'];
        foreach ($list as $asset) {
            $no++;
            $row = array();
            $row[]=$no;
            // $row[] = '<input type="checkbox" name="checkbox[]" value=' . $asset->id . ' class="checkbox column_checkbox" >';
            $row[] = $asset->name;
            $row[] = $asset->note;
            $row[] = $asset->status==1 ? '<span class="label label-success" style="cursor:pointer">Active </span>' : '<span class="label label-danger" style="cursor:pointer">Inactive </span>';
            $str2 = '<div class="btn-group" title="View Account">
										<a class="btn btn-primary btn-o dropdown-toggle" data-toggle="dropdown" href="#">
											Action <span class="caret"></span>
										</a>
										<ul role="menu" class="dropdown-menu dropdown-light pull-right">';

            $str2 .= '<li>
												<a title="Edit Record ?" href="asset_type/edit/' . $asset->id . '">
													<i class="fa fa-fw fa-edit text-blue"></i>Edit
												</a>
											</li>';

            $str2 .= '<li>
												<a style="cursor:pointer" title="Delete Record ?" onclick="delete_asset_type(' . $asset->id . ')">
													<i class="fa fa-fw fa-trash text-red"></i>Delete
												</a>
											</li>
											
										</ul>
									</div>';

            $row[] = $str2;
            $data[] = $row;
        }

        $output = array(
            "draw" => $_POST['draw'],
            "recordsTotal" => count($list),
            "recordsFiltered" => 1,
            "data" => $data,
        );
        //output to json format
        echo json_encode($output);
    }

    public function add()
    {
        //If currency not set retrieve from DB
        if (!$this->session->has_userdata('currency')) {
            $q1 = $this->db->query("SELECT a.currency_name,a.currency,a.currency_code,a.exchange_rate,a.symbol,b.currency_placement FROM db_currency a,db_sitesettings b WHERE a.id=b.currency_id AND b.id=1");
            $currency = $q1->row()->currency;
            $currency_placement = $q1->row()->currency_placement;
            $currency_code = $q1->row()->currency_code;
            $exchange_rate = $q1->row()->exchange_rate;
            $this->session->set_userdata(array('currency'  => $currency, 'currency_placement'  => $currency_placement, 'currency_code'  => $currency_code, 'exchange_rate' => $exchange_rate));
        }
        //end 
        $query = $this->db->select('site_name,version,language_id,timezone,time_format,date_format')->where('id', 1)->get('db_sitesettings');
        date_default_timezone_set(trim($query->row()->timezone));
        $time_format = (trim($query->row()->time_format) == '24') ? date("h:i:s") : date("h:i:s a");
        $date_view_format = trim($query->row()->date_format);
        $this->session->set_userdata(array('view_date'  => $date_view_format));
        $this->session->set_userdata(array('view_time'  => $query->row()->time_format));


        //CHECK LANGUAGE IN SESSION ELSE FROM DB
        if (!$this->session->has_userdata('language') && $this->session->has_userdata('logged_in')) {
            $this->load->model('language_model');
            $this->language_model->set($query->row()->language_id);
        }
        if ($this->session->has_userdata('logged_in')) {
            $this->lang->load($this->session->userdata('language'), $this->session->userdata('language'));
        }
        //End

        $data = array(
            'theme_link'    => base_url() . 'theme/',
            'base_url'      => base_url(),
            'SITE_TITLE'    => $query->row()->site_name,
            'VERSION'       => $query->row()->version,
            'CURRENCY'       => $this->session->userdata('currency'),
            'CURRENCY_PLACE' => $this->session->userdata('curre
			ncy_placement'),
            'CURRENCY_CODE' => $this->session->userdata('currency_code'),
            'EXHCHANGE_RATE' => $this->session->userdata('exchange_rate'),
            'CUR_DATE'      => date("Y-m-d"),
            'VIEW_DATE'      => $date_view_format,
            'CUR_TIME'      => $time_format,
            'SYSTEM_IP'     => $_SERVER['REMOTE_ADDR'],
            'SYSTEM_NAME'   => gethostbyaddr($_SERVER['REMOTE_ADDR']),
            'CUR_USERNAME'  => $this->session->userdata('inv_username'),
            'CUR_USERID'    => $this->session->userdata('inv_userid'),
        );
        $data['page_title'] = 'Asset Type';
        $this->load->view('asset-type', $data);
    }

    public function save(){ 
        if (!$this->session->has_userdata('currency')) {
            $q1 = $this->db->query("SELECT a.currency_name,a.currency,a.currency_code,a.exchange_rate,a.symbol,b.currency_placement FROM db_currency a,db_sitesettings b WHERE a.id=b.currency_id AND b.id=1");
            $currency = $q1->row()->currency;
            $currency_placement = $q1->row()->currency_placement;
            $currency_code = $q1->row()->currency_code;
            $exchange_rate = $q1->row()->exchange_rate;
            $this->session->set_userdata(array('currency'  => $currency, 'currency_placement'  => $currency_placement, 'currency_code'  => $currency_code, 'exchange_rate' => $exchange_rate));
        }
        //end 
        $query = $this->db->select('site_name,version,language_id,timezone,time_format,date_format')->where('id', 1)->get('db_sitesettings');
        date_default_timezone_set(trim($query->row()->timezone));
        $time_format = (trim($query->row()->time_format) == '24') ? date("h:i:s") : date("h:i:s a");
        $date_view_format = trim($query->row()->date_format);
        $this->session->set_userdata(array('view_date'  => $date_view_format));
        $this->session->set_userdata(array('view_time'  => $query->row()->time_format));


        //CHECK LANGUAGE IN SESSION ELSE FROM DB
        if (!$this->session->has_userdata('language') && $this->session->has_userdata('logged_in')) {
            $this->load->model('language_model');
            $this->language_model->set($query->row()->language_id);
        }
        if ($this->session->has_userdata('logged_in')) {
            $this->lang->load($this->session->userdata('language'), $this->session->userdata('language'));
        }
        //End

        $data = array(
            'theme_link'    => base_url() . 'theme/',
            'base_url'      => base_url(),
            'SITE_TITLE'    => $query->row()->site_name,
            'VERSION'       => $query->row()->version,
            'CURRENCY'       => $this->session->userdata('currency'),
            'CURRENCY_PLACE' => $this->session->userdata('curre
            ncy_placement'),
            'CURRENCY_CODE' => $this->session->userdata('currency_code'),
            'EXHCHANGE_RATE' => $this->session->userdata('exchange_rate'),
            'CUR_DATE'      => date("Y-m-d"),
            'VIEW_DATE'      => $date_view_format,
            'CUR_TIME'      => $time_format,
            'SYSTEM_IP'     => $_SERVER['REMOTE_ADDR'],
            'SYSTEM_NAME'   => gethostbyaddr($_SERVER['REMOTE_ADDR']),
            'CUR_USERNAME'  => $this->session->userdata('inv_username'),
            'CUR_USERID'    => $this->session->userdata('inv_userid'),
        );  
        $data = array(
            'name' => $this->input->post('name'),
            'note' => $this->input->post('note'),
            'status' => $this->input->post('status')
        );
        
        $this->db->insert('db_asset_type', $data);
        return 'success';
    }

    public function edit($id)
    {
        if (!$this->session->has_userdata('currency')) {
            $q1 = $this->db->query("SELECT a.currency_name,a.currency,a.currency_code,a.exchange_rate,a.symbol,b.currency_placement FROM db_currency a,db_sitesettings b WHERE a.id=b.currency_id AND b.id=1");
            $currency = $q1->row()->currency;
            $currency_placement = $q1->row()->currency_placement;
            $currency_code = $q1->row()->currency_code;
            $exchange_rate = $q1->row()->exchange_rate;
            $this->session->set_userdata(array('currency'  => $currency, 'currency_placement'  => $currency_placement, 'currency_code'  => $currency_code, 'exchange_rate' => $exchange_rate));
        }
        //end 
        $query = $this->db->select('site_name,version,language_id,timezone,time_format,date_format')->where('id', 1)->get('db_sitesettings');
        date_default_timezone_set(trim($query->row()->timezone));
        $time_format = (trim($query->row()->time_format) == '24') ? date("h:i:s") : date("h:i:s a");
        $date_view_format = trim($query->row()->date_format);
        $this->session->set_userdata(array('view_date'  => $date_view_format));
        $this->session->set_userdata(array('view_time'  => $query->row()->time_format));


        //CHECK LANGUAGE IN SESSION ELSE FROM DB
        if (!$this->session->has_userdata('language') && $this->session->has_userdata('logged_in')) {
            $this->load->model('language_model');
            $this->language_model->set($query->row()->language_id);
        }
        if ($this->session->has_userdata('logged_in')) {
            $this->lang->load($this->session->userdata('language'), $this->session->userdata('language'));
        }
        //End

        $data = array(
            'theme_link'    => base_url() . 'theme/',
            'base_url'      => base_url(),
            'SITE_TITLE'    => $query->row()->site_name,
            'VERSION'       => $query->row()->version,
            'CURRENCY'       => $this->session->userdata('currency'),
            'CURRENCY_PLACE' => $this->session->userdata('curre
			ncy_placement'),
            'CURRENCY_CODE' => $this->session->userdata('currency_code'),
            'EXHCHANGE_RATE' => $this->session->userdata('exchange_rate'),
            'CUR_DATE'      => date("Y-m-d"),
            'VIEW_DATE'      => $date_view_format,
            'CUR_TIME'      => $time_format,
            'SYSTEM_IP'     => $_SERVER['REMOTE_ADDR'],
            'SYSTEM_NAME'   => gethostbyaddr($_SERVER['REMOTE_ADDR']),
            'CUR_USERNAME'  => $this->session->userdata('inv_username'),
            'CUR_USERID'    => $this->session->userdata('inv_userid'),
        );
        $data['page_title'] = 'Asset Type';  
        $query = $this->db->get_where('db_asset_type', array('id' => $id));
        $result = $query->row_array();
        $data['q_id'] = $id;
        
        // Merge the fetched data with existing data
        $data = array_merge($data, $result);
        $this->load->view('asset-type', $data);
    }
    public function update(){ 
        $this->load->library('form_validation');

    // Validation rules
    $this->form_validation->set_rules('name', 'Asset Type Name', 'trim|required');
    $this->form_validation->set_rules('note', 'Note', 'trim');
    $this->form_validation->set_rules('status', 'Status', 'required');

    if ($this->form_validation->run() == FALSE) {
        // Validation failed
        $response['status'] = 'error';
        $response['message'] = validation_errors();
    } else {
        // Validation passed
        $id = $this->input->post('q_id');

        // Data to be saved or updated
        $data = array(
            'name' => $this->input->post('name'),
            'note' => $this->input->post('note'),
            'status' => $this->input->post('status')
        );

        if (empty($id)) {
            // Insert new record
            $this->db->insert('db_asset_type', $data);
            $response['status'] = 'success';
            $response['message'] = 'Asset type added successfully!';
        } else {
            // Update existing record
            $this->db->where('id', $id);
            $this->db->update('db_asset_type', $data);
            $response['status'] = 'success';
            $response['message'] = 'Asset type updated successfully!';
        }
    }

    // Output JSON response
    echo json_encode($response);
    }
     
    public function delete()
    {   

             if (!$this->session->has_userdata('currency')) {
                $q1 = $this->db->query("SELECT a.currency_name,a.currency,a.currency_code,a.exchange_rate,a.symbol,b.currency_placement FROM db_currency a,db_sitesettings b WHERE a.id=b.currency_id AND b.id=1");
                $currency = $q1->row()->currency;
                $currency_placement = $q1->row()->currency_placement;
                $currency_code = $q1->row()->currency_code;
                $exchange_rate = $q1->row()->exchange_rate;
                $this->session->set_userdata(array('currency'  => $currency, 'currency_placement'  => $currency_placement, 'currency_code'  => $currency_code, 'exchange_rate' => $exchange_rate));
            }
            //end 
            $query = $this->db->select('site_name,version,language_id,timezone,time_format,date_format')->where('id', 1)->get('db_sitesettings');
            date_default_timezone_set(trim($query->row()->timezone));
            $time_format = (trim($query->row()->time_format) == '24') ? date("h:i:s") : date("h:i:s a");
            $date_view_format = trim($query->row()->date_format);
            $this->session->set_userdata(array('view_date'  => $date_view_format));
            $this->session->set_userdata(array('view_time'  => $query->row()->time_format));
    
    
            //CHECK LANGUAGE IN SESSION ELSE FROM DB
            if (!$this->session->has_userdata('language') && $this->session->has_userdata('logged_in')) {
                $this->load->model('language_model');
                $this->language_model->set($query->row()->language_id);
            }
            if ($this->session->has_userdata('logged_in')) {
                $this->lang->load($this->session->userdata('language'), $this->session->userdata('language'));
            }
            //End
    
            $data = array(
                'theme_link'    => base_url() . 'theme/',
                'base_url'      => base_url(),
                'SITE_TITLE'    => $query->row()->site_name,
                'VERSION'       => $query->row()->version,
                'CURRENCY'       => $this->session->userdata('currency'),
                'CURRENCY_PLACE' => $this->session->userdata('curre
                ncy_placement'),
                'CURRENCY_CODE' => $this->session->userdata('currency_code'),
                'EXHCHANGE_RATE' => $this->session->userdata('exchange_rate'),
                'CUR_DATE'      => date("Y-m-d"),
                'VIEW_DATE'      => $date_view_format,
                'CUR_TIME'      => $time_format,
                'SYSTEM_IP'     => $_SERVER['REMOTE_ADDR'],
                'SYSTEM_NAME'   => gethostbyaddr($_SERVER['REMOTE_ADDR']),
                'CUR_USERNAME'  => $this->session->userdata('inv_username'),
                'CUR_USERID'    => $this->session->userdata('inv_userid'),
            );  
        $this->db->where('id', $_POST['q_id']);
        $this->db->delete('db_asset_type');
        redirect('index');
    }

    public function multi_asset_type()
    {
        $ids = implode(",", $_POST['checkbox']);
        // return $this->brand->delete_brands_from_table($ids);
        $this->db->whereIN(['id', $ids]);
        $this->db->delete('db_asset_type');
    }
    public function update_asset_type($id, $data)
    {
        $this->db->where('id', $id);
        $this->db->update('db_asset_type', $data);
    }
}
