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

class Users extends CI_Controller {


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

    public function index() {
        $data['users']=$this->User_model->get_user_all();
		$id_user=$this->session->userdata('id_user');
        $data['user']=$this->User_model->get_user($id_user);
	
		$data['nama'] = "Data Users";
		
        $this->load->view('Templates/header.php',$data);
        $this->load->view('Templates/navbar.php',$data);
        $this->load->view('Templates/leftmenu.php',$data);
        $this->load->view('users/index.php',$data);
        $this->load->view('Templates/footer.php',$data);
    }

    public function tambah()
	{
	
		$id_user=$this->session->userdata('id_user');
        $data['user']=$this->User_model->get_user($id_user);
        $data['nama'] = "Tambah data";

        $this->form_validation->set_rules('email', 'email', 'required|is_unique[m_user.email]');
        $this->form_validation->set_rules('username', 'username', 'required|is_unique[m_user.username]');
        $this->form_validation->set_rules('password', 'password', 'required|trim|min_length[3]');

		if ($this->form_validation->run() == false) {

        $this->load->view('Templates/header.php',$data);
        $this->load->view('Templates/navbar.php',$data);
        $this->load->view('Templates/leftmenu.php',$data);
        $this->load->view('users/tambah.php',$data);
        $this->load->view('Templates/footer.php',$data);

        }else{
         
	
        $email=$this->input->post('email', true);
		$username=$this->input->post('username', true);
        $password =$this->input->post('password', true);

		$this->User_model->add($email,$username,$password);


		$this->session->set_flashdata('flash', 'Ditambahkan');
		$this->session->set_flashdata('data', 'Data Users');

		redirect('users');
        }
    }

    public function edit($id)
	{
	
		$id_user=$this->session->userdata('id_user');
        $data['user']=$this->User_model->get_user($id_user);
        $data['nama'] = "Tambah data";

        $this->form_validation->set_rules('password', 'password', 'required|trim|min_length[3]');

		if ($this->form_validation->run() == false) {

        $this->load->view('Templates/header.php',$data);
        $this->load->view('Templates/navbar.php',$data);
        $this->load->view('Templates/leftmenu.php',$data);
        $this->load->view('users/edit.php',$data);
        $this->load->view('Templates/footer.php',$data);

        }else{
         
	
        $password =$this->input->post('password', true);

		$this->User_model->edit($id,$password);


		$this->session->set_flashdata('flash', 'Ditambahkan');
		$this->session->set_flashdata('data', 'Data Users');

		redirect('users');
        }
    }

    public function hapus($id)
	{
	
		$this->User_model->hapus($id);
		$this->session->set_flashdata('flash', 'dihapus');
        $this->session->set_flashdata('data', 'Data Users');
	
		redirect('users');
    }
}