<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Category;

class categoryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function __construct()
    {
        $this->middleware('auth:admin');
    }

    public function index()
    {
        $categories = Category::orderBy('id', 'DESC')->paginate(25);
        return view('admin.category.index')->with(compact('categories'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.category.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate(request(), [
            'category_name' => 'required',
            'slug' => 'required'
        ]);

        Category::create([
            'title' => request()->get('category_name'),
            'slug' => request()->get('slug'),
            'description' => request()->get('descp'),
            'status' => 'ACTIVE'
        ]);

        return redirect()->to('/admin/category');
    }

    /**
     * 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::find($id);
        return view('admin.category.edit')->with(compact('category'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $category = Category::find($id);

        $category->update([
            'title' => request()->get('category_name'),
            'slug' => request()->get('slug'),
            'description' => request()->get('descp')

        ]);

        return redirect()->to('admin/category');
    }

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

        return redirect()->to('admin/category');
    }
    public function status(Request $request, $id)
    {
        if ($request->ajax()) {
            $category = Category::find($id);
            $newStatus = ($category->status == 'DEACTIVE') ? 'ACTIVE' : 'DEACTIVE';
            $category->update([
                'status' => $newStatus
            ]);
            return $newStatus;
        }
    }

    public function statusActive(Request $request)
    {
        if ($request->ajax()) {
            foreach ($request->statusAll as $key => $value) {
                Category::where('id', $value)->update(['status' => 'ACTIVE']);
            }
            $record = Category::find($request->statusAll);
            return $record;
        }
        return false;
    }

    public function statusDeactive(Request $request)
    {
        if ($request->ajax()) {
            foreach ($request->statusAll as $key => $value) {
                Category::where('id', $value)->update(['status' => 'DEACTIVE']);
            }
            $record = Category::find($request->statusAll);
            return $record;
        }
        return false;
    }

    public function deleteAll(Request $request)
    {
        if ($request->ajax()) {
            foreach ($request->statusAll as $key => $value) {
                Category::where('id', $value)->delete();
            }
            $record = Category::find($request->statusAll);
            return $record;
        }
        return false;
    }
}
