<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class NilaiSeleksiInternal
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        $id = $request->route('seleksi_internal');
        $biodata = Auth::user()->rBiodata;
        $noidentitas = $biodata->noidentitas;
        $proposal_id = decrypt($id);

        $status_nilai = DB::select("select ambil_data_reviewer_proposal('".$noidentitas."', '".$proposal_id."', '2')")[0]->ambil_data_reviewer_proposal;
        $status_administrasi = DB::select("select ambil_data_reviewer_proposal('".$noidentitas."', '".$proposal_id."', '3')")[0]->ambil_data_reviewer_proposal;

        if($status_administrasi != 0)
            {
                if($status_nilai != 1)
                {
                    if($status_administrasi != '2')
                        return $next($request);
                }
            }

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