<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\SubCategory;
use App\Models\StatusCategory;

class SubCategoryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $subs = SubCategory::orderBy('id' , 'DESC')->get();
        return view('Admin.SubCategory.index' , compact('subs'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $status = StatusCategory::orderBy('id' , 'DESC')->get();
        return view('Admin.SubCategory.add' , compact('status'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request , [
            'name' => 'required',
            'status_category_id' => 'required'
        ]);
        
        SubCategory::create([
            'name' => $request->name,
            'status_category_id' => $request->status_category_id
        ]); 
        return redirect()->route('SubCategory.index')->with('success_message' , 'Record Successfully Created');
    }

    /**
     * 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)
    {
        $status = StatusCategory::orderBy('id' , 'DESC')->get();
        $sub = SubCategory::find($id);
        
        return view('Admin.SubCategory.update' , compact('status', 'sub'));
    }

    /**
     * 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 , [
            'name' => 'required',
            'status_category_id' => 'required'
        ]);
        
        SubCategory::find($id)->update([
            'name' => $request->name,
            'status_category_id' => $request->status_category_id
        ]); 
            
        return redirect()->route('SubCategory.index')->with('success_message' , 'Record Successfully Updated');
}

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        SubCategory::find($id)->delete();
        return redirect()->route('SubCategory.index')->with('success_message' , 'Record Successfully Deleted!');
    }
}
