<?php

namespace App\Http\Livewire;

use Livewire\Component;
use Carbon\Carbon;

class Dashboard extends Component
{

    public $totMembers = 0;
    public $totSuppliers = 0;
    public $totTodayIn = 0;
    public $totTodayOut = 0;
    public $dayName;
    public $in;
    public $out;
    public $members;

    public array $membersDatas = [];
    public array $recordDatas = [];
    public array $labels = [];

    public function render()
    {
        return view('livewire.dashboard');
    }

    public function mount()
    {

        $this->dayName = Carbon::now()->locale('it_IT')->dayName;

        $this->totMembers = \App\Models\Member::count();
        $this->totSuppliers = \App\Models\Supplier::count();
        $this->totTodayIn = 0;
        $tmp = \App\Models\Record::where('type', 'IN')->where('date', date("Y-m-d"))->get();
        foreach($tmp as $t)
        {
            foreach($t->rows as $r)
            {
                $this->totTodayIn += $r->amount;
            }
        }
        //$this->totTodayIn = \App\Models\Record::where('type', 'IN')->where('date', date("Y-m-d"))->rows->sum('amount');
        $tmp = \App\Models\Record::where('type', 'OUT')->where('date', date("Y-m-d"))->get();
        foreach($tmp as $t)
        {
            foreach($t->rows as $r)
            {
                $this->totTodayOut += $r->amount;
            }
        }
        //$this->totTodayOut = \App\Models\Record::where('type', 'OUT')->where('date', date("Y-m-d"))->rows->sum('amount');

        $this->members = \App\Models\Member::whereBetween('created_at', [date("y-m-d", strtotime('-7 days')), date("Y-m-d 23:59:59")])
            ->where(function($query) {
                $query->where('is_archived', false)
                    ->orWhereNull('is_archived');
            })
            ->where(function($query) {
                $query->where('is_deleted', false)
                    ->orWhereNull('is_deleted');
            })->select(\DB::raw("COUNT(*) as total, created_at"))->groupBy('created_at')->get();

        $this->in = \App\Models\Record::where('type', 'IN')->whereBetween('date', [date("y-m-d", strtotime('-7 days')), date("Y-m-d 23:59:59")])->select(\DB::raw("SUM(amount) as total, date"))->groupBy('date')->get();
        $this->out = \App\Models\Record::where('type', 'OUT')->whereBetween('date', [date("y-m-d", strtotime('-7 days')), date("Y-m-d 23:59:59")])->select(\DB::raw("SUM(amount) as total, date"))->groupBy('date')->get();

        $this->labels = $this->getLabels();

        $this->memberDatas = [
            [
                'label' => 'Utenti',
                'backgroundColor' => 'blue',
                'borderColor' => 'blue',
                // 'data' => $this->getRandomData(),
                'data' => $this->getMemberData(),
            ]
        ];

        $this->recordDatas = [
            [
                'label' => 'Entrate',
                'backgroundColor' => 'green',
                'borderColor' => 'green',
                // 'data' => $this->getRandomData(),
                'data' => $this->getRecordData('IN'),
            ],
            [
                'label' => 'Uscite',
                'backgroundColor' => 'red',
                'borderColor' => 'red',
                'data' => $this->getRecordData('OUT'),
            ]
        ];
    }

    private function getLabels()
    {
        $labels = array();
        for($i=0; $i<=7; $i++)
        {
            $labels[] = date("d/M", strtotime('-' . $i . ' days'));
        }
        return array_reverse($labels);
    }

    private function getRecordData($type)
    {
        $data = [];
        for($i=0; $i<=7; $i++)
        {
            if ($type == 'IN')
            {
                $found = false;
                foreach($this->in as $in)
                {
                    if (date("Y-m-d", strtotime($in->date)) == date("Y-m-d", strtotime('-' . $i . ' days')))
                    {
                        $data[] = $in->total;
                        $found = true;
                    }
                }
                if (!$found)
                    $data[] = 0;
            }
            if ($type == 'OUT')
            {
                $found = false;
                foreach($this->out as $out)
                {
                    if (date("Y-m-d", strtotime($out->date)) == date("Y-m-d", strtotime('-' . $i . ' days')))
                    {
                        $data[] = $out->total;
                        $found = true;
                    }
                }
                if (!$found)
                    $data[] = 0;
            }
        }

        return array_reverse($data);
    }

    private function getMemberData()
    {
        $data = [];
        for($i=0; $i<=7; $i++)
        {
            $found = false;
            foreach($this->members as $member)
            {
                if (date("Y-m-d", strtotime($member->created_at)) == date("Y-m-d", strtotime('-' . $i . ' days')))
                {
                    $data[] = $member->total;
                    $found = true;
                }
            }
            if (!$found)
                $data[] = 0;

        }

        return array_reverse($data);
    }

    public function addMember()
    {
        return redirect()->to('/members?new=1');
    }

    public function addSupplier()
    {
        return redirect()->to('/suppliers?new=1');
    }

    public function addIn()
    {
        return redirect()->to('/in?new=1');
    }

    public function addOut()
    {
        return redirect()->to('/out?new=1');
    }

}
