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

class User extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
    }

    public function index()
    {
        $this->load->view('templates/header');
        $this->load->view('auth/login');
        $this->load->view('templates/footer');
    }
    public function register()
    {
        $this->load->view('templates/header');
        $this->load->view('auth/register');
        $this->load->view('templates/footer');
    }

    public function registration()
    {
        //method untuk registrasi

        //untuk membuat form validasi untuk name email dan pass
        $this->form_validation->set_rules('name', 'Name', 'required|trim');
        $this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email|is_unique[user.email]'); //unique digunakan biar tidak ada yang sama antar email yang terdaftar
        $this->form_validation->set_rules(
            'password',
            'Password',
            'required|trim|min_length[5]|matches[password2]',
            [
                'matches' => 'Password dont match', 'min_length' => 'Password to short'
            ]
        );
        $this->form_validation->set_rules('password', 'Password', 'required|trim|matches[password]');

        //perintah pengkondisian untuk registrasi akun
        if ($this->form_validation->run() == false) {             //jika form validasinya gagal maka dilakukan perintah refresh page ke user/register

            $data['title'] = 'Register';
            redirect('user/register');
        } else {
            //jika form validasinya benar maka dilakukan perintah insert data user
            $data = [
                'name' => htmlspecialchars($this->input->post('name', true)), //html specialchar utk menanggulangi spasi di depan atau blakang
                'email' => htmlspecialchars($this->input->post('email', true)),
                'image' => 'user.jpg',
                'password' => password_hash($this->input->post('password'), PASSWORD_DEFAULT), //membuat password yang disimpan di enkripsi kedalam database
                'id_role' => '2', //id role disesuaikan ya
                'is_active' => '1',
                'datecreated' => time()
            ];
            $this->db->insert('user', $data); //perintah insert data
            $this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">
            Your account has been created
          </div>');
            redirect('user');
        }
    }
    public function login()
    {

        // method untuk login
        $email = $this->input->post('email');
        $password = $this->input->post('password');
        $user = $this->db->get_where('user', ['email' => $email])->row_array();

//membuat pengkondisian login
        if ($user) {
//jika user berhasil login maka yg diverifikasi terlebih dahulu adalah kesamaan passwordnya, dimana $user diambil dari tabel user
            if (password_verify($password, $user['password'])) {
                $data = [ //membuat array untuk session
                    'id_user' => $user['id_user'],
                    'name' => $user['name'],
                    'email' => $user['email'],
                    'id_role' => $user['id_role']
                ];
                $this->session->set_userdata($data); //jika berhasil maka array yang baru dibuat akan disimpan ke dalam session, session ini dapat digunakan untuk menyimpan data idrole dll ketika sudah login

                if ($user['id_role'] == 1) {
                    redirect('admin'); //jika login sebagai admin id role 1, maka akan ke controller admin
                } else if ($user['id_role'] == 2) {
                    redirect('siswa');//jika login sebagai siswa id role 2, maka akan ke controller siswa
                } else {
                    redirect('guru'); //jika tidak maka akan ke controler guru
                }
            } else {
                $this->session->set_flashdata('message', '<div class="alert alert-danger" role="alert">
                    Wrong Password
                  </div>'); //digunakan untuk menampilkan jika password salah
                redirect('user');
            }
        } else {
            $this->session->set_flashdata('message', '<div class="alert alert-danger" role="alert">
            Email is not registered
          </div>');
            redirect('user');
        }
    }



    public function logout()
    {
        $this->session->unset_userdata('id_user');
        $this->session->unset_userdata('name');
        $this->session->unset_userdata('email');
        $this->session->unset_userdata('id_role');
        $this->session->set_flashdata('message', '<div class="alert alert-danger" role="alert">
            You have been logged out
          </div>');
        redirect('user');
    }
    public function blocked()
    {
        echo 'access blocked';
    }
}
