<?php

namespace App\Http\Controllers\Web;

use App\Models\Attribute;
use App\Models\AttributeChoice;
use App\Models\Choice;
use App\Models\ChoiceAttribute;
use App\Models\City;
use App\Models\Page;
use App\Models\Partner;
use App\Models\ProductAttribute;
use App\Models\ProductAttributeChoice;
use App\Models\Slider;
use App\Models\Review;
use App\Services\Response\ResponseService;
use Illuminate\Http\Request;
use Illuminate\View\View;
use App\Http\Controllers\Controller;

class FetchController extends Controller
{
    private $view = 'web.pages.ajax.';

    /**
     * Show the application dashboard.
     * @return View
     */

    public function cities(Request $request) :View
    {
        $cities = City::withDescription()->where('cities.country_id',$request->country_id)->cursor();
        return view($this->view.'cities', get_defined_vars());
    }

    public function attributeSub(Request $request)
    {
        $response = [];
        if($request['attributes'])
        {
            foreach ($request['attributes'] as $key=>$row)
            {
                unset($row['attribute_id']['choice_id']);
                $attribute_ids = ChoiceAttribute::whereIn('attribute_choice_id',$row['attribute_id'])->pluck('attribute_id')->toArray();

                if(count($attribute_ids)>0)
                {
                    $main = Attribute::find($attribute_ids[0]);
                    $attribute = Attribute::withDescription()->where('attributes.id',$attribute_ids[0])->first();
                    $choice_ids = AttributeChoice::whereIn('attribute_id',$attribute_ids)->pluck('choice_id')->toArray();
                    $choices = Choice::withDescription()->whereIn('choices.id',$choice_ids)->get();

                    if($attribute)
                    {
                        $response[$key] = view($this->view.'attribute')->with(['main'=>$main,'attribute'=>$attribute,'choices'=>$choices])->render();
                    }
                }
            }
        }
        $response =
            [
                'status' => 200,
                'message' => '',
                'data'=>$response,
            ];
        return ResponseService::response($response);
    }

    public function attributeSubProduct(Request $request)
    {
        $service = ProductAttribute::where('product_attribute_id',$request->attribute_id)->where('product_attribute_choice_id',$request->choice_id)->first();
        return view($this->view.'choices', get_defined_vars());
    }

    public function showCount(Request $request)
    {
        $count = ProductAttributeChoice::find($request->choice_id)->count;
        return view($this->view.'product_count', get_defined_vars());
    }

}
