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

class Authority 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['authority'] = $this->get_authority(); 
        $data['page_title'] = 'Authority';
        $this->load->view('authority-list', $data);
    }
    public function get_authority()
    {
        $query = $this->db->get('db_authority');
        return $query->result();
    }

    public function ajax_list()
    {
        $query = $this->db->get('db_authority');
        $db_authorities =  $query->result();
        $data = array();
        $no = $_POST['start'];
        foreach ($db_authorities as $authority) {
            $no++;
            
            $row = array();
            $row[] = $no;
            $row[] = $authority->authority_name;
            $row[] = $authority->authority_email;
            $row[] = $authority->authority_contact_number;
            $row[] = $authority->authority_cash_credit; 
            $row[] = $authority->authority_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="authority/edit/' . $authority->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_authority(' . $authority->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($db_authorities),
            "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'),
        );
        $query = $this->db->get('db_authority');
        $data['authorities'] =  $query->result();
        $data['page_title'] = 'Authority';
        $this->load->view('authority', $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(
            'authority_name' => $this->input->post('authority_name'),
            'authority_email' => $this->input->post('authority_email'),
            'authority_contact_number' => $this->input->post('authority_contact_number'),
            'authority_cash_credit' => $this->input->post('authority_cash_credit'),
            'authority_status' => $this->input->post('authority_status'),
            'authority_address' => $this->input->post('authority_address'),
            'authority_note' => $this->input->post('authority_note'), 
        );

        $this->db->insert('db_authority', $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'] = 'Authority';
        $query = $this->db->get_where('db_authority', array('id' => $id));
        $result = $query->row_array();
        $data['q_idd'] = $id;
        $data['authority_name'] = $result['authority_name'];
        $data['authority_email'] = $result['authority_email'];
        $data['authority_contact_number'] = $result['authority_contact_number'];
        $data['authority_cash_credit'] = $result['authority_cash_credit'];
        $data['authority_status'] = $result['authority_status'];
        $data['authority_address'] = $result['authority_address'];
        $data['authority_note'] = $result['authority_note']; 
        $data = array_merge($data, $result);
        $this->load->view('authority', $data);
    }
    public function update()
    {
        $this->load->library('form_validation');


        $this->form_validation->set_rules('authority_name', 'Authority Name', 'required');
        $this->form_validation->set_rules('authority_email', 'Authority Email', 'required');
        $this->form_validation->set_rules('authority_contact_number', 'Contact Number', 'required');
        $this->form_validation->set_rules('authority_cash_credit', 'Cash Credit', 'required');
        $this->form_validation->set_rules('authority_status', 'Status', 'required');
        $this->form_validation->set_rules('authority_address', 'Address', 'required');
        $this->form_validation->set_rules('authority_note', 'Note', '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(
                'authority_name' => $this->input->post('authority_name'),
                'authority_email' => $this->input->post('authority_email'),
                'authority_contact_number' => $this->input->post('authority_contact_number'),
                'authority_cash_credit' => $this->input->post('authority_cash_credit'),
                'authority_status' => $this->input->post('authority_status'),
                'authority_address' => $this->input->post('authority_address'),
                'authority_note' => $this->input->post('authority_note'), 
            );

            if (empty($id)) {
                // Insert new record
                $this->db->insert('db_authority', $data);
                $response['status'] = 'success';
                $response['message'] = 'Authority added successfully!';
            } else {
                // Update existing record
                $this->db->where('id', $id);
                $this->db->update('db_authority', $data);
                $response['status'] = 'success';
                $response['message'] = 'Authority 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_authority');
        $response['status'] = 'success';
        $response['message'] = 'Authority deleted successfully!'; 
        echo json_encode($response); 
    }

    public function update_authroity($id, $data)
    {
        $this->db->where('id', $id);
        $this->db->update('db_authority', $data);
    }
}
