<?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'));
			$this->user_model->update_wallet($member_id);
			$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 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 update_insurance_date($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_insurance_date($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 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_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)) : '',
						)
				);
				
				$_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_register_date' => '', 'member_search_start_date' => '', 'member_search_end_date' => '', 'member_search_member_type' => '', 'member_search_package_type' => '', 'member_search_reinvest_package_type' => ''));
				
				$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') . '.total_profit',
								$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') . '.total_reinvest',
								$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') . '.allowed_game',
								$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_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_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);
			}
			
			$iDisplayStart = $this->input->get_post('iDisplayStart', true);
			$iDisplayLength = $this->input->get_post('iDisplayLength', true);
			$iSortCol_0 = $this->input->get_post('iSortCol_0', true);
			$iSortingCols = $this->input->get_post('iSortingCols', true);
			$sSearch = $this->input->get_post('sSearch', true);
			$sEcho = $this->input->get_post('sEcho', true);
		
			// Paging
			if(isset($iDisplayStart) && $iDisplayLength != '-1')
			{
				$this->db->limit($this->db->escape_str($iDisplayLength), $this->db->escape_str($iDisplayStart));
			}
			
			// Ordering
			if(isset($iSortCol_0))
			{
				for($i=0; $i<intval($iSortingCols); $i++)
				{
					$iSortCol = $this->input->get_post('iSortCol_'.$i, true);
					$bSortable = $this->input->get_post('bSortable_'.intval($iSortCol), true);
					$sSortDir = $this->input->get_post('sSortDir_'.$i, true);
		
					if($bSortable == 'true')
					{
						$this->db->order_by($aColumns[intval($this->db->escape_str($iSortCol))], $this->db->escape_str($sSortDir));
					}
				}
			}
			
			/* 
			 * Filtering
			 * NOTE this does not match the built-in DataTables filtering which does it
			 * word by word on any field. It's possible to do here, but concerned about efficiency
			 * on very large tables, and MySQL's regex functionality is very limited
			 */
			if(isset($sSearch) && !empty($sSearch))
			{
				for($i=0; $i<count($aColumns); $i++)
				{
					$bSearchable = $this->input->get_post('bSearchable_'.$i, true);
					
					// Individual column filtering
					if(isset($bSearchable) && $bSearchable == 'true')
					{
						$this->db->or_like($aColumns[$i], $this->db->escape_like_str($sSearch));
					}
				}
			}
			
			// Select Data
			$this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			$rResult = $this->db->get($sTable);
		
			// Data set length after filtering
			$this->db->select('FOUND_ROWS() AS found_rows');
			$iFilteredTotal = $this->db->get()->row()->found_rows;
		
			// Total data set length
			$iTotal = $this->db->count_all($sTable);
		
			// Output
			$output = array(
				'sEcho' => intval($sEcho),
				'iTotalRecords' => $iTotal,
				'iTotalDisplayRecords' => $iFilteredTotal,
				'aaData' => array()
			);
			
			foreach($rResult->result_array() as $aRow)
			{
				$row = array();
				
				$row[] = $aRow['member_id'];
				$row[] = '<a href="' . base_url() . 'member/structure/' . $aRow['username'] . '/">' . $aRow['username'] . '</a>';
				$row[] = $aRow['full_name'];
				$row[] = $aRow['total_profit'];
				$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'])) . ')' : '');
				$row[] = $aRow['total_reinvest'];
				
				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['allowed_game']) {
					case 0 : $row[] = '<span id="allowed_game_' . $aRow['member_id'] . '" class="label label-fail cursor-pointer" onclick="update_setting(7, ' . $aRow['member_id'] . ')">' . $this->lang->line('registration_deactivated') . '</span>'; break;
					case 1 : $row[] = '<span id="allowed_game_' . $aRow['member_id'] . '" class="label label-success cursor-pointer" onclick="update_setting(7, ' . $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>';

				$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_member_type'),
								$this->lang->line('member_level'),
								$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_game_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') . '.member_type', 
								$this->db->dbprefix('members') . '.level', 
								$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') . '.allowed_game',
								$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_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_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);
			}
			
			$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;
				}
				
				$allowed_game = '';
				switch($aRow['allowed_game']) {
					case 0 : $allowed_game = $this->lang->line('registration_deactivated'); break;
					case 1 : $allowed_game = $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_member_type($aRow['member_type'])),
									$this->lang->line(get_level($aRow['level'])) . (($aRow['special_level'] > 0) ? ' (* ' . $this->lang->line(get_level($aRow['special_level'])) . ')' : ''),
									$allowed_withdraw,
									$unlimited_withdraw,
									$allowed_transfer,
									$allowed_convert,
									$allowed_topup,
									$allowed_game,
									$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());
			
			$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' => ''));
			
			$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',
								// $this->db->dbprefix('member_wallets') . '.v_unit5',
								// $this->db->dbprefix('member_wallets') . '.v_unit6',
								$this->db->dbprefix('member_wallets') . '.v_unit7',
								$this->db->dbprefix('member_wallets') . '.v_unit8',
								// $this->db->dbprefix('member_wallets') . '.v_unit6',
								$this->db->dbprefix('member_wallets') . '.v_unit7',
								$this->db->dbprefix('member_wallets') . '.v_unit8',
								$this->db->dbprefix('members') . '.member_id', 
							);
			
			$jColumns = $this->db->dbprefix('member_wallets') . '.member_id = ' . $this->db->dbprefix('members') . '.member_id';
			
			// DB table to use
			$sTable = 'member_wallets';
			$jTable = 'members';
			//
			
			$iDisplayStart = $this->input->get_post('iDisplayStart', true);
			$iDisplayLength = $this->input->get_post('iDisplayLength', true);
			$iSortCol_0 = $this->input->get_post('iSortCol_0', true);
			$iSortingCols = $this->input->get_post('iSortingCols', true);
			$sSearch = $this->input->get_post('sSearch', true);
			$sEcho = $this->input->get_post('sEcho', true);
		
			// Paging
			if(isset($iDisplayStart) && $iDisplayLength != '-1')
			{
				$this->db->limit($this->db->escape_str($iDisplayLength), $this->db->escape_str($iDisplayStart));
			}
			
			// Ordering
			if(isset($iSortCol_0))
			{
				for($i=0; $i<intval($iSortingCols); $i++)
				{
					$iSortCol = $this->input->get_post('iSortCol_'.$i, true);
					$bSortable = $this->input->get_post('bSortable_'.intval($iSortCol), true);
					$sSortDir = $this->input->get_post('sSortDir_'.$i, true);
		
					if($bSortable == 'true')
					{
						$this->db->order_by($aColumns[intval($this->db->escape_str($iSortCol))], $this->db->escape_str($sSortDir));
					}
				}
			}
			
			/* 
			 * Filtering
			 * NOTE this does not match the built-in DataTables filtering which does it
			 * word by word on any field. It's possible to do here, but concerned about efficiency
			 * on very large tables, and MySQL's regex functionality is very limited
			 */
			if(isset($sSearch) && !empty($sSearch))
			{
				for($i=0; $i<count($aColumns); $i++)
				{
					$bSearchable = $this->input->get_post('bSearchable_'.$i, true);
					
					// Individual column filtering
					if(isset($bSearchable) && $bSearchable == 'true')
					{
						$this->db->or_like($aColumns[$i], $this->db->escape_like_str($sSearch));
					}
				}
			}
			
			// Select Data
			$this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			$this->db->join($jTable, $jColumns);
			$rResult = $this->db->get($sTable);
		
			// Data set length after filtering
			$this->db->select('FOUND_ROWS() AS found_rows');
			$iFilteredTotal = $this->db->get()->row()->found_rows;
		
			// Total data set length
			$iTotal = $this->db->count_all($sTable);
		
			// Output
			$output = array(
				'sEcho' => intval($sEcho),
				'iTotalRecords' => $iTotal,
				'iTotalDisplayRecords' => $iFilteredTotal,
				'aaData' => array()
			);
			
			foreach($rResult->result_array() as $aRow)
			{
				$row = array();
				
				$row[] = $aRow['member_id'];
				$row[] = $aRow['username'];
				// $row[] = $aRow['v_unit'];
				$row[] = $aRow['v_unit2'];
				// $row[] = $aRow['v_unit3'];
				// $row[] = $aRow['v_unit4'];
				// $row[] = $aRow['v_unit5'];
				// $row[] = '<span id="w1-' . $aRow['member_id'] . '">' . $aRow['v_unit6'] . '</span>';
				$row[] = '<span id="w2-' . $aRow['member_id'] . '">' .  $aRow['v_unit7'] . '</span>';
				$row[] = '<span id="w3-' . $aRow['member_id'] . '">' . $aRow['v_unit8'] . '</span>';
				// $row[] = $aRow['v_unit6'];
				$row[] = $aRow['v_unit7'];
				$row[] = $aRow['v_unit8'];
				$row[] = '<a style="cursor: pointer;" id="waction-' . $aRow['member_id'] . '" onclick="update_wallet(' . $aRow['member_id'] . ')">' . $this->lang->line('member_update_wallet') . '</a>';
				
				$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'),
								// $this->lang->line('wallet_5'),
								// $this->lang->line('wallet_6'),
								$this->lang->line('wallet_7'),
								$this->lang->line('wallet_8')
							);
			
			//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',
								// $this->db->dbprefix('member_wallets') . '.v_unit5',
								// $this->db->dbprefix('member_wallets') . '.v_unit6',
								$this->db->dbprefix('member_wallets') . '.v_unit7',
								$this->db->dbprefix('member_wallets') . '.v_unit8'
							);
			
			$jColumns = $this->db->dbprefix('member_wallets') . '.member_id = ' . $this->db->dbprefix('members') . '.member_id';
			
			// DB table to use
			$sTable = 'member_wallets';
			$jTable = 'members';
			//
			
			$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'],
									// $aRow['v_unit5'],
									// $aRow['v_unit6'],
									$aRow['v_unit7'],
									$aRow['v_unit8']
								);
			}
				
			$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)) : '',
						)
				);
				
				$_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' => ''));
				
				$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') . '.full_name',
								$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_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);
			}
			
			$iDisplayStart = $this->input->get_post('iDisplayStart', true);
			$iDisplayLength = $this->input->get_post('iDisplayLength', true);
			$iSortCol_0 = $this->input->get_post('iSortCol_0', true);
			$iSortingCols = $this->input->get_post('iSortingCols', true);
			$sSearch = $this->input->get_post('sSearch', true);
			$sEcho = $this->input->get_post('sEcho', true);
		
			// Paging
			if(isset($iDisplayStart) && $iDisplayLength != '-1')
			{
				$this->db->limit($this->db->escape_str($iDisplayLength), $this->db->escape_str($iDisplayStart));
			}
			
			// Ordering
			if(isset($iSortCol_0))
			{
				for($i=0; $i<intval($iSortingCols); $i++)
				{
					$iSortCol = $this->input->get_post('iSortCol_'.$i, true);
					$bSortable = $this->input->get_post('bSortable_'.intval($iSortCol), true);
					$sSortDir = $this->input->get_post('sSortDir_'.$i, true);
		
					if($bSortable == 'true')
					{
						$this->db->order_by($aColumns[intval($this->db->escape_str($iSortCol))], $this->db->escape_str($sSortDir));
					}
				}
			}
			
			/* 
			 * Filtering
			 * NOTE this does not match the built-in DataTables filtering which does it
			 * word by word on any field. It's possible to do here, but concerned about efficiency
			 * on very large tables, and MySQL's regex functionality is very limited
			 */
			if(isset($sSearch) && !empty($sSearch))
			{
				for($i=0; $i<count($aColumns); $i++)
				{
					$bSearchable = $this->input->get_post('bSearchable_'.$i, true);
					
					// Individual column filtering
					if(isset($bSearchable) && $bSearchable == 'true')
					{
						$this->db->or_like($aColumns[$i], $this->db->escape_like_str($sSearch));
					}
				}
			}
			
			// Select Data
			$this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			$rResult = $this->db->get($sTable);
		
			// Data set length after filtering
			$this->db->select('FOUND_ROWS() AS found_rows');
			$iFilteredTotal = $this->db->get()->row()->found_rows;
		
			// Total data set length
			$iTotal = $this->db->count_all($sTable);
		
			// Output
			$output = array(
				'sEcho' => intval($sEcho),
				'iTotalRecords' => $iTotal,
				'iTotalDisplayRecords' => $iFilteredTotal,
				'aaData' => array()
			);
			
			foreach($rResult->result_array() as $aRow)
			{
				$row = array();
				
				$row[] = $aRow['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[] = $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_name'),
								$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') . '.full_name',
								$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_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);
			}
			
			$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['full_name'],
									$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)) : ''
						)
				);
				
				$_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' => ''));
				
				$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('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);
			}
			
			$iDisplayStart = $this->input->get_post('iDisplayStart', true);
			$iDisplayLength = $this->input->get_post('iDisplayLength', true);
			$iSortCol_0 = $this->input->get_post('iSortCol_0', true);
			$iSortingCols = $this->input->get_post('iSortingCols', true);
			$sSearch = $this->input->get_post('sSearch', true);
			$sEcho = $this->input->get_post('sEcho', true);
		
			// Paging
			if(isset($iDisplayStart) && $iDisplayLength != '-1')
			{
				$this->db->limit($this->db->escape_str($iDisplayLength), $this->db->escape_str($iDisplayStart));
			}
			
			// Ordering
			if(isset($iSortCol_0))
			{
				for($i=0; $i<intval($iSortingCols); $i++)
				{
					$iSortCol = $this->input->get_post('iSortCol_'.$i, true);
					$bSortable = $this->input->get_post('bSortable_'.intval($iSortCol), true);
					$sSortDir = $this->input->get_post('sSortDir_'.$i, true);
		
					if($bSortable == 'true')
					{
						$this->db->order_by($aColumns[intval($this->db->escape_str($iSortCol))], $this->db->escape_str($sSortDir));
					}
				}
			}
			
			/* 
			 * Filtering
			 * NOTE this does not match the built-in DataTables filtering which does it
			 * word by word on any field. It's possible to do here, but concerned about efficiency
			 * on very large tables, and MySQL's regex functionality is very limited
			 */
			if(isset($sSearch) && !empty($sSearch))
			{
				for($i=0; $i<count($aColumns); $i++)
				{
					$bSearchable = $this->input->get_post('bSearchable_'.$i, true);
					
					// Individual column filtering
					if(isset($bSearchable) && $bSearchable == 'true')
					{
						$this->db->or_like($aColumns[$i], $this->db->escape_like_str($sSearch));
					}
				}
			}
			
			// Select Data
			$this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
			$this->db->join($jTable, $jColumns);
			$rResult = $this->db->get($sTable);
		
			// Data set length after filtering
			$this->db->select('FOUND_ROWS() AS found_rows');
			$iFilteredTotal = $this->db->get()->row()->found_rows;
		
			// Total data set length
			$iTotal = $this->db->count_all($sTable);
		
			// Output
			$output = array(
				'sEcho' => intval($sEcho),
				'iTotalRecords' => $iTotal,
				'iTotalDisplayRecords' => $iFilteredTotal,
				'aaData' => array()
			);
			
			foreach($rResult->result_array() as $aRow)
			{
				$row = array();
				
				$row[] = $aRow['member_id'];
				$row[] = $aRow['username'];
				$row[] = $aRow['full_name'];
				$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_package'),
								$this->lang->line('member_register_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('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);
			}
			
			$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_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], $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 update_wallet($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');
			
			$json = $this->user_model->update_wallet($member_id);
		}
		else {
			$json = array('v_unit6' => '0.00', 'v_unit7' => '0.00', 'v_unit8' => '0.00');
		}
		
		$this->output
				->set_status_header(200)
				->set_content_type('application/json', 'utf-8')
				->set_output(json_encode($json))
				->_display();
				
		exit();	
	}
}

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