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

class Users extends CI_Controller
{

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

	public function index()
	{
		$id_user = $this->session->userdata('id_user');
		if (isset($id_user)) {
			$data['membres'] = $this->Global_model->getMembres($id_user);
			$data['total'] = $this->Global_model->getTotal($this->session->userdata('agent_member_code'));
			$data['message'] = $this->session->flashdata('message');
			$this->load->view('dashboard', $data);
		} else {
			redirect('login');
		}
	}

	public function login()
	{
		if ($this->input->server('REQUEST_METHOD') === 'GET') {
			$data['message'] = $this->session->flashdata('message');
			$this->load->view('login',$data);
		} elseif ($this->input->server('REQUEST_METHOD') === 'POST') {
			$user_login = array(
				'user_validation_code' => $this->input->post('code')
			);
			$user = $this->Global_model->login_user($user_login['user_validation_code']);
			if ($user != false) {
				$countryId = $user->country_id;
				$checkIfGameActived = $this->Global_model->checkIfGameActived($countryId);
				if ($checkIfGameActived == true) {
					$data['user'] = $user;
					$data['pays'] = $user->country;
					$data['id_user'] = $user->id;
					$this->session->set_userdata('user_validation_code', $user_login['user_validation_code']);
					$this->session->set_userdata('user', $user->lastname);
					$this->session->set_userdata('pays', $data['pays']);
					$this->session->set_userdata('id_user', $data['id_user']);
					$this->session->set_userdata('id_country', $countryId);
					redirect('/');
				} else {
					$this->session->set_flashdata('message', "Le jeu n'est pas activé pour votre pays");
					redirect('login');
				}
			} else {
				$this->session->set_flashdata('message', "Ce code de validation est incorrect");
				redirect('login');
			}
		}
	}

	public function logout()
	{
		$this->session->sess_destroy();
		$this->session->set_flashdata('message','Vous avez été déconnecté');
		redirect('/');
	}

	public function add_member()
	{
		$validation_code = $this->input->post('code');
		$newMember = $this->Global_model->getSimpleUser($this->session->userdata('id_user'),$validation_code);
		if ($newMember != false) {
			$idNewMember = $newMember->id;
			$codeExist = $this->Global_model->checkIfIdExist($idNewMember);
			$idPaysParrain = $this->session->userdata('id_country');
			$idPaysNewMember = $newMember->country_id;
			if ($codeExist == false) {
				if ($idPaysParrain == $idPaysNewMember) {
					$res = $this->Global_model->addMember($this->session->userdata('id_user'), $idNewMember, $idPaysParrain);
					if ($res == true) {
						$this->session->set_flashdata('message', 'Le membre a bien été ajouté');
					} else {
						$this->session->set_flashdata('message', "Une erreur s'est produite");
					}
				} else {
					$this->session->set_flashdata('message', "Le code de validation appartient à un autre pays.");
				}
			} else {
				$this->session->set_flashdata('message', "Ce code a déjà été ajouté ou appartient à un utilisateur participant au jeu en tant que parrain.");
			}
		} else {
			$this->session->set_flashdata('message', "Ce code est incorrect ou ne correspond à aucun utilisateur.");
		}

		redirect('/');
	}
}
