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

class User extends MY_Controller
{
    
     public $data = array(
        'breadcrumb'    => 'User',
        'modul'         => 'User',
        'pesan'         => '',
        'pagination'    => '',
        'datatable'    => '',
        'main_view'     => 'user/user', ///This View Form or Table is want to the load
        'form_action'   => '',
        'form_value'    => '',
        );
	
    public function __construct()
    {	
        parent::__construct();		
        $this->load->model('User_model', 'user', TRUE);
        $this->load->model('Login_model', 'userSession', TRUE);
        $this->load->library('session');
        $this->load->helper('encryption');
    }
	
    public function index()
    {

        $session_data = $this->session->userdata('full_name');
        $session_role = $this->session->userdata('role_id');

        $this->data['full_name'] = $session_data;   
        $this->data['role_id'] = $session_role;     
        $this->data['role'] = $this->user->getRole();
        //print_r($this->data['role']);
        $this->load->view('template', $this->data);
    }

    public function GetDataUserById()
    {
        $id = $_POST['id'];
        $data = $this->user->GetUserById($id);
           
        echo json_encode($data); 
    }

    public function GetDataUser()
    {
       $columns = array( 
                            0 =>'username', 
                            1 =>'full_name',
                            2=> 'email',
                            3=> 'roleName',
                            4=> 'username',
                        );

        $limit = $this->input->post('length');
        $start = $this->input->post('start');
        $order = $columns[$this->input->post('order')[0]['column']];
        $dir = $this->input->post('order')[0]['dir'];

        $totalData = $this->user->alluser_count();
        $totalFiltered = $totalData; 
            
        if(empty($this->input->post('search')['value']))
        {            
            $posts = $this->user->alluser($limit,$start,$order,$dir);
        }
        else {
            $search = $this->input->post('search')['value']; 

            $posts =  $this->user->user_search($limit,$start,$search,$order,$dir);

            $totalFiltered = $this->user->user_search_count($search);
        }
        
        $data = array();
        if(!empty($posts))
        {
            foreach ($posts as $post)
            {

                $nestedData['username']  = $post->username;
                $nestedData['full_name'] = $post->full_name;
                $nestedData['email']     = $post->email;
                $nestedData['roleName']   = $post->roleName;
                
                $data[] = $nestedData;

            }
        }

      
        $json_data = array(
                "draw"            => intval($this->input->post('draw')),  
                "recordsTotal"    => intval($totalData),  
                "recordsFiltered" => intval($totalFiltered), 
                "data"            => $data   
                );

        echo json_encode($json_data); 
       
    }

    public function InsertUser()
    {
        $password = encrypt($this->input->post('txtAddPassword'));
        $data = array(
                            'username'   => $this->input->post('txtAddUsername'), 
                            'password'   => $password, 
                            'full_name'  => $this->input->post('txtAddFullName'), 
                            'email'      => $this->input->post('txtAddMail'), 
                            'no_telp'    => $this->input->post('txtAddPhone'), 
                            'active'     => $this->input->post('rbstatusactive'),
                            'gender'     => $this->input->post('rbjenkel'),
                            'role_id'    => $this->input->post('role'),
                    );
     
      $simpan = $this->user->InsertUser($data);
      echo 'ok';
    }

    public function UpdateUser()
    {
        $id =  $this->input->post('txtIdUsername');    
        $data = array(
                           
                            'full_name'  => $this->input->post('txtEditFullName'), 
                            'email'      => $this->input->post('txtEditMail'), 
                            'no_telp'    => $this->input->post('txtEditPhone'), 
                            'active'     => $this->input->post('rbstatusactive'),
                            'gender'     => $this->input->post('rbjenkel'),
                            'role_id'    => $this->input->post('roleEdit'),
                    );
     
      $simpan = $this->user->UpdateUser($id,$data);
      echo 'ok';
    }
}

/* End of file user.php */
/* Location: ./application/controllers/user.php */