<?php

use app\libraries\DateHelper;

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

class Event_enroll extends MY_BackendController
{

    public function __construct()
    {
        parent::__construct();
    }

    public function index($eventId, $timeslotId)
    {
        $this->load->model('Event_model');
        $item = $this->Event_model->detail($eventId);
        if (!$item) {
            return $this->exception_dataNotFound();
        }

        if ($this->input->method() == 'post') {
            $params = $this->input->post();

            try {
                $this->load->model('Event_enrollment_invoicing_model');
                $this->Event_enrollment_invoicing_model->run($params, $eventId);

                if (array_key_exists('submit_redirect', $params)) {
                    $this->load->model('Student_model');
                    $student = $this->Student_model->detail($params['student_id']);
                    redirect('/invoicing?studentno=' . $student['student_no'] . '&selectedno=' . $student['student_no']);
                } else {
                    $this->_data['__alert_success_message'] = '儲存成功。';
                }
            } catch (Exception $e) {
                $this->_data['__alert_error_message'] = $e->getMessage();
            }
        }
        // $timeslot = $this->Event_timeslot_model->detail($timeslotId);
        $course = $this->Event_model->detail_only_course($eventId);

        $this->load->model('Course_category_model');
        $this->load->model('Course_subcategory_model');
        $courseCategory = $this->Course_category_model->detail($course['course_category_id']);
        $courseSubCategory = $this->Course_subcategory_model->detail($course['course_category_sub_id']);

        $this->load->model('Student_model');
        $this->load->model('Event_enrollment_invoicing_model');

        $searchStudentPartial = $this->load->view('backend/parent/partials/search_student', [], true);
        $searchStudent = $this->load->view('backend/event/partials/modal_search_student', [
            'content' => $searchStudentPartial,
            'relationship' => $this->Student_model->relationship,
        ], true);
        $this->view('backend/event/enroll', [
            '__eventId' => $eventId,
            '__timeslotId' => $timeslotId,
            '__pathname' => 'enroll',
            'title' => '加入學生',

            // 'title' => sprintf('%s (%s)', $course['name'],  DateHelper::getDefaultTcWeekdays($timeslot['startdate'])),
            'date_from' => date('Y-m-d', $item['date_from']),
            'date_to' => date('Y-m-d', $item['date_to']),
            'course_category_id' => $courseCategory['id'],
            'course_category_name' => $courseCategory['name'],
            'course_subcategory_id' => $courseSubCategory['id'],
            'course_subcategory_name' => $courseSubCategory['name'],
            'course_id' => $course['id'],
            'course_name' => $course['name'],
            'course_mode' => $course['mode'],
            'event_id' => $item['id'],
            'event_name' => $item['display_name'],
            'search_student' => $searchStudent,
            // 'student_id' => null,
            // 'students' => $this->Student_model->listing_in_enrollment_selection(),

            'detail' => $this->Event_enrollment_invoicing_model->enroll_mode($item, $course)
        ]);
    }
}
