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

class Admin_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 {

            $data['pays'] = $this->user_model->getAllCountries();
            $data['hyper'] = $this->user_model->getAllHyper($data['pays']->first_row()->id);
            $data['ville'] = $this->user_model->getVilleNetworkByHyper($data['hyper']->first_row()->code_membre);

            $ville = $this->session->userdata('current_ville');
            $hyper = $this->session->userdata('current_hyper');
            $pays = $this->session->userdata('current_pays');

            $data['c_users'] = $this->user_model->getCoutForAdmin($pays,$hyper,$ville,'simple_users');
            $data['c_geolocated'] = $this->user_model->getCoutForAdmin($pays,$hyper,$ville,'geolocated');
            $data['c_code'] = $this->user_model->getCoutForAdmin($pays,$hyper,$ville,'validated_code');
            $data['c_generated'] = $this->user_model->getCoutForAdmin($pays,$hyper,$ville,'generated_code');
            $data['list_simple_users'] = $this->user_model->getAllSimpleUser($pays,$hyper);
            $data['list_geolocated_users'] = $this->user_model->getOnlyGeolocatedUsersForAdmin($pays,$hyper,$ville);
            $data['active'] = "dash";
            $data['firstname'] = $this->session->userdata('firstname');
            $data['lastname'] = $this->session->userdata('lastname');
            $data['email'] = $this->session->userdata('email');
            $this->load->view('header', $data);
            $this->load->view('admin_dash');
            $this->load->view('footer');
        }

    }

    public function filter(){
        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 {

            $data['pays'] = $this->user_model->getAllCountries();
            $data['hyper'] = $this->user_model->getAllHyper($data['pays']->first_row()->id);
            if($data['hyper']->num_rows()>0){
                $data['ville'] = $this->user_model->getVilleNetworkByHyper($data['hyper']->first_row()->code_membre);
            }else{
                $data['ville'] = null;
            }
            $data['active'] = "dash";
            $data['firstname'] = $this->session->userdata('firstname');
            $data['lastname'] = $this->session->userdata('lastname');
            $data['email'] = $this->session->userdata('email');
            $this->load->view('firstfilter', $data);
        }
    }

    public function firstFilter(){

        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(isset($_POST))
            {
                $this->session->set_userdata('current_pays', $this->input->post('pays'));
                $pays_name = $this->db->query("SELECT name FROM countries WHERE id='".$this->input->post('pays')."'");
                $this->session->set_userdata('current_hyper', $this->input->post('hyper'));
                $infos_network = $this->db->query("SELECT name,phone FROM networks INNER JOIN networks_agents ON networks.id=networks_agents.network_id INNER JOIN codeGenerer ON networks_agents.codeGenerer_id=codeGenerer.id WHERE codeGenerer.code_membre='".$this->input->post('hyper')."'");
                $network = $infos_network->first_row()->name;
                $contact_hyper = $infos_network->first_row()->phone;
                $this->session->set_userdata('current_network', $network);
                $this->session->set_userdata('current_contact_hyper', $contact_hyper);
                $this->session->set_userdata('current_ville', $this->input->post('ville'));
                $town_name = $this->db->query("SELECT name FROM towns WHERE id='".$this->input->post('ville')."'");

                $this->session->set_userdata('name_current_pays',$pays_name->first_row()->name);
                $this->session->set_userdata('name_current_ville',$town_name->first_row()->name);

                $ville = $this->session->userdata('current_ville');
                $hyper = $this->session->userdata('current_hyper');
                $pays = $this->session->userdata('current_pays');

                $data['c_users'] = $this->user_model->getCoutForAdmin($pays,$hyper,$ville,'simple_users');
                $data['c_code'] = $this->user_model->getCoutForAdmin($pays,$hyper,$ville,'validated_code');
                $data['c_geolocated'] = $this->user_model->getCoutForAdmin($pays,$hyper,$ville,'geolocated');
                $data['c_generated'] = $this->user_model->getCoutForAdmin($pays,$hyper,$ville,'generated_code');
                $data['list_simple_users'] = $this->user_model->getAllSimpleUser($pays,$hyper);
                $data['list_geolocated_users'] = $this->user_model->getOnlyGeolocatedUsersForAdmin($pays,$hyper,$ville);

                $data['pays'] = $this->user_model->getAllCountries();
                $data['hyper'] = $this->user_model->getAllHyper($data['pays']->first_row()->id);
                $data['ville'] = $this->user_model->getVilleNetworkByHyper($data['hyper']->first_row()->code_membre);
                $data['active'] = "dash";
                $data['firstname'] = $this->session->userdata('firstname');
                $data['lastname'] = $this->session->userdata('lastname');
                $data['email'] = $this->session->userdata('email');
                $data['network'] = $this->session->userdata('current_network');
                $data['contact_hyper'] = $this->session->userdata('current_contact_hyper');
                $this->load->view('header', $data);
                $this->load->view('admin_dash');
                $this->load->view('footer');
            }
        }
    }

}
