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

class Member 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('member/add', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}

	public function add()
	{
		save_current_url(base_url() . 'member/add/');
		
		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_member_manager') . ' - ' .$this->lang->line('hf_add_new_member');
			
			$this->load->model('package_model');
			$data['package_listing'] = $this->package_model->get_package_list();
			
			$this->load->view('member_add_new', $data);
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function submit()
	{
		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_add_new_member');
			
				$this->load->model(array('package_model', 'user_model'));
				$_POST['package_listing'] = $this->package_model->get_package_list();
			
				$rs = $this->user_model->register_member();
					
				if($rs) {
					redirect('member/add/', 'refresh');
				}
				else {
					$this->load->view('member_add_new', $_POST);
				}
			}
			else {
				redirect('member/add/', 'refresh');
			}
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function edit($member_id = NULL)
	{
		save_current_url(base_url() . 'member/edit/' . $member_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(array('user_model', 'package_model'));
			$data = $this->user_model->get_member_details($member_id);
			$data['placement'] = $this->user_model->get_member_placement($member_id);
			$data['reinvest'] = $this->user_model->get_member_reinvest($member_id);
			$data['package_listing'] = $this->package_model->get_package_list();
			
			$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_member_manager') . ' - ' .$this->lang->line('hf_edit_member');
			
			$this->load->view('member_update', $data);
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function update($member_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_edit_member');
			
				$this->load->model('user_model');
				
				$rs = $this->user_model->update_member_details($member_id);
				
				if($rs) {
					redirect('member/edit/' . $member_id . '/', 'refresh');
				}
				else {
					$this->load->model(array('user_model', 'package_model'));
					$_POST['profile'] = $this->user_model->get_member_details($member_id);
					$_POST['placement'] = $this->user_model->get_member_placement($member_id);
					$_POST['reinvest'] = $this->user_model->get_member_reinvest($member_id);
					$_POST['package_listing'] = $this->package_model->get_package_list();
					
					$this->load->view('member_update', $_POST);
				}
			}
			else {
				redirect('member/edit/' . $member_id . '/', 'refresh');
			}
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function upgrade($member_id = NULL)
	{
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->load->model('user_model');
			$this->user_model->update_member_package($member_id);
			
			redirect('member/edit/' . $member_id . '/', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function upgrade_level($member_id = NULL)
	{
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->load->model('user_model');
			$this->user_model->update_member_level($member_id);
			
			redirect('member/edit/' . $member_id . '/', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function upgrade_star($member_id = NULL)
	{
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->load->model('user_model');
			$this->user_model->update_member_star($member_id);
			
			redirect('member/edit/' . $member_id . '/', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function reset_login($member_id = NULL)
	{
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->load->model('user_model');
			$this->user_model->reset_member_login_password($member_id);
			
			redirect('member/edit/' . $member_id . '/', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function reset_security($member_id = NULL)
	{
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			$this->load->model('user_model');
			$this->user_model->reset_member_security_password($member_id);
			
			redirect('member/edit/' . $member_id . '/', 'refresh');
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function update_setting()
	{
		if($this->session->userdata('user_type') && ($this->session->userdata('user_type') == SUPERADMIN || $this->session->userdata('user_type') == ADMIN)) {
			if(isset($_POST['type']) && isset($_POST['member_id'])) {
				if(trim($_POST['type']) != '' && trim($_POST['member_id']) != '') {
					$this->load->model('user_model');
					$this->user_model->update_member_setting();
					
					echo 1;
				}
				else {
					echo 0;
				}	
			}
			else {
				echo 0;
			}
		}
		else {
			echo 0;
		}
	}
	
	public function reinvest($member_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());
	
				$this->load->model('user_model');
				
				$rs = $this->user_model->update_member_reinvest($member_id);
				
				if($rs) {
					redirect('member/edit/' . $member_id . '/', 'refresh');
				}
				else {
					$this->load->model(array('user_model', 'package_model'));
					$_POST = $this->user_model->get_member_details($member_id);
					$_POST['package_listing'] = $this->package_model->get_package_list();
					$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_member_manager') . ' - ' .$this->lang->line('hf_edit_member');
					
					$this->load->view('member_update', $_POST);
				}
			}
			else {
				redirect('member/edit/' . $member_id . '/', 'refresh');
			}
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function update_username($member_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());
	
				$this->load->model('user_model');
				
				$rs = $this->user_model->update_member_username($member_id);
				
				if($rs) {
					redirect('member/edit/' . $member_id . '/', 'refresh');
				}
				else {
					$this->load->model(array('user_model', 'package_model'));
					$_POST = $this->user_model->get_member_details($member_id);
					$_POST['package_listing'] = $this->package_model->get_package_list();
					
					$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_member_manager') . ' - ' .$this->lang->line('hf_edit_member');
					
					$this->load->view('member_update', $_POST);
				}
			}
			else {
				redirect('member/edit/' . $member_id . '/', 'refresh');
			}
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function update_package($member_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());
	
				$this->load->model('user_model');
				
				$rs = $this->user_model->update_existing_member_package($member_id);
				
				if($rs) {
					redirect('member/edit/' . $member_id . '/', 'refresh');
				}
				else {
					$this->load->model(array('user_model', 'package_model'));
					$_POST = $this->user_model->get_member_details($member_id);
					$_POST['package_listing'] = $this->package_model->get_package_list();
					
					$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_member_manager') . ' - ' .$this->lang->line('hf_edit_member');
					
					$this->load->view('member_update', $_POST);
				}
			}
			else {
				redirect('member/edit/' . $member_id . '/', 'refresh');
			}
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function update_member_type($member_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());
	
				$this->load->model('user_model');
				
				$rs = $this->user_model->update_existing_member_type($member_id);
				
				if($rs) {
					redirect('member/edit/' . $member_id . '/', 'refresh');
				}
				else {
					$this->load->model(array('user_model', 'package_model'));
					$_POST = $this->user_model->get_member_details($member_id);
					$_POST['package_listing'] = $this->package_model->get_package_list();
					
					$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_member_manager') . ' - ' .$this->lang->line('hf_edit_member');
					
					$this->load->view('member_update', $_POST);
				}
			}
			else {
				redirect('member/edit/' . $member_id . '/', 'refresh');
			}
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function update_introducer_name($member_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());
	
				$this->load->model('user_model');
				
				$rs = $this->user_model->update_member_introducer_name($member_id);
				
				if($rs) {
					redirect('member/edit/' . $member_id . '/', 'refresh');
				}
				else {
					$this->load->model(array('user_model', 'package_model'));
					$_POST = $this->user_model->get_member_details($member_id);
					$_POST['package_listing'] = $this->package_model->get_package_list();
					
					$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_member_manager') . ' - ' .$this->lang->line('hf_edit_member');
					
					$this->load->view('member_update', $_POST);
				}
			}
			else {
				redirect('member/edit/' . $member_id . '/', 'refresh');
			}
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function delete($member_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());
	
				$this->load->model('user_model');
				
				$rs = $this->user_model->delete_member($member_id);
				
				if($rs) {
					redirect('member/listing/', 'refresh');
				}
				else {
					$this->load->model(array('user_model', 'package_model'));
					$_POST = $this->user_model->get_member_details($member_id);
					$_POST['package_listing'] = $this->package_model->get_package_list();
					
					$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_member_manager') . ' - ' .$this->lang->line('hf_edit_member');
					
					$this->load->view('member_update', $_POST);
				}
			}
			else {
				redirect('member/edit/' . $member_id . '/', 'refresh');
			}
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function listing()
	{
		save_current_url(base_url() . 'member/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_usdt_trc20_address' => trim($this->input->post('search_usdt_trc20_address', TRUE)), 
						'member_search_register_date' => trim($this->input->post('search_register_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_member_type' => ($this->input->post('search_member_type', TRUE) != '') ? implode(',', $this->input->post('search_member_type', TRUE)) : '', 
						'member_search_package_type' => ($this->input->post('search_package_type', TRUE) != '') ? implode(',', $this->input->post('search_package_type', TRUE)) : '',
						'member_search_reinvest_package_type' => ($this->input->post('search_reinvest_package_type', TRUE) != '') ? implode(',', $this->input->post('search_reinvest_package_type', TRUE)) : '',
						'member_search_member_package' => ($this->input->post('search_member_package', TRUE) != '') ? implode(',', $this->input->post('search_member_package', TRUE)) : '', 
						'member_search_member_level' => ($this->input->post('search_member_level', TRUE) != '') ? implode(',', $this->input->post('search_member_level', TRUE)) : '', 
						)
				);
				
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_member_manager') . ' - ' .$this->lang->line('hf_member_list');
			
				$this->load->view('member_list', $_POST);
			}
			else {
				$this->session->unset_userdata(array('member_search_usdt_trc20_address' => '', 'member_search_register_date' => '', 'member_search_start_date' => '', 'member_search_end_date' => '', 'member_search_member_type' => '', 'member_search_package_type' => '', 'member_search_reinvest_package_type' => '', 'member_search_member_package' => '', 'member_search_member_level' => ''));
				
				$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_member_manager') . ' - ' .$this->lang->line('hf_member_list');
			
				$this->load->view('member_list', $data);
			}
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function getlist()
    {
		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('members') . '.member_id', 
								$this->db->dbprefix('members') . '.username', 
								$this->db->dbprefix('members') . '.full_name',
								$this->db->dbprefix('members') . '.introducer', 
								$this->db->dbprefix('members') . '.package_type',
								$this->db->dbprefix('members') . '.level',
								$this->db->dbprefix('members') . '.stars',
								$this->db->dbprefix('members') . '.member_type', 
								$this->db->dbprefix('members') . '.casino_username', 
								$this->db->dbprefix('members') . '.allowed_withdraw',
								$this->db->dbprefix('members') . '.unlimited_withdraw',
								$this->db->dbprefix('members') . '.allowed_transfer',
								$this->db->dbprefix('members') . '.allowed_convert',
								$this->db->dbprefix('members') . '.allowed_topup',
								$this->db->dbprefix('members') . '.active', 
								$this->db->dbprefix('members') . '.contract_start_date',
								$this->db->dbprefix('members') . '.special_package_type',
								$this->db->dbprefix('members') . '.special_level',
							);
			
			// DB table to use
			$sTable = 'members';
			//
			
			if($this->session->userdata('member_search_usdt_trc20_address') != '') {
				$where = $this->db->dbprefix('members') . ".usdt_trc20_address = '" . $this->session->userdata('member_search_usdt_trc20_address') . "'";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_register_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('members') . ".contract_start_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('members') . ".contract_start_date <= '" .  $end_date . "')";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_member_type') != '') {
				$where = $this->db->dbprefix('members') . ".member_type IN(" .  $this->session->userdata('member_search_member_type') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_member_level') != '') {
				$where = $this->db->dbprefix('members') . ".level IN(" .  $this->session->userdata('member_search_member_level') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_package_type') != '') {
				$where = $this->db->dbprefix('members') . ".package_type IN(" .  $this->session->userdata('member_search_package_type') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_reinvest_package_type') != '') {
				$where = "(SELECT member_reinvest_id FROM sw_member_reinvest WHERE reinvest_package_type IN (" .  $this->session->userdata('member_search_reinvest_package_type') . ") AND member_id = " . $this->db->dbprefix('members') . ".member_id LIMIT 1)";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_member_package') == '1') {
				$where = $this->db->dbprefix('members') . ".package_type > 0";
				$this->db->where($where);
			}
			else if($this->session->userdata('member_search_member_package') == '2') {
				$where = $this->db->dbprefix('members') . ".package_type = 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);
			$rResult = $this->db->get($sTable);
		
			// Data set length after filtering
			$this->db->select('FOUND_ROWS() AS found_rows');
			$iFilteredTotal = $this->db->get()->row()->found_rows;
		
			// Total data set length
			$iTotal = $this->db->count_all($sTable);
		
			// Output
			$output = array(
				'sEcho' => intval($sEcho),
				'iTotalRecords' => $iTotal,
				'iTotalDisplayRecords' => $iFilteredTotal,
				'aaData' => array()
			);
			
			foreach($rResult->result_array() as $aRow)
			{
				$row = array();
				
				$row[] = $aRow['member_id'];
				$row[] = '<a href="' . base_url() . 'member/structure/' . $aRow['username'] . '/">' . $aRow['username'] . '</a>';
				$row[] = $aRow['full_name'];
				$row[] = $aRow['introducer'];
				$row[] = $this->lang->line(get_package($aRow['package_type'])) . (($aRow['special_package_type'] > 0) ? ' (* ' . $this->lang->line(get_package($aRow['special_package_type'])) . ')' : '');
				//$row[] = '<span id="rolling_gaming_' . $aRow['member_id'] . '">'.$aRow['rolling_gaming']."</span>";
				$row[] = $this->lang->line(get_level($aRow['level'])) . (($aRow['special_level'] > 0) ? ' (* ' . $this->lang->line(get_level($aRow['special_level'])) . ')' : '');
				$row[] = $aRow['stars'];
				$row[] = $this->lang->line(get_member_type($aRow['member_type']));
				$row[] = (($aRow['casino_username'] != '') ? $aRow['casino_username'] : '-');
				
				switch($aRow['allowed_withdraw']) {
					case 0 : $row[] = '<span id="allowed_withdraw_' . $aRow['member_id'] . '" class="label label-fail cursor-pointer" onclick="update_setting(1, ' . $aRow['member_id'] . ')">' . $this->lang->line('registration_deactivated') . '</span>'; break;
					case 1 : $row[] = '<span id="allowed_withdraw_' . $aRow['member_id'] . '" class="label label-success cursor-pointer" onclick="update_setting(1, ' . $aRow['member_id'] . ')">' . $this->lang->line('registration_active') . '</span>'; break;
				}
				
				switch($aRow['unlimited_withdraw']) {
					case 0 : $row[] = '<span id="unlimited_withdraw_' . $aRow['member_id'] . '" class="label label-fail cursor-pointer" onclick="update_setting(2, ' . $aRow['member_id'] . ')">' . $this->lang->line('registration_deactivated') . '</span>'; break;
					case 1 : $row[] = '<span id="unlimited_withdraw_' . $aRow['member_id'] . '" class="label label-success cursor-pointer" onclick="update_setting(2, ' . $aRow['member_id'] . ')">' . $this->lang->line('registration_active') . '</span>'; break;
				}
				
				switch($aRow['allowed_transfer']) {
					case 0 : $row[] = '<span id="allowed_transfer_' . $aRow['member_id'] . '" class="label label-fail cursor-pointer" onclick="update_setting(3, ' . $aRow['member_id'] . ')">' . $this->lang->line('registration_deactivated') . '</span>'; break;
					case 1 : $row[] = '<span id="allowed_transfer_' . $aRow['member_id'] . '" class="label label-success cursor-pointer" onclick="update_setting(3, ' . $aRow['member_id'] . ')">' . $this->lang->line('registration_active') . '</span>'; break;
				}
				
				switch($aRow['allowed_convert']) {
					case 0 : $row[] = '<span id="allowed_convert_' . $aRow['member_id'] . '" class="label label-fail cursor-pointer" onclick="update_setting(6, ' . $aRow['member_id'] . ')">' . $this->lang->line('registration_deactivated') . '</span>'; break;
					case 1 : $row[] = '<span id="allowed_convert_' . $aRow['member_id'] . '" class="label label-success cursor-pointer" onclick="update_setting(6, ' . $aRow['member_id'] . ')">' . $this->lang->line('registration_active') . '</span>'; break;
				}
				
				switch($aRow['allowed_topup']) {
					case 0 : $row[] = '<span id="allowed_topup_' . $aRow['member_id'] . '" class="label label-fail cursor-pointer" onclick="update_setting(5, ' . $aRow['member_id'] . ')">' . $this->lang->line('registration_deactivated') . '</span>'; break;
					case 1 : $row[] = '<span id="allowed_topup_' . $aRow['member_id'] . '" class="label label-success cursor-pointer" onclick="update_setting(5, ' . $aRow['member_id'] . ')">' . $this->lang->line('registration_active') . '</span>'; break;
				}
				
				switch($aRow['active']) {
					case 0 : $row[] = '<span id="account_status_' . $aRow['member_id'] . '" class="label label-fail cursor-pointer" onclick="update_setting(4, ' . $aRow['member_id'] . ')">' . $this->lang->line('registration_suspended') . '</span>'; break;
					case 1 : $row[] = '<span id="account_status_' . $aRow['member_id'] . '" class="label label-success cursor-pointer" onclick="update_setting(4, ' . $aRow['member_id'] . ')">' . $this->lang->line('registration_active') . '</span>'; break;
				}
				
				$row[] = date('Y-m-d H:i:s', $aRow['contract_start_date']);
				
				// $button = '<a href="' . base_url() . 'member/edit/' . $aRow['member_id'] . '/">' . $this->lang->line('member_edit') . '</a> <a onclick="resetRolling('. $aRow['member_id'] . ')">' . $this->lang->line('member_reset_rolling') . '</a>';
				$button = '<a href="' . base_url() . 'member/edit/' . $aRow['member_id'] . '/">' . $this->lang->line('member_edit') . '</a>';

				$row[] = $button;
				
				$output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}	
    }
	
	public function listing_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('member_user_id'),
								$this->lang->line('member_username'),
								$this->lang->line('member_name'),
								$this->lang->line('member_rerferral'),
								$this->lang->line('member_package'),
								$this->lang->line('member_level'),
								$this->lang->line('member_stars'),
								$this->lang->line('member_member_type'),
								$this->lang->line('member_game_username'),
								$this->lang->line('member_withdraw_activated'),
								$this->lang->line('member_withdraw_unlimited'),
								$this->lang->line('member_transfer_activated'),
								$this->lang->line('member_convert_activated'),
								$this->lang->line('member_topup_activated'),
								$this->lang->line('member_status'),
								$this->lang->line('member_join_date')
							);
			
			//Get Data
			$aColumns = array(
								$this->db->dbprefix('members') . '.member_id', 
								$this->db->dbprefix('members') . '.username', 
								$this->db->dbprefix('members') . '.full_name',
								$this->db->dbprefix('members') . '.introducer', 
								$this->db->dbprefix('members') . '.package_type',
								$this->db->dbprefix('members') . '.level',
								$this->db->dbprefix('members') . '.stars',
								$this->db->dbprefix('members') . '.member_type', 
								$this->db->dbprefix('members') . '.casino_username', 
								$this->db->dbprefix('members') . '.allowed_withdraw',
								$this->db->dbprefix('members') . '.unlimited_withdraw',
								$this->db->dbprefix('members') . '.allowed_transfer',
								$this->db->dbprefix('members') . '.allowed_convert',
								$this->db->dbprefix('members') . '.allowed_topup',
								$this->db->dbprefix('members') . '.active', 
								$this->db->dbprefix('members') . '.contract_start_date',
								$this->db->dbprefix('members') . '.special_package_type',
								$this->db->dbprefix('members') . '.special_level'
							);
			
			// DB table to use
			$sTable = 'members';
			//
			
			if($this->session->userdata('member_search_usdt_trc20_address') != '') {
				$where = $this->db->dbprefix('members') . ".usdt_trc20_address = '" . $this->session->userdata('member_search_usdt_trc20_address') . "'";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_register_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('members') . ".contract_start_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('members') . ".contract_start_date <= '" .  $end_date . "')";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_member_type') != '') {
				$where = $this->db->dbprefix('members') . ".member_type IN(" .  $this->session->userdata('member_search_member_type') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_member_level') != '') {
				$where = $this->db->dbprefix('members') . ".level IN(" .  $this->session->userdata('member_search_member_level') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_package_type') != '') {
				$where = $this->db->dbprefix('members') . ".package_type IN(" .  $this->session->userdata('member_search_package_type') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_reinvest_package_type') != '') {
				$where = "(SELECT member_reinvest_id FROM sw_member_reinvest WHERE reinvest_package_type IN (" .  $this->session->userdata('member_search_reinvest_package_type') . ") AND member_id = " . $this->db->dbprefix('members') . ".member_id LIMIT 1)";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_member_package') == '1') {
				$where = $this->db->dbprefix('members') . ".package_type > 0";
				$this->db->where($where);
			}
			else if($this->session->userdata('member_search_member_package') == '2') {
				$where = $this->db->dbprefix('members') . ".package_type = 0";
				$this->db->where($where);
			}
			
			$this->db->order_by($this->db->dbprefix('members') . '.contract_start_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)
			{
				$allowed_withdraw = '';
				switch($aRow['allowed_withdraw']) {
					case 0 : $allowed_withdraw = $this->lang->line('registration_deactivated'); break;
					case 1 : $allowed_withdraw = $this->lang->line('registration_active'); break;
				}
				
				$unlimited_withdraw = '';
				switch($aRow['unlimited_withdraw']) {
					case 0 : $unlimited_withdraw = $this->lang->line('registration_deactivated'); break;
					case 1 : $unlimited_withdraw = $this->lang->line('registration_active'); break;
				}
				
				$allowed_transfer = '';
				switch($aRow['allowed_transfer']) {
					case 0 : $allowed_transfer = $this->lang->line('registration_deactivated'); break;
					case 1 : $allowed_transfer = $this->lang->line('registration_active'); break;
				}
				
				$allowed_convert = '';
				switch($aRow['allowed_convert']) {
					case 0 : $allowed_convert = $this->lang->line('registration_deactivated'); break;
					case 1 : $allowed_convert = $this->lang->line('registration_active'); break;
				}
				
				$allowed_topup = '';
				switch($aRow['allowed_topup']) {
					case 0 : $allowed_topup = $this->lang->line('registration_deactivated'); break;
					case 1 : $allowed_topup = $this->lang->line('registration_active'); break;
				}
				
				$status = '';
				switch($aRow['active']) {
					case 0 : $status = $this->lang->line('registration_suspended'); break;
					case 1 : $status = $this->lang->line('registration_active'); break;
				}
				
				$data[] = array (
									$aRow['member_id'],
									$aRow['username'],
									$aRow['full_name'],
									$aRow['introducer'],
									$this->lang->line(get_package($aRow['package_type'])) . (($aRow['special_package_type'] > 0) ? ' (* ' . $this->lang->line(get_package($aRow['special_package_type'])) . ')' : ''),
									$this->lang->line(get_level($aRow['level'])) . (($aRow['special_level'] > 0) ? ' (* ' . $this->lang->line(get_level($aRow['special_level'])) . ')' : ''),
									$aRow['stars'],
									$this->lang->line(get_member_type($aRow['member_type'])),
									(($aRow['casino_username'] != '') ? $aRow['casino_username'] : '-'),
									$allowed_withdraw,
									$unlimited_withdraw,
									$allowed_transfer,
									$allowed_convert,
									$allowed_topup,
									$status,
									date('Y-m-d H:i:s', $aRow['contract_start_date'])
								);
			}
			
			$xls = new Excel_XML('UTF-8', false, 'Member List');
			$xls->addArray($data);
			$xls->generateXML('member_list');
		}
	}
	
	public function wallet_listing()
	{
		save_current_url(base_url() . 'member/wallet_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_downline' => trim($this->input->post('search_downline', TRUE)), 
					)
				);
				
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_member_manager') . ' - ' .$this->lang->line('hf_member_wallet_list');
			
				$this->load->view('member_wallet_list', $_POST);
			}
			else {
				$this->session->unset_userdata(array('member_search_username' => '', 'member_search_downline' => ''));
				
				$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_member_manager') . ' - ' .$this->lang->line('hf_member_wallet_list');
			
				$this->load->view('member_wallet_list', $data);
			}
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function getwalletlist()
    {
		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('members') . '.member_id', 
								$this->db->dbprefix('members') . '.username', 
								$this->db->dbprefix('member_wallets') . '.v_unit', 
								$this->db->dbprefix('member_wallets') . '.v_unit2',
								$this->db->dbprefix('member_wallets') . '.v_unit3',
								$this->db->dbprefix('member_wallets') . '.v_unit4'
							);
			
			$jColumns = $this->db->dbprefix('member_wallets') . '.member_id = ' . $this->db->dbprefix('members') . '.member_id';
			
			// DB table to use
			$sTable = 'member_wallets';
			$jTable = 'members';
			//
			
			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_search_downline') != '') {
				$arr = explode(',', $this->session->userdata('member_search_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 . ",%'";
						}
						else {
							$where .= $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $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[] = $aRow['member_id'];
				$row[] = $aRow['username'];
				$row[] = $aRow['v_unit'];
				$row[] = $aRow['v_unit2'];
				$row[] = $aRow['v_unit3'];
				$row[] = $aRow['v_unit4'];
				
				$output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}	
    }
	
	public function wallet_listing_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('member_user_id'),
								$this->lang->line('member_username'),
								$this->lang->line('wallet_1'),
								$this->lang->line('wallet_2'),
								$this->lang->line('wallet_3'),
								$this->lang->line('wallet_4')
							);
			
			//Get Data
			$aColumns = array(
								$this->db->dbprefix('members') . '.member_id', 
								$this->db->dbprefix('members') . '.username', 
								$this->db->dbprefix('member_wallets') . '.v_unit',
								$this->db->dbprefix('member_wallets') . '.v_unit2',
								$this->db->dbprefix('member_wallets') . '.v_unit3',
								$this->db->dbprefix('member_wallets') . '.v_unit4'
							);
			
			$jColumns = $this->db->dbprefix('member_wallets') . '.member_id = ' . $this->db->dbprefix('members') . '.member_id';
			
			// DB table to use
			$sTable = 'member_wallets';
			$jTable = 'members';
			//
			
			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_search_downline') != '') {
				$arr = explode(',', $this->session->userdata('member_search_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 . ",%'";
						}
						else {
							$where .= $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%'";
						}
					}
					
					if($where != '') {
						$where = "(" . $where . ")";
						$this->db->where($where);
					}
				}
			}
			
			$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)
			{
				$data[] = array (
									$aRow['member_id'],
									$aRow['username'],
									$aRow['v_unit'],
									$aRow['v_unit2'],
									$aRow['v_unit3'],
									$aRow['v_unit4']
								);
			}
				
			$xls = new Excel_XML('UTF-8', false, 'Member Wallet List');
			$xls->addArray($data);
			$xls->generateXML('member_wallet_list');
		}
	}
	
	public function downline()
	{
		save_current_url(base_url() . 'member/downline/');
		
		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_downline' => trim($this->input->post('search_downline', TRUE)), 
						'member_search_full_name' => trim($this->input->post('search_full_name', TRUE)), 
						'member_search_introducer' => trim($this->input->post('search_introducer', TRUE)), 
						'member_search_register_date' => trim($this->input->post('search_register_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_member_type' => ($this->input->post('search_member_type', TRUE) != '') ? implode(',', $this->input->post('search_member_type', TRUE)) : '', 
						'member_search_package_type' => ($this->input->post('search_package_type', TRUE) != '') ? implode(',', $this->input->post('search_package_type', TRUE)) : '',
						'member_search_reinvest_package_type' => ($this->input->post('search_reinvest_package_type', TRUE) != '') ? implode(',', $this->input->post('search_reinvest_package_type', TRUE)) : '',
						'member_search_member_package' => ($this->input->post('search_member_package', TRUE) != '') ? implode(',', $this->input->post('search_member_package', TRUE)) : '', 
						'member_search_member_level' => ($this->input->post('search_member_level', TRUE) != '') ? implode(',', $this->input->post('search_member_level', TRUE)) : '', 
					)
				);
				
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_member_manager') . ' - ' .$this->lang->line('hf_member_downline_list');
			
				$this->load->view('member_downline_list', $_POST);
			}
			else {
				$this->session->unset_userdata(array('member_search_username' => '', 'member_search_downline' => '', 'member_search_full_name' => '', 'member_search_introducer' => '', 'member_search_register_date' => '', 'member_search_start_date' => '', 'member_search_end_date' => '', 'member_search_member_type' => '', 'member_search_package_type' => '', 'member_search_reinvest_package_type' => '', 'member_search_member_package' => '', 'member_search_member_level' => ''));
				
				$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_member_manager') . ' - ' .$this->lang->line('hf_member_downline_list');
			
				$this->load->view('member_downline_list', $data);
			}	
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function getdownlinelist()
    {
		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('members') . '.member_id', 
								$this->db->dbprefix('members') . '.username', 
								$this->db->dbprefix('members') . '.casino_username',
								$this->db->dbprefix('members') . '.introducer',
								$this->db->dbprefix('members') . '.package_type', 
								$this->db->dbprefix('members') . '.member_type', 
								$this->db->dbprefix('members') . '.level',
								$this->db->dbprefix('members') . '.active', 
								$this->db->dbprefix('members') . '.contract_start_date',
								$this->db->dbprefix('members') . '.special_package_type',
								$this->db->dbprefix('members') . '.special_level',
							);
			
			// DB table to use
			$sTable = 'members';
			//
			
			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_search_downline') != '') {
				$arr = explode(',', $this->session->userdata('member_search_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 . ",%'";
						}
						else {
							$where .= $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%'";
						}
					}
					
					if($where != '') {
						$where = "(" . $where . ")";
						$this->db->where($where);
					}
				}
			}
			
			if($this->session->userdata('member_search_full_name') != '') {
				$where = $this->db->dbprefix('members') . ".full_name LIKE '%" .  $this->session->userdata('member_search_full_name') . "%'";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_introducer') != '') {
				$where = $this->db->dbprefix('members') . ".introducer LIKE '%" .  $this->session->userdata('member_search_introducer') . "%'";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_register_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('members') . ".contract_start_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('members') . ".contract_start_date <= '" .  $end_date . "')";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_member_type') != '') {
				$where = $this->db->dbprefix('members') . ".member_type IN(" .  $this->session->userdata('member_search_member_type') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_member_level') != '') {
				$where = $this->db->dbprefix('members') . ".level IN(" .  $this->session->userdata('member_search_member_level') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_package_type') != '') {
				$where = $this->db->dbprefix('members') . ".package_type IN(" .  $this->session->userdata('member_search_package_type') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_reinvest_package_type') != '') {
				$where = "(SELECT member_reinvest_id FROM sw_member_reinvest WHERE reinvest_package_type IN (" .  $this->session->userdata('member_search_reinvest_package_type') . ") AND member_id = " . $this->db->dbprefix('members') . ".member_id LIMIT 1)";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_member_package') == '1') {
				$where = $this->db->dbprefix('members') . ".package_type > 0";
				$this->db->where($where);
			}
			else if($this->session->userdata('member_search_member_package') == '2') {
				$where = $this->db->dbprefix('members') . ".package_type = 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);
			$rResult = $this->db->get($sTable);
		
			// Data set length after filtering
			$this->db->select('FOUND_ROWS() AS found_rows');
			$iFilteredTotal = $this->db->get()->row()->found_rows;
		
			// Total data set length
			$iTotal = $this->db->count_all($sTable);
		
			// Output
			$output = array(
				'sEcho' => intval($sEcho),
				'iTotalRecords' => $iTotal,
				'iTotalDisplayRecords' => $iFilteredTotal,
				'aaData' => array()
			);
			
			foreach($rResult->result_array() as $aRow)
			{
				$row = array();
				
				$row[] = $aRow['member_id'];
				$row[] = '<a href="' . base_url() . 'member/structure/' . $aRow['username'] . '/">' . $aRow['username'] . '</a>';
				$row[] = $aRow['casino_username'];
				$row[] = $aRow['introducer'];
				$row[] = $this->lang->line(get_package($aRow['package_type'])) . (($aRow['special_package_type'] > 0) ? ' (* ' . $this->lang->line(get_package($aRow['special_package_type'])) . ')' : '');
				$row[] = $this->lang->line(get_member_type($aRow['member_type']));
				$row[] = $this->lang->line(get_level($aRow['level'])) . (($aRow['special_level'] > 0) ? ' (* ' . $this->lang->line(get_level($aRow['special_level'])) . ')' : '');
				
				switch($aRow['active']) {
					case 0 : $row[] = $this->lang->line('registration_suspended'); break;
					case 1 : $row[] = $this->lang->line('registration_active'); break;
				}
				
				$row[] = date('Y-m-d H:i:s', $aRow['contract_start_date']);
				$row[] = '<a href="' . base_url() . 'member/edit/' . $aRow['member_id'] . '/">' . $this->lang->line('member_edit') . '</a>';
				
				$output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}	
    }
	
	public function downline_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('member_user_id'),
								$this->lang->line('member_username'),
								$this->lang->line('member_game_username'),
								$this->lang->line('member_id_no'),
								$this->lang->line('member_mobile_no'),
								$this->lang->line('member_address'),
								$this->lang->line('member_rerferral'),
								$this->lang->line('member_rerferral_name'),
								$this->lang->line('member_rerferral_id_no'),
								$this->lang->line('member_rerferral_mobile_no'),
								$this->lang->line('member_package'),
								$this->lang->line('member_member_type'),
								$this->lang->line('member_level'),
								$this->lang->line('member_status'),
								$this->lang->line('member_join_date')
							);
			
			//Get Data
			$aColumns = array(
								$this->db->dbprefix('members') . '.member_id', 
								$this->db->dbprefix('members') . '.username', 
								$this->db->dbprefix('members') . '.casino_username',
								$this->db->dbprefix('members') . '.id_no',
								$this->db->dbprefix('members') . '.mobile_no',
								$this->db->dbprefix('members') . '.address',
								$this->db->dbprefix('members') . '.city',
								$this->db->dbprefix('members') . '.postal_code',
								$this->db->dbprefix('members') . '.state',
								$this->db->dbprefix('members') . '.country', 
								$this->db->dbprefix('members') . '.introducer',
								'b.full_name AS introducer_name',
								'b.id_no AS introducer_id_no',
								'b.mobile_no AS introducer_mobile_no',
								$this->db->dbprefix('members') . '.package_type', 
								$this->db->dbprefix('members') . '.member_type', 
								$this->db->dbprefix('members') . '.level',
								$this->db->dbprefix('members') . '.active', 
								$this->db->dbprefix('members') . '.contract_start_date',
								$this->db->dbprefix('members') . '.special_package_type',
								$this->db->dbprefix('members') . '.special_level',
							);
			$jColumns = $this->db->dbprefix('members') . '.introducer = b.username';				
			
			// DB table to use
			$sTable = 'members';
			$jTable = 'members b';
			//
			
			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_search_downline') != '') {
				$arr = explode(',', $this->session->userdata('member_search_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 . ",%'";
						}
						else {
							$where .= $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%'";
						}
					}
					
					if($where != '') {
						$where = "(" . $where . ")";
						$this->db->where($where);
					}
				}
			}
			
			if($this->session->userdata('member_search_full_name') != '') {
				$where = $this->db->dbprefix('members') . ".full_name LIKE '%" .  $this->session->userdata('member_search_full_name') . "%'";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_introducer') != '') {
				$where = $this->db->dbprefix('members') . ".introducer LIKE '%" .  $this->session->userdata('member_search_introducer') . "%'";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_register_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('members') . ".contract_start_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('members') . ".contract_start_date <= '" .  $end_date . "')";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_member_type') != '') {
				$where = $this->db->dbprefix('members') . ".member_type IN(" .  $this->session->userdata('member_search_member_type') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_member_level') != '') {
				$where = $this->db->dbprefix('members') . ".level IN(" .  $this->session->userdata('member_search_member_level') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_package_type') != '') {
				$where = $this->db->dbprefix('members') . ".package_type IN(" .  $this->session->userdata('member_search_package_type') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_reinvest_package_type') != '') {
				$where = "(SELECT member_reinvest_id FROM sw_member_reinvest WHERE reinvest_package_type IN (" .  $this->session->userdata('member_search_reinvest_package_type') . ") AND member_id = " . $this->db->dbprefix('members') . ".member_id LIMIT 1)";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_register_type') != '') {
				$where = "(SELECT member_transaction_id FROM sw_member_placement WHERE transaction_type = 10 AND credit_amount > 0 AND to_member_id = " . $this->db->dbprefix('members') . ".member_id LIMIT 1)";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_member_package') == '1') {
				$where = $this->db->dbprefix('members') . ".package_type > 0";
				$this->db->where($where);
			}
			else if($this->session->userdata('member_search_member_package') == '2') {
				$where = $this->db->dbprefix('members') . ".package_type = 0";
				$this->db->where($where);
			}
			
			$this->db->order_by($this->db->dbprefix('members') . '.contract_start_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['active']) {
					case 0 : $status = $this->lang->line('registration_suspended'); break;
					case 1 : $status = $this->lang->line('registration_active'); break;
				}
				
				$data[] = array (
									$aRow['member_id'],
									$aRow['username'],
									$aRow['casino_username'],
									$aRow['id_no'],
									$aRow['mobile_no'],
									$aRow['address'] . ', ' . $aRow['postal_code'] . ', ' . $aRow['city'] . ', ' . $aRow['state'] . ', ' . $this->lang->line(get_country_name($aRow['country'])),
									$aRow['introducer'],
									$aRow['introducer_name'],
									$aRow['introducer_id_no'],
									$aRow['introducer_mobile_no'],
									$this->lang->line(get_package($aRow['package_type'])) . (($aRow['special_package_type'] > 0) ? ' (* ' . $this->lang->line(get_package($aRow['special_package_type'])) . ')' : ''),
									$this->lang->line(get_member_type($aRow['member_type'])),
									$this->lang->line(get_level($aRow['level'])) . (($aRow['special_level'] > 0) ? ' (* ' . $this->lang->line(get_level($aRow['special_level'])) . ')' : ''),
									$status,
									date('Y-m-d H:i:s', $aRow['contract_start_date'])
								);
			}
					
			$xls = new Excel_XML('UTF-8', false, 'Member Downline List');
			$xls->addArray($data);
			$xls->generateXML('member_downline_list');
		}
	}
	
	public function reinvest_listing()
	{
		save_current_url(base_url() . 'member/reinvest_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_downline' => trim($this->input->post('search_downline', TRUE)), 
						'member_search_register_date' => trim($this->input->post('search_register_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_package_type' => ($this->input->post('search_package_type', TRUE) != '') ? implode(',', $this->input->post('search_package_type', TRUE)) : '',
						'member_search_status' => ($this->input->post('search_status', TRUE) != '') ? implode(',', $this->input->post('search_status', TRUE)) : '', 
						)
				);
				
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_member_manager') . ' - ' . $this->lang->line('hf_member_reinvest_list');
			
				$this->load->view('member_reinvest_list', $_POST);
			}
			else {
				$this->session->unset_userdata(array('member_search_username' => '', 'member_search_downline' => '', 'member_search_register_date' => '', 'member_search_start_date' => '', 'member_search_end_date' => '', 'member_search_package_type' => '', 'member_search_status' => ''));
				
				$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_member_manager') . ' - ' . $this->lang->line('hf_member_reinvest_list');
			
				$this->load->view('member_reinvest_list', $data);
			}
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function getreinvestlist()
    {
		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_reinvest') . '.member_id', 
								$this->db->dbprefix('members') . '.username', 
								$this->db->dbprefix('members') . '.full_name',
								$this->db->dbprefix('members') . '.introducer',
								$this->db->dbprefix('members') . '.active',
								$this->db->dbprefix('members') . '.origin_package_type',
								$this->db->dbprefix('member_reinvest') . '.reinvest_package_type', 
								$this->db->dbprefix('member_reinvest') . '.reinvest_package_date'
							);
			
			$jColumns = $this->db->dbprefix('member_reinvest') . '.member_id = ' . $this->db->dbprefix('members') . '.member_id';
			
			// DB table to use
			$sTable = 'member_reinvest';
			$jTable = 'members';
			//
			
			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_search_downline') != '') {
				$arr = explode(',', $this->session->userdata('member_search_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 . ",%'";
						}
						else {
							$where .= $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%'";
						}
					}
					
					if($where != '') {
						$where = "(" . $where . ")";
						$this->db->where($where);
					}
				}
			}
			
			if($this->session->userdata('member_search_register_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_reinvest') . ".reinvest_package_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_reinvest') . ".reinvest_package_date <= '" .  $end_date . "')";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_package_type') != '') {
				$where = $this->db->dbprefix('member_reinvest') . ".reinvest_package_type IN(" .  $this->session->userdata('member_search_package_type') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_status') != '') {
				$where = $this->db->dbprefix('members') . ".active IN(" .  $this->session->userdata('member_search_status') . ")";
				$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[] = $aRow['member_id'];
				$row[] = $aRow['username'];
				$row[] = $aRow['full_name'];
				$row[] = $aRow['introducer'];
				
				switch($aRow['active']) {
					case 0 : $row[] = '<span class="label label-fail">' . $this->lang->line('registration_suspended') . '</span>'; break;
					case 1 : $row[] = '<span class="label label-success">' . $this->lang->line('registration_active') . '</span>'; break;
				}
				
				$row[] = $this->lang->line(get_package($aRow['origin_package_type']));
				$row[] = $this->lang->line(get_package($aRow['reinvest_package_type']));
				$row[] = date('Y-m-d H:i:s', $aRow['reinvest_package_date']);
				$row[] = '<a href="' . base_url() . 'member/edit/' . $aRow['member_id'] . '/">' . $this->lang->line('member_edit') . '</a>';
				
				$output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}	
    }
	
	public function reinvest_listing_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('member_user_id'),
								$this->lang->line('member_username'),
								$this->lang->line('member_name'),
								$this->lang->line('member_introducer'),
								$this->lang->line('member_status'),
								$this->lang->line('member_old_package'),
								$this->lang->line('member_package'),
								$this->lang->line('member_reinvest_date')
							);
			
			//Get Data
			$aColumns = array(
								$this->db->dbprefix('member_reinvest') . '.member_id', 
								$this->db->dbprefix('members') . '.username', 
								$this->db->dbprefix('members') . '.full_name',
								$this->db->dbprefix('members') . '.introducer',
								$this->db->dbprefix('members') . '.active',
								$this->db->dbprefix('members') . '.origin_package_type',
								$this->db->dbprefix('member_reinvest') . '.reinvest_package_type', 
								$this->db->dbprefix('member_reinvest') . '.reinvest_package_date'
							);
			
			$jColumns = $this->db->dbprefix('member_reinvest') . '.member_id = ' . $this->db->dbprefix('members') . '.member_id';
			
			// DB table to use
			$sTable = 'member_reinvest';
			$jTable = 'members';
			//
			
			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_search_downline') != '') {
				$arr = explode(',', $this->session->userdata('member_search_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 . ",%'";
						}
						else {
							$where .= $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%'";
						}
					}
					
					if($where != '') {
						$where = "(" . $where . ")";
						$this->db->where($where);
					}
				}
			}
			
			if($this->session->userdata('member_search_register_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_reinvest') . ".reinvest_package_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_reinvest') . ".reinvest_package_date <= '" .  $end_date . "')";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_package_type') != '') {
				$where = $this->db->dbprefix('member_reinvest') . ".reinvest_package_type IN(" .  $this->session->userdata('member_search_package_type') . ")";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_status') != '') {
				$where = $this->db->dbprefix('members') . ".active IN(" .  $this->session->userdata('member_search_status') . ")";
				$this->db->where($where);
			}
			
			$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['active']) {
					case 0 : $status = $this->lang->line('registration_suspended'); break;
					case 1 : $status = $this->lang->line('registration_active'); break;
				}
				
				$data[] = array (
									$aRow['member_id'],
									$aRow['username'],
									$aRow['full_name'],
									$aRow['introducer'],
									$this->lang->line(get_package($aRow['origin_package_type'])),
									$this->lang->line(get_package($aRow['reinvest_package_type'])),
									date('Y-m-d H:i:s', $aRow['reinvest_package_date'])
								);
			}
				
			$xls = new Excel_XML('UTF-8', false, 'Member Reinvest List');
			$xls->addArray($data);
			$xls->generateXML('member_reinvest_list');
		}
	}
	
	public function intro()
	{
		save_current_url(base_url() . 'member/intro/');
		
		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_register_date' => trim($this->input->post('search_register_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_amount_from' => trim($this->input->post('search_amount_from', TRUE)), 
						'member_search_amount_to' => trim($this->input->post('search_amount_to', TRUE))
						)
				);
				
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_member_manager') . ' - ' .$this->lang->line('hf_member_intro_list');
			
				$this->load->view('member_intro_list', $_POST);
			}
			else {
				$this->session->unset_userdata(array('member_search_username' => '', 'member_search_register_date' => '', 'member_search_start_date' => '', 'member_search_end_date' => '', 'member_search_amount_from' => '', 'member_search_amount_to' => ''));
				
				$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_member_manager') . ' - ' .$this->lang->line('hf_member_intro_list');
			
				$this->load->view('member_intro_list', $data);
			}	
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function getintrolist()
    {
		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(
								'c.member_id AS member_id', 
								'c.username', 
								'c.full_name',
								'c.package_type', 
								'c.member_type',
								'SUM(b.package_price) AS package_value',
								'c.special_package_type'
							);
			
			$jColumns = $this->db->dbprefix('members') . '.package_type = b.package_id';
			$j2Columns = $this->db->dbprefix('members') . '.introducer = c.username';
			
			// DB table to use
			$sTable = 'members';
			$jTable = 'packages b';
			$j2Table = 'members c';
			//
			
			if($this->session->userdata('member_search_username') != '') {
				$where = $this->db->dbprefix('members') . ".introducer LIKE '%" .  $this->session->userdata('member_search_username') . "%'";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_register_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('members') . ".contract_start_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('members') . ".contract_start_date <= '" .  $end_date . "')";
				$this->db->where($where);
			}
			
			$this->db->where($this->db->dbprefix('members') . '.introducer !=', '');
			$this->db->where($this->db->dbprefix('members') . '.member_type', 2);
				
			$this->db->group_by('member_id'); 
			
			if($this->session->userdata('member_search_amount_from') != '' && $this->session->userdata('member_search_amount_from') >= 0) {
				$this->db->having('package_value >= ', $this->session->userdata('member_search_amount_from'));
			}
			
			if($this->session->userdata('member_search_amount_to') != '' && $this->session->userdata('member_search_amount_to') >= 0) {
				$this->db->having('package_value <= ', $this->session->userdata('member_search_amount_to'));
			}
			
			$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')
					{
						if($aColumns[intval($this->db->escape_str($iSortCol))] == 'c.member_id AS member_id') {
							$this->db->order_by('member_id', $this->db->escape_str($sSortDir));
						}
						else if($aColumns[intval($this->db->escape_str($iSortCol))] == 'c.username') {
							$this->db->order_by('username', $this->db->escape_str($sSortDir));
						}
						else if($aColumns[intval($this->db->escape_str($iSortCol))] == 'c.full_name') {
							$this->db->order_by('full_name', $this->db->escape_str($sSortDir));
						}
						else if($aColumns[intval($this->db->escape_str($iSortCol))] == 'c.package_type') {
							$this->db->order_by('package_type', $this->db->escape_str($sSortDir));
						}
						else if($aColumns[intval($this->db->escape_str($iSortCol))] == 'c.member_type') {
							$this->db->order_by('member_type', $this->db->escape_str($sSortDir));
						}
						else if($aColumns[intval($this->db->escape_str($iSortCol))] == 'SUM(b.package_price) AS package_value') {
							$this->db->order_by('package_value', $this->db->escape_str($sSortDir));
						}
						else {
							$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);
			$this->db->join($j2Table, $j2Columns, '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();
				
				$row[] = $aRow['member_id'];
				$row[] = $aRow['username'];
				$row[] = $aRow['full_name'];
				$row[] = $this->lang->line(get_package($aRow['package_type'])) . (($aRow['special_package_type'] > 0) ? ' (* ' . $this->lang->line(get_package($aRow['special_package_type'])) . ')' : '');
				$row[] = $this->lang->line(get_member_type($aRow['member_type']));
				$row[] = $aRow['package_value'];
				
				$output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}	
    }
	
	public function intro_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('member_user_id'),
								$this->lang->line('member_username'),
								$this->lang->line('member_name'),
								$this->lang->line('member_package'),
								$this->lang->line('member_member_type'),
								$this->lang->line('member_introduce_value')
							);
			
			//Get Data
			$aColumns = array(
								'c.member_id AS member_id', 
								'c.username', 
								'c.full_name',
								'c.package_type', 
								'c.member_type',
								'SUM(b.package_price) AS package_value',
								'c.special_package_type'
							);
			
			$jColumns = $this->db->dbprefix('members') . '.package_type = b.package_id';
			$j2Columns = $this->db->dbprefix('members') . '.introducer = c.username';
			
			// DB table to use
			$sTable = 'members';
			$jTable = 'packages b';
			$j2Table = 'members c';
			//
			
			if($this->session->userdata('member_search_username') != '') {
				$where = $this->db->dbprefix('members') . ".introducer LIKE '%" .  $this->session->userdata('member_search_username') . "%'";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_register_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('members') . ".contract_start_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('members') . ".contract_start_date <= '" .  $end_date . "')";
				$this->db->where($where);
			}
			
			$this->db->where($this->db->dbprefix('members') . '.introducer !=', '');
			$this->db->where($this->db->dbprefix('members') . '.member_type', 2);
				
			$this->db->group_by('member_id'); 
			
			if($this->session->userdata('member_search_amount_from') != '' && $this->session->userdata('member_search_amount_from') >= 0) {
				$this->db->having('package_value >= ', $this->session->userdata('member_search_amount_from'));
			}
			
			if($this->session->userdata('member_search_amount_to') != '' && $this->session->userdata('member_search_amount_to') >= 0) {
				$this->db->having('package_value <= ', $this->session->userdata('member_search_amount_to'));
			}
			
			$this->db->order_by('package_value', 'asc');
			
			$this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			$this->db->join($jTable, $jColumns);
			$this->db->join($j2Table, $j2Columns, 'left');
			$rResult = $this->db->get($sTable);
			
			foreach($rResult->result_array() as $aRow)
			{
				$data[] = array (
									$aRow['member_id'],
									$aRow['username'],
									$aRow['full_name'],
									$this->lang->line(get_package($aRow['package_type'])) . (($aRow['special_package_type'] > 0) ? ' (* ' . $this->lang->line(get_package($aRow['special_package_type'])) . ')' : ''),
									$this->lang->line(get_member_type($aRow['member_type'])),
									$aRow['package_value']
								);
			}
			
			$filename='introducer_list.xls'; //save our workbook as this file name
			header('Content-Type: application/vnd.ms-excel'); //mime type
			header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
			header('Cache-Control: max-age=0'); //no cache
						
			$xls = new Excel_XML('UTF-8', false, 'Introducer List');
			$xls->addArray($data);
			$xls->generateXML('introducer_list');
		}
	}
	
	public function structure($username = NULL)
	{
		save_current_url(base_url() . 'member/structure/' . $username . '/');
		
		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');
			
			if($_POST) {
				$this->session->set_userdata(array('report_username' => trim($this->input->post('username', TRUE))));
				$username = $this->input->post('username', TRUE);
			
				$_POST['json'] = $this->user_model->get_member_downline($username);
				
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_member_manager') . ' - ' .$this->lang->line('hf_member_structure');
				
				$this->load->view('member_structure', $_POST);
			}
			else {
				$this->session->unset_userdata(array('report_username' => ''));
				
				$data['json'] = $this->user_model->get_member_downline($username);
			
				$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_member_manager') . ' - ' .$this->lang->line('hf_member_structure');
				
				$this->load->view('member_structure', $data);
			}
		}
		else {
			redirect('login', 'refresh');
		}
	}

	public function topup_package_listing()
	{
		save_current_url(base_url() . 'member/topup_package_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_topup_date' => trim($this->input->post('search_topup_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_member_manager') . ' - ' .$this->lang->line('hf_topup_package_history');
			
				$this->load->view('member_topup_package_list', $_POST);
			}
			else {
				$this->session->unset_userdata(array('member_search_username' => '', 'member_search_topup_date' => '', 'member_search_start_date' => '', 'member_search_end_date' => ''));
				
				$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_member_manager') . ' - ' .$this->lang->line('hf_topup_package_history');
			
				$this->load->view('member_topup_package_list', $data);
			}
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	public function get_topup_package_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_topup_package') . '.transaction_date', 
								$this->db->dbprefix('member_topup_package') . '.transaction_amount', 
								$this->db->dbprefix('member_topup_package') . '.from_member_username', 
								$this->db->dbprefix('member_topup_package') . '.status',
							);
			
			// DB table to use
			$sTable = 'member_topup_package';
			//
			
			if($this->session->userdata('member_search_username') != '') {
				$where = $this->db->dbprefix('member_topup_package') . ".from_member_username = '" . $this->session->userdata('member_search_username') . "'";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_topup_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_topup_package') . ".transaction_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_topup_package') . ".transaction_date <= '" .  $end_date . "')";
				$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);
			$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 H:i:s', $aRow['transaction_date']);
				$row[] = number_format($aRow['transaction_amount'], 2, '.', '');
				$row[] = $aRow['from_member_username'];
				
				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;
				}
				
				$output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}	
    }
	
	public function topup_package_listing_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('member_transaction_date'),
								$this->lang->line('member_amount'),
								$this->lang->line('member_username'),
								$this->lang->line('member_status')
							);
			
			//Get Data
			$aColumns = array(
								$this->db->dbprefix('member_topup_package') . '.transaction_date', 
								$this->db->dbprefix('member_topup_package') . '.transaction_amount', 
								$this->db->dbprefix('member_topup_package') . '.from_member_username', 
								$this->db->dbprefix('member_topup_package') . '.status',
							);
			
			// DB table to use
			$sTable = 'member_topup_package';
			//
			
			if($this->session->userdata('member_search_username') != '') {
				$where = $this->db->dbprefix('member_topup_package') . ".from_member_username = '" . $this->session->userdata('member_search_username') . "'";
				$this->db->where($where);
			}
			
			if($this->session->userdata('member_search_topup_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_topup_package') . ".transaction_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_topup_package') . ".transaction_date <= '" .  $end_date . "')";
				$this->db->where($where);
			}
			
			$this->db->order_by($this->db->dbprefix('member_topup_package') . '.transaction_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 (
									date('Y-m-d H:i:s', $aRow['transaction_date']),
									number_format($aRow['transaction_amount'], 2, '.', ''),
									$aRow['from_member_username'],
									$status
								);
			}
			
			$xls = new Excel_XML('UTF-8', false, 'Member List');
			$xls->addArray($data);
			$xls->generateXML('member_topup_package_list');
		}
	}
	
	public function sales()
	{
		save_current_url(base_url() . 'member/sales/');
		
		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_downline' => trim($this->input->post('search_downline', TRUE)), 
						'member_search_register_date' => trim($this->input->post('search_register_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_member_type' => ($this->input->post('search_member_type', TRUE) != '') ? implode(',', $this->input->post('search_member_type', TRUE)) : '', 
						)
				);
				
				$_POST['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_member_manager') . ' - ' .$this->lang->line('hf_member_sales_list');
			
				$this->load->view('member_sales_list', $_POST);
			}
			else {
				$this->session->unset_userdata(array('member_search_username' => '', 'member_search_downline' => '', 'member_search_register_date' => '', 'member_search_start_date' => '', 'member_search_end_date' => '', 'member_search_member_type' => ''));
				
				$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_member_manager') . ' - ' .$this->lang->line('hf_member_sales_list');
			
				$this->load->view('member_sales_list', $data);
			}	
		}
		else {
			redirect('login', 'refresh');
		}
	}
	
	// public function getsaleslist()
    // {
		// 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('members') . '.member_id', 
								// $this->db->dbprefix('members') . '.username', 
								// $this->db->dbprefix('members') . '.full_name',
								// $this->db->dbprefix('members') . '.member_type',
								// 'SUM(' . $this->db->dbprefix('member_sales') . '.amount) AS total_invest'
							// );
			
			// $jColumns = $this->db->dbprefix('member_sales') . '.member_id = ' . $this->db->dbprefix('members') . '.member_id';
			
			// // DB table to use
			// $sTable = 'member_sales';
			// $jTable = 'members';
			// //
			
			// 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_search_downline') != '') {
				// $arr = explode(',', $this->session->userdata('member_search_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 . ",%'";
						// }
						// else {
							// $where .= $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%'";
						// }
					// }
					
					// if($where != '') {
						// $where = "(" . $where . ")";
						// $this->db->where($where);
					// }
				// }
			// }
			
			// if($this->session->userdata('member_search_register_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_sales') . ".transaction_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_sales') . ".transaction_date <= '" .  $end_date . "')";
				// $this->db->where($where);
			// }
			
			// if($this->session->userdata('member_search_member_type') != '') {
				// $where = $this->db->dbprefix('members') . ".member_type IN(" .  $this->session->userdata('member_search_member_type') . ")";
				// $this->db->where($where);
			// }
			
			// $this->db->group_by('member_id'); 
			
			// $iDisplayStart = $this->input->get_post('iDisplayStart', true);
			// $iDisplayLength = $this->input->get_post('iDisplayLength', true);
			// $iSortCol_0 = $this->input->get_post('iSortCol_0', true);
			// $iSortingCols = $this->input->get_post('iSortingCols', true);
			// $sSearch = $this->input->get_post('sSearch', true);
			// $sEcho = $this->input->get_post('sEcho', true);
		
			// // Paging
			// if(isset($iDisplayStart) && $iDisplayLength != '-1')
			// {
				// $this->db->limit($this->db->escape_str($iDisplayLength), $this->db->escape_str($iDisplayStart));
			// }
			
			// // Ordering
			// if(isset($iSortCol_0))
			// {
				// for($i=0; $i<intval($iSortingCols); $i++)
				// {
					// $iSortCol = $this->input->get_post('iSortCol_'.$i, true);
					// $bSortable = $this->input->get_post('bSortable_'.intval($iSortCol), true);
					// $sSortDir = $this->input->get_post('sSortDir_'.$i, true);
		
					// if($bSortable == 'true')
					// {
						// $this->db->order_by($aColumns[intval($this->db->escape_str($iSortCol))], $this->db->escape_str($sSortDir));
					// }
				// }
			// }
			
			// /* 
			 // * Filtering
			 // * NOTE this does not match the built-in DataTables filtering which does it
			 // * word by word on any field. It's possible to do here, but concerned about efficiency
			 // * on very large tables, and MySQL's regex functionality is very limited
			 // */
			// if(isset($sSearch) && !empty($sSearch))
			// {
				// for($i=0; $i<count($aColumns); $i++)
				// {
					// $bSearchable = $this->input->get_post('bSearchable_'.$i, true);
					
					// // Individual column filtering
					// if(isset($bSearchable) && $bSearchable == 'true')
					// {
						// $this->db->or_like($aColumns[$i], $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[] = $aRow['member_id'];
				// $row[] = $aRow['username'];
				// $row[] = $aRow['full_name'];
				// $row[] = $this->lang->line(get_member_type($aRow['member_type']));
				// $row[] = $aRow['total_invest'];
				
				// $output['aaData'][] = $row;
			// }
		
			// echo json_encode($output);
		// }	
    // }
	
	// public function sales_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('member_user_id'),
								// $this->lang->line('member_username'),
								// $this->lang->line('member_name'),
								// $this->lang->line('member_member_type'),
								// $this->lang->line('member_invest_value')
							// );
			
			// //Get Data
			// $aColumns = array(
								// $this->db->dbprefix('members') . '.member_id', 
								// $this->db->dbprefix('members') . '.username', 
								// $this->db->dbprefix('members') . '.full_name',
								// $this->db->dbprefix('members') . '.member_type',
								// 'SUM(' . $this->db->dbprefix('member_sales') . '.amount) AS total_invest'
							// );
			
			// $jColumns = $this->db->dbprefix('member_sales') . '.member_id = ' . $this->db->dbprefix('members') . '.member_id';
			
			// // DB table to use
			// $sTable = 'member_sales';
			// $jTable = 'members';
			// //
			
			// 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_search_downline') != '') {
				// $arr = explode(',', $this->session->userdata('member_search_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 . ",%'";
						// }
						// else {
							// $where .= $this->db->dbprefix('members') . ".introducer_parent_ref NOT LIKE '%," .  $row->member_id . ",%'";
						// }
					// }
					
					// if($where != '') {
						// $where = "(" . $where . ")";
						// $this->db->where($where);
					// }
				// }
			// }
			
			// if($this->session->userdata('member_search_register_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_sales') . ".transaction_date >= '" .  $start_date . "' AND " . $this->db->dbprefix('member_sales') . ".transaction_date <= '" .  $end_date . "')";
				// $this->db->where($where);
			// }
			
			// if($this->session->userdata('member_search_member_type') != '') {
				// $where = $this->db->dbprefix('members') . ".member_type IN(" .  $this->session->userdata('member_search_member_type') . ")";
				// $this->db->where($where);
			// }
			
			// $this->db->group_by('member_id');
			// $this->db->order_by('total_invest', '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)
			// {
				// $data[] = array (
									// $aRow['member_id'],
									// $aRow['username'],
									// $aRow['full_name'],
									// $this->lang->line(get_member_type($aRow['member_type'])),
									// $aRow['total_invest']
								// );
			// }
			
			// $filename='introducer_list.xls'; //save our workbook as this file name
			// header('Content-Type: application/vnd.ms-excel'); //mime type
			// header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
			// header('Cache-Control: max-age=0'); //no cache
						
			// $xls = new Excel_XML('UTF-8', false, 'Member Sales List');
			// $xls->addArray($data);
			// $xls->generateXML('member_sales_list');
		// }
	// }
	
	public function getsaleslist()
    {
		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());
			
			$sEcho = $this->input->get_post('sEcho', true);
			
			//Get all package details
			$packages = array();
			$package_query = $this->db->query("SELECT * FROM sw_packages");
			if($package_query->num_rows() > 0) {
				foreach($package_query->result() as $package_row) {
					$packages[$package_row->package_id] = array(
																'package_price' => $package_row->package_price
															);									
				}
			}
			
			$last_member_id = 10000000;
			$member_query = $this->db->query("SELECT member_id FROM sw_members ORDER BY member_id DESC LIMIT 1");
			if($member_query->num_rows() > 0) {
				$member_row = $member_query->row();
				$last_member_id = $member_row->member_id;
			}
			
			//Prepare reinvest sales
			$reinvest_sales_lists = array();	
			$reinvest_query = $this->db->query("SELECT reinvest_package_type, member_id FROM sw_member_reinvest WHERE member_id <= ?", array($last_member_id));
			if($reinvest_query->num_rows() > 0) {
				foreach($reinvest_query->result() as $reinvest_row) {
					if(isset($reinvest_sales_lists[$reinvest_row->member_id]['package_type'])) {
						if($reinvest_row->reinvest_package_type > $reinvest_sales_lists[$reinvest_row->member_id]['package_type']) {
							$reinvest_sales_lists[$reinvest_row->member_id]['package_type'] = $reinvest_row->reinvest_package_type;
						}
					}
					else {
						$reinvest_sales_lists[$reinvest_row->member_id]['package_type'] = $reinvest_row->reinvest_package_type;
					}					
				}
			}
			
			//Prepare member list
			$member_lists = array();	
			$member_query = $this->db->query("SELECT member_id, username, package_type, member_type FROM sw_members WHERE member_id <= ?", array($last_member_id));
			if($member_query->num_rows() > 0) {
				foreach($member_query->result() as $member_row) {
					$member_lists[$member_row->member_id] = array(
																	'member_id' => $member_row->member_id,
																	'username' => $member_row->username,
																	'package_type' => $member_row->package_type,
																	'member_type' => $member_row->member_type,
																	'total_sales' => 0
																);	
					
					if(isset($reinvest_sales_lists[$member_row->member_id]['package_type'])) {	
						if($reinvest_sales_lists[$member_row->member_id]['package_type'] > $member_lists[$member_row->member_id]['package_type']) {
							$member_lists[$member_row->member_id]['package_type'] = $reinvest_sales_lists[$member_row->member_id]['package_type'];
						}
					}
				}
			}
			
			//Prepare sales
			$member_query = $this->db->query("SELECT member_id, member_type, active, introducer_parent_ref FROM sw_members WHERE member_id <= ?", array($last_member_id));
			if($member_query->num_rows() > 0) {
				foreach($member_query->result() as $member_row) {
					if($member_row->introducer_parent_ref != '') {
						$arr = explode(',', $member_row->introducer_parent_ref);
						for($i=0; $i<sizeof($arr); $i++) {
							if($arr[$i] != '' AND $arr[$i] > 0) {
								if($member_row->member_type == 2 && $member_row->active == 1 && $member_lists[$member_row->member_id]['package_type'] > 0) {
									$member_lists[$arr[$i]]['total_sales'] = ($member_lists[$arr[$i]]['total_sales'] + $packages[$member_lists[$member_row->member_id]['package_type']]['package_price']);
								}
							}
						}	
					}
				}	
			}
			
			$rResult = array();
			foreach($member_lists as $arr) {
				if($arr['package_type'] == 5 && $arr['member_type'] == 2 && $arr['total_sales'] >= 2520000) {
					array_push($rResult, $arr);
				}
			}
			
			// Output
			$output = array(
				'sEcho' => intval($sEcho),
				'iTotalRecords' => sizeof($rResult),
				'iTotalDisplayRecords' => sizeof($rResult),
				'aaData' => array()
			);
			
			foreach($rResult as $aRow)
			{
				$row = array();
				
				$row[] = $aRow['member_id'];
				$row[] = $aRow['username'];
				$row[] = $aRow['total_sales'];
				
				$output['aaData'][] = $row;
			}
		
			echo json_encode($output);
		}	
    }
	
	public function sales_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";
			
			//Get all package details
			$packages = array();
			$package_query = $this->db->query("SELECT * FROM sw_packages");
			if($package_query->num_rows() > 0) {
				foreach($package_query->result() as $package_row) {
					$packages[$package_row->package_id] = array(
																'package_price' => $package_row->package_price
															);									
				}
			}
			
			$last_member_id = 10000000;
			$member_query = $this->db->query("SELECT member_id FROM sw_members ORDER BY member_id DESC LIMIT 1");
			if($member_query->num_rows() > 0) {
				$member_row = $member_query->row();
				$last_member_id = $member_row->member_id;
			}
			
			//Prepare reinvest sales
			$reinvest_sales_lists = array();	
			$reinvest_query = $this->db->query("SELECT reinvest_package_type, member_id FROM sw_member_reinvest WHERE member_id <= ?", array($last_member_id));
			if($reinvest_query->num_rows() > 0) {
				foreach($reinvest_query->result() as $reinvest_row) {
					if(isset($reinvest_sales_lists[$reinvest_row->member_id]['package_type'])) {
						if($reinvest_row->reinvest_package_type > $reinvest_sales_lists[$reinvest_row->member_id]['package_type']) {
							$reinvest_sales_lists[$reinvest_row->member_id]['package_type'] = $reinvest_row->reinvest_package_type;
						}
					}
					else {
						$reinvest_sales_lists[$reinvest_row->member_id]['package_type'] = $reinvest_row->reinvest_package_type;
					}					
				}
			}
			
			//Prepare member list
			$member_lists = array();	
			$member_query = $this->db->query("SELECT member_id, username, package_type, member_type FROM sw_members WHERE member_id <= ?", array($last_member_id));
			if($member_query->num_rows() > 0) {
				foreach($member_query->result() as $member_row) {
					$member_lists[$member_row->member_id] = array(
																	'member_id' => $member_row->member_id,
																	'username' => $member_row->username,
																	'package_type' => $member_row->package_type,
																	'member_type' => $member_row->member_type,
																	'total_sales' => 0
																);	
					
					if(isset($reinvest_sales_lists[$member_row->member_id]['package_type'])) {	
						if($reinvest_sales_lists[$member_row->member_id]['package_type'] > $member_lists[$member_row->member_id]['package_type']) {
							$member_lists[$member_row->member_id]['package_type'] = $reinvest_sales_lists[$member_row->member_id]['package_type'];
						}
					}
				}
			}
			
			//Prepare sales
			$member_query = $this->db->query("SELECT member_id, member_type, active, introducer_parent_ref FROM sw_members WHERE member_id <= ?", array($last_member_id));
			if($member_query->num_rows() > 0) {
				foreach($member_query->result() as $member_row) {
					if($member_row->introducer_parent_ref != '') {
						$arr = explode(',', $member_row->introducer_parent_ref);
						for($i=0; $i<sizeof($arr); $i++) {
							if($arr[$i] != '' AND $arr[$i] > 0) {
								if($member_row->member_type == 2 && $member_row->active == 1 && $member_lists[$member_row->member_id]['package_type'] > 0) {
									$member_lists[$arr[$i]]['total_sales'] = ($member_lists[$arr[$i]]['total_sales'] + $packages[$member_lists[$member_row->member_id]['package_type']]['package_price']);
								}
							}
						}	
					}
				}	
			}
			
			$rResult = array();
			foreach($member_lists as $arr) {
				if($arr['package_type'] == 5 && $arr['member_type'] == 2 && $arr['total_sales'] >= 2520000) {
					array_push($rResult, $arr);
				}
			}
			
			$data[] = array (
								$this->lang->line('member_user_id'),
								$this->lang->line('member_username'),
								$this->lang->line('member_name'),
								$this->lang->line('member_member_type'),
								$this->lang->line('member_invest_value')
							);
			
			foreach($rResult as $aRow)
			{
				$data[] = array (
									$aRow['member_id'],
									$aRow['username'],
									$aRow['total_sales']
								);
			}
			
			$filename='sales_list.xls'; //save our workbook as this file name
			header('Content-Type: application/vnd.ms-excel'); //mime type
			header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
			header('Cache-Control: max-age=0'); //no cache
						
			$xls = new Excel_XML('UTF-8', false, 'Member Sales List');
			$xls->addArray($data);
			$xls->generateXML('member_sales_list');
		}
	}
}

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