<?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\MonevInternal;
use App\Models\JadwalKegiatan;

use Session;
use Alert;
use Auth;

class MonevController extends Controller
{

    public function monev($id)
    {
        $bio      = auth()->user()->rBiodata;
        $nim      = $bio->noidentitas;
        $periode  = Periode::where('status', 1)->first();
        $jadwal   = JadwalKegiatan::where('periode_id', $periode->periode_id)->where('nama','Monev Internal '.$id)->first();

        $title    = 'Monev Internal '.$id;
        $proposal = MonevInternal::with(['rProposal', 'rJenisMonev'])
                    ->whereHas('rProposal', function ($query) use($nim){
                        $query->whereHas('rKelompok', function ($query) use($nim){
                            $query->whereHas('rAnggota', function ($query) use($nim){
                                $query->where('nim', $nim);
                            });
                        });
                    })
                    ->whereHas('rJenisMonev', function ($query) use($id){
                        $query->where('nama', 'Monev Internal '.$id);
                    })
                    ->orderBy('created_at')
                    ->get();

               // dd($proposal);
        $data = [
            'proposal'  => $proposal,
            'title'     => $title,
            'jadwal'    => $jadwal,
        ];

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

    public function revisiMonevEd($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';}
        elseif($id[1] == 'akhir'){ $title = 'Upload Laporan Akhir'; }
        else{$title = 'Upload Revisi Proposal Monev I';}

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

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

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

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

        $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 = MonevInternal::with(['rProposal'])->find($pro['monev_id']);

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

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

        if($pro['type'] == 'logbook'){
            $proposal->logbook_file   = $file_nama;
            $proposal->logbook_date   = now();
        }elseif($pro['type'] == 'kemajuan'){
            $proposal->kemajuan_file  = $file_nama;
            $proposal->kemajuan_date  = now();
        }elseif($pro['type'] == 'akhir'){
            $proposal->akhir_file     = $file_nama;
            $proposal->akhir_date     = now();
        }elseif($pro['type'] == 'proposal'){
            $proposal->upload_dokumen = $file_nama;
            $proposal->date_upload    = now();
        }

        $proposal->save();

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

}
