<?php

namespace App\Controllers\Mobile\Kelas;

use App\Controllers\Mobile\BC;
use App\Models\Mobile\Kelas\Jadwal as JadwalPelajaran;
use CodeIgniter\API\ResponseTrait;

class Jadwal extends BC
{
    use ResponseTrait;
    private $jadwalModel;
    public function __construct()
    {
        $this->jadwalModel = new JadwalPelajaran();
    }
    public function index()
    {
        return view('Mobile/Kelas/jadwal', ['page_title' => 'Jadwal Pelajaran', 'ft' => 'kelas', 'no_header' => true, 'no_sidebar' => true]);
    }
    public function all()
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        if (!options('el_mode')) {
            return $this->respond([]);
        }
        $data = [];
        $day = [date("w", $this->request->getGet('startTime')) + 1, date("w", $this->request->getGet('endTime')) + 1];
        foreach ($this->jadwalModel->getAll($day) as $j) {
            $ed = $j['startTime'] > $j['endTime'] ? $j['startDay'] + 1 : $j['startDay'];
            $ed = $ed > 7 ? 1 : $ed;
            if (($this->request->getGet('startTime') && $this->request->getGet('endTime')) && !(get_time($j['startDay']) >= $this->request->getGet('startTime') && get_time($j['startDay'], $j['startTime'] > $j['endTime'] ? $j['endTime'] : "00:00:00") <= $this->request->getGet('endTime'))) continue;
            $j['startDateTime'] = get_time($j['startDay'], $j['startTime']);
            $j['endDateTime'] = get_time($ed, $j['endTime']);
            $data[] = $j;
        }
        return $this->respond($data);
    }
}
