<?php

namespace App\Http\Livewire;

use Livewire\Component;

class AbsenceReport extends Component
{

    public $records;

    public $year;

    public function mount()
    {
        setlocale(LC_ALL, 'it_IT');
    }

    public function render()
    {
        setlocale(LC_ALL, 'it_IT');

        $this->records = [];

        // $to = date("Y-m-d 23:59:59");
        // $calendars = \App\Models\Calendar::where('from', '<=', $to)->orderBy('from')->get();

        $month = date("n");
        $this->year = ($month >= 9) ? date("Y") : date("Y") - 1;

        $start = date("Y-m-d H:i:s", mktime(0, 0, 0, 9, 1, $this->year));
        $end   = date("Y-m-d 23:59:59");

        $calendars = \App\Models\Calendar::whereBetween('from', [$start, $end])->orderBy('from')->get();

        foreach ($calendars as $calendar) {

            $presences = \App\Models\Presence::where('calendar_id', $calendar->id)->where('status', '<>', 99);
            $presences = $presences->pluck('member_id')->toArray();
            $presences_annullate = \App\Models\Presence::where('calendar_id', $calendar->id)->where('status', 99)->pluck('member_id')->toArray();

            $days = ['dom', 'lun', 'mar', 'mer', 'gio', 'ven', 'sab'];
            $dow = date('w', strtotime($calendar->from));
            $d = $days[$dow];

            $h = date('H:i', strtotime($calendar->from));

            // Elenco corsi per tipologia in base al calendario
            $courses = \App\Models\Course::where('name', $calendar->name)->where('date_from', '<=', $calendar->from)->where('date_to', '>=', $calendar->to);
            $courses = $courses->pluck('id')->toArray();

            $months = date("n", strtotime($calendar->from));

            // Elenco utenti iscritti al corso "padre"
            $members = \App\Models\MemberCourse::where('when', 'like', "%" . $d . "%")
                ->where('when', 'like', '%"from":"' . $h . '"%')
                ->whereNot('months', 'like', '%"m":' . $months . ',"status":2%')
                ->whereDate('date_from', '<=', $calendar->from)                                 
                ->whereDate('date_to', '>=', $calendar->from)      
                ->whereIn('course_id', $courses)
                ->orderBy('member_id')
                ->get();
            //$members = \App\Models\MemberCourse::where('when', 'like', "%" . $d . "%")->where('when', 'like', '%"from":"' . $h . '"%')->whereIn('member_id', $presences)->whereIn('course_id', $courses)->get();
            foreach ($members as $member) {

                $presence = \App\Models\Presence::where('member_id', $member->member->id)->where('calendar_id', $calendar->id)->first();

                if (!in_array($member->member->id, $presences)) {
                    if (!in_array($member->member->id, $presences_annullate)) {
                        if (array_key_exists($member->member->id, $this->records)) {
                            $this->records[$member->member->id]['total'] += 1;
                            $this->records[$member->member->id]['date'] .= " - " . date("d/m", strtotime($calendar->from));
                        } else
                            $this->records[$member->member->id] = array("last_name" => $member->member->last_name, "first_name" => $member->member->first_name, "course" => $calendar->name, "total" => 1, "date" => date("d/m", strtotime($calendar->from)));
                    }
                }
            }
        }

        array_multisort(array_column($this->records, 'total'), SORT_DESC, $this->records);

        return view('livewire.absence_report');
    }
}
