<?php

namespace App\Http\Controllers\Dosen;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Controllers\GetDataApiController;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

use App\Models\Proposal;
use App\Models\Kelompok;
use App\Models\Periode;
use App\Models\Jenis;

use Session;
use Alert;
use Auth;

class ProposalController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //
        $title    = 'Daftar Proposal';
        $bio      = auth()->user()->rBiodata;
        $periode  = Periode::query()->get();
        $year     = !is_null($request->year) ? $request->year : now()->year;

        $proposal = Proposal::with(['rKelompok', 'rJenis'])
                    ->whereHas('rKelompok', function ($query) use($bio){
                        $query->where('nidn_dosen', $bio->noidentitas);
                    })
                    ->whereYear('created_at', $year)
                    ->orderBy('kelompok_id')
                    ->get();

        $data = [
            'proposal'  => $proposal,
            'title'     => $title,
            'periode'   => $periode,
            'year'      => $year,
        ];

        return view('backend.dosen.proposal.index', $data);
    }

    public function setuju(Request $request)
    {
        $proId = $request->except('_token');

        $proposal = Proposal::query()->find(decrypt($proId['id']));

        $proposal->status = 1;
        $proposal->date_approval = now();
        $proposal->save();

        Alert::success('Berhasil disimpan');

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