<?php

namespace App\Http\Controllers\Panel\Categories;
use App\Http\Requests\CategoryRequest;
use App\Models\Category;
use App\Http\Controllers\Controller;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use phpDocumentor\Reflection\Types\Null_;


class CategoryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

        $categories = Category::orderBy('id','DESC');

             if (request()->has('categoryName') && request('categoryName') != '')
            $categories = $categories->whereRaw('title = ?', [request('categoryName')]);

       if (request()->has('categorySlug') && request('categorySlug') != '')
            $categories = $categories->whereRaw('slug = ?', [request('categorySlug')]);

        if (request()->has('creatorId') && request('creatorId') != '')
          $categories = $categories->whereHas('user', function (Builder $query) {
              $query->whereRaw('name= ?', [request('creatorId')]);
           });
        $categories = $categories->paginate();
        return view('panel.categories.index',compact('categories'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
//        $categories = Category::orderBy('parent_id', 'DESC');

//        return view('panel.categories.create',compact('categories'));
//        $categories = Category::orderBy('parent_id')->get();
//       dd($categories);
//        return view('panel.categories.create',compact('categories'));


//            $categories    = Category:: orderBy('parent_id')->get();
//
////        dd($categories);
//        return view('panel.categories.create', compact('categories'));


            $categories = Category::orderBy('parent_id')->where('parent_id', 0)->get();

        return view('panel.categories.create', compact('categories'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CategoryRequest $request)
    {

        $data = [
            'title' => $request->title,
            'slug' => $request->slug,
            'parent_id' => $request->parent_id,
            'category_type' => $request->category_type,
            'discription' => $request->discription,
            'creator_id' => auth()->user()->id
        ];

        $category = Category::create($data);
        $msg = 'ذخیره محصول با موفقیت انجام شد ';
        return redirect(route('categories.edit', $category->id))->with('success', 'دسته بندی جدید با موفقیت ایجاد شد.');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit( $id)
    {
        $category = Category::findOrFail( $id );


            $categories  = Category::orderBy('parent_id')->where('parent_id', 0)->get();


        return view('panel.categories.edit', compact('category', 'categories'));

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(CategoryRequest $request, Category $category)
    {
        $data = [
            'title' => $request->title,
            'slug' => $request->slug,
            'parent_id' => $request->parent_id,
            'category_type' => $request->category_type,
            'discription' => $request->discription,
            'creator_id' => auth()->user()->id
        ];

        $category->update($data);
        $msg = 'ویرایش محصول با موفقیت انجام شد ';
        return redirect(route('categories.index'))->with('success', $msg);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Category $category)
    {
        $category->delete();
        {
            return back();
        }
    }
}
