<?php

namespace App\Http\Livewire;

use App\Http\Middleware\TenantMiddleware;
use Livewire\Component;

class SubscriptionMember extends Component
{
    public $records = [];

    public $subscription_id;
    public $subscription = null;
    public $subscription_years = [];

    public $filter = "";

    public $filterSubscription = "";
    public $chkCertificateType = "";
    public $chkCertificateScadenza = 0;
    public $fromYear = "";
    public $toYear = "";
    public $fromFromYear = "";
    public $toToYear = "";
    public $chkCard = [];
    public $filterStatus = [];


    public function boot()
    {
        app(TenantMiddleware::class)->setupTenantConnection();
    }

    public function mount($id)
    {
        $this->subscription_id = $id;
        $this->subscription = \App\Models\Subscription::findOrFail($this->subscription_id);
    }

    public function render()
    {
        $this->filter = '';
        $members = \App\Models\MemberSubscription::with('member')->where('subscription_id', $this->subscription_id);

        $date_from = $members->orderBy('date_from', 'asc')->pluck('date_from')->first();
        $date_to = $members->orderBy('date_to', 'desc')->pluck('date_to')->first();
        if ($date_from && $date_to) {
            $date_from = \Carbon\Carbon::createFromFormat("Y-m-d", $date_from)->setHour(0)->setMinutes(0)->setSeconds(0);
            $year_from = $date_from->year;
            $month_from = env('FISCAL_YEAR_MONTH_FROM', 9);
            $period_from = \Carbon\Carbon::createFromDate($year_from, $month_from, 1)->startOfMonth();

            $date_to = \Carbon\Carbon::createFromFormat("Y-m-d", $date_to)->setHour(23)->setMinutes(23)->setSeconds(59);
            $year_to = $date_to->year;
            $month_to = env('FISCAL_YEAR_MONTH_TO', 8);
            $period_to = \Carbon\Carbon::createFromDate($year_to, $month_to, 0)->endOfMonth();

            if ($period_from->isAfter($date_from)) {
                $year_from--;
            }

            if ($period_to->isBefore($date_to)) {
                $year_to++;
            }
            $this->subscription_years = [];
            for ($year = $year_from; $year < $year_to; $year++) {
                $this->subscription_years[] = $year . '/' . ($year + 1);
            }
        }

        if ($this->filterSubscription != "") {
            $ids = \App\Models\MemberSubscription::where('subscribed', $this->filterSubscription == 1 ? true : false)->pluck('id');
            $members = $members->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');
            $members = $members->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');
            $members = $members->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');
            $members = $members->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');
            $members = $members->whereIn('member_id', $m_ids);
        }
        if ($this->fromFromYear != "") {
            $m_ids = \App\Models\Member::whereYear('birth_date', '>=', $this->fromFromYear)->pluck('id');
            $members = $members->whereIn('member_id', $m_ids);
        }
        if ($this->toToYear != "") {
            $m_ids = \App\Models\Member::whereYear('birth_date', '<=', $this->toToYear)->pluck('id');
            $members = $members->whereIn('member_id', $m_ids);
        }
        if (sizeof($this->chkCard) > 0) {
            $card_ids = \App\Models\MemberCard::whereIn('card_id', $this->chkCard)->pluck('member_id');
            $members = $members->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 . " ";
            }
        }

        $aRet = [];
        if (sizeof($this->filterStatus) > 0) {
            foreach ($this->filterStatus as $s) {
                foreach ($members->get() as $aaa) {
                    $state = \App\Models\Member::findOrFail($aaa->member_id)->isActive();

                    if ($state["status"] == $s)
                        $aRet[] = $aaa;
                }
            }
        } else {
            $aRet = $members->get();
        }

        $this->records = $aRet;

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