<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Size;
use Illuminate\Support\Str;
class SizeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $size=Size::orderBy('id','DESC')->paginate();
        return view('backend.size.index')->with('size',$size);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('backend.size.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'=>'required|string',
        ]);
        $data=$request->all();
        $slug=Str::slug($request->title);
        $count=Size::where('slug',$slug)->count();
        if($count>0){
            request()->session()->flash('error','Already Created with this name!!!');
            return redirect()->back();
            // $slug=$slug.'-'.date('ymdis').'-'.rand(0,999);
        }
        $data['slug']=$slug;
        // return $data;
        $status=Size::create($data);
        if($status){
            request()->session()->flash('success','Size successfully created');
        }
        else{
            request()->session()->flash('error','Error, Please try again');
        }
        return redirect()->route('size.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)
    {
        $size=Size::find($id);
        if(!$size){
            request()->session()->flash('error','Size not found');
        }
        return view('backend.size.edit')->with('size',$size);
    }


    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $size=Size::find($id);
        $this->validate($request,[
            'title'=>'string|required',
        ]);
        $data=$request->all();
        $count=Size::where('slug',$data['slug'])->count();
        if($count>0){
            request()->session()->flash('error','Already Created with this name!!!');
            return redirect()->back();
            // $slug=$slug.'-'.date('ymdis').'-'.rand(0,999);
        }
        $status=$size->fill($data)->save();
        if($status){
            request()->session()->flash('success','Size successfully updated');
        }
        else{
            request()->session()->flash('error','Error, Please try again');
        }
        return redirect()->route('size.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $size=Size::find($id);
        if($size){
            $status=$size->delete();
            if($status){
                request()->session()->flash('success','Size successfully deleted');
            }
            else{
                request()->session()->flash('error','Error, Please try again');
            }
            return redirect()->route('size.index');
        }
        else{
            request()->session()->flash('error','Size not found');
            return redirect()->back();
        }
    }
}
