<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Pernyataan_Instrument;
use App\Models\Sub_Pernyataan_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 PernyataanInstrumentController extends Controller
{
    private $indexRoute = null;

    public function __construct(){
        $this->indexRoute = route('pernyataan_instrument.index');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data['pernyataan_instrument']         = Pernyataan_Instrument::query()->orderBy('created_at','asc')->get();
        $data['sub_instrument']                = Sub_Instrument::query()->orderBy('created_at','asc')->get();


        $data['sub_instrument_pernyataan']  = Pernyataan_Instrument::with(['sub_instrument' => function ($q) 
                                              {
                                                $q->orderBy('id_instrument','asc')->orderBy('sub_instrument','asc');
                                                    
                                              }])->get();
        $data['sub_pernyataan_instrument'] = Sub_Pernyataan_Instrument::query()->orderBy('sub_peryataan')->get();
       
        return view('content.backend.masters.pernyataan_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([
            'pernyataan_instrument' => 'required',
            'id_sub_instrument' => 'required'
        ]);

       
        $data = $request->except('_token');
        Pernyataan_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['pernyataan_instrument'] = Pernyataan_Instrument::query()->find(decrypt($id));
        $data['sub_instrument']     = Sub_Instrument::query()->orderBy('id_instrument')->get();
        return view('content.backend.masters.pernyataan_instrument.edit', $data);
    }

    public function tambahsub($id)
    {
        $data['pernyataan_instrument'] = Pernyataan_Instrument::query()->find(decrypt($id));
        $data['sub_instrument']     = Sub_Instrument::query()->orderBy('id_instrument')->get();
        return view('content.backend.masters.pernyataan_instrument.tambahsub', $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([
            'pernyataan_instrument' => 'required'
        ]);

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

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


    public function tambahsub_pernyataan_instrument(Request $request, $id)
    {
        
        $request->validate([
            'sub_peryataan' => 'required'
        ]);

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

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

    public function editsub($id)
    {
        $data['sub_pernyataan_instrument'] = Sub_Pernyataan_Instrument::query()->find(decrypt($id));
        return view('content.backend.masters.pernyataan_instrument.editsub', $data);
    }

    public function destroysub($id)
    {
        $sub_pernyataan_instrument = Sub_Pernyataan_Instrument::query()->find(decrypt($id))->delete();
        return response('Berhasil menghapus data!', 200);
    }

    public function updatesub_pernyataan_instrument(Request $request, $id)
    {
        
        $request->validate([
            'sub_peryataan' => 'required'
        ]);

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