<?php

namespace App\Http\Controllers;

use App\Models\SmartDivice;
use Illuminate\Http\Request;

class SmartDiviceController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $smartDevices = SmartDivice::all();
        return view('smartdivice.create', compact('smartDevices'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'status' => 'required',
        ]);
        // dd($request->all());

        $smartDevices = new SmartDivice();
        $smartDevices->name = $request->name;
        $smartDevices->status = $request->status;
        $smartDevices->save();

        return redirect()->route('smartdevice.create')->with('success', 'Smart Device created successfully.');
    }

    /**
     * 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)
    {
        $smartDevice = SmartDivice::findOrFail($id);
        return view('smartdivice.edit', compact('smartDevice'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $request->validate([
            'name' => 'required',
            'status' => 'required',
        ]);

        $smartDevice = SmartDivice::findOrFail($id);
        $smartDevice->name = $request->name;
        $smartDevice->status = $request->status;
        $smartDevice->save();

        return redirect()->route('smartdevice.create')->with('success', 'Smart Device updated successfully.');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
