<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Access_schedule extends CI_Controller {
    public function __construct(){
        parent::__construct();
        $this->load->helper('az_auth');
        $this->load->helper('az_access_time');
        az_check_auth('dashboard');
        if ($this->session->userdata('role_name') != 'administrator') { show_404(); }
    }
    private function _users_nonadmin(){
        $this->db->select('user.iduser, user.name, user.username, role.name as role_name');
        $this->db->from('user');
        $this->db->join('role','role.idrole = user.idrole','left');
        $this->db->where('user.status',1);
        $this->db->where('role.name !=','administrator');
        $this->db->order_by('user.name');
        return $this->db->get()->result();
    }
    public function index(){
        $iduser = (int)$this->input->get('iduser');
        $users = $this->_users_nonadmin();
        if ($iduser <= 0 && count($users) > 0) $iduser = (int)$users[0]->iduser;
        $days = array();
        for ($d=1;$d<=7;$d++){ $days[] = array('dow'=>$d,'name'=>az_at_day_name($d),'data'=>az_at_get_day($iduser,$d)); }
        $data = array(
            'users'=>$users,'iduser'=>$iduser,'days'=>$days,
            'enabled'=>az_at_enabled(),
            'default_start'=>az_at_default_start(),'default_end'=>az_at_default_end(),
        );
        $this->load->view('access_schedule/v_schedule',$data);
    }
    public function save_schedule(){
        $iduser = (int)$this->input->post('iduser');
        if ($iduser <= 0){ redirect('access_schedule'); return; }
        for ($d=1;$d<=7;$d++){
            $off = $this->input->post('is_off_'.$d) ? 1 : 0;
            $st  = (string)$this->input->post('start_'.$d); if ($st=='') $st=az_at_default_start();
            $en  = (string)$this->input->post('end_'.$d);   if ($en=='') $en=az_at_default_end();
            $this->db->where('iduser',$iduser); $this->db->where('day_of_week',$d);
            $exist = $this->db->get('user_schedule')->row();
            $row = array('iduser'=>$iduser,'day_of_week'=>$d,'is_off'=>$off,'time_start'=>$st,'time_end'=>$en,'status'=>1);
            if ($exist){ $this->db->where('id',$exist->id); $this->db->update('user_schedule',$row); }
            else { $this->db->insert('user_schedule',$row); }
        }
        $this->session->set_flashdata('ok','Jadwal berhasil disimpan.');
        redirect('access_schedule?iduser='.$iduser);
    }
    public function reset_schedule(){
        $iduser = (int)$this->input->get('iduser');
        if ($iduser > 0){ $this->db->where('iduser',$iduser); $this->db->update('user_schedule',array('status'=>0)); }
        $this->session->set_flashdata('ok','Jadwal dikembalikan ke default.');
        redirect('access_schedule?iduser='.$iduser);
    }
    public function toggle_enabled(){
        $cur = az_at_enabled();
        $this->db->where('setting_key','enabled');
        $this->db->update('access_time_setting',array('setting_value'=>$cur?'0':'1'));
        $this->session->set_flashdata('ok','Pembatasan jam ' . ($cur?'DIMATIKAN':'DIYALAKAN') . '.');
        redirect('access_schedule');
    }
    public function holiday(){
        $this->db->where('status',1); $this->db->order_by('holiday_date','desc');
        $holidays = $this->db->get('holiday')->result();
        foreach ($holidays as $h){
            $this->db->select('user.name'); $this->db->from('holiday_exception');
            $this->db->join('user','user.iduser = holiday_exception.iduser','left');
            $this->db->where('holiday_exception.holiday_id',$h->id); $this->db->where('holiday_exception.status',1);
            $ex = $this->db->get()->result(); $names=array();
            foreach ($ex as $e){ $names[]=$e->name; }
            $h->exception_names = $names;
        }
        $data = array('holidays'=>$holidays,'users'=>$this->_users_nonadmin());
        $this->load->view('access_schedule/v_holiday',$data);
    }
    public function save_holiday(){
        $date = (string)$this->input->post('holiday_date');
        $label = (string)$this->input->post('label');
        $mode = (string)$this->input->post('mode');
        if (strlen($date)==0){ $this->session->set_flashdata('err','Tanggal wajib diisi.'); redirect('access_schedule/holiday'); return; }
        $all_off = ($mode==='except') ? 0 : 1;
        $this->db->where('holiday_date',$date); $exist=$this->db->get('holiday')->row();
        $row = array('holiday_date'=>$date,'label'=>$label,'all_off'=>$all_off,'status'=>1);
        if ($exist){ $this->db->where('id',$exist->id); $this->db->update('holiday',$row); $hid=$exist->id; }
        else { $this->db->insert('holiday',$row); $hid=$this->db->insert_id(); }
        $this->db->where('holiday_id',$hid); $this->db->update('holiday_exception',array('status'=>0));
        if ($all_off==0){
            $ex = $this->input->post('except_user');
            if (is_array($ex)){ foreach ($ex as $uid){ $uid=(int)$uid; if ($uid>0){
                $this->db->where('holiday_id',$hid); $this->db->where('iduser',$uid); $e=$this->db->get('holiday_exception')->row();
                if ($e){ $this->db->where('id',$e->id); $this->db->update('holiday_exception',array('status'=>1)); }
                else { $this->db->insert('holiday_exception',array('holiday_id'=>$hid,'iduser'=>$uid,'status'=>1)); }
            }}}
        }
        $this->session->set_flashdata('ok','Hari libur berhasil disimpan.');
        redirect('access_schedule/holiday');
    }
    public function delete_holiday(){
        $id=(int)$this->input->get('id');
        if ($id>0){
            $this->db->where('id',$id); $this->db->update('holiday',array('status'=>0));
            $this->db->where('holiday_id',$id); $this->db->update('holiday_exception',array('status'=>0));
        }
        $this->session->set_flashdata('ok','Hari libur dihapus.');
        redirect('access_schedule/holiday');
    }
}