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

class Insurance extends CI_Controller {

	public function __construct() 
	{
		parent::__construct();
		
		$this->lang->load('account_general', get_language());
		$this->load->model('member_model');
    }

	public function claim()
	{
		save_current_url(base_url() . 'insurance/claim/');
		
		if($this->session->userdata('user_type') && $this->session->userdata('user_type') == MEMBER) {
			$data = $this->member_model->get_member_account_details();
			
			$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_insurance_manager') . ' | ' . $this->lang->line('hf_claim_insurance');
			
			$this->load->view('insurance_claim', $data);
		}
		else {
			redirect('', 'refresh');
		}
	}
	
	public function submit()
	{
		if($this->session->userdata('user_type') && $this->session->userdata('user_type') == MEMBER) {
			if($_POST) {
				$rs = $this->member_model->member_claim_insurance();
				
				echo $rs;
			}
			else {
				echo $this->lang->line('permission_denied');
			}
		}
		else {
			echo $this->lang->line('permission_denied');
		}
	}
	
	public function claim_listing()
	{
		save_current_url(base_url() . 'insurance/claim_listing/');
		
		if($this->session->userdata('user_type') && $this->session->userdata('user_type') == MEMBER) {
			$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_insurance_manager') . ' | ' . $this->lang->line('hf_claim_insurance_history');
			
			$this->load->view('insurance_claim_listing', $data);
		}
		else {
			redirect('', 'refresh');
		}
	}
	
	public function getclaimlist()
    {
		if($this->session->userdata('user_type') && $this->session->userdata('user_type') == MEMBER) {
			$this->lang->load('account_general', get_language());
		
			/* Array of database columns which should be read and sent back to DataTables. Use a space where
			 * you want to insert a non-database field (for example a counter or static image)
			 */
			$aColumns = array(
				$this->db->dbprefix('member_claim_insurances') . '.insurance_date',
				$this->db->dbprefix('member_claim_insurances') . '.claim_amount',
				$this->db->dbprefix('member_claim_insurances') . '.claim_slip_1',
				$this->db->dbprefix('member_claim_insurances') . '.remark',
				$this->db->dbprefix('member_claim_insurances') . '.status',
				$this->db->dbprefix('member_claim_insurances') . '.claim_slip_2',
				$this->db->dbprefix('member_claim_insurances') . '.claim_slip_3',
				$this->db->dbprefix('member_claim_insurances') . '.claim_slip_4',
				$this->db->dbprefix('member_claim_insurances') . '.claim_slip_5',
				$this->db->dbprefix('member_claim_insurances') . '.claim_slip_6',
			);
			
			// DB table to use
			$sTable = 'member_claim_insurances';
			//
			
			$this->db->where($this->db->dbprefix('member_claim_insurances') . '.from_member_id', $this->session->userdata('member_id'));
			
			$iDisplayStart = $this->input->get_post('iDisplayStart', true);
			$iDisplayLength = $this->input->get_post('iDisplayLength', true);
			$iSortCol_0 = $this->input->get_post('iSortCol_0', true);
			$iSortingCols = $this->input->get_post('iSortingCols', true);
			$sSearch = $this->input->get_post('sSearch', true);
			$sEcho = $this->input->get_post('sEcho', true);
		
			// Paging
			if(isset($iDisplayStart) && $iDisplayLength != '-1')
			{
				$this->db->limit($this->db->escape_str($iDisplayLength), $this->db->escape_str($iDisplayStart));
			}
			
			// Ordering
			if(isset($iSortCol_0))
			{
				for($i=0; $i<intval($iSortingCols); $i++)
				{
					$iSortCol = $this->input->get_post('iSortCol_'.$i, true);
					$bSortable = $this->input->get_post('bSortable_'.intval($iSortCol), true);
					$sSortDir = $this->input->get_post('sSortDir_'.$i, true);
		
					if($bSortable == 'true')
					{
						$this->db->order_by($aColumns[intval($this->db->escape_str($iSortCol))], $this->db->escape_str($sSortDir));
					}
				}
			}
			
			/* 
			 * Filtering
			 * NOTE this does not match the built-in DataTables filtering which does it
			 * word by word on any field. It's possible to do here, but concerned about efficiency
			 * on very large tables, and MySQL's regex functionality is very limited
			 */
			if(isset($sSearch) && !empty($sSearch))
			{
				for($i=0; $i<count($aColumns); $i++)
				{
					$bSearchable = $this->input->get_post('bSearchable_'.$i, true);
					
					// Individual column filtering
					if(isset($bSearchable) && $bSearchable == 'true')
					{
						$this->db->or_like($aColumns[$i], $this->db->escape_like_str($sSearch));
					}
				}
			}
			
			// Select Data
			$this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			$rResult = $this->db->get($sTable);
		
			// Data set length after filtering
			$this->db->select('FOUND_ROWS() AS found_rows');
			$iFilteredTotal = $this->db->get()->row()->found_rows;
		
			// Total data set length
			$iTotal = $this->db->count_all($sTable);
		
			// Output
			$output = array(
				'sEcho' => intval($sEcho),
				'iTotalRecords' => $iTotal,
				'iTotalDisplayRecords' => $iFilteredTotal,
				'aaData' => array()
			);
			
			foreach($rResult->result_array() as $aRow)
			{
				$row = array();
				
				$row[] = $aRow['insurance_date'];
				$row[] = number_format($aRow['claim_amount'], 2, '.', ',');
				
				$claim_slip = '';
				$claim_slip .= ($aRow['claim_slip_1'] != '') ? '<a href="' . base_url() . 'uploads/claims/' . $aRow['claim_slip_1'] . '" target="_blank"><img src="' . base_url() . 'uploads/claims/' . $aRow['claim_slip_1'] . '" border="0" width="36" /></a> &nbsp; ' : '';
				$claim_slip .= ($aRow['claim_slip_2'] != '') ? '<a href="' . base_url() . 'uploads/claims/' . $aRow['claim_slip_2'] . '" target="_blank"><img src="' . base_url() . 'uploads/claims/' . $aRow['claim_slip_2'] . '" border="0" width="36" /></a> &nbsp; ' : '';
				$claim_slip .= ($aRow['claim_slip_3'] != '') ? '<a href="' . base_url() . 'uploads/claims/' . $aRow['claim_slip_3'] . '" target="_blank"><img src="' . base_url() . 'uploads/claims/' . $aRow['claim_slip_3'] . '" border="0" width="36" /></a> &nbsp; ' : '';
				$claim_slip .= ($aRow['claim_slip_4'] != '') ? '<a href="' . base_url() . 'uploads/claims/' . $aRow['claim_slip_4'] . '" target="_blank"><img src="' . base_url() . 'uploads/claims/' . $aRow['claim_slip_4'] . '" border="0" width="36" /></a> &nbsp; ' : '';
				$claim_slip .= ($aRow['claim_slip_5'] != '') ? '<a href="' . base_url() . 'uploads/claims/' . $aRow['claim_slip_5'] . '" target="_blank"><img src="' . base_url() . 'uploads/claims/' . $aRow['claim_slip_5'] . '" border="0" width="36" /></a> &nbsp; ' : '';
				$claim_slip .= ($aRow['claim_slip_6'] != '') ? '<a href="' . base_url() . 'uploads/claims/' . $aRow['claim_slip_6'] . '" target="_blank"><img src="' . base_url() . 'uploads/claims/' . $aRow['claim_slip_6'] . '" border="0" width="36" /></a>' : '';
				
				$row[] = (($claim_slip != '') ? $claim_slip : '-');
				$row[] = (($aRow['remark'] != '') ? $aRow['remark'] : '-');
				
				switch($aRow['status']) {
					case 1: $row[] = '<span class="badge bg-success">' . $this->lang->line('insurance_confirmed') . '</span>'; break;
					case 2: $row[] = '<span class="badge bg-danger">' . $this->lang->line('insurance_cancelled') . '</span>'; break;
					default: $row[] = '<span class="badge bg-primary">' . $this->lang->line('insurance_pending') . '</span>'; break;
				}
				
				$output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}	
    }
	
	public function buy_listing()
	{
		save_current_url(base_url() . 'insurance/buy_listing/');
		
		if($this->session->userdata('user_type') && $this->session->userdata('user_type') == MEMBER) {
			$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_insurance_manager') . ' | ' . $this->lang->line('hf_buy_insurance_history');
			
			$this->load->view('insurance_buy_listing', $data);
		}
		else {
			redirect('', 'refresh');
		}
	}
	
	public function getbuylist()
    {
		if($this->session->userdata('user_type') && $this->session->userdata('user_type') == MEMBER) {
			$this->lang->load('account_general', get_language());
		
			/* Array of database columns which should be read and sent back to DataTables. Use a space where
			 * you want to insert a non-database field (for example a counter or static image)
			 */
			$aColumns = array(
				$this->db->dbprefix('member_buy_insurances') . '.insurance_date',
				$this->db->dbprefix('member_buy_insurances') . '.credit_amount',
				$this->db->dbprefix('member_buy_insurances') . '.status',
			);
			
			// DB table to use
			$sTable = 'member_buy_insurances';
			//
			
			$this->db->where($this->db->dbprefix('member_buy_insurances') . '.from_member_id', $this->session->userdata('member_id'));
			
			$iDisplayStart = $this->input->get_post('iDisplayStart', true);
			$iDisplayLength = $this->input->get_post('iDisplayLength', true);
			$iSortCol_0 = $this->input->get_post('iSortCol_0', true);
			$iSortingCols = $this->input->get_post('iSortingCols', true);
			$sSearch = $this->input->get_post('sSearch', true);
			$sEcho = $this->input->get_post('sEcho', true);
		
			// Paging
			if(isset($iDisplayStart) && $iDisplayLength != '-1')
			{
				$this->db->limit($this->db->escape_str($iDisplayLength), $this->db->escape_str($iDisplayStart));
			}
			
			// Ordering
			if(isset($iSortCol_0))
			{
				for($i=0; $i<intval($iSortingCols); $i++)
				{
					$iSortCol = $this->input->get_post('iSortCol_'.$i, true);
					$bSortable = $this->input->get_post('bSortable_'.intval($iSortCol), true);
					$sSortDir = $this->input->get_post('sSortDir_'.$i, true);
		
					if($bSortable == 'true')
					{
						$this->db->order_by($aColumns[intval($this->db->escape_str($iSortCol))], $this->db->escape_str($sSortDir));
					}
				}
			}
			
			/* 
			 * Filtering
			 * NOTE this does not match the built-in DataTables filtering which does it
			 * word by word on any field. It's possible to do here, but concerned about efficiency
			 * on very large tables, and MySQL's regex functionality is very limited
			 */
			if(isset($sSearch) && !empty($sSearch))
			{
				for($i=0; $i<count($aColumns); $i++)
				{
					$bSearchable = $this->input->get_post('bSearchable_'.$i, true);
					
					// Individual column filtering
					if(isset($bSearchable) && $bSearchable == 'true')
					{
						$this->db->or_like($aColumns[$i], $this->db->escape_like_str($sSearch));
					}
				}
			}
			
			// Select Data
			$this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			$rResult = $this->db->get($sTable);
		
			// Data set length after filtering
			$this->db->select('FOUND_ROWS() AS found_rows');
			$iFilteredTotal = $this->db->get()->row()->found_rows;
		
			// Total data set length
			$iTotal = $this->db->count_all($sTable);
		
			// Output
			$output = array(
				'sEcho' => intval($sEcho),
				'iTotalRecords' => $iTotal,
				'iTotalDisplayRecords' => $iFilteredTotal,
				'aaData' => array()
			);
			
			foreach($rResult->result_array() as $aRow)
			{
				$row = array();
				
				$row[] = $aRow['insurance_date'];
				$row[] = number_format($aRow['credit_amount'], 2, '.', ',');
				
				switch($aRow['status']) {
					case 1: $row[] = '<span class="badge bg-success">' . $this->lang->line('insurance_confirmed') . '</span>'; break;
					case 2: $row[] = '<span class="badge bg-danger">' . $this->lang->line('insurance_cancelled') . '</span>'; break;
					default: $row[] = '<span class="badge bg-primary">' . $this->lang->line('insurance_pending') . '</span>'; break;
				}
				
				$output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}	
    }
}

/* End of file insurance.php */
/* Location: ./application/controllers/insurance.php */