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

class CodesG extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $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['success'] = "";
            $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['list'] = $this->user_model->getGeneratedCodes($hyper);
            $data['active'] = "codes";
            $data['alert'] = "";
            $data['pays'] = $this->user_model->getAllCountries();
            $data['success'] = "ok";
            $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('codeg');
            $this->load->view('footer');
        }
    }

    public function generateCode()
    {

        if ($this->input->post('c') && $this->input->post('a')) {

            $number = intval($this->input->post('c'));
            $member_code = $this->input->post('a');
            for ($i = 0; $i < $number; $i++) {


                $code = $this->randomString();

                $data = array('code_parrain' => $member_code, 'code_membre' => $code, 'etat' => 0, 'category' => 'super');

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

            }

            $data['alert'] = "ok";
            $data['success'] = "ok";
            $data['active'] = "members";
            $data['message'] = "$number codes have been added!";


            $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()->country_code);
            $data['ville'] = $this->user_model->getVilleNetworkByHyper($data['hyper']->first_row()->member_code);
            $data['list'] = $this->user_model->getGeneratedCodes($hyper);
            $data['active'] = "codes";
            $data['pays'] = $this->user_model->getAllCountries();
            $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('codeg');
            $this->load->view('footer');

        } else {
            $data['alert'] = "ok";
            $data['success'] = "";
            $data['active'] = "members";
            $data['message'] = "Can't add codes!";


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

    }

    private function randomString($length = 10)
    {
        $str = "";
        $characters = array_merge(range('A', 'Z'), range('a', 'z'), range('0', '9'));
        $max = count($characters) - 1;
        for ($i = 0; $i < $length; $i++) {
            $rand = mt_rand(0, $max);
            $str .= $characters[$rand];
        }
        return $str;

    }
}
