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

use Cms\Classes\Controller;
use Illuminate\Http\Request;
use AhmadFatoni\ApiGenerator\Helpers\Helpers;
use Illuminate\Support\Facades\Validator;

use TPS\Birzha\Models\Notification;
use RainLab\User\Models\User as Vendor;

class VendorApiController extends Controller
{
	protected $Vendor;

    protected $helpers;

    public function __construct(Vendor $Vendor, Helpers $helpers)
    {
        parent::__construct();
        $this->Vendor    = $Vendor;
        $this->helpers          = $helpers;
    }
    
    
    public function deleteVendor(Request $request){
        
        $data = $request->all();
        
        $currentUser = \JWTAuth::parseToken()->authenticate(); 
        
        $vendor = Vendor::where("id", $currentUser->id)->delete();
        
        if($vendor){
            return response()->json(["message"=>"deleted"], 200);
        }else{
        
             return $this->helpers->apiArrayResponseBuilder(400, 'fail');    
            
        }
        
       
        
    }
    

    public function updateVendor(Request $request){
        
        $data = $request->all();
        
        $validator = Validator::make($data, [
           'shop_title' => 'required',
           'slogan' => 'required',
           'work_time' => 'required',
           'description' => 'required',
        ]);
        
       

       // $validator = $this->validateForm($data, $rules);
        if($validator->fails()) {
            return $this->helpers->apiArrayResponseBuilder(400, 'fail', $validator->errors() );
        }
        //dd($data);
        $currentUser = \JWTAuth::parseToken()->authenticate(); 
        $vendor = Vendor::where("id", $currentUser->id)->with(['categories' => function($q){
                $q->with('translations:locale,model_id,attribute_data');
            }])
        ->with('sliders')->first();
        
        //dd($vendor->type);
        
        if ($vendor->type != "simple"){
        
            $vendor->shop_title = $data["shop_title"];
            $vendor->slogan = $data["slogan"];
            $vendor->work_time = $data["work_time"];
            $vendor->description = $data["description"];
            $vendor->short_description = $data["short_description"] ?? "";
            $vendor->web2 = (int) $data["discount"];
            $vendor->tiktok = $data["tiktok"];
            $vendor->instagram = $data["instagram"];
            $vendor->site = $data["site"];
            $vendor->save();
            
            $vendor->pathUrl = 'https://gurlushyk.com.tm' . \Config::get('cms.storage.media.path');

         
            $all = array(
                         "id" => 0,
                         "name"=> "Hemmesi",
                     ); 
    
        
            $vendor->categories->prepend($all);
        
            return response()->json($vendor, 200);
            
        }else{
        
            return $this->helpers->apiArrayResponseBuilder(400, 'fail');
            
        }
       
    }
    

    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);
    }

}
