<?php

namespace App\Http\Controllers\Mahasiswa;

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 SeleksiController extends Controller
{

    public function seleksiInternal(Request $request)
    {
        //
        $title    = 'Seleksi Internal Mahasiswa';
        $bio      = auth()->user()->rBiodata;
        $nim      = $bio->noidentitas;
        $periode  = Periode::query()->get();
        $year     = !is_null($request->year) ? $request->year : now()->year;

        $proposal = Proposal::with(['rKelompok', 'rJenis', 'rNilai', 'rDaftarPro'])
                    ->whereHas('rKelompok', function ($query) use($nim){
                        $query->whereHas('rAnggota', function ($query) use($nim){
                            $query->where('nim', $nim);
                        });
                    })
                    ->whereYear('created_at', $year)
                    ->orderBy('kelompok_id')
                    ->get();
                // dd($proposal);

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

        return view('backend.mahasiswa.seleksi.index_internal', $data);
    }

    public function internalRevisi()
    {
        //
        $title      = 'Tambah Proposal Mahasiswa';
        $periode    = Periode::where('status', 1)->first();
        $getDosen   = GetDataApiController::getDosen();
        $jenis      = Jenis::pluck('nama','jenis_id');
        $bio        = auth()->user()->rBiodata;

        $kelompok   = Kelompok::where('created_user', auth()->user()->id)
                              ->where('periode_id', $periode->periode_id)
                              ->where('status', '1')
                              ->first();

        if(is_null($kelompok)){

            return redirect()->route('mahasiswa.proposal.index')->with('warning', 'Belum ada kelompok!');
        }

        $data = [
            'title'     => $title,
            'dosen'     => $getDosen['data'],
            'periode'   => $periode,
            'jenis'     => $jenis,
            'kelompok'  => $kelompok
        ];

        return view('backend.mahasiswa.seleksi.internal_revisi', $data);
    }


    public function seleksiBelmawa(Request $request)
    {
        //
        $title    = 'Seleksi Belmawa Mahasiswa';
        $bio      = auth()->user()->rBiodata;
        $nim      = $bio->noidentitas;
        $periode  = Periode::query()->get();
        $year     = !is_null($request->year) ? $request->year : now()->year;

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

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

        return view('backend.mahasiswa.seleksi.index_belmawa', $data);
    }

    public function editBelmawa($id)
    {
        //
        $title    = 'Edit Proposal Belmawa';

        $proposal = Proposal::with(['rJenis', 'rNilai'])->find($id);

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

        return view('backend.mahasiswa.seleksi.revisi_belmawa', $data);
    }

    public function uploadBelmawa(Request $request)
    {
        //
        $pro   = $request->except('_token');

        $this->validate($request,
            [
                'file' => 'required|mimes:pdf|max:5000'
            ], [
                'file.required' => 'Tidak ada file yang di upload',
                'file.mimes' => 'File harus pdf',
                'file.max' => 'File tidak boleh lebih dari 5 mb',
            ]);

        $proposal = Proposal::with(['rPeriode'])->find($pro['proposal_id']);

        $file_nama  = $proposal->rPeriode->nama.'_'.$proposal->proposal_id.'.'.$pro['file']->getClientOriginalExtension();

        Storage::disk('static')->put('simpkm/proposal/revisi/'.$proposal->rPeriode->nama.'/'.$file_nama, file_get_contents($pro['file']->getRealPath()));

        $proposal->revisi_filebelmawa = $file_nama ;
        $proposal->revisi_datebelmawa = now();
        $proposal->save();

        return redirect()->route('mahasiswa.seleksi-belmawa')->with('success', 'Proposal Berhasil revisi');
    }

}
