<?php

namespace App\Http\Controllers\Admin;

use App\Http\Support\ValidationRule;
use App\Http\Controllers\Controller;
use App\Models\Admin\Penelaah;
use App\Models\Admin\Pengajuan;
use Exception;
use Storage;
use Session;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redirect;

class TransaksiController extends Controller
{

    public function index(Request $request) {

        Session::put('status', $request->status); 

        if(empty(Session::get('status'))) {

            $pengusul = DB::table('bantuan_pengusul')->get();

        } else {

            $pengajuan = DB::table('tr_pengajuan')->select('id_pengajuan')->get();

            foreach($pengajuan as $p){
                $list[$p->id_pengajuan] = $p->id_pengajuan;
            }

            if(Session::get('status') == '1'){

                $pengusul = DB::table('bantuan_pengusul')->whereIn('id_pengajuan',$list)->get();

            }elseif(Session::get('status') == '2'){

                $pengusul = DB::table('bantuan_pengusul')->whereNotIn('id_pengajuan',$list)->get();

            }

        }

        $kategori = DB::table('ms_kategori')->orderBy('kode_kategori','asc')->get();

        $data = [
            'pengusul' => $pengusul,
            'kategori' => $kategori
        ];

        return view('transaksi.index', $data);
    }

    public function store(Request $request){
        
        $rule = [
            'kategori' => 'required',
            'file_ec' => 'required|mimes:pdf|max:3072',
            'nosurat' => 'required'
        ];
        
        $request->validate($rule, ValidationRule::getErrorMessage($rule));
        
        DB::beginTransaction();
        
        try{
            
            if($request->hasFile('file_ec')){
                $file = $request->file('file_ec');
                $filename = $file->getClientOriginalName();
            
                $path_dir = 'sipeka/file/';
                $move = $request->file('file_ec')->move($path_dir,$filename);
    
                // $cek = Storage::disk('uploads')->put('sipeka/'.$filename, file_get_contents($file->getRealPath()));
                // dd($cek);
            }

                $transaksi = [
                    'id_pengajuan' => $request->idpengajuan,
                    'judul' => $request->judul,
                    'peneliti' => $request->peneliti,
                    'klasifikasi' => $request->klasifikasi,
                    'tahun' => $request->tahun,
                    'id_kategori' => $request->kategori,
                    'file_ec' => $filename,
                    'no_sk' => $request->nosurat,
                ];
                
                Pengajuan::query()->create($transaksi);
                
                $hasil = DB::table('tr_pengajuan')->where('id_pengajuan',$request->idpengajuan)->first();
                
                $reviewer = DB::table('bantuan_penelaah')->where('id_pengajuan',$request->idpengajuan)->get();
                
                foreach($reviewer as $r){
                    $isdm = DB::connection('isdm')->table('v_detil_pegawai')->where('noktp',$r->nik)->select('namalengkap','nip')->first();
                    if(!empty($isdm)){

                    $saku = [
                            'id_pengajuan' => $hasil->id,
                            'nik' => $r->nik,
                            'nama' => $isdm->namalengkap,
                            'nip' => $isdm->nip
                    ];          
                    
                } else {
                    $saku = [
                        'id_pengajuan' => $hasil->id,
                        'nik' => $r->nik,
                        'nama' => $r->penelaah,
                        'nip' => 'NIK belum terdaftar di ISDM. Silakan update ke Kepegawaian.'
                    ];
                }

                    Penelaah::query()->create($saku);
                }
                        
                DB::commit();
                        
                return Redirect::route('transaksi.index')
                ->with('success', 'Data kategori berhasil diupdate');
        }
        catch(Exception $e){
            Log::error($e);
            DB::rollBack();

            return Redirect::route('transaksi.index')
            ->with('error', 'Data kategori gagal diupdate');
        }
    }
}
