<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\MsJenjangPendidikan;
use App\Models\MsRefJenjangPendidikan;
use Illuminate\Http\Request;

class JenjangPendidikanController extends Controller
{
    private $indexRoute = null;

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

        $jenjang = MsRefJenjangPendidikan::query()->where('nm_jenj_didik', 'ilike', $request->nm_jenj_didik)->get();

        if(count($jenjang) > 0){
            return redirect()->back()->withErrors(['jenjang' => 'Jenjang Pendidikan sudah ada!']);
        }

        $data = $request->except('_token');
        $data['id_jenj_didik'] = MsRefJenjangPendidikan::query()->max('id_jenj_didik') + 1;
        MsRefJenjangPendidikan::query()->create($data);
        return redirect()->intended($this->indexRoute);
    }

    /**
     * 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['jenjang_pendidikan'] = MsRefJenjangPendidikan::query()->find(decrypt($id));
        return view('content.backend.masters.jenjang_pendidikan.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([
            'nm_jenj_didik' => 'required'
        ]);

        $jenjang_pendidikan = MsRefJenjangPendidikan::query()->find(decrypt($id));
        $jenjang = MsRefJenjangPendidikan::query()->where('nm_jenj_didik', 'ilike', $request->nm_jenj_didik)->first();

        if(!is_null($jenjang)){
            if($jenjang_pendidikan->id_jenj_didik != $jenjang->id_jenj_didik){
                return redirect()->back()->withErrors(['jenjang' => 'Jenjang Pendidikan sudah ada!']);
            }
        }


        $data = $request->except('_token');
        $jenjang_pendidikan->update($data);

        return redirect()->intended($this->indexRoute);
    }

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