<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\View\View;
use App\Models\User;
use App\Models\Syarikat;
use App\Models\LesenPenjaja;
use App\Models\BorangUlasanIk;
use App\Models\BilPelbagai;
use App\Models\BilPelbagaiItem;
use App\Models\EpbtBpBil;
use App\Models\EpbtEcasResit;
use App\Models\UlasanPegawai;

class PegawaiPenjajaController extends Controller
{
        //
    public function senarai_cadangan(Request $request){

        //dapatkan status bayaran
        $senarais = LesenPenjaja::where('status_progress', 'LIKE', 'ulasan pegawai')->get();

        return view('admin.penjaja.senaraicadangan', compact('senarais'));
    }

    public function senarai_ulasan_cadangan(Request $request){

        //dapatkan status bayaran
        $senarais = LesenPenjaja::where('status_progress', 'LIKE', 'ulasan pengarah')->get();

        return view('admin.penjaja.senaraiulasan', compact('senarais'));
    }

    public function papar_cadangan_pegawai(Request $request, $id, $borang_ik_id){
        
        $show = 'cadangan pegawai';
        $mohon_id = $id;

        $permohonan = LesenPenjaja::find($id); 
        $status_progress = $permohonan->status_progress;   

        $borangulas = BorangUlasanIk::find($borang_ik_id);  

        $pegs = null;
        if(auth()->user()->role == 'super'){
            $pegs = User::where('role', 'pegawai tadbir')->get();
        }   
     
        return view('admin.penjaja.papar_permohonan', compact('show', 'permohonan', 'status_progress', 'mohon_id', 'borangulas', 'pegs'));
    }

    public function papar_ulasan_cadangan_pengarah(Request $request, $id, $borang_ik_id){
        
        $show = 'cadangan pengarah';
        $mohon_id = $id;

        $permohonan = LesenPenjaja::find($id); 
        $status_progress = $permohonan->status_progress;   

        $borangulas = BorangUlasanIk::find($borang_ik_id);  

        $pengs = null;
        if(auth()->user()->role == 'super'){
            $pengs = User::where('role', 'pengarah')->get();
        }           
     
        return view('admin.penjaja.papar_permohonan', compact('show', 'permohonan', 'status_progress', 'mohon_id', 'borangulas', 'pengs'));
    }

    public function simpan_cadangan(Request $request){
        $permohonan = UlasanPegawai::firstOrCreate([
            'borang_ulasan_ik_id' => $request->borang_ik_id,   
            'lesen_penjaja_id' => $request->permohonan_id,        
        ]);    

        if(auth()->user()->role == 'super'){
            $permohonan->dt_pegawai_ulas = $request->input('dt_cadangan');
            $permohonan->pegawai_id = $request->input('peg_id');

            $user = User::find($request->input('peg_id'));
            $permohonan->pegawai_jawatan = $user->role;
        }
        else{
            $permohonan->dt_pegawai_ulas = now();
        
            $user = $request->user();
            $permohonan->pegawai_id = $user->id;
            $permohonan->pegawai_jawatan = $user->role;
        }
        $permohonan->pegawai_cadangan = $request->input('cadangan');
        $permohonan->pegawai_syarat = $request->input('tiada_halangan_syarat');
        $permohonan->pegawai_notis_perlu_dipatuhi = $request->has('tiada_halangan_notis_dipatuhi') ? $request->input('tiada_halangan_notis_dipatuhi') : null;
        $permohonan->pegawai_tidak_menyokong_sebab = $request->input('tidak_menyokong_atas_sebab');
        $permohonan->pegawai_dirujuk_unit_penguatkuasa = $request->has('dirujuk_unit_penguatkuasa') ? $request->input('dirujuk_unit_penguatkuasa') : null;
        $permohonan->pegawai_catatan = $request->input('catatan');

        $permohonan->save();

        $mohon = LesenPenjaja::find($request->permohonan_id);
        $mohon->status_progress = 'ulasan pengarah';
        $mohon->save();   

        return redirect(route('admin.papar-cadangan-pegawai', ['id'=>$request->permohonan_id, 'borang_ik_id' => $request->borang_ik_id]))->with('success', 'Cadangan telah disimpan.');   
     
    }

    public function simpan_ulasan_cadangan(Request $request){
        $permohonan = UlasanPegawai::firstOrCreate([
            'borang_ulasan_ik_id' => $request->borang_ik_id,   
            'lesen_penjaja_id' => $request->permohonan_id,        
        ]);    

        $permohonan->pengarah_ulasan = $request->input('cadangan');
        $permohonan->pengarah_bawa_mesyuarat = $request->has('dibawa_ke_mesyuarat') ? $request->input('dibawa_ke_mesyuarat') : '0';
        $permohonan->pengarah_ditolak_sebab = $request->input('ditolak_sebab');
        $permohonan->pengarah_catatan = $request->input('catatan');

        if(auth()->user()->role == 'super'){
            $permohonan->dt_pengarah_ulas = $request->input('dt_cadangan');
            $permohonan->pengarah_id = $request->input('peg_id');
            
            $user = User::find($request->input('peg_id'));
            $permohonan->pengarah_jawatan = $user->role;
        }
        else{
            $permohonan->dt_pengarah_ulas = now();
        
            $user = $request->user();
            $permohonan->pengarah_id = $user->id;
            $permohonan->pengarah_jawatan = $user->role;
        }
        
        $permohonan->save();

        if($permohonan->pengarah_ulasan == 'ditangguhkan'){
            $mohon = LesenPenjaja::find($request->permohonan_id);
            $mohon->status_progress = 'lawatan tapak';
            $mohon->save();

            $borang = new BorangUlasanIK;
            $borang->lesen_penjaja_id = $request->permohonan_id;
            $borang->save();  
        }
        else{
            $mohon = LesenPenjaja::find($request->permohonan_id);
            $mohon->status_progress = 'sokong dibawa ke mesyuarat';
            $mohon->save();   
        }

        return redirect(route('admin.papar-ulasan-cadangan-pengarah', ['id'=>$request->permohonan_id, 'borang_ik_id' => $request->borang_ik_id]))->with('success', 'Cadangan telah disimpan.');          
    }

}