<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 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');

    }

    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'));
            $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'] = "";
            $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'] = "";
            $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->post('super'))
            {
                //$this->session->set_userdata('current_demande_ville', $this->input->post('ville'));

                $ville = $this->session->userdata('ville');
                $data['active'] = "demandes";
                $data['type'] = $this->lang->line('crédits');
                $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($this->session->userdata('member_code'),$this->session->userdata('current_demande_ville'),$this->input->post('super'));
                $data['tab_slowDemande'] = $this->user_model->getRangeDESCDemandesByNetwork($this->session->userdata('member_code'),$this->session->userdata('current_demande_ville'),$this->input->post('super'));
                $data['list'] = $this->user_model->getDemandesBySuper($this->session->userdata('member_code'),$this->input->post('super'));
                $data['temp_moyen'] = $this->user_model->getTempsMoyenByVille($this->session->userdata('member_code'),$this->input->post('super'));
                $data['networks'] = $this->user_model->getNetworkByHyp($this->session->userdata('member_code'));
                $this->load->view('header_hyp', $data);
                $this->load->view('demande');
                $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');

            $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($this->session->userdata('member_code'));
            $data['tab_slowDemande'] = $this->user_model->getRangeDESCDemandesAd($this->session->userdata('member_code'));
            $data['temp_moyen'] = $this->user_model->getTempsMoyenAd($this->session->userdata('member_code'));
            $data['list'] = $this->user_model->getDemandesAdForSuper($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');
            $this->load->view('header_hyp', $data);
            $this->load->view('demande');
            $this->load->view('footer');
        }
    }

    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 {
            if(!empty($_GET))
            { //
                $id_network = $this->input->get('r'); 
                $data["taux_com_retrait"] = "";
                $data["taux_com_ag_retrait"] = "";
                $data["taux_com_ag_depot"] = "";
                $data["com_depot"] = "";
                $data["transactions"] = "";
                $res = $this->user_model->getTaux($id_network);
                if ($res!=null){
                    foreach ($res->result() AS $row){
                        $data["taux_com_retrait"] = $row->taux_r;
                        $data["taux_com_ag_depot"] = $row->taux_ag_d;
                        $data["taux_com_ag_retrait"] = $row->taux_ag_r;
                        $data["com_depot"] = $row->taux_d;
                    }
                    $data["transactions"] = $this->user_model->getTransactions($id_network);
                }else{
                    $data["taux_com_retrait"] = "N/A";
                    $data["taux_com_ag_retrait"] = "N/A";
                    $data["taux_com_ag_depot"] = "N/A";
                    $data["com_depot"] = "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['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);
                $this->load->view('gestion_wallet');
                $this->load->view('footer');
            }
        }
    }
}
