<?php

namespace App\Http\Controllers;

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

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


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

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