<?php
/*
 * Copyright (c) 2023.
 *
 * Aidyn Sapargaliyev
 * ТОО Дизайн лаборатория А7
 *
 */

namespace App\Modules\main\Components;

use A7kz\Platform\Modules\Platform\Core\Services\Base\Component;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Http;

class Main extends Component
{
    public function __construct(array $params = [], $module = 'Platform.Core')
    {
        parent::__construct($params, $module);
        $this->template = 'pipicar::main.views.main';

        $class_filters = Request::input('class_filters') ?? null;
        $started_at = Request::input('started_at') ?? null;
        $ended_at = Request::input('ended_at') ?? null;
        $bodywork_filters = Request::input('bodywork_filters') ?? null;

        $request = new \Illuminate\Http\Request([
            'started_at' => $started_at ?? Carbon::now()->toDateString(),
            'ended_at' => $ended_at ?? Carbon::now()->addDays(3)->toDateString(),
            'class' => $class_filters ?? null,
            'bodywork' => $bodywork_filters ?? null,
        ]);

        $response = (new \App\Http\Controllers\MobileApiController)->getAvailableMarksList($request);

        $this->params['cars'] = collect($response->getData(true))->filter(function ($item) {
            return !empty($item['tariffs']);
        })->values();

//        $response = Http::get('https://cvm10.a7.kz/api/mobile/getAvailableMarksList', [
//            'started_at' => $started_at ?? Carbon::now()->toDateString(),
//            'ended_at' => $ended_at ?? Carbon::now()->addDays(3)->toDateString(),
//            'class' => $class_filters ?? null,
//            'bodywork' => $bodywork_filters ?? null,
//        ]);
//
//        $this->params['cars'] = collect($response->json())->filter(function ($item) {
//            return !empty($item['tariffs']);
//        })->values();
    }
}
