<?php

namespace App\Http\Controllers\Admin;

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

class SubInstrumentController extends Controller
{
    private $indexRoute = null;

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

       
        $data = $request->except('_token');
        Sub_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['sub_instrument'] = Sub_Instrument::query()->find(decrypt($id));
        $data['instrument']     = Instrument::query()->orderBy('instrument')->get();
        return view('content.backend.masters.sub_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([
            'sub_instrument' => 'required'
        ]);

     	$sub_instrument = Sub_Instrument::query()->find(decrypt($id));
        $data = $request->except('_token');
        $sub_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)
    {
        $sub_instrument = Sub_Instrument::query()->find(decrypt($id))->delete();
        return response('Berhasil menghapus data!', 200);
    }
}
