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

class Login extends CI_Controller
{

    // CONSTRUCT
    public function __construct()
    {
        parent::__construct();
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->helper('url');
    }

    public function index()
    {

        $doctor = $this->db->query("select * from doctor where Active = 'T' and FirstName != '' ")->result_array();

        $data['judul'] = "Login";
        $data['doctor'] = $doctor;
        $this->load->view('login', $data);
        // $this->load->view('link/index', $data);
    }

    public function cekLogin()
    {
        $this->_auth();
    }

    private function _auth()
    {
        $username = $this->input->post('username');
        $password = $this->input->post('password');
        $shift    = $this->input->post('shift');
        $loginIdentity = $this->input->post('loginIdentity');
        $userLoginAs = 'general';


        $doctorId = '';
        $dept = '';
        $user = $this->db->get_where('userV2', ['username' => $username])->row_array();
        if($loginIdentity == 'dokter'){  
            $user = $this->db->query("select * , Active as userstatus from doctor where FirstName != '' and username = '$username' ")->row_array();
            $userLoginAs = 'dokter';
            $doctorId = $user['DoctorID'];
        }

        $this->form_validation->set_rules('username', 'username', 'trim|required', array('required' => 'Harap masukan username anda'));
        $this->form_validation->set_rules('password', 'password', 'trim|required', array('required' => 'Harap masukan password'));

        if ($user) {

            if(isset($user['dept'])){
                $dept = $user['dept'];
            }

            if ($user['userstatus'] == 1 || $user['userstatus'] == 'T' ) {
                if ($password == $user['password'] || password_verify($password, $user['password'])) {
                    $data = [
                        'username' => $user['username'],
                        'dept' => $dept,
                        'doctorId' => $user['IdAlias'],
                        'shift'  => $shift,
                        'wareHouseCode'  => '1', // KHUSUS SUDIRMAN NADYA BEKASI wareHouseCode = 1
                        'userLoginAs' => $userLoginAs
                    ];
                    $this->session->set_userdata($data);
                    redirect('rekamMedis');
                } else {
                    $this->session->set_flashdata('message', '<div class="alert alert-warning" role="alert"> <u>password</u> yang dimasukan salah!</div>');
                    redirect('login');
                }
            } else {
                $this->session->set_flashdata('message', '<div class="alert alert-warning" role="alert"> <u>username</u> tidak tidak aktif!</div>');
                redirect('login');
            }
        } else {
            $this->session->set_flashdata('message', '<div class="alert alert-warning" role="alert"> <u>username</u> tidak terdaftar!</div>');
            redirect('login');
        }
    }
}
