<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Syarikat;
use App\Models\LesenPenjaja;
use App\Models\BilPelbagai;
use App\Models\BilPelbagaiItem;
use App\Models\EpbtBpBil;
use App\Models\EpbtEcasResit;
use Illuminate\View\View;

class PenjajaController extends Controller
{
    //
    public function senarai_pt(Request $request){


        $txt_bayar = '';

        $senarais = LesenPenjaja::where('status_progress', 'baru')->get();

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

    //
    public function senarai_pt_proses(Request $request){

        $txt_bayar = '';

        $senarais = LesenPenjaja::where('status_progress', 'semakan bayaran proses')->get();

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

    public function papar(Request $request, $id){
        $show = 'utama';
        $permohonan = LesenPenjaja::find($id);        
        $user = $request->user();

        $doc_kad_pengenalan = null;
        $doc_gambar = null;
        $doc_lokasi = null;
        $doc_kebenaran = null;
        $doc_kenderaan = null;
        $doc_kenderaan_geran = null;
        $doc_suntikan_ty2 = null;
        $doc_kursus_makanan = null;
        if(!$permohonan->dokumens->isEmpty()){
            $doc_kad_pengenalan = $permohonan->dokumens->firstWhere('nama', 'doc_kad_pengenalan');
            $doc_gambar = $permohonan->dokumens->firstWhere('nama', 'doc_gambar');
            $doc_lokasi = $permohonan->dokumens->firstWhere('nama', 'doc_lokasi');
            $doc_kebenaran = $permohonan->dokumens->firstWhere('nama', 'doc_kebenaran');
            $doc_kenderaan = $permohonan->dokumens->firstWhere('nama', 'doc_kenderaan');
            $doc_kenderaan_geran = $permohonan->dokumens->firstWhere('nama', 'doc_kenderaan_geran');
            $doc_suntikan_ty2 = $permohonan->dokumens->firstWhere('nama', 'doc_suntikan_ty2');
            $doc_kursus_makanan = $permohonan->dokumens->firstWhere('nama', 'doc_kursus_makanan');
        }
     
        return view('admin.penjaja.papar_permohonan', compact('show', 'permohonan', 'user', 'doc_kad_pengenalan','doc_gambar',
            'doc_lokasi','doc_kebenaran','doc_kenderaan','doc_kenderaan_geran',
            'doc_suntikan_ty2', 'doc_kursus_makanan'));
    }

    public function paparLampiran(Request $request, $id): View
    {
        $lesen_penjaja = LesenPenjaja::find($id);

        $doc_kad_pengenalan = null;
        $doc_gambar = null;
        $doc_lokasi = null;
        $doc_kebenaran = null;
        $doc_kenderaan = null;
        $doc_kenderaan_geran = null;
        $doc_suntikan_ty2 = null;
        $doc_kursus_makanan = null;
        if(!$lesen_penjaja->dokumens->isEmpty()){
            $doc_kad_pengenalan = $lesen_penjaja->dokumens->firstWhere('nama', 'doc_kad_pengenalan');
            $doc_gambar = $lesen_penjaja->dokumens->firstWhere('nama', 'doc_gambar');
            $doc_lokasi = $lesen_penjaja->dokumens->firstWhere('nama', 'doc_lokasi');
            $doc_kebenaran = $lesen_penjaja->dokumens->firstWhere('nama', 'doc_kebenaran');
            $doc_kenderaan = $lesen_penjaja->dokumens->firstWhere('nama', 'doc_kenderaan');
            $doc_kenderaan_geran = $lesen_penjaja->dokumens->firstWhere('nama', 'doc_kenderaan_geran');
            $doc_suntikan_ty2 = $lesen_penjaja->dokumens->firstWhere('nama', 'doc_suntikan_ty2');
            $doc_kursus_makanan = $lesen_penjaja->dokumens->firstWhere('nama', 'doc_kursus_makanan');
        }

        return view('admin.penjaja.papar_mohon_lampiran', [
            'user' => $request->user(),
            'permohonan' => $lesen_penjaja,
            'doc_kad_pengenalan' => $doc_kad_pengenalan,
            'doc_gambar' => $doc_gambar,
            'doc_lokasi' => $doc_lokasi,
            'doc_kebenaran' => $doc_kebenaran,
            'doc_kenderaan' => $doc_kenderaan,
            'doc_kenderaan_geran' => $doc_kenderaan_geran,
            'doc_suntikan_ty2' => $doc_suntikan_ty2,
            'doc_kursus_makanan' => $doc_kursus_makanan,
        ]);
    }

    public function simpanWangProses(Request $request)
    {
        $mohon = LesenPenjaja::find($request->permohonan_id);
        $mohon->status_progress = 'menunggu bayaran proses';
        $mohon->save();

        $bil_p = BilPelbagai::firstOrCreate(
            ['no_akaun_bilpelbagai' => $request->no_bil_pelbagai, 'lesen_penjaja_id' => $request->permohonan_id],
            ['no_akaun_bilpelbagai' => $request->no_bil_pelbagai, 'lesen_penjaja_id' => $request->permohonan_id, 'cara_jana' => 'mylesen', 'status_mylesen'=>'1']
        );

        if($bil_p->wasRecentlyCreated){

            $epbtbil = EpbtBpBil::where('noakaun', $request->no_bil_pelbagai)->first();

            if($epbtbil){
                $bil_p->status_mylesen_error = ''; 
                $bil_p->ref_no = $epbtbil->norujukan; 
                $bil_p->description = $epbtbil->keterangan; 
                $bil_p->pay_status = $epbtbil->statusbyr; 
                $bil_p->jabatan = $epbtbil->dept_code; 
                $bil_p->ent_opr = $epbtbil->ent_opr; 
                $bil_p->no_bilpelbagai = $epbtbil->nobil; 
                $bil_p->save();

                foreach($epbtbil->bil_items as $epbtbil_item){
                    $bilitem = BilPelbagaiItem::create([
                        'bil_pelbagai_id' => $bil_p->id,
                        'itemno' => $epbtbil_item->noitem, 
                        'itemdesc' => $epbtbil_item->keterangan, 
                        'itemprice' => $epbtbil_item->amaun, 
                        'gst_type' => $epbtbil_item->jenis_gst, 
                        'gst_code' => $epbtbil_item->kod_gst, 
                        'cr_code' => $epbtbil_item->kodcr, 
                        'dr_code' => $epbtbil_item->koddr, 
                        'cost_center' => $epbtbil_item->pusatkos, 
                        'exp_date' => $epbtbil_item->tamat_tempoh
                    ]);                   
                }

            }

        }

        $bp_bayar = EpbtEcasResit::whereHas('resitItems', function($query) use ($request){
                                                $query->where('noakaun', $request->no_bil_pelbagai);
                                    })->select('noresit', 'tarikh','amaun')
                                    ->orderBy('tarikh')
                                    ->first();
        if(!is_null($bp_bayar)){  //cek no resit daripada epbt..klu ada, kita simpan rekod    
            $bil_p->dt_bayar = $bp_bayar->tarikh; 
            $bil_p->no_resit = $bp_bayar->noresit;
            $bil_p->amaun_bayar = $bp_bayar->amaun;
            $bil_p->save();
        }               

        return redirect(route('pt.penjaja-papar', ['id'=>$request->permohonan_id]));

    }

    public function simpanNoFail(Request $request){

        $mohon = LesenPenjaja::find($request->permohonan_id);

        $mohon->no_fail_lesen = $request->no_fail_lesen;
        $mohon->no_akaun_lesen = $request->no_akaun_lesen;
        $mohon->save();

        return redirect(route('pt.penjaja-papar', ['id'=>$request->permohonan_id]))->with('msg', 'Maklumat telah disimpan');
    }

    public function hantarPPK(Request $request){

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

        return redirect(route('pt.penjaja-papar', ['id'=>$request->permohonan_id]))->with('msg', 'Maklumat telah disimpan');

    }
}
