<?php

namespace App\Http\Livewire;

use Illuminate\Support\Facades\Auth;
use App\Http\Middleware\TenantMiddleware;
use Livewire\Component;

class CourseMember extends Component
{

    public $records = array();

    public $course = null;
    public $courses = [];
    public $course_frequencies = [];
    // public $course_types = [];
    public $course_levels = [];
    public $course_durations = [];
    public $course_years = [];

    public $fromYear = "";
    public $toYear = "";
    public $fromFromYear = "";
    public $toToYear = "";

    public $filterCourse = [];
    public $filterLevel = [];
    public $filterFrequency = [];
    public $filterType = [];
    public $filterDuration = [];

    public $filterDays = [];
    public $filterHours = [];
    public $filterSubscription = "";
    public $filterStatus = [];
    public $filterYear = "";

    /*public $chkCertificateNormal = 0;
    public $chkCertificateAgonistico = 0;
    public $chkCertificateScaduti = 0;*/
    public $chkCertificateType = "";
    public $chkCertificateScadenza = 0;
    public $chkCard = [];

    public $filter = '';
    public $type = '';

    public $filterFromPrevious = '';
    public function boot()
    {
        app(TenantMiddleware::class)->setupTenantConnection();
    }
    public function mount()
    {

        if (isset($_GET["id"]))
        {
            $this->filterFromPrevious = $_GET["id"];
            $this->course = \App\Models\Course::findOrFail($_GET["id"]);
            $this->type = $this->course->type;
        }

        // $this->course_types = \App\Models\CourseType::select('*')->where('enabled', true)->get();
        $this->course_frequencies = \App\Models\CourseFrequency::select('*')->where('enabled', true)->get();
        $this->course_levels = \App\Models\CourseLevel::select('*')->where('enabled', true)->get();
        $this->course_durations = \App\Models\CourseDuration::select('*')->where('enabled', true)->get();
        $this->course_years = \App\Models\Course::select('year')->where('year', '<>', '')->groupBy('year')->pluck('year');
        $this->courses = \App\Models\Course::orderBy('name')->groupBy('name')->pluck('name');

    }

    public function render()
    {

        // Carico tutti i corsi associati
        $this->filter = '';
        $datas = \App\Models\MemberCourse::with('member');

        if (sizeof($this->filterCourse) > 0)
        {
            $course_ids = [];
            foreach($this->filterCourse as $c)
            {
                $all = \App\Models\Course::where('name', 'like', '%' . $c . "%")->get();
                foreach($all as $a)
                {
                    $course_ids[] = $a->id;
                }
            }
            $datas = $datas->whereIn('course_id', $course_ids);
        }
        if (sizeof($this->filterLevel) > 0)
        {
            $course_ids = \App\Models\Course::whereIn('course_level_id', $this->filterLevel)->pluck('id');
            $datas = $datas->whereIn('course_id', $course_ids);
        }
        if (sizeof($this->filterFrequency) > 0)
        {
            $course_ids = \App\Models\Course::whereIn('course_frequency_id', $this->filterFrequency)->pluck('id');
            $datas = $datas->whereIn('course_id', $course_ids);
        }
        if (sizeof($this->filterType) > 0)
        {
            $course_ids = \App\Models\Course::whereIn('course_type_id', $this->filterType)->pluck('id');
            $datas = $datas->whereIn('course_id', $course_ids);
        }
        if (sizeof($this->filterDuration) > 0)
        {
            $course_ids = \App\Models\Course::whereIn('course_duration_id', $this->filterDuration)->pluck('id');
            $datas = $datas->whereIn('course_id', $course_ids);
        }
        if (sizeof($this->filterDays) > 0)
        {
            $m_ids = [];
            foreach($this->filterDays as $c)
            {
                $all = \App\Models\MemberCourse::where('when', 'like', "%" . $c . "%")->get();
                foreach($all as $a)
                {
                    $m_ids[] = $a->member_id;
                }
            }
            $datas = $datas->whereIn('member_id', $m_ids);
        }
        if (sizeof($this->filterHours) > 0)
        {
            $m_ids = [];
            foreach($this->filterHours as $c)
            {
                $all = \App\Models\MemberCourse::where('when', 'like', '%"from":"' . $c . "%")->get();
                foreach($all as $a)
                {
                    $m_ids[] = $a->member_id;
                }
            }
            $datas = $datas->whereIn('member_id', $m_ids);
        }
        /*if ($this->filterDays != "")
        {
            $members_ids = \App\Models\MemberCourse::where('when', 'like', "%" . $this->filterDays . "%")->pluck('member_id');
            $datas = $datas->whereIn('member_id', $members_ids);
            $this->filter .= $this->filter != '' ? ', ' : '';
            $this->filter .= "Giorni : " . $this->filterDays . " ";
        }
        if ($this->filterHours != "")
        {
            $members_ids = \App\Models\MemberCourse::where('when', 'like', '%"from":"' . $this->filterHours . "%")->pluck('member_id');
            $datas = $datas->whereIn('member_id', $members_ids);
            $this->filter .= $this->filter != '' ? ', ' : '';
            $this->filter .= "Ore : " . $this->filterHours . " ";
        }*/

        if ($this->filterSubscription != "")
        {
            $ids = \App\Models\MemberCourse::where('subscribed', $this->filterSubscription == 1 ? true : false)->pluck('id');
            $datas = $datas->whereIn('id', $ids);
            $this->filter .= $this->filter != '' ? ', ' : '';
            $this->filter .= "Pagata sottoscrizione : " . ($this->filterSubscription == 1 ? "SI" : "NO") . " ";
        }
        if ($this->chkCertificateType != "")
        {
            $types = \App\Models\MemberCertificate::where('type', $this->chkCertificateType)->pluck('member_id');
            $datas = $datas->whereIn('member_id', $types);
        }
        if ($this->chkCertificateScadenza > 0)
        {
            if ($this->chkCertificateScadenza == "1")
                $scad = \App\Models\MemberCertificate::where('expire_date', '<', date("Y-m-d"))->pluck('member_id');
            if ($this->chkCertificateScadenza == "2")
                $scad = \App\Models\MemberCertificate::whereBetween('expire_date', [date("Y-m-d"), date("Y-m-d", strtotime("+1 month"))])->pluck('member_id');
            $datas = $datas->whereIn('member_id', $scad);
        }
        if ($this->fromYear != "")
        {
            $m_ids = \App\Models\Member::where('birth_date', '<', date("Y-m-d", strtotime("-" . $this->fromYear . " year", time())))->pluck('id');
            $datas = $datas->whereIn('member_id', $m_ids);
        }
        if ($this->toYear != "")
        {
            $m_ids = \App\Models\Member::where('birth_date', '>', date("Y-m-d", strtotime("-" . $this->toYear . " year", time())))->pluck('id');
            $datas = $datas->whereIn('member_id', $m_ids);
        }
        if ($this->fromFromYear != "")
        {
            $m_ids = \App\Models\Member::whereYear('birth_date', '>=', $this->fromFromYear)->pluck('id');
            $datas = $datas->whereIn('member_id', $m_ids);
        }
        if ($this->toToYear != "")
        {
            $m_ids = \App\Models\Member::whereYear('birth_date', '<=', $this->toToYear)->pluck('id');
            $datas = $datas->whereIn('member_id', $m_ids);
        }
        /*if ($_GET["toYear"] != "")
        {
            $x = $x->where('birth_date', '>', date("Y-m-d", strtotime("-" . $_GET["toYear"] . " year", time())));
        }
        if ($_GET["fromYearYear"] != "")
        {
            $x = $x->whereYear('birth_date', '>=', $_GET["fromYearYear"]);
        }
        if ($_GET["toYearYear"] != "")
        {
            $x = $x->whereYear('birth_date', '<=', $_GET["toYearYear"]);
        }*/
        /*
        if ($this->chkCertificateNormal > 0)
        {
            $normal = \App\Models\MemberCertificate::where('type', 'N')->pluck('member_id');
            $datas = $datas->whereIn('member_id', $normal);
            $this->filter .= $this->filter != '' ? ', ' : '';
            $this->filter .= "Certificato normale : SI ";
        }
        if ($this->chkCertificateAgonistico > 0)
        {
            $agonistic = \App\Models\MemberCertificate::where('type', 'A')->pluck('member_id');
            $datas = $datas->whereIn('member_id', $agonistic);
            $this->filter .= $this->filter != '' ? ', ' : '';
            $this->filter .= "Certificato agonistico : SI ";
        }
        if ($this->chkCertificateScaduti > 0)
        {
            $scaduto = \App\Models\MemberCertificate::where('expire_date', '<', date("Y-m-d"))->pluck('member_id');
            $datas = $datas->whereIn('member_id', $scaduto);
            $this->filter .= $this->filter != '' ? ', ' : '';
            $this->filter .= "Certificato scaduto : SI ";
        }
        if ($this->chkCertificateScadenza > 0)
        {
            $scadenza = \App\Models\MemberCertificate::whereBetween('expire_date', [date("Y-m-d"), date("Y-m-d", strtotime("+1 month"))])->pluck('member_id');
            $datas = $datas->whereIn('member_id', $scadenza);
            $this->filter .= $this->filter != '' ? ', ' : '';
            $this->filter .= "Certificato in scadenza : SI ";
        }*/
        if (sizeof($this->chkCard) > 0)
        {
            $card_ids = \App\Models\MemberCard::whereIn('card_id', $this->chkCard)->pluck('member_id');
            $datas = $datas->whereIn('member_id', $card_ids);
            $this->filter .= $this->filter != '' ? ', ' : '';
            $this->filter .= "Tessera : ";
            foreach($this->chkCard as $card)
            {
                $this->filter .= \App\Models\Card::findOrFail($card)->name . " ";
            }

        }
        if ($this->filterYear != "")
        {
            $course_ids = \App\Models\Course::where('year', $this->filterYear)->pluck('id');
            $datas = $datas->whereIn('course_id', $course_ids);
            $this->filter .= $this->filter != '' ? ', ' : '';
            $this->filter .= "Anno : " . $this->filterYear . " ";
        }

        $aRet = [];
        if (sizeof($this->filterStatus) > 0)
        {
            foreach($this->filterStatus as $s)
            {
                foreach($datas->get() as $aaa)
                {

                    $state = \App\Models\Member::findOrFail($aaa->member_id)->isActive();
                    if ($state["status"] == $s)
                        $aRet[] = $aaa;
                }
            }
        }
        else
            $aRet = $datas->get();

        $this->records = $aRet;

        $this->emit('load-data-table');

        return view('livewire.course_member');

    }

    public function disableSearch()
    {
        $this->filterCourse = [];
        $this->filterLevel = [];
        $this->filterType = [];
        $this->filterDuration = [];
        $this->filterFrequency = [];
        $this->filterDays = [];
        $this->filterHours = [];
        $this->filterSubscription = "";
        $this->filterStatus = [];
        $this->chkCertificateNormal = 0;
        $this->chkCertificateAgonistico = 0;
        $this->chkCertificateScaduti = 0;
        $this->chkCertificateScadenza = 0;
        $this->chkCard = [];

        $this->filterYear = "";
        $this->fromYear = "";
        $this->toYear = "";
        $this->fromFromYear = "";
        $this->toToYear = "";

    }

}
