<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Models\Biodata;
use App\Models\User\KekayaanIntelektual;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Validator;

class KekayaanIntelController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = KekayaanIntelektual::query()->with('dosen')->orderBy('updated_at', 'desc')->get();

        return view('user.kekayaan.index', ['data' => $data]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('user.kekayaan.create', ['edit' => false]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->except('_token');
        Validator::make($data, KekayaanIntelektual::RULES, KekayaanIntelektual::ERROR_MESSAGES)->validate();

        try {

            $biodata = Biodata::query()->where('nidn', $request->nidn)->firstOrFail();
            $data['userid_created'] = Auth::user()->id;
            $data['userid_updated'] = Auth::user()->id;

            KekayaanIntelektual::query()->create($data);

            alert()->success('Berhasil', 'Kekayaan intelektual berhasil disimpan');

        } catch (Exception $ex) {

            alert()->error('Gagal', 'Kekayaan intelektual gagal disimpan');

            return redirect()->back()->withInput();
        }

        return redirect()->route('kekayaanintelek.index');
    }

    /**
     * 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)
    {
        try {
            $data = KekayaanIntelektual::query()->find(decrypt($id));

            return view('user.kekayaan.create', [
                'edit' => true,
                'data' => $data,
            ]);
        } catch (Exception $ex) {
            return redirect()->back();
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->except('_token');
        Validator::make($data, KekayaanIntelektual::RULES, KekayaanIntelektual::ERROR_MESSAGES)->validate();

        try {
            $biodata = Biodata::query()->where('nidn', $request->nidn)->firstOrFail();
            $kekayaan = KekayaanIntelektual::query()->findOrFail(decrypt($id));
            $data['userid_updated'] = Auth::user()->id;

            $kekayaan->update($data);

            alert()->success('Berhasil', 'Kekayaan intelektual berhasil disimpan');

        } catch (Exception $ex) {

            alert()->error('Gagal', 'Kekayaan intelektual gagal disimpan');

            return redirect()->back()->withInput();
        }

        return redirect()->route('kekayaanintelek.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        try {
            $kekayaan = KekayaanIntelektual::query()->findOrFail(decrypt($id));
            $kekayaan->delete();

            alert()->success('Berhasil', 'Kekayaan intelektual berhasil dihapus');

        } catch (Exception $ex) {

            alert()->error('Gagal', 'Kekayaan intelektual gagal dihapus');
        }

        return redirect()->route('kekayaanintelek.index');
    }
}
