<?php
    
namespace App\Http\Controllers;
    
use App\Models\Outfit;
use Illuminate\Http\Request;
    
class OutfitController extends Controller
{ 
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    function __construct()
    {
         $this->middleware('permission:outfit-list|outfit-create|outfit-edit|outfit-delete', ['only' => ['index','show']]);
         $this->middleware('permission:outfit-create', ['only' => ['create','store']]);
         $this->middleware('permission:outfit-edit', ['only' => ['edit','update']]);
         $this->middleware('permission:outfit-delete', ['only' => ['destroy']]);
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $outfits = Outfit::latest()->paginate(5);
        return view('outfits.index',compact('outfits'))
            ->with('i', (request()->input('page', 1) - 1) * 5);
    }
    
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('outfits.create');
    }
    
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        request()->validate([
            'nama' => 'required',
            'gambar' => 'required',
            'harga' => 'required',
            'stok' => 'required',
            'detail' => 'required',
        ]);
    
        Outfit::create($request->all());
    
        return redirect()->route('outfits.index')
                        ->with('success','Outfit created successfully.');
    }
    
    /**
     * Display the specified resource.
     *
     * @param  \App\Outfit  $outfit
     * @return \Illuminate\Http\Response
     */
    public function show(Outfit $outfit)
    {
        return view('outfits.show',compact('outfit'));
    }
    
    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Outfit  $outfit
     * @return \Illuminate\Http\Response
     */
    public function edit(Outfit $outfit)
    {
        return view('outfits.edit',compact('outfit'));
    }
    
    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Outfit  $outfit
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Outfit $outfit)
    {
         request()->validate([
            'nama' => 'required',
            'gambar' => 'required',
            'harga' => 'required',
            'stok' => 'required',
            'detail' => 'required',
        ]);
    
        $outfit->update($request->all());
    
        return redirect()->route('outfits.index')
                        ->with('success','outfit updated successfully');
    }
    
    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Outfit  $outfit
     * @return \Illuminate\Http\Response
     */
    public function destroy(Outfit $outfit)
    {
        $outfit->delete();
    
        return redirect()->route('outfits.index')
                        ->with('success','outfit deleted successfully');
    }
}