<?php

namespace App\Http\Controllers\User;

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

class PublikasiArtikelController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $data['tingkat'] = $request->tingkat;
        $data['rs'] = PublikasiArtikel::query()->with('biodata')->where('jenis', strtoupper(substr($request->tingkat, 0, 1)))->orderBy('updated_at', 'desc')->get();
        return view('user.publikasiartikel.index', $data);
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->except('_token');
        Validator::make($data, PublikasiArtikel::RULES, PublikasiArtikel::ERROR_MESSAGES)->validate();
        $jenis = $data['jenis'];
        $data['userid_created'] = Auth::user()->id;
        $data['userid_updated'] = Auth::user()->id;
        $data['jenis'] = strtoupper(substr($data['jenis'], 0, 1));

        try{
            PublikasiArtikel::query()->create($data);
            return redirect()->intended('publikasiartikel?tingkat='.$jenis);
        }
        catch(Exception $ex){
            dd($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['data'] = PublikasiArtikel::query()->find(decrypt($id));
        $data['tingkat'] = $data['data']->jenis == 'R' ? 'Regional' : ($data['data']->jenis == 'N' ? 'Nasional' : ($data['data']->jenis == 'I' ? 'Internasional' : ''));
        $data['edit'] = true;
        return view('user.publikasiartikel.create', $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)
    {
        $data = $request->except('_token');
        Validator::make($data, PublikasiArtikel::RULES, PublikasiArtikel::ERROR_MESSAGES)->validate();
        $jenis = $data['jenis'];
        $data['userid_created'] = Auth::user()->id;
        $data['userid_updated'] = Auth::user()->id;
        $data['jenis'] = strtoupper(substr($data['jenis'], 0, 1));

        try{
            PublikasiArtikel::query()->find(decrypt($id))->update($data);
            return redirect()->intended('publikasiartikel?tingkat='.$jenis);
        }
        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{
            PublikasiArtikel::query()->find(decrypt($id))->delete();
            return redirect()->back();
        }
        catch(Exception $ex){
            return redirect()->back();
        }
    }
}
