<?php

namespace App\Exports;

use App\Models\DaftarProposal;

use Illuminate\Support\Facades\DB;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;

class DaftarProposalExport implements FromView,ShouldAutoSize
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function __construct($jenis,$status,$tahun)
    {
        $this->jenis= $jenis;
        $this->status = $status;
        $this->tahun= $tahun;
    }

    public function view(): View
    {
        $statement = " STATUS_HAPUS = '0' ";

        if($this->jenis != "0")
        {
            $statement .= " AND jenis_id = '".$this->jenis."'";
        }

        if($this->status != "0")
        {
            // dikurang 1 karena di view mulai dari 0 sedangkan 0 itu menunggu persetujuan
            $status = $this->status - 1;
            $statement .= " AND STATUS_FINAL = '".$status."'";
        }

        if($this->tahun != "0")
        {
            $statement .= " AND PERIODE = '".$this->tahun."'";
        }

        $data = DaftarProposal::with('rKelompokDetil')
        ->select("*")
        ->selectRaw(" round((ambil_jumlah_penilaian_seleksi(reviewer_proposal_id_1) + ambil_jumlah_penilaian_seleksi(reviewer_proposal_id_2 )) / 2, 2) as rata ")->whereRaw($statement)->get();
        $result = [];

        $no = 0;
        $dospem = '';
        foreach ($data as $key => $value) {
            // $dospem = "(".str_replace("###",") ",$value->identitas_dospem);
            $data_dospem = explode('###', $value->identitas_dospem);
            $data_ketua = explode('###', $value->identitas_ketua);
            $dospem = "(".$data_dospem[0].") ".$data_dospem[1];
            $url = "https://statik.unesa.ac.id/simpkm/proposal/".$value->periode."/".$value->upload_dokumen;

            if($value->status_final == '1')
                $status_final = "Lolos";
            elseif($value->status_final == '2')
                $status_final = "Tidak Lolos";
            else
                $status_final = "Menunggu Persetujuan";

            $urut = 1;
            foreach ($value->rKelompokDetil as $keys => $detil) {

                if($detil->status_ketua == '1')
                {
                    $keanggotaan = "Ketua";
                }
                else
                {
                    $keanggotaan = "Anggota ".$urut;
                    $urut++;
                }

                $result[$no]['rowspan'] = count($value->rKelompokDetil);
                $result[$no]['kode'] = $value->kode;
                $result[$no]['jenis'] = $value->jenis_pkm;
                $result[$no]['fak'] = $detil->fakultas;
                $result[$no]['nama'] = $detil->nama;
                $result[$no]['nim'] = $detil->nim;
                $result[$no]['keanggotaan'] = $keanggotaan;
                $result[$no]['judul'] = $value->judul;
                $result[$no]['dospem'] = $dospem;
                $result[$no]['link'] = $url;
                $result[$no]['status'] = $status_final;
                $result[$no]['rata'] = $value->rata;
                $result[$no]['email_dospem'] = end($data_dospem);
                $result[$no]['email_ketua'] = end($data_ketua);

                $no++;
            }
        }

        $datas['list_proposal'] = $result;
        $datas['jenis_monev'] = 'Seleksi Internal';

        return view('backend.operator.daftar_proposal.excel', $datas);
    }
}
