<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Instrument;
use Carbon\Carbon;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Validator;

class InstrumentController extends Controller
{
    private $indexRoute = null;

    public function __construct(){
        $this->indexRoute = route('instrument.index');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data['instrument'] = Instrument::query()->orderBy('instrument')->get();
        return view('content.backend.masters.instrument.index', $data);
    }

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

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

       
        $data = $request->except('_token');
        Instrument::query()->create($data);

        return redirect()->intended($this->indexRoute)->with('success', 'Berhasil menambahkan data');
    }

   
    /**
     * 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)
    {
        $data['instrument'] = Instrument::query()->find(decrypt($id));
        return view('content.backend.masters.instrument.edit', $data);
    }

   
    /**
     * 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([
            'instrument' => 'required'
        ]);

     	$instrument = Instrument::query()->find(decrypt($id));
        $data = $request->except('_token');
        $instrument->update($data);

        return redirect()->intended($this->indexRoute)->with('success', 'Berhasil mengupdate data');
    }

   

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $instrument = Instrument::query()->find(decrypt($id))->delete();
        return response('Berhasil menghapus data!', 200);
    }
}
