<?php

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

class Slip extends MY_BackendController
{
    //    protected $authorizedRoutes = ['index', 'add', 'edit', 'delete'];

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

    public function index()
    {
        $this->load->model('Rbac_permission_model', 'Permission_model');

        $t = $this->input->get('t');
        $data['items'] = $this->Slip_model->get_all_slip();
        $template = 'backend/slip/index';
        $this->view($template, $data);
    }

    public function add()
    {
        $this->load->model('Course_model');
        $this->load->model('Centre_model');

        $student_centres = $this->Centre_model->listing();
        $sub2 = $this->load->view('backend/search_student', [
            'student_centres' => $student_centres,
            'student_centre_code' => null,
        ], true);
        $sub1 = $this->load->view('backend/modal_search_student', ['content' => $sub2], true);
        return $this->view('backend/slip/add', [
            'courses' => $this->Course_model->all(),
            'search_student' => $sub1,
            'start_datetime' => date('Y-m-d 00:00:00'),
            'end_datetime' => date('Y-m-d 23:59:00', strtotime('+7 days')),
        ]);
    }

    public function edit()
    {
        $id = $this->input->get('id');
        $template = 'backend/slip/edit';


        $this->load->model('Slip_model');
        $item = $this->Slip_model->get_slip($id);
        $studentIds = $item['student_ids'];

        $this->load->model('Student_model');
        $students = $this->Student_model->get_students($studentIds);
        $this->view($template, [
            'id' => $id,
            'item' => $item,
            'pushed' => intval($item['push']) === 1 ? 'checked' : '',
            'optionAttend' => intval($item['option_attend']) === 1 ? 'checked' : '',
            'optionOpt' => intval($item['option_opt']) === 1 ? 'checked' : '',
            'optionAgree' => intval($item['option_agree']) === 1 ? 'checked' : '',
            'sendAll' => intval($item['send_all']) === 1 ? 'checked' : '',
            'students' => $students
        ]);
    }

    public function save()
    {
        $data = $this->input->post();
        $date = date('Y-m-d', strtotime($data['issued_date']));
        $due = $data['due'];
        $id = isset($data['id']) ? $data['id'] : 0;
        $this->Slip_model->save_slip(array(
            'issued_date' => $date,
            'issued_time' => $data['issued_time'],
            'title' => $data['title'],
            'content' => $data['content'],
            'remark' => $data['remark'],
            'due_datetime' => $due,
            'push' => $data['push'] == '1' ? 1 : 0,
            'option_attend' => $data['option_attend'] == '1' ? 1 : 0,
            'option_opt' => $data['option_opt']  == '1' ? 1 : 0,
            'option_agree' => $data['option_agree']  == '1' ? 1 : 0,
            'send_all' => $data['send_all']  == '1' ? 1 : 0,
            'student_ids' => $data['student_ids'] ? implode(',', $data['student_ids']) : null,
        ), $data['mode'], $id);
        echo json_encode('0');
    }

    public function delete()
    {
        $this->Slip_model->delete_slip($this->input->get('id'));
        echo json_encode('0');
    }
}
