<?php

namespace App\Http\Controllers\Admin;

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

class ProdiController extends Controller
{
    private $indexRoute = null;

    public function __construct(){
        $this->indexRoute = route('prodi.index');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $nama = $request->nama;
        $jenjang = !is_null($request->jenjang) ? decrypt($request->jenjang) : null;

        $data['prodi'] = MsRefSms::query()
        ->when($nama, function($query, $nama){
            $query->where('nama', 'ilike', '%'.$nama.'%');
        })
        ->when($jenjang, function($query, $jenjang){
            $query->where('id_jenj_didik', $jenjang);
        })
        ->with('jenjangPendidikan')->orderBy('created_at', 'desc')->paginate(10)->withQueryString();
        $data['jenjang_pendidikan'] = MsRefJenjangPendidikan::query()->orderBy('nm_jenj_didik')->get();

        $data['nama'] = $nama;
        $data['jenjang'] = $jenjang;

        return view('content.backend.masters.prodi.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([
            'nama' => 'required',
            'jenjang_pendidikan_id' => 'required'
        ]);


        $data['id_jenj_didik'] = decrypt($request->jenjang_pendidikan_id);
        $data['nama'] = $request->nama;

        $prodi = MsRefSms::query()->where([['nama', 'ilike', $request->nama], ['id_jenj_didik', $data['id_jenj_didik']]])->get();

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

        MsRefSms::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['prodi'] = MsRefSms::query()->find(decrypt($id));
        $data['jenjang_pendidikan'] = MsRefJenjangPendidikan::query()->orderBy('nm_jenj_didik')->get();

        return view('content.backend.masters.prodi.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)
    {
        $prodi = MsRefSms::query()->find(decrypt($id));

        $request->validate([
            'nama' => 'required',
            'jenjang_pendidikan_id' => 'required'
        ]);


        $data['id_jenj_didik'] = decrypt($request->jenjang_pendidikan_id);
        $data['nama'] = $request->nama;

        $prodiExist = MsRefSms::query()->where([['nama', 'ilike', $request->nama], ['id_jenj_didik', $data['id_jenj_didik']]])->first();

        if(!is_null($prodiExist)){
            if($prodiExist->id != $prodi->id){
                return redirect()->back()->with('error', 'Prodi sudah ada');
            }
        }

        $prodi->update($data);

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

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