<?php namespace App\Libs\Composers;

use EventItem;
use Lang;
use DB;

class EventsComposer
{
    use SidebarComposerTrait;

    public function compose($view)
    {
        $year = date('Y');
        $month = date('m');

        $this->composeSidebar($view, \DefaultTemplateConfiguration::query());

        $nextMonths = DB::table('event_items')
            ->select(DB::raw("DISTINCT YEAR(date) as year, MONTH(date) as month"))
            ->whereRaw(DB::raw('end_date >= \''.$year.'-'.$month.'-01\''))
            ->whereNull('deleted_at')
            ->orderBy('year', 'asc')
            ->orderBy('month', 'asc')
            ->get();
        $monthsNames = [
            'january', 'february', 'march', 'april', 'may', 'june', 'july', 'august',
            'september', 'october', 'november', 'december'
        ];

        $months = array();
        array_push($months, array("id" => "0", "label" => Lang::get('frontend.all')));
        foreach ($nextMonths as $nextMonth) {
            $label = $monthsNames[($nextMonth->month - 1)];
            array_push($months, array(
                "id" => $nextMonth->month, "label" => Lang::get('frontend.'.$label), "sufix" => $nextMonth->year
            ));
        }

        $view->with(array(
            'filters' => array(
                array("id" => "month", "name" => Lang::get('frontend.month'), "values" => $months),
                array("id" => "archive", "name" => Lang::get('frontend.archive'), "values" => [
                    ["id" => "false", "label" => "Next events"],
                    ["id" => "true", "label" => "Past events"]
                ])
            )
        ));
    }
}
