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

use Brick\Money\Context\CustomContext;
use Brick\Money\CurrencyConverter;
use Brick\Money\ExchangeRateProvider\PDOProvider;
use Brick\Money\ExchangeRateProvider\PDOProviderConfiguration;
use Brick\Money\ExchangeRateProvider\BaseCurrencyProvider;
use Brick\Math\RoundingMode;
use Brick\Money\Money;
use Brick\Money\Context\AutoContext;
/**
 * Created by PhpStorm.
 * User: Hanry Nzale
 * Date: 03/08/2018
 * Time: 02:03
 */
class Hyperviseur_dash extends CI_Controller
{
	public function _contruct()
	{
		parent::_contruct();
		$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_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);
			$count_d = $this->user_model->getCountDemande($this->session->userdata('member_code'));

			$data['active'] = "dash";
			$data['list_geolocated_users'] = $this->user_model->getAllGeolocatedUsersForHyp($this->session->userdata('member_code'));
			$data['listdem'] = $this->user_model->getDemandes($this->session->userdata('member_code'));
			$data['allGeo'] = $this->user_model->getAllUserGeoForHyp($this->session->userdata('member_code'));
			$data['allDem'] = $this->user_model->getDemandes($this->session->userdata('member_code'));
			$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['superviseurs'] = $this->user_model->getSuperNameAndCodeForHyp($this->session->userdata('member_code'));
			$data['villes'] = $this->user_model->getVilleByUserGeo($data['network'], $this->session->userdata('current_pays'));
			$data['count_geo'] = $this->user_model->countUserGeoForHyp($this->session->userdata('member_code'));
			$data['count_sup'] = $this->user_model->countUser("super", 'all', $this->session->userdata('member_code'));
			$data['count_d_traite'] = $count_d_traite;
			$data['count_d_no_traite'] = $count_d_no_traite;
			$data['count_d_users'] = $count_d;
			$data['networks'] = $this->user_model->getNetworkByHyp($this->session->userdata('member_code'));
			$data['hasWallet'] = $this->user_model->getConfigWallet($this->session->userdata('network_id'));
			$this->load->view('header_hyp', $data);
			$this->load->view('hyperviseur_dash');
			$this->load->view('footer');
		}
	}

	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 {

			if ($this->input->post('ville')) {
				$this->session->set_userdata('current_ville', $this->input->post('ville'));
			}

			$data['active'] = "geolocated";
			$data['total_points'] = $this->user_model->getCoutGeolocatedUsersForHyper($this->session->userdata('member_code'), $this->session->userdata('current_ville'));
			$data['map_title'] = $this->lang->line('utilisateurs géolocalisés à') . " " . $this->session->userdata('current_ville');
			$data['tab'] = $this->user_model->getUsersGeolocatedByVilleForHyp($this->session->userdata('current_ville'), $this->session->userdata('member_code'));
			$data['positions'] = $this->user_model->getUsersGeolocatedPositionByVilleForHyp($this->session->userdata('current_ville'), $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['superviseurs'] = $this->user_model->getSuperNameAndCodeForHyp($this->session->userdata('member_code'));
			$data['villes'] = $this->user_model->getVilleByUserGeo($data['network'], $this->session->userdata('current_pays'));
			$data['networks'] = $this->user_model->getNetworkByHyp($this->session->userdata('member_code'));
			$data['alert'] = "";
			$data['hasWallet'] = $this->user_model->getConfigWallet($this->session->userdata('network_id'));
			$this->load->view('header_hyp', $data);
			$this->load->view('listeadmin');
			$this->load->view('footer');
		}

	}

	public function getAllSupervisor()
	{

		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 {

			/*$this->db->select('*');
			$this->db->from('users');
			$this->db->where('category','super');
			$this->db->where('network',$this->session->userdata('network'));
			$this->db->where('code_parrain',$this->session->userdata('code_parrain'));*/
			$query = $this->user_model->getSupervisorsForHyp($this->session->userdata('member_code'));

			$network = $this->session->userdata('network');
			$category = 'super';
			$position = $this->user_model->getAllpositions($network, $category, $this->session->userdata('member_code'));
			if ($position != null) {
				//var_dump($position);
				//echo json_encode($position);
			}
			$data['total_points'] = $this->user_model->getCoutSuperForHyper($this->session->userdata('member_code'));
			$data['positions'] = $position;
			$data['active'] = "super";
			$data['map_title'] = $this->lang->line('Superviseurs');
			$data['tab'] = $query;
			$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['villes'] = $this->user_model->getVilleByUserGeo($data['network'], $this->session->userdata('current_pays'));
			$data['superviseurs'] = $this->user_model->getSuperNameAndCodeForHyp($this->session->userdata('member_code'));
			$data['networks'] = $this->user_model->getNetworkByHyp($this->session->userdata('member_code'));
			$data['alert'] = "";
			$data['hasWallet'] = $this->user_model->getConfigWallet($this->session->userdata('network_id'));
			$this->load->view('header_hyp', $data);
			$this->load->view('listeadmin');
			$this->load->view('footer');
		}
	}

	public function getAllSupervisorPositions()
	{
		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 {
			$network = $this->session->userdata('network');
			$category = 'super';

			$position = $this->user_model->getAllpositions($network, $category, $this->session->userdata('member_code'));
			if ($position != null) {
				return $position;
			}
		}
	}

	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) {
				$super = $this->input->get("u");
				$this->session->set_userdata("current_super_demande", $super);

				$debut = "";
				$fin = "";
				if ($this->input->get("d") !== null) {
					$debut = $this->input->get("d");
					$fin = $this->input->get("f");
				} else {
					$fin = date('Y-m-d');
					$debut = Date('Y-m-d', strtotime("-5 days"));
				}
				$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"));

				$ville = $this->session->userdata('ville');
				$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['villes'] = $this->user_model->getVilleByUserGeo($data['network'], $this->session->userdata('current_pays'));
				$data['superviseurs'] = $this->user_model->getSuperNameAndCodeForHyp($this->session->userdata('member_code'));
				$data['tab_fastDemande'] = $this->user_model->getRangeASCDemandesByNetwork($debut, $fin, $this->session->userdata('member_code'), $this->session->userdata('current_demande_ville'), $super);
				$data['tab_slowDemande'] = $this->user_model->getRangeDESCDemandesByNetwork($debut, $fin, $this->session->userdata('member_code'), $this->session->userdata('current_demande_ville'), $super);
				$data['list'] = $this->user_model->getDemandesBySuper($this->session->userdata('member_code'), $super);
				$data['temp_moyen'] = $this->user_model->getTempsMoyenByVille($this->session->userdata('member_code'), $super, $this->session->userdata('network'));
				$data['networks'] = $this->user_model->getNetworkByHyp($this->session->userdata('member_code'));
				$data['hasWallet'] = $this->user_model->getConfigWallet($this->session->userdata('network_id'));
				$data['nameOfcurentGeolocatedUser'] = $this->user_model->nameOfCurrentGeolocatedUser_forSuper($super);
				$this->load->view('header_hyp', $data);
				$this->load->view('demande_credit');
				$this->load->view('footer');
			}
		}
	}

	public function addvilles()
	{
		$id = 1;
		$date = "2018-08-29";
		for ($i = 1; $i <= 267; $i++) {
			$newdate = date("Y-m-d", strtotime($date . " + 1 day + 1 hour"));
			$data = array(
				'dateAjout' => $date,
				'dateModif' => $newdate,
			);


			$newdate = date("Y-m-d", strtotime($date . " + 1 day + 3 hour"));
			$date = $newdate;

			$this->db->update('demande_superviseur', $data);
		}
	}

	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['demand_type'] = 'adhesion';
			$data['active'] = "demandesAd";
			$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->getRangeASCDemandesAd($debut, $fin, $this->session->userdata('member_code'));
			$data['tab_slowDemande'] = $this->user_model->getRangeDESCDemandesAd($debut, $fin, $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['villes'] = $this->user_model->getVilleByUserGeo($data['network'], $this->session->userdata('current_pays'));
			$data['superviseurs'] = $this->user_model->getSuperNameAndCodeForHyp($this->session->userdata('member_code'));
			$data['networks'] = $this->user_model->getNetworkByHyp($this->session->userdata('member_code'));
			$data['type'] = $this->lang->line('Adhésion');
			$data['hasWallet'] = $this->user_model->getConfigWallet($this->session->userdata('network_id'));
			$this->load->view('header_hyp', $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['hasWallet'] = $this->user_model->getConfigWallet($this->session->userdata('network_id'));
			if ($data['hasWallet']) {

				if ($this->input->get('history')) {
					$this->historique($id_network, $this->input->get('d'), $this->input->get('f'), $this->input->get('history'));
				} elseif ($this->input->get('config')) {
					$this->config_wallet($this->input->get('config'), $this->input->get('country'));
				} elseif ($this->input->get('show')) {
					$this->show_config_infos($this->input->get('show'));
				}else {
					$context = new AutoContext();

					$data["commission"] = "";
					$data["principal"] = "";
					$data["transactions"] = "";
					$agent_id = $this->session->userdata('agent_id');


					$taux = $data['hasWallet']->first_row();
					$data['idConfig'] = $taux->id;
					if ($data['hasWallet']->first_row()->type == 'visa') {
						$data['taux_client_r'] = $taux->taux_com_client_retrait;
						$data['taux_client_d'] = $taux->taux_com_client_depot;
						$data['taux_ag_r'] = $taux->taux_com_ag_retrait;
						$data['taux_ag_d'] = $taux->taux_com_ag_depot;
						$data['taux_sup_r'] = $taux->taux_com_sup_retrait;
						$data['taux_sup_d'] = $taux->taux_com_sup_depot;
						$data['taux_bq_d'] = $taux->part_banque_depot;
						$data['taux_bq_r'] = $taux->part_banque_retrait;
						$data['frais_d'] = $taux->frais_min_banque_depot;

						$agents_g = $this->user_model->getAllGeolocatedUsersForHyp($this->session->userdata('member_code'));

						//Fetch all transactions of any geolocated agent of the network
						$data["transactions"] = array();
						$totalCommissionBanque = 0;
						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) {
											$totalCommissionBanque += $trans->commission_banque;
											$trans->code_parrain = $row->code_parrain; // Add code parrain to transaction
											array_push($data['transactions'], $trans);
										}
									}
								}
							}
						}
						$data["totalCommissionBanque"] = $totalCommissionBanque;

					} elseif ($data['hasWallet']->first_row()->type == 'ilink') {
						$data['plr_user_wallet_wallet'] = $this->user_model->getPalierConfigWallet("user_wallet_wallet_international", $data['idConfig']);
						$data['plr_user_wallet_cash'] = $this->user_model->getPalierConfigWallet("user_wallet_cash_international", $data['idConfig']);
						$data['plr_agent_depot_wallet_ilink'] = $this->user_model->getPalierConfigWallet("agent_depot_wallet_ilink_international" , $data['idConfig']);
						$data['plr_agent_depot_autre_wallet'] = $this->user_model->getPalierConfigWallet("agent_depot_autre_wallet_international" , $data['idConfig']);
						$data['plr_agent_cash_cash'] = $this->user_model->getPalierConfigWallet("agent_cash_cash_international", $data['idConfig']);

						$data['plr_user_wallet_wallet_national'] = $this->user_model->getPalierConfigWallet("user_wallet_wallet_national", $data['idConfig']);
						$data['plr_user_wallet_cash_national'] = $this->user_model->getPalierConfigWallet("user_wallet_cash_national", $data['idConfig']);
						$data['plr_agent_depot_wallet_ilink_national'] = $this->user_model->getPalierConfigWallet("agent_depot_wallet_ilink_national" , $data['idConfig']);
						$data['plr_agent_depot_autre_wallet_national'] = $this->user_model->getPalierConfigWallet("agent_depot_autre_wallet_national" , $data['idConfig']);
						$data['plr_agent_cash_cash_national'] = $this->user_model->getPalierConfigWallet("agent_cash_cash_national", $data['idConfig']);

						$data['taxes'] = $this->user_model->getTaxes($data['idConfig']);

						$data['taux_u_w_cart'] = $taux->taux_com_user_wallet_carte;
						$data['taux_u_c_w'] = $taux->taux_com_user_carte_wallet;
						$data['taux_u_c_c'] = $taux->taux_com_user_carte_cash;
						$data['taux_ag_d_c_c'] = $taux->taux_com_wallet_ag_envoi_cash_carte;
						$data['taux_ag_cart_c'] = $taux->taux_com_wallet_ag_carte_cash;
						$data['taux_ag_cash_c'] = $taux->taux_com_wallet_ag_depot_carte;
						$data['taux_ag_s_c'] = $taux->taux_com_ag_envoi_cash;
						$data['taux_sup_s_c'] = $taux->taux_com_sup_envoi_cash;
						$data['taux_hyp_s_c'] = $taux->taux_com_hyp_envoi_cash;
						$data['taux_ag_r_c'] = $taux->taux_com_ag_retrait_cash;
						$data['taux_sup_r_c'] = $taux->taux_com_sup_retrait_cash;
						$data['taux_hyp_r_c'] = $taux->taux_com_hyp_retrait_cash;
						$data['taux_ag_d_c'] = $taux->taux_com_ag_depot_cash_carte;
						$data['taux_sup_d_c'] = $taux->taux_com_sup_depot_cash_carte;
						$data['taux_hyp_d_c'] = $taux->taux_com_hyp_depot_cash_carte;
						$data['taux_bq_d_c'] = $taux->taux_com_banque_depot_cash_carte;
						$data['taux_ag_r_cart'] = $taux->taux_com_ag_retrait_carte_cash;
						$data['taux_sup_r_cart'] = $taux->taux_com_sup_retrait_carte_cash;
						$data['taux_hyp_r_cart'] = $taux->taux_com_hyp_retrait_carte_cash;
						$data['taux_bq_r_cart'] = $taux->taux_com_banque_retrait_carte_cash;
						$data['taux_hyp_r_cart_ilink'] = $taux->taux_com_hyp_retrait_carte_cash_ilink;
						$data['taux_bq_r_cart_ilink'] = $taux->taux_com_banque_retrait_carte_cash_ilink;
						$data['taux_hyp_s_wallet_cart_ilink'] = $taux->taux_com_hyp_envoi_wallet_carte_ilink;
						$data['taux_bq_s_wallet_cart_ilink'] = $taux->taux_com_banque_envoi_wallet_carte_ilink;

						$transactions = $this->user_model->getNetworkIlinTransactions($id_network);
						$data["transactions"] = array();
						$totalCommissionBanque = 0;
						if ($transactions) {
							foreach ($transactions->result() as $trans) {
								$totalCommissionBanque += $trans->commission_banque;
								array_push($data['transactions'], $trans);
							}
						}
						$data["totalCommissionBanque"] = Money::of(round($totalCommissionBanque,2), $this->session->userdata('currency_code'), $context )->formatTo('fr_FR');

					}

					//Create wallet if it not exist
					$res = $this->user_model->getWallet($agent_id);
					if ($res == false) {
						$this->user_model->addWallet($agent_id);
						$res = $this->user_model->getWallet($agent_id);
					}

					$row = $res->first_row();
					$data["commission"] = Money::of(round($row->balance_com,2), $this->session->userdata('currency_code'),$context)->formatTo('fr_FR');
					$data["principal"] = Money::of(round($row->balance_princ,2), $this->session->userdata('currency_code'),$context)->formatTo('fr_FR');
					$data['wallet_id'] = $row->wallet_id;
					$data['curreny_code'] = $this->session->userdata('currency_code');


					$res = $this->user_model->getWalletPassword($id_network);
					$data['walletPassword'] = $res ? $res->first_row() : null;
					$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['country'] = $this->session->userdata('current_pays');
					$data['villes'] = $this->user_model->getVilleByUserGeo($data['network'], $this->session->userdata('current_pays'));
					$data['superviseurs'] = $this->user_model->getSuperNameAndCodeForHyp($this->session->userdata('member_code'));
					$data['pays'] = $this->user_model->getAllGameCountries();
					$data['networks'] = $this->user_model->getActiveNetwork();
					$data['agentWalletInfos'] = $this->user_model->getInfosWalletAgentForHyper($id_network);
					$this->load->view('header_hyp', $data);
					if ($data['hasWallet']->first_row()->type == 'visa')
						$this->load->view('gestion_wallet_hyp');
					elseif ($data['hasWallet']->first_row()->type == 'ilink')
						$this->load->view('gestion_wallet_ilink_hyp');
					$this->load->view('footer');
				}
			}
		}
	}

	private function isLogged()
	{
		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);
			return false;
		}
		return true;
	}

	private function historique($network_id, $startDate, $endDate, $type)
	{
		$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"));
		if ($type == 'transaction')
			$data['transactions'] = $this->user_model->getTransactions($startDate, $endDate, $network_id);
		else if ($type == 'transaction_ilink')
			$data['transactions'] = $this->user_model->getIlinkTransactions($startDate, $endDate, $network_id);
		else
			$data['transactions'] = $this->user_model->getRecharges($startDate, $endDate, $network_id);

		$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');

		$this->load->view('header_hyp', $data);
		if ($type == 'transaction')
			$this->load->view('historique_transactions');
		else if ($type == 'transaction_ilink')
			$this->load->view('historique_transactions_ilink');
		else
			$this->load->view('historique_recharges');
		$this->load->view('footer');
	}

	public function walletPassword()
	{
		if ($this->isLogged()) {
			$data['active'] = "wallet_password";
			$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['network_id'] = $this->session->userdata('network_id');
			$res = $this->user_model->getWalletPassword($this->session->userdata('network_id'));
			$data['walletPassword'] = $res ? $res->first_row() : null;
			$this->load->view('header_hyp', $data);
			$this->load->view('wallet_password');
			$this->load->view('footer');
		}
	}

	public function generate_wallet_password()
	{
		if ($this->isLogged()) {
			if (isset($_POST)) {
				$network_id = isset($_POST['network_id']) ? $_POST['network_id'] : null;
				$email = $_POST['email'];
//				$size = $_POST['size'];
				$wallet_password_id = isset($_POST['wallet_password_id']) ? $_POST['wallet_password_id'] : null;
				$network = $_POST['network'];
				$country = $_POST['country'];

				$password = $this->generate_string();
				$hash = $this->hashSSHA($password);
				$encrypted_password = $hash['encrypted'];
				$salt = $hash['salt'];

				$this->load->library('email');
				$this->email->from('noreply@ilink-app.com', 'iLink World');
				$this->email->to($email);

				$this->email->subject($this->lang->line('wallet_password') . ' ' . $network . ' - ' . $country);
				$this->email->message($this->lang->line('mot de passe') . ' : ' . $password);

				if ($this->email->send()) {
					if ($wallet_password_id == null)
						$this->user_model->addWalletPassword($network_id, $encrypted_password, $salt, $email);
					else
						$this->user_model->updateWalletPassword($wallet_password_id, $encrypted_password, $salt, $email);
					$res = true;
				} else {
//					show_error($this->email->print_debugger());
					$res = false;
				}

				if ($res) {
					echo json_encode("200");
				} else {
					echo json_encode("500");
				}
			}
		}
	}

	private function generate_string($length = 10)
	{
		$chars = 'abcdefghjkmnpqrstuvwxyz' .
			'23456789';

		$str = '';
		$max = strlen($chars) - 1;

		for ($i = 0; $i < $length; $i++)
			$str .= $chars[mt_rand(0, $max)];

		return $str;
	}

	private function hashSSHA($password)
	{
		$salt = sha1(rand());
		$salt = substr($salt, 0, 10);
		$encrypted = base64_encode(sha1($password . $salt, true) . $salt);
		$hash = array("salt" => $salt, "encrypted" => $encrypted);
		return $hash;
	}

	public function resetWalletPassword()
	{
		if ($this->isLogged()) {
			if (isset($_POST)) {
				$email = $_POST['email'];
				$wallet_password_id = isset($_POST['wallet_password_id']) ? $_POST['wallet_password_id'] : null;
				$network = $_POST['network'];
				$country = $_POST['country'];

				$password = $this->generate_string();
				$hash = $this->hashSSHA($password);
				$encrypted_password = $hash['encrypted'];
				$salt = $hash['salt'];

				$this->load->library('email');
				$this->email->from('noreply@ilink-app.com', 'iLink World');
				$this->email->to($email);

				$this->email->subject($this->lang->line('reset_wallet_password') . ' ' . $network . ' - ' . $country);
				$this->email->message($this->lang->line('mot de passe') . ' : ' . $password);

				if ($this->email->send()) {
					$this->user_model->updateWalletPassword($wallet_password_id, $encrypted_password, $salt, $email);
					$res = true;
				} else {
//					show_error($this->email->print_debugger());
					$res = false;
				}

				if ($res) {
					echo json_encode("200");
				} else {
					echo json_encode("500");
				}
			}
		}
	}

	public function recharge_wallet()
	{
		if ($this->isLogged()) {
			if (isset($_POST)) {
				$password = $_POST['password'];
				$wallet_id = $_POST['wallet_id'];
				$salt = $_POST['salt'];
				$encrypted_password = $_POST['encrypted_password'];
				$montant = $_POST['montant'];

				$hash = base64_encode(sha1($password . $salt, true) . $salt);

				if ($encrypted_password == $hash) {
					$this->user_model->updateWalletBalance($montant, $wallet_id);
					$res = $this->user_model->addWalletRecharge($montant, $wallet_id);
					$code = $res ? '200' : '500';
				} else {
					$code = '400';
				}

				echo json_encode($code);

			}
		}
	}

	public function calculator()
	{
		if ($this->isLogged()) {
			$data['active'] = "calculator";
			$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['idConfig'] = $data['hasWallet']->first_row()->id;
			$data['country'] = $this->session->userdata('current_pays');
			$data['network_id'] = $this->session->userdata('network_id');
			$data['countries'] = $this->user_model->getWalletsCountries();
			$showResult = false;
			$frais = 0;
			$taxe = 0;
			$config = $data['hasWallet']->first_row();

			//Simulateur
			$com_ag =  $this->session->userdata('com_ag') ? $this->session->userdata('com_ag') : 0 ;
			$com_sup =  $this->session->userdata('com_sup') ? $this->session->userdata('com_sup') : 0 ;
			$com_hyp =  $this->session->userdata('com_hyp') ? $this->session->userdata('com_hyp') : 0 ;
			$com_bq =  $this->session->userdata('com_bq') ? $this->session->userdata('com_bq') : 0 ;
//			$frais_retrait =  $this->session->userdata('frais_retrait') ? $this->session->userdata('frais_retrait') : 0 ;

			if (isset($_POST)) {
				$operation =  $this->input->post('operation');
				$montant =  $this->input->post('montant');
				$init_country =  $this->input->post('init_country');
				$final_country =  $this->input->post('final_country');
				$save =  $this->input->post('save');
				$code =  $this->input->post('code');


				$taxes = $this->user_model->getTaxes($data['idConfig']);

				$plr_user_wallet_wallet = $this->user_model->getPalierConfigWallet("user_wallet_wallet_international", $data['idConfig']);
				$plr_user_wallet_cash = $this->user_model->getPalierConfigWallet("user_wallet_cash_international", $data['idConfig']);
				$plr_agent_depot_wallet_ilink = $this->user_model->getPalierConfigWallet("agent_depot_wallet_ilink_international" , $data['idConfig']);
				$plr_agent_depot_autre_wallet = $this->user_model->getPalierConfigWallet("agent_depot_autre_wallet_international" , $data['idConfig']);
				$plr_agent_cash_cash = $this->user_model->getPalierConfigWallet("agent_cash_cash_international", $data['idConfig']);

				$plr_user_wallet_wallet_national= $this->user_model->getPalierConfigWallet("user_wallet_wallet_national", $data['idConfig']);
				$plr_user_wallet_cash_national = $this->user_model->getPalierConfigWallet("user_wallet_cash_national", $data['idConfig']);
				$plr_agent_depot_wallet_ilink_national = $this->user_model->getPalierConfigWallet("agent_depot_wallet_ilink_national" , $data['idConfig']);
				$plr_agent_depot_autre_wallet_national = $this->user_model->getPalierConfigWallet("agent_depot_autre_wallet_national" , $data['idConfig']);
				$plr_agent_cash_cash_national = $this->user_model->getPalierConfigWallet("agent_cash_cash_national", $data['idConfig']);

				$plr_user_wallet_wallet = $plr_user_wallet_wallet ? $plr_user_wallet_wallet->result() : [];
				$plr_user_wallet_cash = $plr_user_wallet_cash ? $plr_user_wallet_cash->result() : [];
				$plr_agent_depot_wallet_ilink = $plr_agent_depot_wallet_ilink ? $plr_agent_depot_wallet_ilink->result() : [];
				$plr_agent_depot_autre_wallet = $plr_agent_depot_autre_wallet ? $plr_agent_depot_autre_wallet->result() : [];
				$plr_agent_cash_cash = $plr_agent_cash_cash ? $plr_agent_cash_cash->result() : [];

				$plr_user_wallet_wallet_national = $plr_user_wallet_wallet_national ? $plr_user_wallet_wallet_national->result() : [];
				$plr_user_wallet_cash_national = $plr_user_wallet_cash_national ? $plr_user_wallet_cash_national->result() : [];
				$plr_agent_depot_wallet_ilink_national = $plr_agent_depot_wallet_ilink_national ? $plr_agent_depot_wallet_ilink_national->result() : [];
				$plr_agent_depot_autre_wallet_national = $plr_agent_depot_autre_wallet_national ? $plr_agent_depot_autre_wallet_national->result() : [];
				$plr_agent_cash_cash_national = $plr_agent_cash_cash_national ? $plr_agent_cash_cash_national->result() : [];

				$taxes = $taxes ? $taxes->result() : [];
				$taxesNationales = array_filter($taxes, function ($tax) {
					return $tax->destination == 'national';
				});

				$taxesInternationales = array_filter($taxes, function ($tax) {
					return $tax->destination == 'international';
				});

				if (isset($save)) {
					$showResult = true;
					switch ($operation) {
						case 1: //Envoi wallet à wallet
							$frais = ($init_country != $final_country) ? $this->calculateFees1($plr_user_wallet_wallet, $montant) : $this->calculateFees1($plr_user_wallet_wallet_national, $montant);
							$taxe = ($init_country != $final_country) ? $this->calculateTax($taxesInternationales , $frais) : $this->calculateTax($taxesNationales ,$frais);

							$com_hyp += $frais ;
							$this->session->set_userdata('com_hyp',$com_hyp);
							break;
						case 2: //Envoi de wallet à carte
							$frais = $montant * $config->taux_com_user_wallet_carte / 100;
//			$taxe = ($init_country != $final_country) ? $this->calculateTax($taxesInternationales , $frais) : $this->calculateTax($taxesNationales ,$frais);
							$com_hyp += $frais * $config->taux_com_hyp_envoi_wallet_carte_ilink / 100 ;
							$com_bq += $frais * $config->taux_com_banque_envoi_wallet_carte_ilink / 100 ;
							$this->session->set_userdata('com_hyp',$com_hyp);$this->session->set_userdata('com_bq',$com_bq);
							break;
						case 3: //Envoi de wallet à cash
							$frais = ($init_country != $final_country) ? $this->calculateFees1($plr_user_wallet_cash, $montant) : $this->calculateFees1($plr_user_wallet_cash_national, $montant);
							$taxe = ($init_country != $final_country) ? $this->calculateTax($taxesInternationales , $frais) : $this->calculateTax($taxesNationales ,$frais);

							$com_hyp += $frais ;
							$this->session->set_userdata('com_hyp',$com_hyp);
//							$this->session->set_userdata('frais_retrait',$frais);
							$code = $this->random_string();
							$data['message_code'] = ''.$code;
							$this->user_model->insertSimulateur($frais,$montant-$frais-$taxe,$code,$montant);
							break;
						case 4: //Envoi de wallet à banque
							// Non disponible
							//Pas de taxes
							break;
//		case 5: //Envoi de carte à wallet
//			$frais =$montant * $config->taux_com_user_carte_wallet / 100;
////			$taxe = ($init_country != $final_country) ? $this->calculateTax($taxesInternationales , $frais) : $this->calculateTax($taxesNationales ,$frais);
//			break;
//		case 6: //Envoi de carte à cash
//			$frais =$montant * $config->taux_com_user_carte_cash / 100;
////			$taxe = ($init_country != $final_country) ? $this->calculateTax($taxesInternationales , $frais) : $this->calculateTax($taxesNationales ,$frais);
//			break;
						case 9: // Retrait de wallet en cash
							$frais = ($init_country != $final_country) ? $this->calculateFees1($plr_user_wallet_cash, $montant) : $this->calculateFees1($plr_user_wallet_cash_national, $montant);
							$taxe = ($init_country != $final_country) ? $this->calculateTax($taxesInternationales , $frais) : $this->calculateTax($taxesNationales ,$frais);

							$com_hyp += $frais ;
							$this->session->set_userdata('com_hyp',$com_hyp);
							$code = $this->random_string();
							$data['message_code'] = ''.$code;
							$this->user_model->insertSimulateur($frais,$montant-$frais-$taxe,$code,$montant);
							break;
						case 10: //Retrait de carte vers wallet
							$frais = $montant * $config->taux_com_user_carte_wallet / 100;

							$com_hyp += $frais * $config->taux_com_hyp_retrait_carte_cash_ilink / 100 ;
							$com_bq += $frais * $config->taux_com_banque_retrait_carte_cash_ilink / 100 ;
							$this->session->set_userdata('com_hyp',$com_hyp);$this->session->set_userdata('com_bq',$com_bq);
							break;
						case 11: // Retrait de carte vers cash
							$frais =  $montant * $config->taux_com_user_carte_cash / 100;

							$com_hyp += $frais * $config->taux_com_hyp_retrait_carte_cash_ilink / 100 ;
							$com_bq += $frais * $config->taux_com_banque_retrait_carte_cash_ilink / 100 ;
							$this->session->set_userdata('com_hyp',$com_hyp);$this->session->set_userdata('com_bq',$com_bq);
							$code = $this->random_string();
							$data['message_code'] = ''.$code;
							$this->user_model->insertSimulateur($frais * $config->taux_com_hyp_retrait_carte_cash_ilink / 100,$montant-$frais-$taxe,$code,$montant);
							break;
						case 12: // Agent  - Retrait en cash
//			empty
//Sauvegarder les frais lors de chaque operation d'envoi
							if(isset($code)){
								$simulateur = $this->user_model->getSimulateur($code);
								if($simulateur){
									$part_ag = $simulateur->first_row()->com_hyp * $config->taux_com_ag_retrait_cash/ 100 ;
									$part_sup =  $simulateur->first_row()->com_hyp * $config->taux_com_sup_retrait_cash/ 100;
									$com_ag += $part_ag;
									$com_sup += $part_sup ;
									$com_hyp -= ($part_ag + $part_sup);
									$data['montant_retrait'] = $simulateur->first_row()->montant_retrait;
									$data['frais_retrait'] = $simulateur->first_row()->com_hyp;
									$data['montant_init'] = $simulateur->first_row()->montant;
									$this->session->set_userdata('com_ag',$com_ag);$this->session->set_userdata('com_sup',$com_sup);
									$this->session->set_userdata('com_hyp',$com_hyp);
								}else{
									$data['error_simulateur'] = 'Code de retrait incorrect';
								}
							}else{
								$data['error_simulateur'] = 'Code de retrait incorrect';
							}


							break;
						case 13: // Agent - Retrait de la carte vers cash
							$frais = $montant * $config->taux_com_wallet_ag_carte_cash / 100;

							$com_ag += $frais * $config->taux_com_ag_retrait_carte_cash/ 100 ;
							$com_sup += $frais * $config->taux_com_sup_retrait_carte_cash / 100 ;
							$com_hyp += $frais * $config->taux_com_hyp_retrait_carte_cash / 100 ;
							$com_bq += $frais * $config->taux_com_banque_retrait_carte_cash / 100 ;
							$this->session->set_userdata('com_ag',$com_ag);$this->session->set_userdata('com_sup',$com_sup);
							$this->session->set_userdata('com_hyp',$com_hyp);$this->session->set_userdata('com_bq',$com_bq);
							break;
						case 14: // Agent - Envoi de cash vers wallet iLink
							$frais = ($init_country != $final_country) ? $this->calculateFees1($plr_agent_depot_wallet_ilink, $montant) : $this->calculateFees1($plr_agent_depot_wallet_ilink_national, $montant);
							$taxe = ($init_country != $final_country) ? $this->calculateTax($taxesInternationales , $frais) : $this->calculateTax($taxesNationales ,$frais);
							$com_ag += $frais * $config->taux_com_ag_envoi_cash / 100 ;
							$com_sup += $frais * $config->taux_com_sup_envoi_cash / 100 ;
							$com_hyp += $frais * $config->taux_com_hyp_envoi_cash / 100 ;
							$this->session->set_userdata('com_ag',$com_ag);$this->session->set_userdata('com_sup',$com_sup);
							$this->session->set_userdata('com_hyp',$com_hyp);
							break;
						case 15: // Agent - Envoi de cash vers autre wallet
							$frais = ($init_country != $final_country) ? $this->calculateFees1($plr_agent_depot_autre_wallet, $montant) : $this->calculateFees1($plr_agent_depot_autre_wallet_national, $montant);
							$taxe = ($init_country != $final_country) ? $this->calculateTax($taxesInternationales , $frais) : $this->calculateTax($taxesNationales ,$frais);
							$com_ag += $frais * $config->taux_com_ag_envoi_cash / 100 ;
							$com_sup += $frais * $config->taux_com_sup_envoi_cash / 100 ;
							$com_hyp += $frais * $config->taux_com_hyp_envoi_cash / 100 ;
							$this->session->set_userdata('com_ag',$com_ag);$this->session->set_userdata('com_sup',$com_sup);
							$this->session->set_userdata('com_hyp',$com_hyp);
							break;
						case 16: // Agent - Envoi de cash vers une carte visa
							$frais = $montant * $config->taux_com_wallet_ag_envoi_cash_carte / 100;
							$com_ag += $frais * $config->taux_com_ag_depot_cash_carte/ 100 ;
							$com_sup += $frais * $config->taux_com_sup_depot_cash_carte / 100 ;
							$com_hyp += $frais * $config->taux_com_hyp_depot_cash_carte / 100 ;
							$com_bq += $frais * $config->taux_com_banque_depot_cash_carte / 100 ;
							$this->session->set_userdata('com_ag',$com_ag);$this->session->set_userdata('com_sup',$com_sup);
							$this->session->set_userdata('com_hyp',$com_hyp);$this->session->set_userdata('com_bq',$com_bq);
							break;
						case 17: // Agent - Envoi de cash vers cash
							$frais = ($init_country != $final_country) ? $this->calculateFees1($plr_agent_cash_cash, $montant) : $this->calculateFees1($plr_agent_cash_cash_national, $montant);
							$taxe = ($init_country != $final_country) ? $this->calculateTax($taxesInternationales , $frais) : $this->calculateTax($taxesNationales ,$frais);
							$com_ag += $frais * $config->taux_com_ag_envoi_cash / 100 ;
							$com_sup += $frais * $config->taux_com_sup_envoi_cash / 100 ;
							$com_hyp += $frais * $config->taux_com_hyp_envoi_cash / 100 ;
							$this->session->set_userdata('com_ag',$com_ag);$this->session->set_userdata('com_sup',$com_sup);
							$this->session->set_userdata('com_hyp',$com_hyp);
							$code = $this->random_string();
							$data['message_code'] = ''.$code;
							$this->user_model->insertSimulateur($frais,$montant-$frais-$taxe,$code,$montant);
							break;
						case 18: // Agent - Envoi de cash vers banque
							// Indisponible
							break;
					}
				}
			}

			$data['showResult'] = $showResult;
			$data['frais'] = $frais;
			$data['taxe'] = $taxe;

			$data['operation'] =  $operation;
			$data['init_country'] =  $init_country;
			$data['final_country'] =  $final_country;
			$data['montant'] = $montant;


			$data['com_ag'] =  $com_ag;
			$data['com_sup'] =  $com_sup;
			$data['com_hyp'] =  $com_hyp;
			$data['com_bq'] =  $com_bq;

			$operations = [
				new Operation(1, $this->lang->line('op1')),
				new Operation(2, $this->lang->line('op2')),
				new Operation(3, $this->lang->line('op3')),
				new Operation(4, $this->lang->line('op4')),
//				new Operation(5, $this->lang->line('op5')),
//				new Operation(6, $this->lang->line('op6')),
//				new Operation(7, $this->lang->line('op7')),
//				new Operation(8, $this->lang->line('op8')),
				new Operation(9, $this->lang->line('op9')),
				new Operation(10, $this->lang->line('op10')),
				new Operation(11, $this->lang->line('op11')),
				new Operation(12, $this->lang->line('op12')),
				new Operation(13, $this->lang->line('op13')),
				new Operation(14, $this->lang->line('op14')),
				new Operation(15, $this->lang->line('op15')),
				new Operation(16, $this->lang->line('op16')),
				new Operation(17, $this->lang->line('op17')),
				new Operation(18, $this->lang->line('op18')),
			];
			$data['operations'] = $operations;
//			if (isset($_POST)) {
//				$data['montant'] = isset($_POST['montant']) ? $_POST['montant'] : 0 ;
//			}

			$this->load->view('header_hyp', $data);
			$this->load->view('calculator');
			$this->load->view('footer');
		}
	}

	//Calcul des frais internationaux
	function calculateFees1(array $paliers, $montant)
	{
		$size = sizeof($paliers);
		if($size > 0){
			$min = $paliers[0]->min;
			$max = $size > 0 ? $paliers[$size - 1]->max : 0;
			$palier = null;
			foreach ($paliers as $p) {
				if ($montant >= $p->min && $montant <= $p->max) {
					$palier = $p;
					break;
				}
			}

			if ($palier) {
				return (($palier->min + $palier->max) / 2 * $palier->taux / 100);
			} else {
				if ($montant < $min)
					return $min * $paliers[0]->taux / 100;
				else if ($montant > $max)
					return $max * $paliers[$size - 1]->taux / 100;
			}
		}
	}

//Calcul des frais nationaux
	function calculateFees2($min, $max, $taux, $montant)
	{
		if ($montant < $min)
			return $min * $taux / 100;
		else if ($montant > $max)
			return $max * $taux / 100;
		else
			return $montant * $taux / 100;
	}

//Calcul des taxes
	function calculateTax(array $taxes, $frais)
	{
		$sommeTaux = 0;
		$sommeFixe = 0;
		foreach ($taxes as $tax) {
			if ($tax->type == '%')
				$sommeTaux += $tax->valeur;

			if ($tax->type == 'fixe')
				$sommeFixe += $tax->valeur;
		}
		return ($frais * $sommeTaux / 100) + $sommeFixe;
	}

	public function random_string()
	{
		$character_set_array = array();
		$character_set_array[] = array('count' => 12, 'characters' => 'abcdefghjkmnpqrstuvwxyz');
		$character_set_array[] = array('count' => 4, 'characters' => '23456789');
		$temp_array = array();
		foreach ($character_set_array as $character_set) {
			for ($i = 0; $i < $character_set['count']; $i++) {
				$temp_array[] = $character_set['characters'][rand(0, strlen($character_set['characters']) - 1)];
			}
		}
		shuffle($temp_array);
		return implode('', $temp_array);
	}

	private function config_wallet($config , $country)
	{
//		$data['configWallet'] = $this->user_model->getConfigWallet($network_id);
		$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['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['idConfig'] = $data['hasWallet']->first_row()->id;
		if (isset($country)) {
			$data['country_id'] = $country;
			$data['country'] = $this->user_model->getCountry($country)->first_row();
			$res = $this->user_model->getActiveNetworksByCountry($country,$this->session->userdata('network_id'));
			$data['networks'] = [];
			if($res){
				$result = $res->result();
				foreach ($result as $network){
					$res = $this->user_model->getSharingRate($network->id , $data['idConfig']);
					if($res){
						$network->rate = $res->first_row()->taux_partage;
					}else{
						$network->rate = null;
					}
					$data['networks'][] = $network;
				}
			}
			$this->load->view('header_hyp', $data);
			$this->load->view('gestion_wallet_ilink_config_paying_network');
			$this->load->view('footer');
			return;
		}
		$res = $this->user_model->getWalletsCountries();
		$data['countries'] = [];
		if($res){
			$result = $res->result();
			foreach ($result as $country){
				$res = $this->user_model->countPayingNetworks($country->id, $data['idConfig']);
				if($res){
					$country->total = $res->first_row()->total;
				}else{
					$country->total = null;
				}
				$data['countries'][] = $country;
			}
		}
		$this->load->view('header_hyp', $data);
//		if ($type == 'transaction')
//			$this->load->view('historique_transactions');
//		else
			$this->load->view('gestion_wallet_ilink_config_paying_networks');
		$this->load->view('footer');
	}

	private function show_config_infos($type)
	{

		$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['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['idConfig'] = $data['hasWallet']->first_row()->id;
		$data['networks'] = $this->user_model->getPayingNetworksByConfigWallet($data['idConfig']);

		$this->load->view('header_hyp', $data);
		$this->load->view('gestion_wallet_ilink_paying_network_account');
		$this->load->view('footer');
	}

	public function config_sharing_rate(){
		if ($this->isLogged()) {
			if (isset($_POST)) {
				$network_id = $_POST['network_id'];
				$config_id = $_POST['config_id'];
				$rate = $_POST['rate'];

				$res = $this->user_model->setSharingRate($network_id,$config_id,$rate);

				if ($res) {
					echo json_encode("200");
				} else {
					echo json_encode("500");
				}

			}
		}
	}
}

class Operation
{
	public $name;
	public $id;

	public function __construct($id, $name)
	{
		$this->name = $name;
		$this->id = $id;
	}
}
