<?php

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

class Event_pay extends MY_BackendController
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('Event_model');
        $this->load->model('Course_model');
        $this->load->model('Course_category_model');
        $this->load->model('Student_model');
        $this->load->model('Event_timeslot_model');
        $this->load->model('Event_enrollment_model');
    }

    public function index()
    {
        $courseId = $courseCategory = $studentId = null;
        if ($this->input->method() == 'post') {
            $params = $this->input->post();
            // $courseId = $params['course_id'];
            // $courseCategoryId = $params['course_category_id'];
            // $studentId = $params['student_id'];

            try {
                $eventId = $params['event_id'];

                $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();
            }
        }
		
        $this->view('backend/event/pay', [
            'course_id' => null,
            'courses' => $this->Course_model->listing_in_event_selection(),
            'course_category_id' => null,
            'courseCategories' => $this->Course_category_model->listing_selection(),
            'search_student' => $this->load->view('backend/event/partials/modal_search_student', [
                'content' => $this->load->view('backend/parent/partials/search_student', [], true),
                'relationship' => $this->Student_model->relationship,
            ], true),
        ]);
    }
}
