<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Models\Admin\MsBidangKepakaranKategori;
use App\Models\Admin\MsBidangPenelitianKategori;
use App\Models\Admin\MsNegara;
use App\Models\User\PenelitiAsing;
use Exception;
use Illuminate\Http\Request;
use Validator;

class PenelitiAsingController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data['rs'] = PenelitiAsing::query()->with(['negara', 'bidang'])->orderBy('updated_at')->get();
        return view('user.penelitiasing.index', $data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $kategori = MsBidangKepakaranKategori::query()->orderBy('namabidang')->get();
        $negara = MsNegara::query()->orderBy('nama', 'asc')->get();

        return view('user.penelitiasing.create', [
            'kategori' => $kategori,
            'negara' => $negara,
            '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, PenelitiAsing::RULES, PenelitiAsing::ERROR_MESSAGES)->validate();
        $data['bidang_id'] = decrypt($data['bidang_id']);
        $data['negara_id'] = decrypt($data['negara_id']);
        $data['userid_created'] = Auth::user()->id;
        $data['userid_updated'] = Auth::user()->id;

        try{
            PenelitiAsing::query()->create($data);
            return redirect()->route('penelitiasing.index');
        }
        catch(Exception $ex){
            return redirect()->back()->withInput();
        }
    }

    /**
     * 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['kategori'] = MsBidangKepakaranKategori::query()->orderBy('namabidang')->get();
        $data['negara'] = MsNegara::query()->orderBy('nama', 'asc')->get();
        $data['edit'] = true;
        $data['data'] = PenelitiAsing::query()->find(decrypt($id));

        return view('user.penelitiasing.create', $data);
    }

    /**
     * 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, PenelitiAsing::RULES, PenelitiAsing::ERROR_MESSAGES)->validate();

        $data['bidang_id'] = decrypt($data['bidang_id']);
        $data['negara_id'] = decrypt($data['negara_id']);
        $data['userid_updated'] = Auth::user()->id;

        try{
            PenelitiAsing::query()->find(decrypt($id))->update($data);
            return redirect()->route('penelitiasing.index');
        }
        catch(Exception $ex){
            return redirect()->back()->withInput();
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        try{
            PenelitiAsing::query()->find(decrypt($id))->delete();
            return redirect()->route('penelitiasing.index');
        }
        catch(Exception $ex){
            return redirect()->route('penelitiasing.index');
        }
    }
}
