<?php

use app\libraries\DateHelper;

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

class Event_clone extends MY_BackendController {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('Event_model');
        $this->load->model('Room_model');
        $this->load->model('Teacher_model');
        $this->load->model('Course_model');
        $this->load->model('Event_timeslot_model');
    }

	public function index($eventId, $timeslotId)
	{
        $eventCourse = $this->Event_course_model->detail_eventId($eventId);
        $courseId = $eventCourse['course_id'];
        $course = $this->Course_model->detail($courseId);
        $item = $this->Event_model->detail($eventId);
        $timeslot = $this->Event_timeslot_model->detail($timeslotId);

        // Date
        $dateTimestamp = $timeslot['startdate'];
        $date = date('Y-m-d', $dateTimestamp);
        $dayOfWeek = intval(date('w', $dateTimestamp));

        // Time
        $time = $timeslot['starttime'];

        // Date + Time => Weekdays
        $weekdays = [$dayOfWeek => $time];

		$this->view('backend/event/clone', [
            '__eventId' => $eventId,
            '__timeslotId' => $timeslotId,
            '__pathname' => 'clone',
            'title' => $this->Course_model->fullTitle($course['name'], $item['weekdays']),
            'date' => $date,
            'date_from' => $date,
            // TODO: Remove this by adding back the settings functionality
            'date_to' => '2021-06-30',
            'default_weekdays' => DateHelper::getDefaultWeektimes(),
            'weekdays' => $weekdays,
            'room_id' => $item['room_id'],
            'rooms' => $this->Room_model->listing(),
            'pic_1' => $item['pic_1'],
            'pic_2' => $item['pic_2'],
            'teachers' => $this->Teacher_model->listing(),
            'course_id' => $courseId,
            'courses' => $this->Course_model->listing_in_event_selection()
        ]);
    }
}
