<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Makeslist;
use DB;
use Validator;


class MakesController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $makeslist = Makeslist::all();
        if ($request->is("api*")) {
            return  json_encode($makeslist);
             }
            else{
         
        return view('backend.make_view',compact('makeslist'));
        }
    }


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

        $login=$request->session()->get('uname');
        if($login==null)
        {
            return redirect('/');
        }
        else{
        $makeslist=Makeslist::get();
       // return view('backend.gramuser');
       return view('backend.make_view',compact('makeslist'));
        }
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

         $validation = Validator::make(
            $request->all(),
            [
                   
                'name' => 'required|unique:makeslists'
             
            ]
        );

        if ($validation->fails()) {
             if ($request->is("api*")) {
                $data=["error"=>true,"error_msg"=>$validation->getMessageBag()->first()];
                return  json_encode($data);
             }
            
        else{
            return redirect(url('make_view'))->with('error',$validation->getMessageBag()->first());
        }
    }
       $makeslist=new  Makeslist([
        'name'=>$request->get('name')
        
        ]);
        $makeslist->save();

         if ($request->is("api*")) {
            return  json_encode($makeslist);
             }
            else{
         
        //$makeslist = Makelist::create($storeData);
        return redirect(url('make_view/create'))->with('completed', 'makes has been saved!');
    }
}

    /**
     * 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)
    {
        $makeslist = Makeslist::findOrFail($id);
        return view('backend.make_edit',compact('makeslist'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {

        $validation = Validator::make(
            $request->all(),
            [
                
                'name' => 'required',
                
            ]
        );
        if($validation->fails()) {
             if ($request->is("api*")) {
                $data=["error"=>true,"error_msg"=>$validation->getMessageBag()->first()];
                return  json_encode($data);
             }
            
        else{
            return redirect(url('/make_view'))->with('error',$validation->getMessageBag()->first());
        }
    }
        $makeslist=Makeslist::find($id);
        $makeslist->name=$request->get('name');
        $makeslist->update();
        if ($request->is("api*")) {
            return  json_encode($makeslist);
             }
            else{
         
        return redirect(url('/make_view'))->with('completed', 'make has been updated');
    }
}

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $makeslist = Makeslist::findOrFail($id);
        $makeslist ->delete();
        return redirect('/make_view')->with('completed', 'make has been deleted');
    }
}
