<?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 App\Models\BuktiAnggaran;

use Session;
use Alert;
use Auth;

class MonevController extends Controller
{

    public function monev($id, Request $request)
    {
        $bio      = auth()->user()->rBiodata;
        $nim      = $bio->noidentitas;
        $periode  = Periode::query()->get();
        $year     = !is_null($request->year) ? $request->year : now()->year;
        $jadwal   = JadwalKegiatan::where('periode_id', $periode->where('status', 1)->first()->periode_id)
                                ->where('nama','Monev Internal '.$id)->first();

        $title    = 'Monev Internal '.$id;
        $proposal = MonevInternal::with(['rProposal', 'rJenisMonev', 'rNilaiMonevAll'])
                    ->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);
                    })
                    ->whereYear('created_at', $year)
                    ->orderBy('created_at')
                    ->get();

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

        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] == 'produk'){ $title = 'Upload Video Produk Akhir';}
        elseif($id[1] == 'akhir'){ $title = 'Upload Laporan Akhir'; }
        elseif($id[2] == 'II'){ $title = 'Upload Revisi Proposal Monev II'; }
        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]);
        $proposal = MonevInternal::with(['rProposal'])->where('proposal_id', $id[0])->first();

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

        $label = '';
            if ($id[1] == 'logbook') {
                $label = 'Upload Logbook';
            } elseif ($id[1] == 'produk') {
                $label = 'Upload Video Produk Akhir';
            } elseif ($id[1] == 'akhir') {
                $label = 'Upload Laporan Akhir';
            } elseif ($id[2] == 'II') {
                $label = 'Upload Revisi Proposal Monev II';
            } else {
                $label = 'Upload Revisi Proposal Monev I';
            }

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

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


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

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

        Storage::disk('static')->put('simpmw/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'] == 'produk'){
            $request->validate([
                'produk_url' => 'required|url', // Validasi bahwa produk_url harus berupa URL yang valid
            ]);
            $proposal->produk_url     = $request->produk_url;
            $proposal->produk_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');
    }

    public function buktiAnggaran($id)
    {
        $monev = decrypt($id);
        
        $title = 'Bukti Penggunaan Anggaran';
        
        $anggaran = BuktiAnggaran::with(['rProposal', 'rMonev'])
                                ->where('monev_id', $monev)
                                // ->paginate(10);
                                ->get();

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

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

    public function storeAnggaran()
    {
        $title    = 'Bukti Penggunaan Anggaran';
        // $proposal = Proposal::with(['rluaran', 'rPeriode'])->find(decrypt($id));

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

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