<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\MsJenisFormasi;
use Illuminate\Http\Request;

class JenisFormasiController extends Controller
{
    private $indexRoute = null;

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

        $jenisFormasi = MsJenisFormasi::query()->where('nama', 'ilike', $request->nama)->get();

        if(count($jenisFormasi) > 0){
            return redirect()->back()->withErrors(['nama' => 'Jenis Formasi sudah ada!']);
        }

        $data = $request->except('_token');
        MsJenisFormasi::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['jenis_formasi'] = MsJenisFormasi::query()->find(decrypt($id));
        return view('content.backend.masters.jenis_formasi.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([
            'nama' => 'required'
        ]);

        $jenis_formasi = MsJenisFormasi::query()->find(decrypt($id));
        $jenis = MsJenisFormasi::query()->where('nama', 'ilike', $request->nama)->first();

        if(!is_null($jenis)){
            if($jenis_formasi->id != $jenis->id){
                return redirect()->back()->withErrors(['nama' => 'Jenis Formasi sudah ada!']);
            }
        }


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

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

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