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

class Game 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('game/withdrawal_listing', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function withdrawal_listing()
	{
		save_current_url(base_url() . 'game/withdrawal_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_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)) : '',
						'member_search_allowed_game' => ($this->input->post('search_allowed_game', TRUE) != '') ? implode(',', $this->input->post('search_allowed_game', TRUE)) : '',
						'member_search_prefix' => ($this->input->post('search_prefix', TRUE) != '') ? implode(',', $this->input->post('search_prefix', TRUE)) : ''
						)
				);
				
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_game') . ' - ' .$this->lang->line('hf_platform_withdraw_history') . '(' . $this->lang->line('game_unfreeze') . ')';
			
				$this->load->view('game_withdraw_history', $_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' => '', 'member_search_allowed_game' => '', 'member_search_prefix' => ''));
				
				$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_game') . ' - ' .$this->lang->line('hf_platform_withdraw_history') . '(' . $this->lang->line('game_unfreeze') . ')';
			
				$this->load->view('game_withdraw_history', $data);
			}
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function get_withdrawal_list()
    {
		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_game_withdrawals') . '.member_transaction_id',
								$this->db->dbprefix('member_game_withdrawals') . '.from_member_username',
								// $this->db->dbprefix('member_game_withdrawals') . '.p1_w_date',
								// $this->db->dbprefix('member_game_withdrawals') . '.p1_w_amount',
								// $this->db->dbprefix('member_game_withdrawals') . '.p1_total_bet',
								// $this->db->dbprefix('member_game_withdrawals') . '.p1_total_turnover',
								$this->db->dbprefix('member_game_withdrawals') . '.p2_w_date',
								$this->db->dbprefix('member_game_withdrawals') . '.p1_w_amount',
								$this->db->dbprefix('member_game_withdrawals') . '.p2_w_amount',
								$this->db->dbprefix('member_game_withdrawals') . '.p2_total_bet',
								$this->db->dbprefix('member_game_withdrawals') . '.p2_total_turnover',
								$this->db->dbprefix('member_game_withdrawals') . '.lend',
								$this->db->dbprefix('member_game_withdrawals') . '.deposit_amount',
								$this->db->dbprefix('member_game_withdrawals') . '.profit',
								$this->db->dbprefix('member_game_withdrawals') . '.updated_by',
								$this->db->dbprefix('member_game_withdrawals') . '.withdrawal_status',
								$this->db->dbprefix('members') . '.allowed_game',
								// $this->db->dbprefix('members') . '.insurance_date',
								// $this->db->dbprefix('members') . '.insurance_date',
								$this->db->dbprefix('members') . '.member_id',
								$this->db->dbprefix('members') . '.member_id',
								// $this->db->dbprefix('member_game_withdrawals') . '.p1_total_bet',
								// $this->db->dbprefix('member_game_withdrawals') . '.p1_total_turnover',
								$this->db->dbprefix('member_game_withdrawals') . '.p2_total_bet',
								$this->db->dbprefix('member_game_withdrawals') . '.p2_total_turnover',
							);
			
			$jColumns = $this->db->dbprefix('member_game_withdrawals') . '.from_member_id = ' . $this->db->dbprefix('members') . '.member_id';
			
			// DB table to use
			$sTable = 'sw_member_game_withdrawals';
			$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_game_withdrawals') . ".withdrawal_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_game_withdrawals') . ".withdrawal_date <= '" .  $end_date . "')";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_search_username') != '') {
				$where = $this->db->dbprefix('member_game_withdrawals') . ".from_member_username = '" . $this->session->userdata('member_search_username') . "'";
				$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_withdrawal_status') != '') {
				$where = $this->db->dbprefix('member_game_withdrawals') . ".withdrawal_status IN(" .  $this->session->userdata('member_search_withdrawal_status') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_allowed_game') != '') {
				$where = $this->db->dbprefix('members') . ".allowed_game IN(" .  $this->session->userdata('member_search_allowed_game') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_prefix') != '') {
				$arr = explode(',', $this->session->userdata('member_search_prefix'));
				$arr = "'" . implode("','", $arr) . "'";
				
				$where = $this->db->dbprefix('members') . ".prefix IN(" .  $arr . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_downline') != '') {
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username = ? LIMIT 1", array($this->session->userdata('member_search_downline')));
				if($query->num_rows() > 0) {
					$row = $query->row();
					$member_id = $row->member_id;
					$where = "(" . $this->db->dbprefix('members') . ".introducer_parent_ref LIKE '%,$member_id,%' OR " . $this->db->dbprefix('members') . ".member_id = $member_id)";
					$this->db->where($where);
				}
			}
			
			$this->db->where($this->db->dbprefix('member_game_withdrawals') . '.processing_status', 1);
			$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['withdrawal_status'] == 0) {
					$row[] = '<input type="checkbox" name="chkbox[]" value="' . $aRow['member_transaction_id'] . '" onchange="checkOrUncheck(this);" />';
				}
				else {
					$row[] = '';
				}
				
				$row[] = $aRow['from_member_username'];
				// $row[] = (($aRow['p1_w_date'] > 0) ? date('Y-m-d H:i:s', $aRow['p1_w_date']) : '-');
				// $row[] = number_format($aRow['p1_w_amount'], 2, '.', '');
				// $row[] = '<span id="w1-' . $aRow['member_transaction_id'] . '">' . $aRow['p1_total_bet'] . '</span>';
				// $row[] = '<span id="w2-' . $aRow['member_transaction_id'] . '">' . number_format($aRow['p1_total_turnover'], 2, '.', '') . '</span>';
				$row[] = (($aRow['p2_w_date'] > 0) ? date('Y-m-d H:i:s', $aRow['p2_w_date']) : '-');
				$row[] = number_format($aRow['p1_w_amount'], 2, '.', '');
				$row[] = number_format($aRow['p2_w_amount'], 2, '.', '');
				$row[] = '<span id="w3-' . $aRow['member_transaction_id'] . '">' . $aRow['p2_total_bet'] . '</span>';
				$row[] = '<span id="w4-' . $aRow['member_transaction_id'] . '">' . number_format($aRow['p2_total_turnover'], 2, '.', '') . '</span>';
				$row[] = number_format($aRow['lend'], 2, '.', '');
				$row[] = number_format($aRow['deposit_amount'], 2, '.', '');
				$row[] = number_format($aRow['profit'], 2, '.', '');
				$row[] = (($aRow['updated_by'] != '') ? $aRow['updated_by'] : '-');
				
				switch($aRow['withdrawal_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;
				}
				
				// switch($aRow['allowed_game']) {
					// case 0 : $row[] = '<span id="allowed_game_' . $aRow['member_transaction_id'] . '_' . $aRow['member_id'] . '" class="label label-fail cursor-pointer" onclick="update_setting(' . $aRow['member_transaction_id'] . ', ' . $aRow['member_id'] . ')">' . $this->lang->line('game_freeze') . '</span>'; break;
					// case 1 : $row[] = '<span id="allowed_game_' . $aRow['member_transaction_id'] . '_' . $aRow['member_id'] . '" class="label label-success cursor-pointer" onclick="update_setting(' . $aRow['member_transaction_id'] . ', ' . $aRow['member_id'] . ')">' . $this->lang->line('game_unfreeze') . '</span>'; break;
				// }
				switch($aRow['allowed_game']) {
					case 0 : $row[] = '<span class="label label-fail">' . $this->lang->line('game_freeze') . '</span>'; break;
					case 1 : $row[] = '<span class="label label-success">' . $this->lang->line('game_unfreeze') . '</span>'; break;
				}
				
				// $row[] = date('Y-m-d H:i:s', $aRow['insurance_date']);
				// $row[] = (($aRow['insurance_date'] > time()) ? '<span class="label label-success">' . $this->lang->line('game_protected') . '</span>' : '<span class="label label-important">' . $this->lang->line('game_unprotect') . '</span>');
				
				if($aRow['withdrawal_status'] == 0) {
					$row[] = '<select id="update_status" name="update_status" class="small m-wrap" onchange="update_withdrawal_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>';
							
					$action_links = '<a style="cursor: pointer;" id="waction-' . $aRow['member_transaction_id'] . '" onclick="update_turnover(' . $aRow['member_transaction_id'] . ')">' . $this->lang->line('game_update_turnover') . '</a> &nbsp;|&nbsp; ';
					$action_links .= '<a href="' . base_url() . 'game/edit_profit/' . $aRow['member_transaction_id'] . '/">' . $this->lang->line('game_edit_profit') . '</a> &nbsp;|&nbsp; ';
					$action_links .= '<a href="' . base_url() . 'game/edit_credit/' . $aRow['member_transaction_id'] . '/">' . $this->lang->line('game_edit_credit') . '</a> &nbsp;|&nbsp; ';
					$action_links .= '<a href="' . base_url() . 'game/update_freeze_status/' . $aRow['member_id'] . '/">' . $this->lang->line('game_freeze') . '</a> &nbsp;|&nbsp; ';
					$action_links .= '<a href="' . base_url() . 'game/bet_history/' . $aRow['member_transaction_id'] . '/" target="_blank">' . $this->lang->line('hf_platform_bet_history') . '</a>';	
					
					$row[] = $action_links;
				}
				else {
					$row[] = '-';
					$row[] = '<a href="' . base_url() . 'game/bet_history/' . $aRow['member_transaction_id'] . '/" target="_blank">' . $this->lang->line('hf_platform_bet_history') . '</a>';
				}
				
				// $row[] = $aRow['p1_total_bet'];
				// $row[] = number_format($aRow['p1_total_turnover'], 2, '.', '');
				$row[] = $aRow['p2_total_bet'];
				$row[] = number_format($aRow['p2_total_turnover'], 2, '.', '');
				
				$output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}
    }
	
	public function withdrawal_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('game_username'),
								// $this->lang->line('game_withdrawal_date') . ' (' . $this->lang->line('platform_1') . ')',
								// $this->lang->line('game_amount') . ' (' . $this->lang->line('platform_1') . ')',
								// $this->lang->line('game_total_bet') . ' (' . $this->lang->line('platform_1') . ')',
								// $this->lang->line('game_total_turnover') . ' (' . $this->lang->line('platform_1') . ')',
								$this->lang->line('game_withdrawal_date') . ' (' . $this->lang->line('platform_2') . ')',
								$this->lang->line('game_amount') . ' (' . $this->lang->line('platform_3') . ')',
								$this->lang->line('game_amount') . ' (' . $this->lang->line('platform_2') . ')',
								$this->lang->line('game_total_bet') . ' (' . $this->lang->line('platform_2') . ')',
								$this->lang->line('game_total_turnover') . ' (' . $this->lang->line('platform_2') . ')',
								$this->lang->line('game_lend'),
								$this->lang->line('game_total_deposit'),
								$this->lang->line('game_profit'),
								$this->lang->line('game_admin'),
								$this->lang->line('game_status')
							);
			
			//Get Data
			$aColumns = array(
								$this->db->dbprefix('member_game_withdrawals') . '.from_member_username',
								// $this->db->dbprefix('member_game_withdrawals') . '.p1_w_date',
								// $this->db->dbprefix('member_game_withdrawals') . '.p1_w_amount',
								// $this->db->dbprefix('member_game_withdrawals') . '.p1_total_bet',
								// $this->db->dbprefix('member_game_withdrawals') . '.p1_total_turnover',
								$this->db->dbprefix('member_game_withdrawals') . '.p2_w_date',
								$this->db->dbprefix('member_game_withdrawals') . '.p1_w_amount',
								$this->db->dbprefix('member_game_withdrawals') . '.p2_w_amount',
								$this->db->dbprefix('member_game_withdrawals') . '.p2_total_bet',
								$this->db->dbprefix('member_game_withdrawals') . '.p2_total_turnover',
								$this->db->dbprefix('member_game_withdrawals') . '.lend',
								$this->db->dbprefix('member_game_withdrawals') . '.deposit_amount',
								$this->db->dbprefix('member_game_withdrawals') . '.profit',
								$this->db->dbprefix('member_game_withdrawals') . '.updated_by',
								$this->db->dbprefix('member_game_withdrawals') . '.withdrawal_status'
							);
			
			// DB table to use
			$sTable = 'sw_member_game_withdrawals';
			//
			
			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_game_withdrawals') . ".withdrawal_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_game_withdrawals') . ".withdrawal_date <= '" .  $end_date . "')";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_search_username') != '') {
				$where = $this->db->dbprefix('member_game_withdrawals') . ".from_member_username = '" . $this->session->userdata('member_search_username') . "'";
				$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_withdrawal_status') != '') {
				$where = $this->db->dbprefix('member_game_withdrawals') . ".withdrawal_status IN(" .  $this->session->userdata('member_search_withdrawal_status') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_allowed_game') != '') {
				$where = $this->db->dbprefix('members') . ".allowed_game IN(" .  $this->session->userdata('member_search_allowed_game') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_prefix') != '') {
				$arr = explode(',', $this->session->userdata('member_search_prefix'));
				$arr = "'" . implode("','", $arr) . "'";
				
				$where = $this->db->dbprefix('members') . ".prefix IN(" .  $arr . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_downline') != '') {
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username = ? LIMIT 1", array($this->session->userdata('member_search_downline')));
				if($query->num_rows() > 0) {
					$row = $query->row();
					$member_id = $row->member_id;
					$where = "(" . $this->db->dbprefix('members') . ".introducer_parent_ref LIKE '%,$member_id,%' OR " . $this->db->dbprefix('members') . ".member_id = $member_id)";
					$this->db->where($where);
				}
			}
			
			$this->db->where($this->db->dbprefix('member_game_withdrawals') . '.processing_status', 1);
			
			$this->db->order_by($this->db->dbprefix('member_game_withdrawals') . '.withdrawal_date', 'asc');
			
			$this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			$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 (
									$aRow['from_member_username'],
									// (($aRow['p1_w_date'] > 0) ? date('Y-m-d H:i:s', $aRow['p1_w_date']) : '-'),
									// number_format($aRow['p1_w_amount'], 2, '.', ''),
									// $aRow['p1_total_bet'],
									// number_format($aRow['p1_total_turnover'], 2, '.', ''),
									(($aRow['p2_w_date'] > 0) ? date('Y-m-d H:i:s', $aRow['p2_w_date']) : '-'),
									number_format($aRow['p1_w_amount'], 2, '.', ''),
									number_format($aRow['p2_w_amount'], 2, '.', ''),
									$aRow['p2_total_bet'],
									number_format($aRow['p2_total_turnover'], 2, '.', ''),
									number_format($aRow['lend'], 2, '.', ''),
									number_format($aRow['deposit_amount'], 2, '.', ''),
									number_format($aRow['profit'], 2, '.', ''),
									(($aRow['updated_by'] != '') ? $aRow['updated_by'] : '-'),
									$status
								);
			}
					
			$xls = new Excel_XML('UTF-8', false, 'Game Withdrawal List');
			$xls->addArray($data);
			$xls->generateXML('game_withdrawal_list');
		}
	}
	
	public function update_withdrawal($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_withdrawal_status_platform($transaction_id, $status);
				
			redirect('game/withdrawal_listing/', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function 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_withdrawal_status_platform(trim($_POST['chkbox'][$i]), trim($_POST['update_status']));
					}
				}
			}
			
			redirect('game/withdrawal_listing/', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function update_turnover($transaction_id = NULL)
	{
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			// $otherdb1 = $this->load->database('otherdb1', TRUE);
			$otherdb2 = $this->load->database('otherdb2', TRUE);
			
			$query = $this->db->query("SELECT a.deposit_date, b.casino_plyid_1, 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 = ? AND withdrawal_status = ? LIMIT 1",array($transaction_id, 0));
			if($query->num_rows() > 0) {
				foreach($query->result() as $row) {
					$total_bet_1 = 0;
					$total_turnover_1 = 0;
					$total_winloss_1 = 0;
					// $query_1 = $otherdb1->query("SELECT COUNT(transaction_id) AS total_bet, SUM(bet_amount) AS total_turnover, SUM(win_loss) AS total_winloss FROM bctp_transaction_report WHERE player_id = ? AND payout_time > ? AND status = ? AND win_loss != ? AND game_code = ?", array($row->casino_plyid_1, $row->deposit_date, 1, 0, 'Baccarat'));
					// if($query_1->num_rows() > 0) {
						// $row_1 = $query_1->row();
						// $total_bet_1 = (isset($row_1->total_bet) ? $row_1->total_bet : 0);
						// $total_turnover_1 = (isset($row_1->total_turnover) ? $row_1->total_turnover : 0);
						// $total_winloss_1 = (isset($row_1->total_winloss) ? $row_1->total_winloss : 0);
					// }
					
					$total_bet_2 = 0;
					$total_turnover_2 = 0;
					$total_winloss_2 = 0;
					$query_2 = $otherdb2->query("SELECT COUNT(transaction_id) AS total_bet, SUM(bet_amount) AS total_turnover, SUM(win_loss) AS total_winloss FROM bctp_transaction_report WHERE player_id = ? AND payout_time > ? AND status = ? AND win_loss != ? AND game_code = ?", array($row->casino_plyid_2, $row->deposit_date, 1, 0, 'Baccarat'));
					if($query_2->num_rows() > 0) {
						$row_2 = $query_2->row();
						$total_bet_2 = (isset($row_2->total_bet) ? $row_2->total_bet : 0);
						$total_turnover_2 = (isset($row_2->total_turnover) ? $row_2->total_turnover : 0);
						$total_winloss_2 = (isset($row_2->total_winloss) ? $row_2->total_winloss : 0);
					}
					
					$DBdata = array(
					   // 'p1_total_bet' => $total_bet_1,
					   // 'p1_total_turnover' => $total_turnover_1,
					   // 'p1_total_winloss' => $total_winloss_1,
					   'p2_total_bet' => $total_bet_2,
					   'p2_total_turnover' => $total_turnover_2,
					   'p2_total_winloss' => $total_winloss_2,
					);	
					
					$this->db->where('member_transaction_id', $transaction_id);
					$this->db->limit(1);
					$this->db->update('sw_member_game_withdrawals', $DBdata);
					
					$json = array('p1_total_bet' => $total_bet_1, 'p1_total_turnover' => number_format($total_turnover_1, 2, '.', ''), 'p2_total_bet' => $total_bet_2, 'p2_total_turnover' => number_format($total_turnover_2, 2, '.', ''));
					
					$this->output
							->set_status_header(200)
							->set_content_type('application/json', 'utf-8')
							->set_output(json_encode($json))
							->_display();
							
					exit();	
				}
			}	
		}
	}
	
	public function edit_profit($transaction_id = NULL)
	{
		save_current_url(base_url() . 'game/edit_profit/' . $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());
	
			$this->load->model('user_model');
			$data = $this->user_model->get_game_withdrawal_details($transaction_id);
			
			$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_member_manager') . ' - ' .$this->lang->line('hf_game_edit_profit');
			
			$this->load->view('game_edit_profit', $data);
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function submit_profit($transaction_id = NULL)
	{
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			if($_POST) {
				$this->lang->load('account_general', get_language());
	
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_member_manager') . ' - ' .$this->lang->line('hf_game_edit_profit');
			
				$this->load->model('user_model');
				
				$rs = $this->user_model->update_game_withdrawal_details($transaction_id, 1);
				
				if($rs) {
					redirect('game/edit_profit/' . $transaction_id . '/', 'refresh');
				}
				else {
					$this->load->model('user_model');
					$_POST = $this->user_model->get_game_withdrawal_details($transaction_id);
					
					$this->load->view('game_edit_profit', $_POST);
				}
			}
			else {
				redirect('game/edit_profit/' . $transaction_id . '/', 'refresh');
			}
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function edit_credit($transaction_id = NULL)
	{
		save_current_url(base_url() . 'game/edit_credit/' . $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());
	
			$this->load->model('user_model');
			$data = $this->user_model->get_game_withdrawal_details($transaction_id);
			
			$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_member_manager') . ' - ' .$this->lang->line('hf_game_edit_credit');
			
			$this->load->view('game_edit_credit', $data);
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function submit_credit($transaction_id = NULL)
	{
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			if($_POST) {
				$this->lang->load('account_general', get_language());
	
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_member_manager') . ' - ' .$this->lang->line('hf_game_edit_credit');
			
				$this->load->model('user_model');
				
				$rs = $this->user_model->update_game_withdrawal_details($transaction_id, 2);
				
				if($rs) {
					redirect('game/edit_credit/' . $transaction_id . '/', 'refresh');
				}
				else {
					$this->load->model('user_model');
					$_POST = $this->user_model->get_game_withdrawal_details($transaction_id);
					
					$this->load->view('game_edit_credit', $_POST);
				}
			}
			else {
				redirect('game/edit_credit/' . $transaction_id . '/', '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('game/withdrawal_listing/', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function withdrawal_listing_2()
	{
		save_current_url(base_url() . 'game/withdrawal_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)) : '',
						'member_search_allowed_game' => ($this->input->post('search_allowed_game', TRUE) != '') ? implode(',', $this->input->post('search_allowed_game', TRUE)) : '',
						'member_search_prefix' => ($this->input->post('search_prefix', TRUE) != '') ? implode(',', $this->input->post('search_prefix', TRUE)) : ''
						)
				);
				
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_game') . ' - ' .$this->lang->line('hf_platform_withdraw_history') . '(' . $this->lang->line('game_freeze') . ')';
			
				$this->load->view('game_withdraw_history_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' => '', 'member_search_allowed_game' => '', 'member_search_prefix' => ''));
				
				$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_game') . ' - ' .$this->lang->line('hf_platform_withdraw_history') . '(' . $this->lang->line('game_freeze') . ')';
			
				$this->load->view('game_withdraw_history_2', $data);
			}
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function get_withdrawal_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_game_withdrawals') . '.member_transaction_id',
								$this->db->dbprefix('member_game_withdrawals') . '.from_member_username',
								// $this->db->dbprefix('member_game_withdrawals') . '.p1_w_date',
								// $this->db->dbprefix('member_game_withdrawals') . '.p1_w_amount',
								// $this->db->dbprefix('member_game_withdrawals') . '.p1_total_bet',
								// $this->db->dbprefix('member_game_withdrawals') . '.p1_total_turnover',
								$this->db->dbprefix('member_game_withdrawals') . '.p2_w_date',
								$this->db->dbprefix('member_game_withdrawals') . '.p1_w_amount',
								$this->db->dbprefix('member_game_withdrawals') . '.p2_w_amount',
								$this->db->dbprefix('member_game_withdrawals') . '.p2_total_bet',
								$this->db->dbprefix('member_game_withdrawals') . '.p2_total_turnover',
								$this->db->dbprefix('member_game_withdrawals') . '.lend',
								$this->db->dbprefix('member_game_withdrawals') . '.deposit_amount',
								$this->db->dbprefix('member_game_withdrawals') . '.profit',
								$this->db->dbprefix('member_game_withdrawals') . '.updated_by',
								$this->db->dbprefix('member_game_withdrawals') . '.withdrawal_status',
								$this->db->dbprefix('members') . '.allowed_game',
								// $this->db->dbprefix('members') . '.insurance_date',
								// $this->db->dbprefix('members') . '.insurance_date',
								$this->db->dbprefix('members') . '.member_id',
								$this->db->dbprefix('members') . '.member_id',
								// $this->db->dbprefix('member_game_withdrawals') . '.p1_total_bet',
								// $this->db->dbprefix('member_game_withdrawals') . '.p1_total_turnover',
								$this->db->dbprefix('member_game_withdrawals') . '.p2_total_bet',
								$this->db->dbprefix('member_game_withdrawals') . '.p2_total_turnover',
							);
			
			$jColumns = $this->db->dbprefix('member_game_withdrawals') . '.from_member_id = ' . $this->db->dbprefix('members') . '.member_id';
			
			// DB table to use
			$sTable = 'sw_member_game_withdrawals';
			$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_game_withdrawals') . ".withdrawal_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_game_withdrawals') . ".withdrawal_date <= '" .  $end_date . "')";
					$this->db->where($where);
				}
			}
			
			if($this->session->userdata('member_search_username') != '') {
				$where = $this->db->dbprefix('member_game_withdrawals') . ".from_member_username = '" . $this->session->userdata('member_search_username') . "'";
				$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_withdrawal_status') != '') {
				$where = $this->db->dbprefix('member_game_withdrawals') . ".withdrawal_status IN(" .  $this->session->userdata('member_search_withdrawal_status') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_allowed_game') != '') {
				$where = $this->db->dbprefix('members') . ".allowed_game IN(" .  $this->session->userdata('member_search_allowed_game') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_prefix') != '') {
				$arr = explode(',', $this->session->userdata('member_search_prefix'));
				$arr = "'" . implode("','", $arr) . "'";
				
				$where = $this->db->dbprefix('members') . ".prefix IN(" .  $arr . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_downline') != '') {
				$query = $this->db->query("SELECT member_id FROM sw_members WHERE username = ? LIMIT 1", array($this->session->userdata('member_search_downline')));
				if($query->num_rows() > 0) {
					$row = $query->row();
					$member_id = $row->member_id;
					$where = "(" . $this->db->dbprefix('members') . ".introducer_parent_ref LIKE '%,$member_id,%' OR " . $this->db->dbprefix('members') . ".member_id = $member_id)";
					$this->db->where($where);
				}
			}
			
			$this->db->where($this->db->dbprefix('member_game_withdrawals') . '.processing_status', 1);
			$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['withdrawal_status'] == 0) {
					$row[] = '<input type="checkbox" name="chkbox[]" value="' . $aRow['member_transaction_id'] . '" onchange="checkOrUncheck(this);" />';
				}
				else {
					$row[] = '';
				}
				
				$row[] = $aRow['from_member_username'];
				// $row[] = (($aRow['p1_w_date'] > 0) ? date('Y-m-d H:i:s', $aRow['p1_w_date']) : '-');
				// $row[] = number_format($aRow['p1_w_amount'], 2, '.', '');
				// $row[] = '<span id="w1-' . $aRow['member_transaction_id'] . '">' . $aRow['p1_total_bet'] . '</span>';
				// $row[] = '<span id="w2-' . $aRow['member_transaction_id'] . '">' . number_format($aRow['p1_total_turnover'], 2, '.', '') . '</span>';
				$row[] = (($aRow['p2_w_date'] > 0) ? date('Y-m-d H:i:s', $aRow['p2_w_date']) : '-');
				$row[] = number_format($aRow['p1_w_amount'], 2, '.', '');
				$row[] = number_format($aRow['p2_w_amount'], 2, '.', '');
				$row[] = '<span id="w3-' . $aRow['member_transaction_id'] . '">' . $aRow['p2_total_bet'] . '</span>';
				$row[] = '<span id="w4-' . $aRow['member_transaction_id'] . '">' . number_format($aRow['p2_total_turnover'], 2, '.', '') . '</span>';
				$row[] = number_format($aRow['lend'], 2, '.', '');
				$row[] = number_format($aRow['deposit_amount'], 2, '.', '');
				$row[] = number_format($aRow['profit'], 2, '.', '');
				$row[] = (($aRow['updated_by'] != '') ? $aRow['updated_by'] : '-');
				
				switch($aRow['withdrawal_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;
				}
				
				// switch($aRow['allowed_game']) {
					// case 0 : $row[] = '<span id="allowed_game_' . $aRow['member_transaction_id'] . '_' . $aRow['member_id'] . '" class="label label-fail cursor-pointer" onclick="update_setting(' . $aRow['member_transaction_id'] . ', ' . $aRow['member_id'] . ')">' . $this->lang->line('game_freeze') . '</span>'; break;
					// case 1 : $row[] = '<span id="allowed_game_' . $aRow['member_transaction_id'] . '_' . $aRow['member_id'] . '" class="label label-success cursor-pointer" onclick="update_setting(' . $aRow['member_transaction_id'] . ', ' . $aRow['member_id'] . ')">' . $this->lang->line('game_unfreeze') . '</span>'; break;
				// }
				switch($aRow['allowed_game']) {
					case 0 : $row[] = '<span class="label label-fail">' . $this->lang->line('game_freeze') . '</span>'; break;
					case 1 : $row[] = '<span class="label label-success">' . $this->lang->line('game_unfreeze') . '</span>'; break;
				}
				
				// $row[] = date('Y-m-d H:i:s', $aRow['insurance_date']);
				// $row[] = (($aRow['insurance_date'] > time()) ? '<span class="label label-success">' . $this->lang->line('game_protected') . '</span>' : '<span class="label label-important">' . $this->lang->line('game_unprotect') . '</span>');
				
				if($aRow['withdrawal_status'] == 0) {
					$row[] = '<select id="update_status" name="update_status" class="small m-wrap" onchange="update_withdrawal_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>';
							
					$action_links = '<a style="cursor: pointer;" id="waction-' . $aRow['member_transaction_id'] . '" onclick="update_turnover(' . $aRow['member_transaction_id'] . ')">' . $this->lang->line('game_update_turnover') . '</a> &nbsp;|&nbsp; ';
					$action_links .= '<a href="' . base_url() . 'game/edit_profit/' . $aRow['member_transaction_id'] . '/">' . $this->lang->line('game_edit_profit') . '</a> &nbsp;|&nbsp; ';
					$action_links .= '<a href="' . base_url() . 'game/edit_credit/' . $aRow['member_transaction_id'] . '/">' . $this->lang->line('game_edit_credit') . '</a> &nbsp;|&nbsp; ';
					$action_links .= '<a href="' . base_url() . 'game/update_unfreeze_status/' . $aRow['member_id'] . '/">' . $this->lang->line('game_freeze') . '</a> &nbsp;|&nbsp; ';
					$action_links .= '<a href="' . base_url() . 'game/bet_history/' . $aRow['member_transaction_id'] . '/" target="_blank">' . $this->lang->line('hf_platform_bet_history') . '</a>';	
					
					$row[] = $action_links;
				}
				else {
					$row[] = '-';
					$row[] = '<a href="' . base_url() . 'game/bet_history/' . $aRow['member_transaction_id'] . '/" target="_blank">' . $this->lang->line('hf_platform_bet_history') . '</a>';
				}
				
				// $row[] = $aRow['p1_total_bet'];
				// $row[] = number_format($aRow['p1_total_turnover'], 2, '.', '');
				$row[] = $aRow['p2_total_bet'];
				$row[] = number_format($aRow['p2_total_turnover'], 2, '.', '');
				
				$output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}
    }
	
	public function update_withdrawal_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_withdrawal_status_platform($transaction_id, $status);
				
			redirect('game/withdrawal_listing_2/', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function listing_update_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_withdrawal_status_platform(trim($_POST['chkbox'][$i]), trim($_POST['update_status']));
					}
				}
			}
			
			redirect('game/withdrawal_listing_2/', '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('game/withdrawal_listing_2/', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function bet_history($member_transaction_id = NULL)
	{
		save_current_url(base_url() . 'game/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());
			
			$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($member_transaction_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',
								$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'], 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());
			
			$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($member_transaction_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',
								$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'], 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 game.php */
/* Location: ./application/controllers/game.php */