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

class Listes_members 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'] = "simple_members";
            $data['list'] = $this->user_model->getAllSimpleUser($pays,$hyper);
            $data['firstname'] = $this->session->userdata('firstname');
            $data['lastname'] = $this->session->userdata('lastname');
            $data['email'] = $this->session->userdata('email');
            $data['alert'] = "";
			$data['hasWallet'] = $this->wallet_model->getConfigWallet($this->session->userdata('current_network_id'));

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

    public function ajoutbtn()
    {
        if ($this->input->post('ajbtn')) {
            $nom = $this->input->post('nm');
            $prenom = $this->input->post('prnm');
            $Cpays = $this->input->post('cdpays');
            $Reso = $this->input->post('reso');
            $Cmbre = $this->input->post('cmbre');
            $emaill = $this->input->post('maill');
            $Catgry = $this->input->post('cat');
            $phoneNm = $this->input->post('tel');


            $data = array(
                'firstname' => $nom, 'lastname' => $prenom, 'country_code' => $Cpays, 'network' => $Reso,
                'membre_code' => $Cmbre, 'emaill' => $emaill, 'category' => $Catgry, 'phone' => $phoneNm

            );


            $query = $this->db->insert('users_simple', $data);
            if ($query) {

                $data['alert'] = "ok";
                $data['success'] = "ok";
                $data['active'] = "simple_members";
                $data['message'] = "The member $phoneNm has been added!";


                redirect($this->uri->uri_string());
            } else {

                $data['alert'] = "ok";
                $data['active'] = "simple_members";
                $data['message'] = "The member $phoneNm couldn't been added!";


                redirect('Listes_members');

            }
        } else {

            $data['alert'] = "ok";
            $data['active'] = "simple_members";
            $data['message'] = "Database error!";


            redirect('Listes_members');
        }
    }

    function suppr()
    {
		$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);
            $query = $this->db->delete('users');
            */
            $query = $this->db->delete('users_simple', array('phone' => $id));

            if ($query) {
                $data['alert'] = "ok";
                $data['success'] = "ok";
                $data['active'] = "simple_members";
                $data['message'] = "The member $id has been deleted!";


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


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


        } else {
            $data['alert'] = "ok";
            $data['active'] = "simple_members";
            $data['message'] = "The member $id couldn't been deleted! No data sent";


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

    }

    public function modif()
    {
        if ($this->input->post('modifbtn')) {
            $nom = $this->input->post('nom');
            $adresse = $this->input->post('adresse');
            $phoneNm = $this->input->post('tel');
            $email = $this->input->post('email');


            $data = array('lastname' => $nom, 'adresse' => $adresse, 'email' => $email);

            $this->db->where('phone', $phoneNm);

            $query = $this->db->update('users', $data);
            if ($query) {

                $data['alert'] = "ok";
                $data['success'] = "ok";
                $data['active'] = "simple_members";
                $data['message'] = "The member $phoneNm has been updated!";


                redirect('Listes_members');

                //redirect('Listes_admin');
            } else {

                $data['alert'] = "ok";
                $data['active'] = "simple_members";
                $data['message'] = "The member $phoneNm couldn't been updated!";


                redirect('Listes_members');
            }
        } else {
            $data['alert'] = "ok";
            $data['active'] = "simple_members";
            $data['message'] = "The record couldn't been updated! No data sent.";


            redirect('Listes_members');
        }
    }


}

?>
