<?php

use app\libraries\ICSParser;

defined('BASEPATH') or exit('No direct script access allowed');
require_once "Event_index_calendar.php";


class Event extends MY_BackendController
{
    use Event_index_calendar;
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        // $res = $this->calendar();
        //$data['_inactive_loading'] = true;
        $this->load->model('Event_model');
        $this->load->model('Course_model');
        $this->load->model('Event_timeslot_model');
        $event_data = $this->Event_model->simple_get();
        $course_data = $this->Course_model->listing_selection();
        $timeslot_data = $this->Event_timeslot_model->listing();
        //var_dump( json_decode($result[4]['weekday_json']));  




        $this->view('backend/event/index', [
            //'_events' => $res['events'],
            //'_resources' => $res['resources'],
            '_action_' => '',
            '_inactive_loading' => true,
            'event_data' => $event_data,
            'course_data' => $course_data,
            'timeslot_data' => $timeslot_data
        ]);
    }

    public function newEvent()
    {
        $request = $this->input->post();

        $this->load->model('Event_model');
        $weekday = "";
        for ($i = 0; $i < 7; $i++) {
            if ($request['time'][$i] != "") {
                $weekday .= $i . "|";
            }
        }

        //waiting for modify fetch name from the database
        if (isset($request['selected_timeslot'])) {
            $data = [

                'display_name' => $request['event_code'],
                'date_from' => $request['start_date'],
                'event_code' =>  $request['event_code'],
                'course_id' => $request['course_name'],
                'date_to' => $request['end_date'],
                'weekday' => $weekday,
                //'mode' => $request['course_mode'],
                'duration' => $request['course_duration'],
                'weekday-sun-time' => $request['time'][0],
                'weekday-mon-time' => $request['time'][1],
                'weekday-tue-time' => $request['time'][2],
                'weekday-wed-time' => $request['time'][3],
                'weekday-thu-time' => $request['time'][4],
                'weekday-fri-time' => $request['time'][5],
                'weekday-sat-time' => $request['time'][6],
                'selected_timeslot' => $request['selected_timeslot'],
                'location' => $request['location'],
                'staff' => $request['staff'],
                'assist' => $request['assist'],
                'remark' => $request['remark']
            ];
        } else {
            $data = [

                'display_name' => $request['event_code'],
                'date_from' => $request['start_date'],
                'event_code' =>  $request['event_code'],
                'course_id' => $request['course_name'],
                'date_to' => $request['end_date'],
                'weekday' => $weekday,
                //'mode' => $request['course_mode'],
                'duration' => $request['course_duration'],
                'weekday-sun-time' => $request['time'][0],
                'weekday-mon-time' => $request['time'][1],
                'weekday-tue-time' => $request['time'][2],
                'weekday-wed-time' => $request['time'][3],
                'weekday-thu-time' => $request['time'][4],
                'weekday-fri-time' => $request['time'][5],
                'weekday-sat-time' => $request['time'][6],
                'location' => $request['location'],
                'staff' => $request['staff'],
                'assist' => $request['assist'],
                'remark' => $request['remark']
            ];
        }
        $this->Event_model->create_new_event($data);
        redirect('/backend/event');
    }

    public function detail()
    {
        $id = $this->input->get();
        $data['_inactive_loading'] = true;

        $data['id'] = $id['id'];

        $this->load->model("Event_timeslot_model");
        $data["timeslot_information"] = $this->Event_timeslot_model->get_id($id['id']);
        $this->load->model("Event_model");
        $data["event_information"] = $this->Event_model->get_id($data["timeslot_information"]["event_id"]);
        $data['event_course_information'] = $this->Event_course_model->detail_eventId($data["timeslot_information"]["event_id"]);
        $data["course_information"] = $this->Course_model->get_id($data['event_course_information']['course_id']);
        $this->load->model("Event_enrollment_item_model");
        $data["enrollment_item"] = json_decode(json_encode($this->Event_enrollment_item_model->listing_in_event_attendance2($id['id'])), true);

        $this->view('backend/event/detail/attendance', $data);
    }

    public function attendance()
    {
        $id = $this->input->get();
        $data['_inactive_loading'] = true;

        $data['id'] = $id['id'];

        $this->load->model("Event_timeslot_model");
        $data["timeslot_information"] = $this->Event_timeslot_model->get_id($id['id']);
        $this->load->model("Event_model");
        $data["event_information"] = $this->Event_model->get_id($data["timeslot_information"]["event_id"]);
        $data['event_course_information'] = $this->Event_course_model->detail_eventId($data["timeslot_information"]["event_id"]);
        $data["course_information"] = $this->Course_model->get_id($data['event_course_information']['course_id']);
        $this->load->model("Event_enrollment_item_model");
        $data["enrollment_item"] = json_decode(json_encode($this->Event_enrollment_item_model->listing_in_event_attendance2($id['id'])), true);

        $this->view('backend/event/detail/attendance', $data);
    }

    public function attendace_update()
    {
        $get = $this->input->get();
        $post = $this->input->post();
        var_dump($get);
        var_dump($post);

        $this->load->model("Event_enrollment_item_model");
        $this->Event_enrollment_item_model->update_attendance($post);


        redirect("backend/event/attendance?id=" . $post['timeslot_id']);
    }

    public function add_new_student()
    {
        $id = $this->input->get();


        $data['_inactive_loading'] = true;
        $data['id'] = $id['id'];

        $this->load->model("Event_timeslot_model");
        $this->load->model("Event_model");
        $this->load->model("Course_model");
        $this->load->model("Event_course_model");
        $this->load->model("Event_enrollment_model");

        $data["timeslot_information"] = $this->Event_timeslot_model->get_id($id['id']);
        $data["event_information"] = $this->Event_model->get_id($data["timeslot_information"]["event_id"]);
        $data['event_enrollment_information'] = $this->Event_enrollment_model->listing_by_event_id($data["timeslot_information"]['event_id']);
        $data["event_information"]["weekdays"] = explode("|", $data["event_information"]["weekdays"]);
        $data['event_course_information'] = $this->Event_course_model->detail_eventId($data["timeslot_information"]["event_id"]);
        $data["course_information"] = $this->Course_model->get_id($data['event_course_information']['course_id']);
        $this->view('backend/event/detail/add_new_student', $data);
    }

    public function add_student_to_event()
    {
        $post = $this->input->post();
        $event_id = $post['event_id'];
        $course_id = $post["course_name"];
        $this->load->model("Course_model");
        $this->load->model("Event_model");
        $course_information = $this->Course_model->get_id($course_id);
        $price = $net_amount = $course_information["price"];
        $discount_amount = 0;
        $enrollment_information = [
            'event_id' => $post['event_id'],
            'student_id' => $post['student_no'],
            "enroll_date" => time(),
        ];
        try {
            $this->load->model('Event_enrollment_invoicing_model');
            $enrollment_information['net_amount'] =  $net_amount;
            $enrollment_information['discount_amount'] = $discount_amount;
            $enrollment_information['price'] = $price;
            $enrollment_information['discount'] = $discount_amount;
            $this->Event_enrollment_invoicing_model->run($enrollment_information, $event_id);

            if (array_key_exists('submit_redirect', $enrollment_information)) {
                $this->load->model('Student_model');
                $student = $this->Student_model->detail($enrollment_information['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();
        }
        redirect('backend/event/index');
    }

    public function makeup()
    {
        $id = $this->input->get();
        $data['_inactive_loading'] = true;
        $data['id'] = $id['id'];
        $this->load->model("Event_timeslot_model");
        $this->load->model("Event_model");
        $this->load->model("Course_model");
        $this->load->model("Event_course_model");
        $this->load->model("Event_enrollment_item_model");
        $data["timeslot_information"] = $this->Event_timeslot_model->get_id($id['id']);
        $data["event_information"] = $this->Event_model->get_id($data["timeslot_information"]["event_id"]);
        $data["event_information"]["weekdays"] = explode("|", $data["event_information"]["weekdays"]);
        $data['event_course_information'] = $this->Event_course_model->detail_eventId($data["timeslot_information"]["event_id"]);
        $data["course_information"] = $this->Course_model->get_id($data['event_course_information']['course_id']);
        $data["enrollment_item"] = json_decode(json_encode($this->Event_enrollment_item_model->listing_in_event_attendance2($id['id'])), true);

        $this->view("backend/event/detail/makeup", $data);
    }

    public function makeup_create()
    {
        $post = $this->input->post();

        $this->load->model("Event_enrollment_item_model");
        $this->Event_enrollment_item_model->makeup($post);
        redirect('/backend/event/makeup?id=' . $post["id"]);
    }

    public function edit()
    {
        $id = $this->input->get();
        $data['_inactive_loading'] = true;
        $data['id'] = $id['id'];

        $this->load->model("Event_timeslot_model");
        $this->load->model("Event_model");
        $this->load->model("Course_model");
        $this->load->model("Event_course_model");
        $this->load->model("Event_enrollment_model");

        $data["timeslot_information"] = $this->Event_timeslot_model->get_id($id['id']);
        $data["event_information"] = $this->Event_model->get_id($data["timeslot_information"]["event_id"]);
        $data['event_enrollment_information'] = $this->Event_enrollment_model->listing_by_event_id($data["timeslot_information"]['event_id']);
        $data["event_information"]["weekdays"] = explode("|", $data["event_information"]["weekdays"]);
        $data['event_course_information'] = $this->Event_course_model->detail_eventId($data["timeslot_information"]["event_id"]);
        $data["course_information"] = $this->Course_model->get_id($data['event_course_information']['course_id']);
        /* var_dump($data["timeslot_information"]);
        var_dump($data["event_information"]);
        var_dump($data['course_information']);
        exit;*/



        $this->view("backend/event/detail/edit", $data);
    }

    public function event_update()
    {
        $post = $this->input->post();
        $data["_inactive_loading"] = true;
        $event_id = $post['event_id'];
        $this->load->model("Event_timeslot_model");
        $this->load->model("Event_model");
        //del the exist timeslot data
        $this->Event_timeslot_model->delete_by_event_id($event_id);
        //
        $weekday = "";
        for ($i = 0; $i < 7; $i++) {
            if ($post['time'][$i] != "") {
                $weekday .= $i . "|";
            }
        }
        $data = [
            'display_name' => $post['event_code'],
            'date_from' => $post['start_date'],
            'event_code' =>  $post['event_code'],
            'course_id' => $post['course_name'],
            'date_to' => $post['end_date'],
            'weekday' => $weekday,
            'duration' => $post['course_duration'],
            'weekday-sun-time' => $post['time'][0],
            'weekday-mon-time' => $post['time'][1],
            'weekday-tue-time' => $post['time'][2],
            'weekday-wed-time' => $post['time'][3],
            'weekday-thu-time' => $post['time'][4],
            'weekday-fri-time' => $post['time'][5],
            'weekday-sat-time' => $post['time'][6],
            'location' => $post['location'],
            'staff' => $post['staff'],
            'assist' => $post['assist'],
            'remark' => $post['remark']
        ];
        $this->Event_model->edit_event($data, $event_id);
        redirect("backend/event");
    }

    public function delete()
    {
        $get = $this->input->get();
        $this->load->model("Event_timeslot_model");
        $this->load->model("Event_enrollment_model");
        $this->load->model("Event_model");
        $event_information = $this->Event_timeslot_model->get_id($get['id']);
        $event_id = $event_information['event_id'];
        $event_enrollment_record = $this->Event_enrollment_model->listing_by_event_id($event_id);
        if (sizeof($event_enrollment_record) != 0) {
            redirect("backend/event/edit?id=" . $get['id']);
        } else {
            $this->Event_timeslot_model->delete_by_event_id($event_id);
            $this->Event_model->delete($event_id);
            redirect("backend/event/index");
        }
    }
}
