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

class Users extends MY_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
    function __construct() {
        parent::__construct();
        $this->load->model("User_model", "user");
        $this->load->model('Group_model', "group");
        $this->load->model('Uuid_model', "uuid");
    }
    public function index()
	{
        $res = $this->user->get_all();
        $result = array();
        if($res["success"]){
            $users = $res["data"];
            foreach($users as $user){
                $user["password"] = "********";
                array_push($result, $user);
            }
        }

        $group_options = $this->group->get_groups_list();
        $groups_result = $this->group->get_all();
        $uuids = $this->uuid->unassigned_uuid();
        $groups = array();
        foreach($groups_result["data"] as $row){
            $groups[$row["id"]] = $row["group_name"];
        }
        $res_data['view'] = 'pages/user_list';
        $res_data['data'] = array('users'=>$result, "groups"=>$groups, "group_options"=> $group_options, "uuids"=>$uuids);
        $this->load->view('layout', $res_data);
    }

    public function get_users_status()
    {
        $res = $this->user->get_users_status();
        $groups_result = $this->group->get_all();
        $groups = array();
        foreach($groups_result["data"] as $row){
            $groups[$row["id"]] = $row["group_name"];
        }
        $res_data['view'] = 'pages/user_status';
        $res_data['data'] = array('users'=>$res, "groups"=>$groups);
        $this->load->view('layout', $res_data);
    }

    public function add_user(){
        $uuid = $this->input->post("uuid");
        $user_email = $this->input->post("user_email");
        $user_name = $this->input->post("user_name");
        $password = $this->input->post("password");
        $group_id = $this->input->post("group_id");
        if(!isset($group_id)) {
            $group_id = 0;
        }
        $level = $this->input->post("level");
        $row = array("uuid"=>$uuid, 
                     "user_email" => $user_email,
                     "user_name" => $user_name,
                     "password"=> md5($password),
                     "group_id" => $group_id,
                     "level" => $level);
        $result = $this->user->insert($row);
        $this->uuid->set_assign($uuid);
        $row["id"] = $result;
        header("Content-Type: application/json");
        echo json_encode($row);
    }

    public function update_user(){
        $uuid = $this->input->post("uuid");
        $user_email = $this->input->post("user_email");
        $user_name = $this->input->post("user_name");
        $group_id = $this->input->post("group_id");
        if(!isset($group_id)) {
            $group_id = 0;
        }
        $level = $this->input->post("level");
        $id = $this->input->post("id");
        $row = array("id" => $id,
                     "uuid" =>$uuid, 
                     "user_email" => $user_email,
                     "user_name" => $user_name,
                     "group_id" => $group_id,
                     "level" => $level);
        $this->user->update($row);
        $this->uuid->set_assign($uuid);
        header("Content-Type: application/json");
        $row["password"] = "********";
        echo json_encode($row);
    }

    public function delete_user($id){
        $result = $this->user->delete($id);
        echo json_encode(TRUE);
    }

    public function get_user($user_id){
        $user = $this->user->get($user_id);
        echo json_encode($user["data"]);
    }
}
