<?php

namespace App\Http\Controllers;

use App\Course;
use App\Professor;
use Illuminate\Http\Request;
use App\Semester;

class DashboardController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        return view('dashboard', [
            'professors' => Professor::orderBy('last_name')->get(),
            'semesters' => Semester::orderBy('code')->get()
        ]);
    }

    /**
     * Download the schedule for the given semester.
     *
     * @param string $semester
     * @return View
     */
    public function schedule($semester_code) {
        $data = Semester::findOrFail($semester_code);
        $data->courses = $data->courses->sort('cmpCourseCode');
        foreach($data->courses as $course) {
            $course->sections = $course->sections->where('semester_code', '=', $semester_code);
            foreach($course->sections as $section) {
                $section->professors = $section->professors;
            }
        }
        // header('Content-Type: application/pdf');
        // header(`Content-Disposition: attachment; filename="${semester_code}.pdf";`);
        // return $data->toJson();
        return '<script type="text/javascript">var data='.$data->toJson().'</script><script type="text/javascript" src="/js/createPDF.js"></script>';
    }

    public function export($semester_code) {
        $semester = Semester::findOrFail($semester_code);
        $professors = Professor::with(['sections', 'semesters:code,admin_load,investigative_load'])->whereIn('id', function($query) use ($semester_code) {
            $query->select('professor_id')
            ->from('professor_semester')
            ->where('semester_code', '=', $semester_code);
        })->orWhereIn('id', function($query) use ($semester_code) {
            $query->select('professor_id')
            ->from('sections')
            ->join('professor_section', 'sections.id', '=', 'professor_section.section_id')
            ->where('semester_code', '=', $semester_code);
        })->orderBy('last_name')->get();
        header('Content-Type: application/csv');
        header(`Content-Disposition: attachment; filename="${semester_code}.csv";`);
        if ($file = fopen('php://output', 'w+')) {
            fputcsv($file, ['PROFESOR', 'CARGA ACADEMICA', 'CARGA ADMINISTRATIVA', 'CARGA INVESTIGATIVE', 'CARGA TOTAL']);
            foreach ($professors as $professor) {
                $professor->academic_load = $professor->getAcademicLoad($semester);
                $prof_loads = $professor->semesters->find($semester);

                fputcsv($file, [
                    $professor->last_name . ', ' . $professor->first_name,
                    $professor->academic_load,
                    $prof_loads->admin_load ?? 0,
                    $prof_loads->investigative_load ?? 0,
                    $professor->academic_load + ($prof_loads->admin_load ?? 0) + ($prof_loads->investigative_load ?? 0),
                ]);
            }
            fclose($file);
            die();
        }
    }
}
