<?php

namespace App\Http\Livewire;
use Livewire\Component;

class Reminder extends Component
{
    public $records, $dataId;

    public $filterC = false;
    public $filterT = false;
    public $filterType = [];
    public $hasFilter = false;
    public $filterFrom = '', $filterTo = '';

    public $sortField ='date';
    public $sortAsc = true;

    public $cards = array();

    public function mount()
    {
        $this->cards = \App\Models\Card::select('id', 'name')->get();
    }

    public function sortBy($field)
    {
        if($this->sortField === $field)
        {
            $this->sortAsc = ! $this->sortAsc;
        } else {
            $this->sortAsc = true;
        }

        $this->sortField = $field;
    }

    public function render()
    {

        $data = [];

        /*
        if ($this->filterT)
        {

            foreach($member_cards as $member_card)
            {
                $data[] = array('type' => 'Tessera', 'first_name' => $member_card->member->first_name, "last_name" => $member_card->member->last_name, 'date' => $member_card->expire_date);
            }
        }*/

        $member_cards = \App\Models\MemberCard::with('member')->where('expire_date', '>', date("y-m-d"));
        if ($this->filterFrom != '')
            $member_cards = $member_cards->where('expire_date', '>=', $this->filterFrom);
        if ($this->filterTo != '')
            $member_cards = $member_cards->where('expire_date', '<=', $this->filterTo);
        $member_cards = $member_cards->orderBy('expire_date')->get();

        $member_certificates = \App\Models\MemberCertificate::with('member')->where('expire_date', '>', date("y-m-d"));
        if ($this->filterFrom != '')
            $member_certificates = $member_certificates->where('expire_date', '>=', $this->filterFrom);
        if ($this->filterTo != '')
            $member_certificates = $member_certificates->where('expire_date', '<=', $this->filterTo);
        $member_certificates = $member_certificates->orderBy('expire_date')->get();

        if (sizeof($this->filterType) > 0)
        {

            foreach($this->filterType as $type)
            {
                if ($type == "C")
                {
                    foreach($member_certificates as $member_certificate)
                    {
                        $data[] = array('type' => 'Certificato', 'first_name' => $member_certificate->member->first_name, "last_name" => $member_certificate->member->last_name, 'date' => $member_certificate->expire_date);
                    }
                }
                else
                {
                    foreach($member_cards as $member_card)
                    {
                        if ($member_card->card_id == $type)
                            $data[] = array('type' => 'Tessera ' . $member_card->card->name, 'first_name' => $member_card->member->first_name, "last_name" => $member_card->member->last_name, 'date' => $member_card->expire_date);
                    }
                }
            }
        }
        else
        {
            foreach($member_certificates as $member_certificate)
            {
                $data[] = array('type' => 'Certificato', 'first_name' => $member_certificate->member->first_name, "last_name" => $member_certificate->member->last_name, 'date' => $member_certificate->expire_date);
            }
            foreach($member_cards as $member_card)
            {
                $data[] = array('type' => 'Tessera ' . $member_card->card->name, 'first_name' => $member_card->member->first_name, "last_name" => $member_card->member->last_name, 'date' => $member_card->expire_date);
            }
        }
        /*
        if ($this->filterC)
        {
            $member_certificates = \App\Models\MemberCertificate::with('member')->where('expire_date', '>', date("y-m-d"));
            if ($this->filterFrom != '')
                $member_certificates = $member_certificates->where('expire_date', '>=', $this->filterFrom);
            if ($this->filterTo != '')
                $member_certificates = $member_certificates->where('expire_date', '<=', $this->filterTo);
            $member_certificates = $member_certificates->orderBy('expire_date')->get();
            foreach($member_certificates as $member_certificate)
            {
                $data[] = array('type' => 'Certificato', 'first_name' => $member_certificate->member->first_name, "last_name" => $member_certificate->member->last_name, 'date' => $member_certificate->expire_date);
            }
        }
        */
        if ($data)
        {
            $key_values = array_column($data, 'date');
            array_multisort($key_values, SORT_ASC, $data);
            $this->records = $data;
        }
        else
        {
            $this->records = [];
        }

        /*if ($this->sortAsc)
            usort($this->records, function ($a, $b) {return $a[$this->sortField] > $b[$this->sortField];});
        else
            usort($this->records, function ($a, $b) {return $a[$this->sortField] < $b[$this->sortField];});*/

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

        return view('livewire.reminders');
    }

    public function search()
    {
        $this->hasFilter = true;
    }

    public function disableSearch()
    {
        $this->filterC = true;
        $this->filterT = true;
        $this->filterTo = '';
        $this->filterFrom = '';
        $this->hasFilter = false;
    }

}
