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

class Member_code_admin extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('user_model');
		$this->load->model('wallet_model');

    }

    public function index()
    {


        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 {

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

            $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'] = "members_code";
            $data['list_dem'] = $this->user_model->getDemandeSupByHyperviseur($hyper);
            $data['list_codes'] = $this->user_model->getCodesByHyperviseur($hyper);
            $data['firstname'] = $this->session->userdata('firstname');
            $data['lastname'] = $this->session->userdata('lastname');
            $data['email'] = $this->session->userdata('email');
            $data['alert'] = "";
            $data['pays'] = $this->user_model->getAllCountries();
			$data['hasWallet'] = $this->wallet_model->getConfigWallet($this->session->userdata('current_network_id'));

			$this->load->view('header', $data);
            $this->load->view('membreCodeAdmin');
            $this->load->view('footer');
        }

    }

    function enregist()
    {

        if ($this->input->post('enrig')) {

            $cod = $this->input->post('cd');
            $categ = $this->input->post('cate');
            $nbrcd = $this->input->post('nbrcod');
            $nbrfone = $this->input->post('nbrphone');
            $Statu = $this->input->post('stat');
            $Stat = "encours";


            $data = array('code' => $cod, 'categorie' => $categ, 'nbrecode' => $nbrcd, 'phone' => $nbrfone);

            $this->db->set('Statue', $Statu);
            $this->db->where('Statue', $Stat);
            $rslt = $this->db->update('demande_superviseur');

            $query = $this->db->insert('codemembre', $data);

			$data['hasWallet'] = $this->wallet_model->getConfigWallet($this->session->userdata('current_network_id'));

            if ($query && $rslt) {

                $data['active'] = "members_code";
                $data['alert'] = "ok";
                $data['success'] = "ok";
                $data['message'] = "The member $id has been updated!";
                $this->load->view('header', $data);
                $this->load->view('membreCodeAdmin');
                $this->load->view('footer');
            } else {

                $data['active'] = "members_code";
                $data['alert'] = "ok";
                $data['success'] = "ok";
                $data['message'] = "The member $id couldn't been updated!";
                $this->load->view('header', $data);
                $this->load->view('membreCodeAdmin');
                $this->load->view('footer');
            }

        }
    }

    public function delete()
    {
		$data['hasWallet'] = $this->wallet_model->getConfigWallet($this->session->userdata('current_network_id'));

		if ($this->input->get('recordId')) {
            $id = "+" . $this->input->get('recordId');
            $id = str_replace(' ', '', $id);


            $this->db->where('phone', $id);
            $this->db->delete('demande_superviseur');

            $data['active'] = "members_code";
            $data['alert'] = "ok";
            $data['success'] = "ok";
            $data['message'] = "The member $id has been deleted!";
            $this->load->view('header', $data);
            $this->load->view('membreCodeAdmin');
            $this->load->view('footer');


        } else {
            $data['active'] = "members_code";
            $data['alert'] = "ok";
            $data['message'] = "The member $id couldn't been deleted!";
            $this->load->view('header', $data);
            $this->load->view('membreCodeAdmin');
            $this->load->view('footer');
        }
    }

    public function suppress()
    {
        if ($this->input->get('recordId')) {
            $id = $this->input->get('recordId');
            $id = str_replace(' ', '', $id);


            $this->db->where('code_membre', $id);
            $this->db->delete('codeGenerer');
            $data['active'] = "members_code";
            $data['alert'] = "ok";
            $data['success'] = "ok";
            $data['message'] = "Le code membre $id a bien été supprimé !";
            redirect('Member_code_admin/index');

        } else {
            $data['active'] = "members_code";
            $data['alert'] = "ok";
            $data['message'] = "Une erreur s'est produite !";
            redirect('Member_code_admin/index');

        }
    }


}
