<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Attribute;
use App\Models\Brand;
use App\Models\Category;
use App\Models\CategoryAttribute;
use App\Models\City;
use App\Models\BirthCode;
use Illuminate\Http\Request;
use App\Models\User;

class FetchController extends Controller
{
    private $view = 'admin.ajax.';
    
    public function birth()
    {
        $head = 'كوبون اليوم : '.BirthCode::whereDate('date',date('Y-m-d'))->first()->code;
        $users = User::whereMonth('date_of_birth',date('m'))->whereDay('date_of_birth',date('d'))->cursor();
        return view('admin.members.birth',compact('head','users'));
    }
    public function addlink()
    {
        return view('admin.ajax.links');
    }
    public function typeselect($role)
    {
        $users = User::where('role',$role)->get();
        return view('admin.ajax.typeselect',compact('users','role'));
    }
    public function country(Request $request)
    {
        $cities = City::withDescription()->where('country_id',$request->id)->cursor();
        return view('admin.ajax.city',compact('cities'));
    }
    public function addrate(Request $request)
    {
        $key = $request->key;
        return view('admin.ajax.rates',compact('key'));
    }
    public function attribute(Request $request)
    {
        $category = Category::withDescription()->findOrFail($request->id);
        $models = Category::withDescription()->where('categories.category_id',$request->id)->cursor();
        $brands = Brand::withDescription()->cursor();
        $ids = CategoryAttribute::where('category_id',$request->id)->pluck('attribute_id')->toArray();
        $ones = Attribute::withDescription()->find($ids);
        $main = Attribute::find($category->main_id);
        $sub = Attribute::find($category->sub_id);
        
    	$view = view($this->view.'attributes')->with(['models'=>$models,'ones'=>$ones,'main'=>$main,'sub'=>$sub,'brands'=>$brands,'category'=>$category])->render();
    	$count_check = $main ? false : true;
    	return [
    	    'view'=>$view,
    	    'count_check'=>$count_check
    	    ];
    	    
    }
}
