<?php namespace AhmadFatoni\ApiGenerator\Controllers\API;

use Cms\Classes\Controller;
use BackendMenu;

use Illuminate\Http\Request;
use AhmadFatoni\ApiGenerator\Helpers\Helpers;
use Illuminate\Support\Facades\Validator;
use TPS\Birzha\Models\Category;
class CategoriesAPIController extends Controller
{
	protected $Category;

    protected $helpers;

    public function __construct(Category $Category, Helpers $helpers)
    {
        parent::__construct();
        $this->Category    = $Category;
        $this->helpers          = $helpers;
    }

    public function index(Request $request){
        
        $data = $request->all();
        $validator = Validator::make($data, [
            //'locale' => 'required|in:ru,en,tm',
            'featured' => 'required'
        ]);
        if($validator->fails()) {
            return $this->helpers->apiArrayResponseBuilder(400, 'fail', $validator->errors() );
        }
        
        if($data["featured"] == 3){
             $dataCat = $this->Category
            ->select('id', 'name', 'icon', 'status', 'slug', 'sort_order', 'is_featured', 'is_file_category', 'primary_key')
            ->with('translations:locale,model_id,attribute_data')
            ->where('primary_key', $data["primary"] ?? 0)
            ->orderBy('sort_order', 'asc')
            ->active()
            ->paginate(99);
        }else{
             $dataCat = $this->Category
            ->select('id', 'name', 'icon', 'status', 'slug', 'sort_order', 'is_featured', 'is_file_category', 'primary_key')
            ->with('translations:locale,model_id,attribute_data')
            ->where('is_featured', $data["featured"])
            ->where('primary_key', $data["primary"] ?? 0)
            ->orderBy('sort_order', 'asc')
            ->active()
            ->paginate(99);
                
        }
        
        if($dataCat) {
            $dataCat->each(function ($item, $key) {
                 $dataHaveSub = $this->Category->where("primary_key", $item->id)->count();
                
                $item->icon = 'https://gurlushyk.com.tm' . \Config::get('cms.storage.media.path') . $item->icon;
                $item->sub_category = $dataHaveSub > 0 ? true : false;
            });
        }
        

        return response()->json($dataCat, 200);
    }

    public function show($id){

        $data = $this->Category::find($id);

        if ($data){
            return $this->helpers->apiArrayResponseBuilder(200, 'success', [$data]);
        } else {
            $this->helpers->apiArrayResponseBuilder(404, 'not found', ['error' => 'Resource id=' . $id . ' could not be found']);
        }

    }
    
    
    public function getFilters(Request $request){
        $data = $request->all();

        $data = $this->Category::find($id);

        if ($data){
            return $this->helpers->apiArrayResponseBuilder(200, 'success', [$data]);
        } else {
            $this->helpers->apiArrayResponseBuilder(404, 'not found', ['error' => 'Resource id=' . $id . ' could not be found']);
        }

    }
    
    public function subs($id){
        $subs = Category::where('primary_key', $id)->with("subs")->get();
        return $subs;
    }

    // public function store(Request $request){

    // 	$arr = $request->all();

    //     while ( $data = current($arr)) {
    //         $this->Category->{key($arr)} = $data;
    //         next($arr);
    //     }

    //     $validation = Validator::make($request->all(), $this->Category->rules);
        
    //     if( $validation->passes() ){
    //         $this->Category->save();
    //         return $this->helpers->apiArrayResponseBuilder(201, 'created', ['id' => $this->Category->id]);
    //     }else{
    //         return $this->helpers->apiArrayResponseBuilder(400, 'fail', $validation->errors() );
    //     }

    // }

    // public function update($id, Request $request){

    //     $status = $this->Category->where('id',$id)->update($data);
    
    //     if( $status ){
            
    //         return $this->helpers->apiArrayResponseBuilder(200, 'success', 'Data has been updated successfully.');

    //     }else{

    //         return $this->helpers->apiArrayResponseBuilder(400, 'bad request', 'Error, data failed to update.');

    //     }
    // }

    // public function delete($id){

    //     $this->Category->where('id',$id)->delete();

    //     return $this->helpers->apiArrayResponseBuilder(200, 'success', 'Data has been deleted successfully.');
    // }

    // public function destroy($id){

    //     $this->Category->where('id',$id)->delete();

    //     return $this->helpers->apiArrayResponseBuilder(200, 'success', 'Data has been deleted successfully.');
    // }


    public static function getAfterFilters() {return [];}
    public static function getBeforeFilters() {return [];}
    public static function getMiddleware() {return [];}
    public function callAction($method, $parameters=false) {
        return call_user_func_array(array($this, $method), $parameters);
    }
    
}