<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\systemAdministration\category;
use App\Models\systemAdministration\subCategory;

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

        $category = category::all();
        $subCategory = subCategory::orderBy('id','desc')->paginate(20);
        return view('back-end.sub-categories.create', compact('category', 'subCategory'));
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'title_en' => 'required',
            'category_id' => 'required',
        ]);
        $SSubCategory = new subCategory();
        $SSubCategory->title_en = $request->title_en;
        $SSubCategory->title_ps = $request->title_ps;
        $SSubCategory->title_dr = $request->title_dr;
        $SSubCategory->category_id = $request->category_id;
        $SSubCategory->save();
        if ($SSubCategory) {
            session()->flash("msg", "Success!");
            return redirect()->route('sub_categories.index');
        }
    }

    /**
     * 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::all();
        $subCategory = subCategory::find($id);
        return view('back-end.sub-categories.edit', compact('category', 'subCategory'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'title_en' => 'required',
        ]);
        $SSubCategory = subCategory::find($id);
        $SSubCategory->title_en = $request->title_en;
        $SSubCategory->title_ps = $request->title_ps;
        $SSubCategory->title_dr = $request->title_dr;
        $SSubCategory->category_id = $request->category_id;
        $SSubCategory->save();
        if ($SSubCategory) {
            session()->flash("msg", "Success!");
            return redirect()->route('sub_categories.index');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function status($id)
    {
        $SSubCategory = subCategory::find($id);
        if ($SSubCategory->status == 0) {
            subCategory::where('id', $id)->update([
                'status' => 1
            ]);
        }
        if ($SSubCategory->status == 1) {
            subCategory::where('id', $id)->update([
                'status' => 0
            ]);
        }
        session()->flash("msg", "Success!");
        return back();
    }
}
