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

class Insurance extends MY_Controller {

	public function index()
	{
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			redirect('insurance/buy_listing', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function buy_listing()
	{
		save_current_url(base_url() . 'insurance/buy_listing/');
		
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->lang->load('account_general', get_language());
			
			if($_POST) {
				$this->session->set_userdata(
					array(
						'member_search_username' => trim($this->input->post('search_username', TRUE)), 
						'member_exclude_downline' => trim($this->input->post('exclude_downline', TRUE)), 
						'member_search_insurance_date' => trim($this->input->post('search_insurance_date', TRUE)), 
						'member_search_start_date' => trim($this->input->post('search_start_date', TRUE)), 
						'member_search_end_date' => trim($this->input->post('search_end_date', TRUE))
						)
				);
				
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_insurance') . ' - ' .$this->lang->line('hf_buy_insurance_history');
			
				$this->load->view('insurance_buy_history', $_POST);
			}
			else {
				$this->session->unset_userdata(array('member_search_username' => '', 'member_exclude_downline' => '', 'member_search_insurance_date' => '', 'member_search_start_date' => '', 'member_search_end_date' => ''));
				
				$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_insurance') . ' - ' .$this->lang->line('hf_buy_insurance_history');
			
				$this->load->view('insurance_buy_history', $data);
			}
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function getbuylist()
    {
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$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') . '.transaction_date',
								$this->db->dbprefix('member_buy_insurances') . '.credit_amount',
								$this->db->dbprefix('member_buy_insurances') . '.from_member_username',
								$this->db->dbprefix('member_buy_insurances') . '.status',
							);
			
			$jColumns = $this->db->dbprefix('member_buy_insurances') . '.from_member_id = ' . $this->db->dbprefix('members') . '.member_id';
			
			// DB table to use
			$sTable = 'sw_member_buy_insurances';
			$jTable = 'sw_members';
			//
			
			if($this->session->userdata('member_search_insurance_date') != '') {
				if($this->session->userdata('member_search_insurance_date') == 2) {
					$start_date = strtotime(date('Y-m-d 00:00:00', strtotime($this->session->userdata('member_search_start_date'))));
					$end_date = strtotime(date('Y-m-d 23:59:59', strtotime($this->session->userdata('member_search_end_date'))));
					$where = "(" . $this->db->dbprefix('member_buy_insurances') . ".transaction_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_buy_insurances') . ".transaction_date <= '" .  $end_date . "')";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_search_username') != '') {
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username = ? LIMIT 1", array($this->session->userdata('member_search_username')));
				if($query->num_rows > 0) {
					$row = $query->row();
					$where = $this->db->dbprefix('members') . ".introducer_parent_ref LIKE '%," .  $row->member_id . ",%'";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_exclude_downline') != '') {
				$arr = explode(',', $this->session->userdata('member_exclude_downline'));
				$arr = implode("','", $arr);
				
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username IN('" . $arr . "')");
				if($query->num_rows > 0) {
					$where = '';
					foreach($query->result() as $row) {
						if($where != '') {
							$where .= " AND (" . $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%' AND " . $this->db->dbprefix('members') . ".member_id != " . $row->member_id . ")";
						}
						else {
							$where .= "(" . $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%' AND " . $this->db->dbprefix('members') . ".member_id != " . $row->member_id . ")";
						}
					}
					
					if($where != '') {
						$where = "(" . $where . ")";
						$this->db->where($where);
					}
				}
			}
			
			$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], $sSearch);
						// $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);
			$this->db->join($jTable, $jColumns);
			$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[] = date('Y-m-d', $aRow['transaction_date']);
				$row[] = number_format($aRow['credit_amount'], 2, '.', '');
				$row[] = $aRow['from_member_username'];
				
				switch($aRow['status']) {
					case 0: $row[] = '<span class="label label-info">' . $this->lang->line('insurance_pending') . '</span>'; break;
					case 1: $row[] = '<span class="label label-success">' . $this->lang->line('insurance_confirmed') . '</span>'; break;
					case 2: $row[] = '<span class="label label-important">' . $this->lang->line('insurance_cancelled') . '</span>'; break;
					default: $row[] = '<span class="label label-info">' . $this->lang->line('insurance_pending') . '</span>'; break;
				}
				
				$output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}	
    }
	
	public function buy_insurance_export()
	{
		set_time_limit(0);
		
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->lang->load('account_general', get_language());
			
			require_once APPPATH."/third_party/php-excel.class.php";
			
			$data[] = array (
								$this->lang->line('insurance_buy_date'),
								$this->lang->line('insurance_amount'),
								$this->lang->line('insurance_from_member'),
								$this->lang->line('insurance_status')
							);
			
			//Get Data
			$aColumns = array(
								$this->db->dbprefix('member_buy_insurances') . '.transaction_date',
								$this->db->dbprefix('member_buy_insurances') . '.credit_amount',
								$this->db->dbprefix('member_buy_insurances') . '.from_member_username',
								$this->db->dbprefix('member_buy_insurances') . '.status',
							);
			
			$jColumns = $this->db->dbprefix('member_buy_insurances') . '.from_member_id = ' . $this->db->dbprefix('members') . '.member_id';
			
			// DB table to use
			$sTable = 'sw_member_buy_insurances';
			$jTable = 'sw_members';
			//
			
			if($this->session->userdata('member_search_insurance_date') != '') {
				if($this->session->userdata('member_search_insurance_date') == 2) {
					$start_date = strtotime(date('Y-m-d 00:00:00', strtotime($this->session->userdata('member_search_start_date'))));
					$end_date = strtotime(date('Y-m-d 23:59:59', strtotime($this->session->userdata('member_search_end_date'))));
					$where = "(" . $this->db->dbprefix('member_buy_insurances') . ".transaction_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_buy_insurances') . ".transaction_date <= '" .  $end_date . "')";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_search_username') != '') {
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username = ? LIMIT 1", array($this->session->userdata('member_search_username')));
				if($query->num_rows > 0) {
					$row = $query->row();
					$where = $this->db->dbprefix('members') . ".introducer_parent_ref LIKE '%," .  $row->member_id . ",%'";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_exclude_downline') != '') {
				$arr = explode(',', $this->session->userdata('member_exclude_downline'));
				$arr = implode("','", $arr);
				
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username IN('" . $arr . "')");
				if($query->num_rows > 0) {
					$where = '';
					foreach($query->result() as $row) {
						if($where != '') {
							$where .= " AND (" . $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%' AND " . $this->db->dbprefix('members') . ".member_id != " . $row->member_id . ")";
						}
						else {
							$where .= "(" . $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%' AND " . $this->db->dbprefix('members') . ".member_id != " . $row->member_id . ")";
						}
					}
					
					if($where != '') {
						$where = "(" . $where . ")";
						$this->db->where($where);
					}
				}
			}
			
			$this->db->order_by($this->db->dbprefix('member_buy_insurances') . '.transaction_date', 'asc');
			
			$this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			$this->db->join($jTable, $jColumns);
			$rResult = $this->db->get($sTable);
			
			foreach($rResult->result_array() as $aRow)
			{
				$status = '';
				switch($aRow['status']) {
					case 0: $status = $this->lang->line('insurance_pending'); break;
					case 1: $status = $this->lang->line('insurance_confirmed'); break;
					case 2: $status = $this->lang->line('insurance_cancelled'); break;
					default: $status = $this->lang->line('insurance_pending'); break;
				}
				
				$data[] = array (
									date('Y-m-d', $aRow['transaction_date']),
									number_format($aRow['credit_amount'], 2, '.', ''),
									$aRow['from_member_username'],
									$status
								);
			}
					
			$xls = new Excel_XML('UTF-8', false, 'Member Buy Insurance List');
			$xls->addArray($data);
			$xls->generateXML('member_buy_insurance_list');
		}
	}
	
	public function claim_listing()
	{
		save_current_url(base_url() . 'insurance/claim_listing/');
		
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->lang->load('account_general', get_language());
			
			if($_POST) {
				$this->session->set_userdata(
					array(
						'member_search_username' => trim($this->input->post('search_username', TRUE)), 
						'member_exclude_downline' => trim($this->input->post('exclude_downline', TRUE)), 
						'member_search_insurance_date' => trim($this->input->post('search_insurance_date', TRUE)), 
						'member_search_start_date' => trim($this->input->post('search_start_date', TRUE)), 
						'member_search_end_date' => trim($this->input->post('search_end_date', TRUE)),
						'member_search_insurance_status' => ($this->input->post('search_insurance_status', TRUE) != '') ? implode(',', $this->input->post('search_insurance_status', TRUE)) : ''
						)
				);
				
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_insurance') . ' - ' .$this->lang->line('hf_claim_insurance_history');
			
				$this->load->view('insurance_claim_history', $_POST);
			}
			else {
				$this->session->unset_userdata(array('member_search_username' => '', 'member_exclude_downline' => '', 'member_search_insurance_date' => '', 'member_search_start_date' => '', 'member_search_end_date' => '', 'member_search_insurance_status' => ''));
				
				$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_insurance') . ' - ' .$this->lang->line('hf_claim_insurance_history');
			
				$this->load->view('insurance_claim_history', $data);
			}
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function getclaimlist()
    {
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$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') . '.member_transaction_id',
								$this->db->dbprefix('member_claim_insurances') . '.transaction_date',
								$this->db->dbprefix('member_claim_insurances') . '.claim_amount',
								$this->db->dbprefix('member_claim_insurances') . '.from_member_username',
								$this->db->dbprefix('member_claim_insurances') . '.claim_slip_1',
								$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'
							);
							
			$jColumns = $this->db->dbprefix('member_claim_insurances') . '.from_member_id = ' . $this->db->dbprefix('members') . '.member_id';
			
			// DB table to use
			$sTable = 'member_claim_insurances';
			$jTable = 'members';
			//
			
			if($this->session->userdata('member_search_insurance_date') != '') {
				if($this->session->userdata('member_search_insurance_date') == 2) {
					$start_date = strtotime(date('Y-m-d 00:00:00', strtotime($this->session->userdata('member_search_start_date'))));
					$end_date = strtotime(date('Y-m-d 23:59:59', strtotime($this->session->userdata('member_search_end_date'))));
					$where = "(" . $this->db->dbprefix('member_claim_insurances') . ".transaction_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_claim_insurances') . ".transaction_date <= '" .  $end_date . "')";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_search_username') != '') {
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username = ? LIMIT 1", array($this->session->userdata('member_search_username')));
				if($query->num_rows > 0) {
					$row = $query->row();
					$where = $this->db->dbprefix('members') . ".introducer_parent_ref LIKE '%," .  $row->member_id . ",%'";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_exclude_downline') != '') {
				$arr = explode(',', $this->session->userdata('member_exclude_downline'));
				$arr = implode("','", $arr);
				
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username IN('" . $arr . "')");
				if($query->num_rows > 0) {
					$where = '';
					foreach($query->result() as $row) {
						if($where != '') {
							$where .= " AND (" . $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%' AND " . $this->db->dbprefix('members') . ".member_id != " . $row->member_id . ")";
						}
						else {
							$where .= "(" . $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%' AND " . $this->db->dbprefix('members') . ".member_id != " . $row->member_id . ")";
						}
					}
					
					if($where != '') {
						$where = "(" . $where . ")";
						$this->db->where($where);
					}
				}
			}
			
			if($this->session->userdata('member_search_insurance_status') != '') {
				$where = $this->db->dbprefix('member_claim_insurances') . ".status IN(" .  $this->session->userdata('member_search_insurance_status') . ")";
				$this->db->where($where);
			}
			else {
				$where = $this->db->dbprefix('member_claim_insurances') . ".status = 0";
				$this->db->where($where);
			}
			
			$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], $sSearch);
						// $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);
			$this->db->join($jTable, $jColumns, 'left');
			$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();
				
				if($aRow['status'] == 0) {
					$row[] = '<input type="checkbox" name="chkbox[]" value="' . $aRow['member_transaction_id'] . '" onchange="checkOrUncheck(this);" />';
				}
				else {
					$row[] = '';
				}
				
				$row[] = date('Y-m-d H:i:s', $aRow['transaction_date']);
				$row[] = number_format($aRow['claim_amount'], 2, '.', '');
				$row[] = $aRow['from_member_username'];
				
				$claim_slip = '';
				$claim_slip .= ($aRow['claim_slip_1'] != '') ? '<a href="' . str_replace(BO_FOLDER, MEMBER_FOLDER, base_url()) . 'uploads/claims/' . $aRow['claim_slip_1'] . '" target="_blank"><img src="' . str_replace(BO_FOLDER, MEMBER_FOLDER, base_url()) . 'uploads/claims/' . $aRow['claim_slip_1'] . '" border="0" width="36" /></a> &nbsp; ' : '';
				$claim_slip .= ($aRow['claim_slip_2'] != '') ? '<a href="' . str_replace(BO_FOLDER, MEMBER_FOLDER, base_url()) . 'uploads/claims/' . $aRow['claim_slip_2'] . '" target="_blank"><img src="' . str_replace(BO_FOLDER, MEMBER_FOLDER, base_url()) . 'uploads/claims/' . $aRow['claim_slip_2'] . '" border="0" width="36" /></a> &nbsp; ' : '';
				$claim_slip .= ($aRow['claim_slip_3'] != '') ? '<a href="' . str_replace(BO_FOLDER, MEMBER_FOLDER, base_url()) . 'uploads/claims/' . $aRow['claim_slip_3'] . '" target="_blank"><img src="' . str_replace(BO_FOLDER, MEMBER_FOLDER, base_url()) . 'uploads/claims/' . $aRow['claim_slip_3'] . '" border="0" width="36" /></a> &nbsp; ' : '';
				$claim_slip .= ($aRow['claim_slip_4'] != '') ? '<a href="' . str_replace(BO_FOLDER, MEMBER_FOLDER, base_url()) . 'uploads/claims/' . $aRow['claim_slip_4'] . '" target="_blank"><img src="' . str_replace(BO_FOLDER, MEMBER_FOLDER, base_url()) . 'uploads/claims/' . $aRow['claim_slip_4'] . '" border="0" width="36" /></a> &nbsp; ' : '';
				$claim_slip .= ($aRow['claim_slip_5'] != '') ? '<a href="' . str_replace(BO_FOLDER, MEMBER_FOLDER, base_url()) . 'uploads/claims/' . $aRow['claim_slip_5'] . '" target="_blank"><img src="' . str_replace(BO_FOLDER, MEMBER_FOLDER, base_url()) . 'uploads/claims/' . $aRow['claim_slip_5'] . '" border="0" width="36" /></a>' : '';
				$row[] = $claim_slip;
				
				switch($aRow['status']) {
					case 0: $row[] = '<span class="label label-info">' . $this->lang->line('insurance_pending') . '</span>'; break;
					case 1: $row[] = '<span class="label label-success">' . $this->lang->line('insurance_confirmed') . '</span>'; break;
					case 2: $row[] = '<span class="label label-important">' . $this->lang->line('insurance_cancelled') . '</span>'; break;
					default: $row[] = '<span class="label label-info">' . $this->lang->line('insurance_pending') . '</span>'; break;
				}
				
				if($aRow['status'] == 0) {
					$row[] = '<select id="update_status" name="update_status" class="small m-wrap" onchange="update_claim_status(' . $aRow['member_transaction_id'] . ', this.value)">
								<option value="0">' . $this->lang->line('withdrawal_pending_withdrawal') . '</option>
								<option value="1">' . $this->lang->line('withdrawal_approve_withdrawal') . '</option>
								<option value="2">' . $this->lang->line('withdrawal_cancel_withdrawal') . '</option>
							</select>';
				}
				else {
					$row[] = '';
				}
				
				$output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}	
    }
	
	public function claim_insurance_export()
	{
		set_time_limit(0);
		
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->lang->load('account_general', get_language());
			
			require_once APPPATH."/third_party/php-excel.class.php";
			
			$data[] = array (
								$this->lang->line('insurance_claim_date'),
								$this->lang->line('insurance_amount'),
								$this->lang->line('insurance_from_member'),
								$this->lang->line('insurance_claim_slip') . ' 1',
								$this->lang->line('insurance_claim_slip') . ' 2',
								$this->lang->line('insurance_claim_slip') . ' 3',
								$this->lang->line('insurance_claim_slip') . ' 4',
								$this->lang->line('insurance_claim_slip') . ' 5',
								$this->lang->line('insurance_status')
							);
			
			//Get Data
			$aColumns = array(
								$this->db->dbprefix('member_claim_insurances') . '.member_transaction_id',
								$this->db->dbprefix('member_claim_insurances') . '.transaction_date',
								$this->db->dbprefix('member_claim_insurances') . '.claim_amount',
								$this->db->dbprefix('member_claim_insurances') . '.from_member_username',
								$this->db->dbprefix('member_claim_insurances') . '.claim_slip_1',
								$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'
							);
							
			$jColumns = $this->db->dbprefix('member_claim_insurances') . '.from_member_id = ' . $this->db->dbprefix('members') . '.member_id';
			
			// DB table to use
			$sTable = 'member_claim_insurances';
			$jTable = 'members';
			//
			
			if($this->session->userdata('member_search_insurance_date') != '') {
				if($this->session->userdata('member_search_insurance_date') == 2) {
					$start_date = strtotime(date('Y-m-d 00:00:00', strtotime($this->session->userdata('member_search_start_date'))));
					$end_date = strtotime(date('Y-m-d 23:59:59', strtotime($this->session->userdata('member_search_end_date'))));
					$where = "(" . $this->db->dbprefix('member_claim_insurances') . ".transaction_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_claim_insurances') . ".transaction_date <= '" .  $end_date . "')";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_search_username') != '') {
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username = ? LIMIT 1", array($this->session->userdata('member_search_username')));
				if($query->num_rows > 0) {
					$row = $query->row();
					$where = $this->db->dbprefix('members') . ".introducer_parent_ref LIKE '%," .  $row->member_id . ",%'";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_exclude_downline') != '') {
				$arr = explode(',', $this->session->userdata('member_exclude_downline'));
				$arr = implode("','", $arr);
				
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username IN('" . $arr . "')");
				if($query->num_rows > 0) {
					$where = '';
					foreach($query->result() as $row) {
						if($where != '') {
							$where .= " AND (" . $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%' AND " . $this->db->dbprefix('members') . ".member_id != " . $row->member_id . ")";
						}
						else {
							$where .= "(" . $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%' AND " . $this->db->dbprefix('members') . ".member_id != " . $row->member_id . ")";
						}
					}
					
					if($where != '') {
						$where = "(" . $where . ")";
						$this->db->where($where);
					}
				}
			}
			
			if($this->session->userdata('member_search_insurance_status') != '') {
				$where = $this->db->dbprefix('member_claim_insurances') . ".status IN(" .  $this->session->userdata('member_search_insurance_status') . ")";
				$this->db->where($where);
			}
			
			$this->db->order_by( $this->db->dbprefix('member_claim_insurances') . '.transaction_date', 'asc');
			
			$this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			$this->db->join($jTable, $jColumns, 'left');
			$rResult = $this->db->get($sTable);
			
			foreach($rResult->result_array() as $aRow)
			{
				$status = '';
				switch($aRow['status']) {
					case 0: $status = $this->lang->line('insurance_pending'); break;
					case 1: $status = $this->lang->line('insurance_confirmed'); break;
					case 2: $status = $this->lang->line('insurance_cancelled'); break;
					default: $status = $this->lang->line('insurance_pending'); break;
				}
				
				$data[] = array (
									date('Y-m-d H:i:s', $aRow['transaction_date']),
									number_format($aRow['claim_amount'], 2, '.', ''),
									$aRow['from_member_username'],
									($aRow['claim_slip_1'] != '') ? str_replace(BO_FOLDER, MEMBER_FOLDER, base_url()) . 'uploads/claims/' . $aRow['claim_slip_1'] : '-',
									($aRow['claim_slip_2'] != '') ? str_replace(BO_FOLDER, MEMBER_FOLDER, base_url()) . 'uploads/claims/' . $aRow['claim_slip_2'] : '-',
									($aRow['claim_slip_3'] != '') ? str_replace(BO_FOLDER, MEMBER_FOLDER, base_url()) . 'uploads/claims/' . $aRow['claim_slip_3'] : '-',
									($aRow['claim_slip_4'] != '') ? str_replace(BO_FOLDER, MEMBER_FOLDER, base_url()) . 'uploads/claims/' . $aRow['claim_slip_4'] : '-',
									($aRow['claim_slip_5'] != '') ? str_replace(BO_FOLDER, MEMBER_FOLDER, base_url()) . 'uploads/claims/' . $aRow['claim_slip_5'] : '-',
									$status
								);
			}
					
			$xls = new Excel_XML('UTF-8', false, 'Member Claim Insurance List');
			$xls->addArray($data);
			$xls->generateXML('member_claim_insurance_list');
		}
	}
	
	public function update_claim($transaction_id = NULL, $status = NULL)
	{
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->lang->load('account_general', get_language());
			
			$this->load->model('user_model');
			
			$rs = $this->user_model->update_claim_status($transaction_id, $status);
				
			redirect('insurance/claim_listing/', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function claim_listing_update()
	{
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->lang->load('account_general', get_language());
			
			$this->load->model('user_model');
			
			#Recursive deletion
			if(isset($_POST['chkbox'])) {
				for($i=0;$i<sizeof($_POST['chkbox']);$i++) {
					if($_POST['chkbox'][$i] != 'on') {
						$this->user_model->update_claim_status(trim($_POST['chkbox'][$i]), trim($_POST['update_status']));
					}
				}
			}
			
			redirect('insurance/claim_listing/', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function platform_listing_1()
	{
		save_current_url(base_url() . 'insurance/platform_listing_1/');
		
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->lang->load('account_general', get_language());
			
			if($_POST) {
				$this->session->set_userdata(
					array(
						'member_search_username' => trim($this->input->post('search_username', TRUE)), 
						'member_search_withdrawal_date' => trim($this->input->post('search_withdrawal_date', TRUE)), 
						'member_search_start_date' => trim($this->input->post('search_start_date', TRUE)), 
						'member_search_end_date' => trim($this->input->post('search_end_date', TRUE)), 
						'member_search_withdrawal_status' => ($this->input->post('search_withdrawal_status', TRUE) != '') ? implode(',', $this->input->post('search_withdrawal_status', TRUE)) : ''
						)
				);
				
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_insurance') . ' - ' .$this->lang->line('hf_platform_miss_bet_history_1');
			
				$this->load->view('insurance_miss_bet_history_platform_1', $_POST);
			}
			else {
				$this->session->unset_userdata(array('member_search_username' => '', 'member_search_withdrawal_date' => '', 'member_search_start_date' => '', 'member_search_end_date' => '', 'member_search_withdrawal_status' => ''));
				
				$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_insurance') . ' - ' .$this->lang->line('hf_platform_miss_bet_history_1');
			
				$this->load->view('insurance_miss_bet_history_platform_1', $data);
			}
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function get_platform_list_1()
    {
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$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_miss_bets') . '.member_transaction_id',
								$this->db->dbprefix('member_miss_bets') . '.transaction_date',
								$this->db->dbprefix('member_miss_bets') . '.insurance_expiry_date',
								$this->db->dbprefix('member_miss_bets') . '.from_member_username',
								$this->db->dbprefix('member_miss_bets') . '.v_unit6',
								$this->db->dbprefix('member_miss_bets') . '.v_unit7',
								$this->db->dbprefix('member_miss_bets') . '.v_unit8',
								$this->db->dbprefix('member_miss_bets') . '.status',
								$this->db->dbprefix('member_miss_bets') . '.member_transaction_id',
								$this->db->dbprefix('member_miss_bets') . '.from_member_id',
							);
			
			$jColumns = $this->db->dbprefix('member_miss_bets') . '.from_member_id = ' . $this->db->dbprefix('members') . '.member_id';
			
			// DB table to use
			$sTable = 'sw_member_miss_bets';
			$jTable = 'sw_members';
			//
			
			if($this->session->userdata('member_search_withdrawal_date') != '') {
				if($this->session->userdata('member_search_withdrawal_date') == 2) {
					$start_date = strtotime(date('Y-m-d 00:00:00', strtotime($this->session->userdata('member_search_start_date'))));
					$end_date = strtotime(date('Y-m-d 23:59:59', strtotime($this->session->userdata('member_search_end_date'))));
					$where = "(" . $this->db->dbprefix('member_miss_bets') . ".transaction_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_miss_bets') . ".transaction_date <= '" .  $end_date . "')";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_search_username') != '') {
				$where = $this->db->dbprefix('member_miss_bets') . ".from_member_username = '" . $this->session->userdata('member_search_username') . "'";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_withdrawal_status') != '') {
				$where = $this->db->dbprefix('member_miss_bets') . ".status IN(" .  $this->session->userdata('member_search_withdrawal_status') . ")";
				$this->db->where($where);
			}
			
			$this->db->where($this->db->dbprefix('members') . '.allowed_game', 1);
			
			$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);
			$this->db->join($jTable, $jColumns);
			$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();
				
				if($aRow['status'] == 0) {
					$row[] = '<input type="checkbox" name="chkbox[]" value="' . $aRow['member_transaction_id'] . '" onchange="checkOrUncheck(this);" />';
				}
				else {
					$row[] = '';
				}
				
				$row[] = date('Y-m-d H:i:s', $aRow['transaction_date']);
				$row[] = date('Y-m-d H:i:s', $aRow['insurance_expiry_date']);
				$row[] = $aRow['from_member_username'];
				$row[] = number_format($aRow['v_unit6'], 2, '.', ',');
				$row[] = number_format($aRow['v_unit7'], 2, '.', ',');
				$row[] = number_format($aRow['v_unit8'], 2, '.', ',');
				
				switch($aRow['status']) {
					case 0: $row[] = '<span class="label label-info">' . $this->lang->line('withdrawal_pending') . '</span>'; break;
					case 1: $row[] = '<span class="label label-success">' . $this->lang->line('withdrawal_confirmed') . '</span>'; break;
					case 2: $row[] = '<span class="label label-important">' . $this->lang->line('withdrawal_cancelled') . '</span>'; break;
					default: $row[] = '<span class="label label-info">' . $this->lang->line('withdrawal_pending') . '</span>'; break;
				}
				
				if($aRow['status'] == 0) {
					$row[] = '<select id="update_status" name="update_status" class="small m-wrap" onchange="update_missbet_status_platform_1(' . $aRow['member_transaction_id'] . ', this.value)">
								<option value="0">' . $this->lang->line('withdrawal_pending_withdrawal') . '</option>
								<option value="1">' . $this->lang->line('withdrawal_approve_withdrawal') . '</option>
								<option value="2">' . $this->lang->line('withdrawal_cancel_withdrawal') . '</option>
							</select>';
					$row[] = '<a href="' . base_url() . 'insurance/update_freeze_status/' . $aRow['from_member_id'] . '/">' . $this->lang->line('game_freeze') . '</a> &nbsp;|&nbsp; <a href="' . base_url() . 'insurance/bet_history/' . $aRow['member_transaction_id'] . '/" target="_blank">' . $this->lang->line('hf_platform_bet_history') . '</a>';	
				}
				else {
					$row[] = '';
					$row[] = '<a href="' . base_url() . 'insurance/bet_history/' . $aRow['member_transaction_id'] . '/" target="_blank">' . $this->lang->line('hf_platform_bet_history') . '</a>';
				}
				
				$output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}	
    }
	
	public function missbet_platform_export_1()
	{
		set_time_limit(0);
		
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->lang->load('account_general', get_language());
			
			require_once APPPATH."/third_party/php-excel.class.php";
			
			$data[] = array (
								$this->lang->line('insurance_claim_date'),
								$this->lang->line('insurance_expiry_date'),
								$this->lang->line('insurance_from_member'),
								$this->lang->line('wallet_6'),
								$this->lang->line('wallet_7'),
								$this->lang->line('wallet_8'),
								$this->lang->line('insurance_status')
							);
			
			$aColumns = array(
								$this->db->dbprefix('member_miss_bets') . '.transaction_date',
								$this->db->dbprefix('member_miss_bets') . '.insurance_expiry_date',
								$this->db->dbprefix('member_miss_bets') . '.from_member_username',
								$this->db->dbprefix('member_miss_bets') . '.v_unit6',
								$this->db->dbprefix('member_miss_bets') . '.v_unit7',
								$this->db->dbprefix('member_miss_bets') . '.v_unit8',
								$this->db->dbprefix('member_miss_bets') . '.status',
							);
			
			$jColumns = $this->db->dbprefix('member_miss_bets') . '.from_member_id = ' . $this->db->dbprefix('members') . '.member_id';
			
			// DB table to use
			$sTable = 'sw_member_miss_bets';
			$jTable = 'sw_members';
			//
			
			if($this->session->userdata('member_search_withdrawal_date') != '') {
				if($this->session->userdata('member_search_withdrawal_date') == 2) {
					$start_date = strtotime(date('Y-m-d 00:00:00', strtotime($this->session->userdata('member_search_start_date'))));
					$end_date = strtotime(date('Y-m-d 23:59:59', strtotime($this->session->userdata('member_search_end_date'))));
					$where = "(" . $this->db->dbprefix('member_miss_bets') . ".transaction_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_miss_bets') . ".transaction_date <= '" .  $end_date . "')";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_search_username') != '') {
				$where = $this->db->dbprefix('member_miss_bets') . ".from_member_username = '" . $this->session->userdata('member_search_username') . "'";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_withdrawal_status') != '') {
				$where = $this->db->dbprefix('member_miss_bets') . ".status IN(" .  $this->session->userdata('member_search_withdrawal_status') . ")";
				$this->db->where($where);
			}
			
			$this->db->where($this->db->dbprefix('members') . '.allowed_game', 1);
			$this->db->order_by($this->db->dbprefix('member_miss_bets') . '.transaction_date', 'asc');
			
			$this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			$this->db->join($jTable, $jColumns);
			$rResult = $this->db->get($sTable);
			
			foreach($rResult->result_array() as $aRow)
			{
				$status = '';
				switch($aRow['status']) {
					case 0: $status = $this->lang->line('withdrawal_pending'); break;
					case 1: $status = $this->lang->line('withdrawal_confirmed'); break;
					case 2: $status = $this->lang->line('withdrawal_cancelled'); break;
					default: $status = $this->lang->line('withdrawal_pending'); break;
				}
				
				$data[] = array (
									date('Y-m-d H:i:s', $aRow['transaction_date']),
									date('Y-m-d H:i:s', $aRow['insurance_expiry_date']),
									$aRow['from_member_username'],
									number_format($aRow['v_unit6'], 2, '.', ','),
									number_format($aRow['v_unit7'], 2, '.', ','),
									number_format($aRow['v_unit8'], 2, '.', ','),
									$status
								);
			}
					
			$xls = new Excel_XML('UTF-8', false, 'Missbet List');
			$xls->addArray($data);
			$xls->generateXML('missbet_list');
		}
	}
	
	public function update_missbet_platform_1($transaction_id = NULL, $status = NULL)
	{
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->lang->load('account_general', get_language());
			
			$this->load->model('user_model');
			
			$rs = $this->user_model->update_missbet_status($transaction_id, $status);
				
			redirect('insurance/platform_listing_1/', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function listing_update_platform_1()
	{
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->lang->load('account_general', get_language());
			
			$this->load->model('user_model');
			
			#Recursive deletion
			if(isset($_POST['chkbox'])) {
				for($i=0;$i<sizeof($_POST['chkbox']);$i++) {
					if($_POST['chkbox'][$i] != 'on') {
						$this->user_model->update_missbet_status(trim($_POST['chkbox'][$i]), trim($_POST['update_status']));
					}
				}
			}
			
			redirect('insurance/platform_listing_1/', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function platform_listing_2()
	{
		save_current_url(base_url() . 'insurance/platform_listing_2/');
		
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->lang->load('account_general', get_language());
			
			if($_POST) {
				$this->session->set_userdata(
					array(
						'member_search_username' => trim($this->input->post('search_username', TRUE)), 
						'member_search_withdrawal_date' => trim($this->input->post('search_withdrawal_date', TRUE)), 
						'member_search_start_date' => trim($this->input->post('search_start_date', TRUE)), 
						'member_search_end_date' => trim($this->input->post('search_end_date', TRUE)), 
						'member_search_withdrawal_status' => ($this->input->post('search_withdrawal_status', TRUE) != '') ? implode(',', $this->input->post('search_withdrawal_status', TRUE)) : ''
						)
				);
				
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_insurance') . ' - ' .$this->lang->line('hf_platform_miss_bet_history_2');
			
				$this->load->view('insurance_miss_bet_history_platform_2', $_POST);
			}
			else {
				$this->session->unset_userdata(array('member_search_username' => '', 'member_search_withdrawal_date' => '', 'member_search_start_date' => '', 'member_search_end_date' => '', 'member_search_withdrawal_status' => ''));
				
				$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_insurance') . ' - ' .$this->lang->line('hf_platform_miss_bet_history_2');
			
				$this->load->view('insurance_miss_bet_history_platform_2', $data);
			}
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function get_platform_list_2()
    {
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$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_miss_bets') . '.member_transaction_id',
								$this->db->dbprefix('member_miss_bets') . '.transaction_date',
								$this->db->dbprefix('member_miss_bets') . '.insurance_expiry_date',
								$this->db->dbprefix('member_miss_bets') . '.from_member_username',
								$this->db->dbprefix('member_miss_bets') . '.v_unit6',
								$this->db->dbprefix('member_miss_bets') . '.v_unit7',
								$this->db->dbprefix('member_miss_bets') . '.v_unit8',
								$this->db->dbprefix('member_miss_bets') . '.status',
								$this->db->dbprefix('member_miss_bets') . '.member_transaction_id',
								$this->db->dbprefix('member_miss_bets') . '.from_member_id',
							);
			
			$jColumns = $this->db->dbprefix('member_miss_bets') . '.from_member_id = ' . $this->db->dbprefix('members') . '.member_id';
			
			// DB table to use
			$sTable = 'sw_member_miss_bets';
			$jTable = 'sw_members';
			//
			
			if($this->session->userdata('member_search_withdrawal_date') != '') {
				if($this->session->userdata('member_search_withdrawal_date') == 2) {
					$start_date = strtotime(date('Y-m-d 00:00:00', strtotime($this->session->userdata('member_search_start_date'))));
					$end_date = strtotime(date('Y-m-d 23:59:59', strtotime($this->session->userdata('member_search_end_date'))));
					$where = "(" . $this->db->dbprefix('member_miss_bets') . ".transaction_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_miss_bets') . ".transaction_date <= '" .  $end_date . "')";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_search_username') != '') {
				$where = $this->db->dbprefix('member_miss_bets') . ".from_member_username = '" . $this->session->userdata('member_search_username') . "'";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_withdrawal_status') != '') {
				$where = $this->db->dbprefix('member_miss_bets') . ".status IN(" .  $this->session->userdata('member_search_withdrawal_status') . ")";
				$this->db->where($where);
			}
			
			$this->db->where($this->db->dbprefix('members') . '.allowed_game', 0);
			
			$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);
			$this->db->join($jTable, $jColumns);
			$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();
				
				if($aRow['status'] == 0) {
					$row[] = '<input type="checkbox" name="chkbox[]" value="' . $aRow['member_transaction_id'] . '" onchange="checkOrUncheck(this);" />';
				}
				else {
					$row[] = '';
				}
				
				$row[] = date('Y-m-d H:i:s', $aRow['transaction_date']);
				$row[] = date('Y-m-d H:i:s', $aRow['insurance_expiry_date']);
				$row[] = $aRow['from_member_username'];
				$row[] = number_format($aRow['v_unit6'], 2, '.', ',');
				$row[] = number_format($aRow['v_unit7'], 2, '.', ',');
				$row[] = number_format($aRow['v_unit8'], 2, '.', ',');
				
				switch($aRow['status']) {
					case 0: $row[] = '<span class="label label-info">' . $this->lang->line('withdrawal_pending') . '</span>'; break;
					case 1: $row[] = '<span class="label label-success">' . $this->lang->line('withdrawal_confirmed') . '</span>'; break;
					case 2: $row[] = '<span class="label label-important">' . $this->lang->line('withdrawal_cancelled') . '</span>'; break;
					default: $row[] = '<span class="label label-info">' . $this->lang->line('withdrawal_pending') . '</span>'; break;
				}
				
				if($aRow['status'] == 0) {
					$row[] = '<select id="update_status" name="update_status" class="small m-wrap" onchange="update_missbet_status_platform_2(' . $aRow['member_transaction_id'] . ', this.value)">
								<option value="0">' . $this->lang->line('withdrawal_pending_withdrawal') . '</option>
								<option value="1">' . $this->lang->line('withdrawal_approve_withdrawal') . '</option>
								<option value="2">' . $this->lang->line('withdrawal_cancel_withdrawal') . '</option>
							</select>';
					$row[] = '<a href="' . base_url() . 'insurance/update_unfreeze_status/' . $aRow['from_member_id'] . '/">' . $this->lang->line('game_unfreeze') . '</a> &nbsp;|&nbsp; <a href="' . base_url() . 'insurance/bet_history/' . $aRow['member_transaction_id'] . '/" target="_blank">' . $this->lang->line('hf_platform_bet_history') . '</a>';	
				}
				else {
					$row[] = '';
					$row[] = '<a href="' . base_url() . 'insurance/bet_history/' . $aRow['member_transaction_id'] . '/" target="_blank">' . $this->lang->line('hf_platform_bet_history') . '</a>';
				}
				
				$output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}	
    }
	
	public function missbet_platform_export_2()
	{
		set_time_limit(0);
		
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->lang->load('account_general', get_language());
			
			require_once APPPATH."/third_party/php-excel.class.php";
			
			$data[] = array (
								$this->lang->line('insurance_claim_date'),
								$this->lang->line('insurance_expiry_date'),
								$this->lang->line('insurance_from_member'),
								$this->lang->line('wallet_6'),
								$this->lang->line('wallet_7'),
								$this->lang->line('wallet_8'),
								$this->lang->line('insurance_status')
							);
			
			$aColumns = array(
								$this->db->dbprefix('member_miss_bets') . '.transaction_date',
								$this->db->dbprefix('member_miss_bets') . '.insurance_expiry_date',
								$this->db->dbprefix('member_miss_bets') . '.from_member_username',
								$this->db->dbprefix('member_miss_bets') . '.v_unit6',
								$this->db->dbprefix('member_miss_bets') . '.v_unit7',
								$this->db->dbprefix('member_miss_bets') . '.v_unit8',
								$this->db->dbprefix('member_miss_bets') . '.status',
							);
			
			$jColumns = $this->db->dbprefix('member_miss_bets') . '.from_member_id = ' . $this->db->dbprefix('members') . '.member_id';
			
			// DB table to use
			$sTable = 'sw_member_miss_bets';
			$jTable = 'sw_members';
			//
			
			if($this->session->userdata('member_search_withdrawal_date') != '') {
				if($this->session->userdata('member_search_withdrawal_date') == 2) {
					$start_date = strtotime(date('Y-m-d 00:00:00', strtotime($this->session->userdata('member_search_start_date'))));
					$end_date = strtotime(date('Y-m-d 23:59:59', strtotime($this->session->userdata('member_search_end_date'))));
					$where = "(" . $this->db->dbprefix('member_miss_bets') . ".transaction_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_miss_bets') . ".transaction_date <= '" .  $end_date . "')";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_search_username') != '') {
				$where = $this->db->dbprefix('member_miss_bets') . ".from_member_username = '" . $this->session->userdata('member_search_username') . "'";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_withdrawal_status') != '') {
				$where = $this->db->dbprefix('member_miss_bets') . ".status IN(" .  $this->session->userdata('member_search_withdrawal_status') . ")";
				$this->db->where($where);
			}
			
			$this->db->where($this->db->dbprefix('members') . '.allowed_game', 0);
			$this->db->order_by($this->db->dbprefix('member_miss_bets') . '.transaction_date', 'asc');
			
			$this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			$this->db->join($jTable, $jColumns);
			$rResult = $this->db->get($sTable);
			
			foreach($rResult->result_array() as $aRow)
			{
				$status = '';
				switch($aRow['status']) {
					case 0: $status = $this->lang->line('withdrawal_pending'); break;
					case 1: $status = $this->lang->line('withdrawal_confirmed'); break;
					case 2: $status = $this->lang->line('withdrawal_cancelled'); break;
					default: $status = $this->lang->line('withdrawal_pending'); break;
				}
				
				$data[] = array (
									date('Y-m-d H:i:s', $aRow['transaction_date']),
									date('Y-m-d H:i:s', $aRow['insurance_expiry_date']),
									$aRow['from_member_username'],
									number_format($aRow['v_unit6'], 2, '.', ','),
									number_format($aRow['v_unit7'], 2, '.', ','),
									number_format($aRow['v_unit8'], 2, '.', ','),
									$status
								);
			}
					
			$xls = new Excel_XML('UTF-8', false, 'Missbet List');
			$xls->addArray($data);
			$xls->generateXML('missbet_list');
		}
	}
	
	public function update_missbet_platform_2($transaction_id = NULL, $status = NULL)
	{
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->lang->load('account_general', get_language());
			
			$this->load->model('user_model');
			
			$rs = $this->user_model->update_missbet_status($transaction_id, $status);
				
			redirect('insurance/platform_listing_2/', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function listing_update_platform_2()
	{
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->lang->load('account_general', get_language());
			
			$this->load->model('user_model');
			
			#Recursive deletion
			if(isset($_POST['chkbox'])) {
				for($i=0;$i<sizeof($_POST['chkbox']);$i++) {
					if($_POST['chkbox'][$i] != 'on') {
						$this->user_model->update_missbet_status(trim($_POST['chkbox'][$i]), trim($_POST['update_status']));
					}
				}
			}
			
			redirect('insurance/platform_listing_2/', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function update_freeze_status($member_id = NULL)
	{
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->lang->load('account_general', get_language());
			
			$this->load->model('user_model');
			
			$rs = $this->user_model->update_freeze_status($member_id, 0);
				
			redirect('insurance/platform_listing_1/', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function update_unfreeze_status($member_id = NULL)
	{
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->lang->load('account_general', get_language());
			
			$this->load->model('user_model');
			
			$rs = $this->user_model->update_freeze_status($member_id, 1);
				
			redirect('insurance/platform_listing_2/', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function bet_history($member_transaction_id = NULL)
	{
		save_current_url(base_url() . 'insurance/bet_history/' . $member_transaction_id . '/');
		
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->lang->load('account_general', get_language());
			
			$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_game') . ' - ' . $this->lang->line('hf_platform_bet_history');
			
			$this->load->view('game_bet_history', $data);
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function get_bet_list_1($member_transaction_id = NULL)
    {
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->lang->load('account_general', get_language());
			
			$from_member_id = 0;
			$miss_bet_date = 0;
			$query = $this->db->query("SELECT from_member_id, transaction_date FROM sw_member_miss_bets WHERE member_transaction_id = ? LIMIT 1", array($member_transaction_id));
			if($query->num_rows() > 0) {
				$row = $query->row();
				$from_member_id = $row->from_member_id;
				$miss_bet_date = $row->transaction_date;
			}
			
			$withdrawal_id = 0;
			$query = $this->db->query("SELECT member_transaction_id FROM sw_member_game_withdrawals WHERE from_member_id = ? AND deposit_date < ? ORDER BY member_transaction_id DESC LIMIT 1", array($from_member_id, $miss_bet_date));
			if($query->num_rows() > 0) {
				$row = $query->row();
				$withdrawal_id = $row->member_transaction_id;
			}
			
			$casino_player_id = 0;
			$start_date = 0;
			$end_date = time();
			$query = $this->db->query("SELECT a.deposit_date, b.casino_plyid_1 FROM sw_member_game_withdrawals a, sw_members b WHERE (a.from_member_id = b.member_id) AND a.member_transaction_id = ? LIMIT 1", array($withdrawal_id));
			if($query->num_rows() > 0) {
				$row = $query->row();
				$start_date = $row->deposit_date;
				$casino_player_id = $row->casino_plyid_1;
			}
			
			$query = $this->db->query("SELECT a.deposit_date FROM sw_member_game_withdrawals a, sw_members b WHERE (a.from_member_id = b.member_id) AND b.casino_plyid_1 = ? AND a.deposit_date > ? ORDER BY a.member_transaction_id ASC LIMIT 1", array($casino_player_id, $start_date));
			if($query->num_rows() > 0) {
				$row = $query->row();
				$end_date = $row->deposit_date;
			}
			
			$otherdb = $this->load->database('otherdb1', TRUE);
		
			/* 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(
								$otherdb->dbprefix('transaction_report') . '.bet_time',
								$otherdb->dbprefix('transaction_report') . '.payout_time',
								$otherdb->dbprefix('transaction_report') . '.bet_id',
								$otherdb->dbprefix('transaction_report') . '.game_provider_code',
								$otherdb->dbprefix('transaction_report') . '.game_type_code',
								$otherdb->dbprefix('transaction_report') . '.bet_amount_valid',
								$otherdb->dbprefix('transaction_report') . '.win_loss',
								$otherdb->dbprefix('transaction_report') . '.bet_code',
								$otherdb->dbprefix('transaction_report') . '.game_result',
								$otherdb->dbprefix('transaction_report') . '.game_username',
								$otherdb->dbprefix('transaction_report') . '.game_real_code',
							);
			
			// DB table to use
			$sTable = 'bctp_transaction_report';
			//
			
			$where = "(" . $otherdb->dbprefix('transaction_report') . ".payout_time >= '" .  $start_date . "' AND " . $otherdb->dbprefix('transaction_report') . ".payout_time < '" .  $end_date . "')";
			$otherdb->where($where);
			
			$otherdb->where($otherdb->dbprefix('transaction_report') . '.player_id', $casino_player_id);
			$otherdb->where($otherdb->dbprefix('transaction_report') . '.status', 1);
			// $otherdb->where($otherdb->dbprefix('transaction_report') . '.win_loss != 0');
			
			$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')
			{
				$otherdb->limit($otherdb->escape_str($iDisplayLength), $otherdb->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')
					{
						$otherdb->order_by($aColumns[intval($otherdb->escape_str($iSortCol))], $otherdb->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')
					{
						$otherdb->or_like($aColumns[$i], $otherdb->escape_like_str($sSearch));
					}
				}
			}
			
			// Select Data
			$otherdb->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			$rResult = $otherdb->get($sTable);
		
			// Data set length after filtering
			$otherdb->select('FOUND_ROWS() AS found_rows');
			$iFilteredTotal = $otherdb->get()->row()->found_rows;
		
			// Total data set length
			$iTotal = $otherdb->count_all($sTable);
		
			// Output
			$output = array(
				'sEcho' => intval($sEcho),
				'iTotalRecords' => $iTotal,
				'iTotalDisplayRecords' => $iFilteredTotal,
				'aaData' => array()
			);
			
			foreach($rResult->result_array() as $aRow)
			{
				$row = array();
				
				$row[] = date('Y-m-d H:i:s', $aRow['bet_time']);
				$row[] = date('Y-m-d H:i:s', $aRow['payout_time']);
				$row[] = $aRow['bet_id'];
				$row[] = $aRow['game_provider_code'];
				$row[] = $aRow['game_type_code'];
				$row[] = number_format($aRow['bet_amount_valid'], 2, '.', '');
				$row[] = number_format($aRow['win_loss'], 2, '.', '');
				$row[] = bet_code_decision($aRow['game_provider_code'], $aRow['game_type_code'], $aRow['game_real_code'], $aRow['bet_code']);
				$row[] = game_result_decision($aRow['game_provider_code'], $aRow['game_type_code'], $aRow['game_real_code'], $aRow['game_result']);
				$row[] = $aRow['game_username'];
				
				$output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}	
    }
	
	public function get_bet_list_2($member_transaction_id = NULL)
    {
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->lang->load('account_general', get_language());
			
			$from_member_id = 0;
			$miss_bet_date = 0;
			$query = $this->db->query("SELECT from_member_id, transaction_date FROM sw_member_miss_bets WHERE member_transaction_id = ? LIMIT 1", array($member_transaction_id));
			if($query->num_rows() > 0) {
				$row = $query->row();
				$from_member_id = $row->from_member_id;
				$miss_bet_date = $row->transaction_date;
			}
			
			$withdrawal_id = 0;
			$query = $this->db->query("SELECT member_transaction_id FROM sw_member_game_withdrawals WHERE from_member_id = ? AND deposit_date < ? ORDER BY member_transaction_id DESC LIMIT 1", array($from_member_id, $miss_bet_date));
			if($query->num_rows() > 0) {
				$row = $query->row();
				$withdrawal_id = $row->member_transaction_id;
			}
			
			$casino_player_id = 0;
			$start_date = 0;
			$end_date = time();
			$query = $this->db->query("SELECT a.deposit_date, b.casino_plyid_2 FROM sw_member_game_withdrawals a, sw_members b WHERE (a.from_member_id = b.member_id) AND a.member_transaction_id = ? LIMIT 1", array($withdrawal_id));
			if($query->num_rows() > 0) {
				$row = $query->row();
				$start_date = $row->deposit_date;
				$casino_player_id = $row->casino_plyid_2;
			}
			
			$query = $this->db->query("SELECT a.deposit_date FROM sw_member_game_withdrawals a, sw_members b WHERE (a.from_member_id = b.member_id) AND b.casino_plyid_2 = ? AND a.deposit_date > ? ORDER BY a.member_transaction_id ASC LIMIT 1", array($casino_player_id, $start_date));
			if($query->num_rows() > 0) {
				$row = $query->row();
				$end_date = $row->deposit_date;
			}
			
			$otherdb = $this->load->database('otherdb2', TRUE);
		
			/* 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(
								$otherdb->dbprefix('transaction_report') . '.bet_time',
								$otherdb->dbprefix('transaction_report') . '.payout_time',
								$otherdb->dbprefix('transaction_report') . '.bet_id',
								$otherdb->dbprefix('transaction_report') . '.game_provider_code',
								$otherdb->dbprefix('transaction_report') . '.game_type_code',
								$otherdb->dbprefix('transaction_report') . '.bet_amount_valid',
								$otherdb->dbprefix('transaction_report') . '.win_loss',
								$otherdb->dbprefix('transaction_report') . '.bet_code',
								$otherdb->dbprefix('transaction_report') . '.game_result',
								$otherdb->dbprefix('transaction_report') . '.game_username',
								$otherdb->dbprefix('transaction_report') . '.game_real_code',
							);
			
			// DB table to use
			$sTable = 'bctp_transaction_report';
			//
			
			$where = "(" . $otherdb->dbprefix('transaction_report') . ".payout_time >= '" .  $start_date . "' AND " . $otherdb->dbprefix('transaction_report') . ".payout_time < '" .  $end_date . "')";
			$otherdb->where($where);
			
			$otherdb->where($otherdb->dbprefix('transaction_report') . '.player_id', $casino_player_id);
			$otherdb->where($otherdb->dbprefix('transaction_report') . '.status', 1);
			// $otherdb->where($otherdb->dbprefix('transaction_report') . '.win_loss != 0');
			
			$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')
			{
				$otherdb->limit($otherdb->escape_str($iDisplayLength), $otherdb->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')
					{
						$otherdb->order_by($aColumns[intval($otherdb->escape_str($iSortCol))], $otherdb->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')
					{
						$otherdb->or_like($aColumns[$i], $otherdb->escape_like_str($sSearch));
					}
				}
			}
			
			// Select Data
			$otherdb->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			$rResult = $otherdb->get($sTable);
		
			// Data set length after filtering
			$otherdb->select('FOUND_ROWS() AS found_rows');
			$iFilteredTotal = $otherdb->get()->row()->found_rows;
		
			// Total data set length
			$iTotal = $otherdb->count_all($sTable);
		
			// Output
			$output = array(
				'sEcho' => intval($sEcho),
				'iTotalRecords' => $iTotal,
				'iTotalDisplayRecords' => $iFilteredTotal,
				'aaData' => array()
			);
			
			foreach($rResult->result_array() as $aRow)
			{
				$row = array();
				
				$row[] = date('Y-m-d H:i:s', $aRow['bet_time']);
				$row[] = date('Y-m-d H:i:s', $aRow['payout_time']);
				$row[] = $aRow['bet_id'];
				$row[] = $aRow['game_provider_code'];
				$row[] = $aRow['game_type_code'];
				$row[] = number_format($aRow['bet_amount_valid'], 2, '.', '');
				$row[] = number_format($aRow['win_loss'], 2, '.', '');
				$row[] = bet_code_decision($aRow['game_provider_code'], $aRow['game_type_code'], $aRow['game_real_code'], $aRow['bet_code']);
				$row[] = game_result_decision($aRow['game_provider_code'], $aRow['game_type_code'], $aRow['game_real_code'], $aRow['game_result']);
				$row[] = $aRow['game_username'];
				
				$output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}	
    }
}

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