<?php

namespace App\Dashboard\Contrato;

use \koolreport\dashboard\inputs\DateRangePicker;

class PeriodoRanger extends DateRangePicker
{
    protected function onCreated()
    {
        $this->defaultValue($this::thisMonth());
    }

    protected function onInit()
    {
        // $this->format("MMM Do, YYYY")
        // ->value(["2020-01-01 00:00:00","2020-03-01 00:00:00"])
        // ->language("en")
        // ->minDate("2020-01-01 00:00:00")
        // ->maxDate("2020-04-01 00:00:00")
        // ->icon("fa fa-calendar-alt")
        // ->caret("fa fa-caret-down")

        $this->ranges([
            "Hoje" => DateRangePicker::today(),
            "Ontem" => DateRangePicker::yesterday(),
            "Últimos 7 dias" => DateRangePicker::last7days(),
            "Últimos 30 dias" => DateRangePicker::last30days(),
            "Este mês" => DateRangePicker::thisMonth(),
            "Último Mês" => DateRangePicker::lastMonth(),
            "Este Ano" => $this->thisYear(),
            "Ano Anterior" => $this->lastYear(),
            "Dois Anos Atrás" => $this->lastTwoYears(),
        ]);
    }

    protected function actionChange($request, $response)
    {
        //On client change, update        
        $this->sibling("ValorCompradoContratos")->update();
        $this->sibling("VolumeCompradoContratos")->update();
        $this->sibling("StatusContratosPie")->update();
        $this->sibling("ProdutosMaisCompradosContrato")->update();
    }

    static function thisYear()
    {
        $start = new \DateTime('first day of January this year');
        $end   = new \DateTime('today');

        return array($start->format('Y-m-d 00:00:00'), $end->format('Y-m-d 23:59:59'));
    }    

    static function lastYear()
    {
        $start = new \DateTime('first day of January last year');
        $end = new \DateTime('last day of December last year');
        // $start->sub(new \DateInterval('P30D'));
        return array($start->format('Y-m-d 00:00:00'), $end->format('Y-m-d 23:59:59'));
    }

    static function lastTwoYears()
    {
        $start = new \DateTime('first day of January this year');
        $start->sub(new \DateInterval('P2Y'));
        $end = new \DateTime('last day of December this year');
        $end->sub(new \DateInterval('P2Y'));
        return array($start->format('Y-m-d 00:00:00'), $end->format('Y-m-d 23:59:59'));
    }

}