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

class Superviseur_dash extends CI_Controller
{

	public function __construct(){
		parent::__construct();
		$this->load->model('Users_simple');
		$this->load->model('User_model');
		date_default_timezone_set ($this->session->userdata('timezone'));
	}

	public function index()
	{
		if (!$this->session->userdata('email')) {

			$this->session->set_flashdata('error', 'log in first');
			$data['alert'] = "ok";
			$data['message'] = "Login first!";
			redirect('index.php', $data);

		} else {

			$count_g = $this->User_model->geolocatedCountByParrain($this->session->userdata('member_code'));
			$count_d = $this->User_model->getCountDemande($this->session->userdata('member_code'));
			$count_d_traite = $this->User_model->getCountDemandeByStatut($this->session->userdata('member_code'),1);
			$count_d_no_traite = $this->User_model->getCountDemandeByStatut($this->session->userdata('member_code'),0);

			$data['list_geolocated_users'] = $this->user_model->getUsersGeolocatedForSup($this->session->userdata('member_code'));

			$data['active'] = "dash";
			$data['token'] = $this->session->userdata('token');
			$data['email'] = $this->session->userdata('email');
			$data['firstname'] = $this->session->userdata('firstname');
			$data['lastname'] = $this->session->userdata('lastname');
			$data['member_code'] = $this->session->userdata('member_code');
			$data['phone'] = $this->session->userdata('phone');
			$data['adresse'] = $this->session->userdata('adresse');
			$data['category'] = $this->session->userdata('category');
			$data['network'] = $this->session->userdata('network');
			$data['code_parrain'] = $this->session->userdata('parrain');
			$data['listdem'] = $this->user_model->getAllDemandesForSuper($this->session->userdata('member_code'));
			$data['geolocated_user'] = $this->user_model->getSuperNameAndCodeForSup($this->session->userdata('member_code'));
			$data['count_g_users'] = $count_g;
			$data['count_d_users'] = $count_d;
			$data['count_d_traite'] = $count_d_traite;
			$data['count_d_no_traite'] = $count_d_no_traite;
			$data['hasWallet'] = $this->user_model->getConfigWallet($this->session->userdata('network_id'));
			$this->load->view('header_sup', $data);
			$this->load->view('superviseur_dash');
			$this->load->view('footer');
		}
	}

	/**
	 * Affiche la table des points agents du superviseur
	 */
	public function getAllUser_g(){

		if (!$this->session->userdata('email')) {
			$this->session->set_flashdata('error', 'log in first');

			$data['alert'] = "ok";
			$data['message'] = "Login first!";


			redirect('index.php', $data);
		}
		else {

			$position = $this->user_model->getAllpositions($this->session->userdata('network'),'geolocated',$this->session->userdata('member_code'));

			$data['active'] = "geolocated";
			$data['positions']= $position;
			$numberGeo = $this->user_model->getNumberGeoBySuper($this->session->userdata('member_code'));
			if($numberGeo!=false){
				$data['numberGeo'] = $numberGeo;
			}else{
				$data['numberGeo'] = "";
			}
			$data['map_title'] = $this->lang->line('Utilisateurs géolocalisés');
			$data['total_points'] = $this->user_model->getCoutGeolocatedUsersForSuper($this->session->userdata('member_code'));
			$data['tab'] = $this->user_model->getUsersGeolocatedForSup($this->session->userdata('member_code'));
			$data['latitude'] = $this->session->userdata('latitude');
			$data['longitude'] = $this->session->userdata('longitude');
			$data['token'] = $this->session->userdata('token');
			$data['email'] = $this->session->userdata('email');
			$data['firstname'] = $this->session->userdata('firstname');
			$data['lastname'] = $this->session->userdata('lastname');
			$data['code_parrain'] = $this->session->userdata('code_parrain');
			$data['phone'] = $this->session->userdata('phone');
			$data['adresse'] = $this->session->userdata('adresse');
			$data['category'] = $this->session->userdata('category');
			$data['network'] = $this->session->userdata('network');
			$data['geolocated_user'] = $this->user_model->getSuperNameAndCodeForSup($this->session->userdata('member_code'));
			$data['alert'] = "";
			$data['hasWallet'] = $this->user_model->getConfigWallet($this->session->userdata('network_id'));
			$this->load->view('header_sup', $data);
			$this->load->view('listeadmin');
			$this->load->view('footer');
		}
	}

	/**
	 * Affiche les demandes de credits
	 */
	public function getDemandes(){

		if (!$this->session->userdata('email')) {
			$this->session->set_flashdata('error', 'log in first');
			$data['alert'] = "ok";
			$data['message'] = "Login first!";
			$this->load->view('login', $data);
		} else {

			if($this->input->get("u") !== null){

				$user_geo = $this->input->get("u");
				$debut = $this->input->get("d");
				$fin = $this->input->get("f");
				$format = $this->session->userdata('site_lang') === 'french' ? 'd-m-Y' : 'Y-m-d' ;
				$data['debut'] = $debut ? date($format, strtotime($debut)) : null ;
				$data['fin'] = $fin ?date($format, strtotime($fin)): null ;
				$fin = Date('Y-m-d', strtotime($fin."+1 day")); // Increment date because SQL BETWEEN

				$network = $this->session->userdata('network');
				$data['active'] = "demandes";
				$data['type'] = $this->lang->line('crédits');
				$data['demand_type'] = 'credit';
				$data['alert'] = "";
				$data['token'] = $this->session->userdata('token');
				$data['email'] = $this->session->userdata('email');
				$data['firstname'] = $this->session->userdata('firstname');
				$data['lastname'] = $this->session->userdata('lastname');
				$data['code_parrain'] = $this->session->userdata('code_parrain');
				$data['phone'] = $this->session->userdata('phone');
				$data['adresse'] = $this->session->userdata('adresse');
				$data['category'] = $this->session->userdata('category');
				$data['network'] = $this->session->userdata('network');
				$data['tab_fastDemande'] = $this->user_model->getRangeASCDemandes($user_geo,$debut,$fin,$this->session->userdata('member_code'));
				$data['tab_fastDemande_s'] = $this->user_model->getRangeASCDemandesOut($this->session->userdata('member_code'));
				$data['tab_slowDemande'] = $this->user_model->getRangeDESCDemandes($user_geo,$debut,$fin,$this->session->userdata('member_code'));
				$data['tab_slowDemande_s'] = $this->user_model->getRangeDESCDemandesOut($this->session->userdata('member_code'));
				$data['mydem'] = $this->user_model->getDemandesHyperForSuper($this->session->userdata('member_code'));
				$data['temp_moyen'] = $this->user_model->getTempsMoyen($user_geo,$debut,$fin,$this->session->userdata('member_code'));
				$data['temp_moyen_mine'] = $this->user_model->getTempsMoyenDemandeSup($this->session->userdata('member_code'));
				$data['list'] = $this->user_model->getDemandesForSuper($user_geo,$debut,$fin,$this->session->userdata('member_code'));
				$data['geolocated_user'] = $this->user_model->getSuperNameAndCodeForSup($this->session->userdata('member_code'));
				$data['nameOfcurentGeolocatedUser'] = $this->user_model->nameOfCurrentGeolocatedUser_forSuper($user_geo);
				$data['hasWallet'] = $this->user_model->getConfigWallet($this->session->userdata('network_id'));
				$this->load->view('header_sup', $data);
				$this->load->view('demande_credit');
				$this->load->view('footer');
			}else{

			}

		}
	}

	/**
	 * Afficher les demandes d'adhesion
	 */
	public function getDemandesAd(){

		if (!$this->session->userdata('email')) {
			$this->session->set_flashdata('error', 'log in first');
			$data['alert'] = "ok";
			$data['message'] = "Login first!";
			$this->load->view('login', $data);
		} else {
			$network = $this->session->userdata('network');
			$debut = $this->input->get("d");
			$fin = $this->input->get("f");
			$format = $this->session->userdata('site_lang') === 'french' ? 'd-m-Y' : 'Y-m-d' ;
			$data['debut'] = $debut ? date($format, strtotime($debut)) : null ;
			$data['fin'] = $fin ?date($format, strtotime($fin)): null ;
			$data['active'] = "demandesAd";
			$data['alert'] = "";
			$data['demand_type'] = 'adhesion';
			$data['token'] = $this->session->userdata('token');
			$data['email'] = $this->session->userdata('email');
			$data['firstname'] = $this->session->userdata('firstname');
			$data['lastname'] = $this->session->userdata('lastname');
			$data['code_parrain'] = $this->session->userdata('code_parrain');
			$data['phone'] = $this->session->userdata('phone');
			$data['adresse'] = $this->session->userdata('adresse');
			$data['category'] = $this->session->userdata('category');
			$data['network'] = $this->session->userdata('network');
			$data['tab_fastDemande'] = $this->user_model->getRangeASCDemandesAd($debut, $fin ,$this->session->userdata('member_code'));
			$data['tab_slowDemande'] = $this->user_model->getRangeDESCDemandesAd($debut, $fin ,$this->session->userdata('member_code'));
			$data['mydem'] = $this->user_model->getDemandesHyperForSuper($this->session->userdata('member_code'));
			$data['temp_moyen'] = $this->user_model->getTempsMoyenAd($debut, $fin ,$this->session->userdata('member_code'));
			$data['list'] = $this->user_model->getDemandesAdForSuper($debut, $fin ,$this->session->userdata('member_code'));
			$data['type'] = $this->lang->line('Adhésion');
			$data['hasWallet'] = $this->user_model->getConfigWallet($this->session->userdata('network_id'));
			$data['geolocated_user'] = $this->user_model->getSuperNameAndCodeForSup($this->session->userdata('member_code'));
			$this->load->view('header_sup', $data);
			$this->load->view('demande_adhesion');
			$this->load->view('footer');
		}
	}

	// Wallet
	public function wallet(){
		if (!$this->session->userdata('email')) {
			$this->session->set_flashdata('error', 'log in first');

			$data['alert'] = "ok";
			$data['message'] = "Login first!";
			$this->load->view('login', $data);
		} else {
			$id_network = $this->session->userdata('network_id');
			$data["commission"] = "";
			$data["principal"] = "";
			$data["transactions"] = "";
			$agent_id = $this->session->userdata('agent_id');
			$data['hasWallet'] = $this->user_model->getConfigWallet($this->session->userdata('network_id'));

			if ($this->input->get('history')) {
				$this->historique($id_network, $this->input->get('d'), $this->input->get('f'), $this->session->userdata('member_code'));
			} else {
				//Create wallet if it not exist
				$res = $this->user_model->getWallet($agent_id );
				if($res == false){
					if($data['hasWallet']){
						$this->user_model->addWallet($agent_id );
					}
				}

				$res = $this->user_model->getWallet($agent_id);
				if ($res!=null){
					$row = $res->first_row();
					$data["commission"] = $row->balance_com;
					$data["principal"] = $row->balance_princ;
					$agents_g = $this->user_model->getUsersGeolocatedForSup($this->session->userdata('member_code'));
					$data['agents_g'] = $agents_g;

					//Fetch all transactions of any geolocated agent of the network
					$data["transactions"] = array();
					if($agents_g){
						foreach($agents_g->result() AS $row){
							$wallet =  $this->user_model->getWallet($row->agent_id);
							if($wallet){
								$transactions = $this->user_model->getAgentTransactions($wallet->first_row()->wallet_id);
								if($transactions){
									foreach($transactions->result() AS $trans){
										$trans->agent_id = $row->agent_id ; // Add agent id to transaction
										array_push( $data['transactions'], $trans);
									}
								}
							}
						}
					}
				}else{
					$data["commission"] = "N/A";
					$data["principal"] = "N/A";
					$data["totalCommissionBanque"] = "N/A";
					$data["transactions"] = false;
				}


				$data['alert'] = "";
				$data['active'] = "wallet";
				$data['token'] = $this->session->userdata('token');
				$data['email'] = $this->session->userdata('email');
				$data['firstname'] = $this->session->userdata('firstname');
				$data['lastname'] = $this->session->userdata('lastname');
				$data['code_parrain'] = $this->session->userdata('code_parrain');
				$data['phone'] = $this->session->userdata('phone');
				$data['adresse'] = $this->session->userdata('adresse');
				$data['category'] = $this->session->userdata('category');
				$data['network'] = $this->session->userdata('network');
				$data['network_id'] = $id_network;
				$data['villes'] = $this->user_model->getVilleByUserGeo($data['network'],$this->session->userdata('current_pays'));
				$data['pays'] = $this->user_model->getAllGameCountries();
				$data['networks'] = $this->user_model->getActiveNetwork();
				$data['agentWalletInfos'] = $this->user_model->getInfosWalletAgentForHyper($id_network);
				$data['geolocated_user'] = $this->user_model->getSuperNameAndCodeForSup($this->session->userdata('member_code'));
				$this->load->view('header_sup',$data);
				$this->load->view('gestion_wallet_sup');
				$this->load->view('footer');
			}

		}
	}

	private function historique($network_id, $startDate, $endDate, $codeMembre)
	{
		$data['configWallet'] = $this->user_model->getConfigWallet($network_id);
		$format = $this->session->userdata('site_lang') === 'french' ? 'd-m-Y' : 'Y-m-d';
		$data['startDate'] = $startDate ? date($format, strtotime($startDate)) : null;
		$data['endDate'] = $endDate ? date($format, strtotime($endDate)) : null;
		$endDate = Date('Y-m-d', strtotime($endDate . "+1 day"));
		$data['transactions'] = $this->user_model->getTransactionsSup($startDate, $endDate, $codeMembre);

		$data['active'] = "wallet";
		$data['alert'] = "";
		$data['firstname'] = $this->session->userdata('firstname');
		$data['lastname'] = $this->session->userdata('lastname');
		$data['email'] = $this->session->userdata('email');
		$data['network'] = $this->session->userdata('network');
		$data['villes'] = $this->user_model->getVilleByUserGeo($data['network'], $this->session->userdata('current_pays'));
		$data['networks'] = $this->user_model->getActiveNetwork();
		$data['superviseurs'] = $this->user_model->getSuperNameAndCodeForHyp($this->session->userdata('member_code'));
		$data['hasWallet'] = $this->user_model->getConfigWallet($this->session->userdata('network_id'));
		$data['country'] = $this->session->userdata('current_pays');
		$data['category'] = $this->session->userdata('category');
		$data['geolocated_user'] = $this->user_model->getSuperNameAndCodeForSup($this->session->userdata('member_code'));

		$this->load->view('header_sup', $data);
		$this->load->view('historique_transactions');
		$this->load->view('footer');
	}
}
