<?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', 'rDaftarPro'])
                    ->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');
    }

    public function lihat(Request $request)
    {

        $item = Proposal::where('proposal_id', $request->proposal_id)->first();

        $data = '';
        $no = 1;

            if($item->upload_dokumen){
                $dokumen = ' <a href="https://statik.unesa.ac.id/simpkm/proposal/'. $item->rPeriode->nama .'/'. $item->upload_dokumen.'"  target="_blank" class="btn btn-success btn-sm"> <i class="far fa-eye"></i> Lihat Proposal</a>
                             <iframe id="view-pdf" width="100%" height="400px" src="https://statik.unesa.ac.id/simpkm/proposal/'. $item->rPeriode->nama .'/'.$item->upload_dokumen .'" frameborder="0"></iframe>';
                }else{
                $dokumen = '<div class="alert alert-danger alert-dismissible fade show mb-0" role="alert">
                                <strong>Informasi!</strong><br/> File tidak ditemukan.
                            </div>';
                }

            if(is_null($item->status) OR $item->status == '0'){
                $status = '<span class="badge bg-danger">Menunggu Persetujuan Dosen</span>';
            }else{
                $status = '<span class="badge bg-success">Sudah Disetujui Dosen</span>';
            }

            $data .= "
                        <tr>
                                <td>Judul Proposal</td>
                                <td>".$item->judul."</td>
                            </tr>
                            <tr>
                                <td>Jenis PKM</td>
                                <td>".$item->rJenis->nama."</td>
                            </tr>
                            <tr>
                                <td>Status</td>
                                <td>".$status."</td>
                            </tr>
                            <tr>
                                <td>Proposal</td>
                                <td>".$dokumen."</td>
                        </tr>

                    ";

        $response['html'] = $data;
        echo json_encode($response);

    }

    public function approve($id)
    {
        $proposal = Proposal::query()->find(decrypt($id));

	    return response()->json([
	      'data' => $proposal
	    ]);
    }

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

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

        $proposal->status = 2;
        $proposal->alasan_revisi = $proId['alasan_revisi'];
        $proposal->save();

        Alert::success('Berhasil ditolak');

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

}
