<?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 MonevController extends Controller
{
    public function monevsatu()
    {
        $title    = 'Monev Internal I';
        $bio      = auth()->user()->rBiodata;
        $nim      = $bio->noidentitas;

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

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

        return view('backend.mahasiswa.monev.indexmonev1', $data);
    }

    public function monevdua()
    {
        $title    = 'Monev Internal II';
        $bio      = auth()->user()->rBiodata;
        $nim      = $bio->noidentitas;

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

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

        return view('backend.mahasiswa.monev.indexmonev2', $data);
    }

    public function monevtiga()
    {
        $title    = 'Monev Internal III';
        $bio      = auth()->user()->rBiodata;
        $nim      = $bio->noidentitas;

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

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

        return view('backend.mahasiswa.monev.indexmonev3', $data);
    }

    public function revisiMonev($id)
    {
        //
        $id = explode('__', decrypt($id));
        $proposal = Proposal::with(['rJenis'])->find($id[0]);

        if($id[1] == 'logbook'){ $title = 'Upload Logbook'; }
        elseif($id[1] == 'kemajuan'){ $title = 'Upload Laporan Kemajuan';}
        else{ $title = 'Upload Laporan Akhir'; }

        $data = [
            'proposal'  => $proposal,
            'title'     => $title,
            'type'      => $id[1],
            'monev'     => $id[2],
        ];

        return view('backend.mahasiswa.monev.upload_monev', $data);
    }

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

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

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

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

        if($pro['type'] == 'logbook') {

            $proposal->logbook_file = $file_nama ;
            $proposal->logbook_date = now();
            $proposal->save();

        } elseif($pro['type'] == 'kemajuan') {

            $proposal->laporan_kemajuan_file = $file_nama ;
            $proposal->laporan_kemajuan_date = now();
            $proposal->save();

        } else {

            $proposal->laporan_akhir_file = $file_nama ;
            $proposal->laporan_akhir_date = now();
            $proposal->save();

        }

        return redirect()->route('mahasiswa.monev-'.$pro['monev'])->with('success', 'Proposal Berhasil revisi');
    }

}
