<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Storage;
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;

class IkPenjajaController extends Controller
{
        //
    public function senarai_ik(Request $request){

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

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

    public function papar_pemeriksaan(Request $request, $id){
        
        $show = 'ulasan ik';
        $mohon_id = $id;
        $permohonan = LesenPenjaja::find($id); 
        $status_progress = $permohonan->status_progress;   

        $borangulas = $permohonan->pemeriksaanTerkini;  

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

    public function papar_pemeriksaan_lepas(Request $request, $id, $borang_ik_id){
        
        $show = 'ulasan ik';
        $mohon_id = $id;
        $permohonan = LesenPenjaja::find($id); 
        $status_progress = $permohonan->status_progress;   

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

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

    public function simpanLokasi(Request $request){
        
        $permohonan = LesenPenjaja::find($request->permohonan_id);    
        $permohonan->longitude = $request->input('longitude');
        $permohonan->latitude = $request->input('latitude');

        $permohonan->save();
     
        return redirect(route('ik.papar-pemeriksaan-map', ['id'=>$request->permohonan_id]));
    }

    public function simpanUlasan(Request $request){
        if(isset($request->borang_ik_id)){
            $permohonan = BorangUlasanIk::firstOrCreate([
                'id' => $request->borang_ik_id,   
                'lesen_penjaja_id' => $request->permohonan_id,        
            ]);   
        }
        else{
            $permohonan = new BorangUlasanIk;
            $permohonan->lesen_penjaja_id = $request->permohonan_id;
        }          
        
        $permohonan->dt_periksa = $request->input('dt_periksa');
        $permohonan->kesesuaian_lokasi = $request->input('kesesuaian_lokasi');
        $permohonan->kesesuaian_lokasi_sebab = $request->input('kesesuaian_lokasi_sebab');
        $permohonan->dijalankan_oleh = $request->input('dijalankan_oleh');
        $permohonan->pematuhan_syarat = $request->input('pematuhan_syarat');
        $permohonan->perkara_perlu_dipatuhi = $request->input('perkara_perlu_dipatuhi');

        $permohonan->pemprosesan_makanan = $request->input('pemprosesan_makanan');
        if($permohonan->pemprosesan_makanan == 'ada'){
            $permohonan->pemprosesan_makanan_memasak = $request->has('pemprosesan_makanan_memasak') ? $request->input('pemprosesan_makanan_memasak') : null;
            $permohonan->pemprosesan_makanan_merebus = $request->has('pemprosesan_makanan_merebus') ? $request->input('pemprosesan_makanan_merebus') : null;
            $permohonan->pemprosesan_makanan_menggoreng = $request->has('pemprosesan_makanan_menggoreng') ? $request->input('pemprosesan_makanan_menggoreng') : null;
            $permohonan->pemprosesan_makanan_lain = $request->has('pemprosesan_makanan_lain') ? $request->input('pemprosesan_makanan_lain') : null;
            if($permohonan->pemprosesan_makanan_lain == '1'){
                $permohonan->pemprosesan_makanan_lain_txt = $request->input('pemprosesan_makanan_lain_txt');
            }
            else{
                $permohonan->pemprosesan_makanan_lain_txt = '';
            }
        }

        $permohonan->mendirikan_bangunan = $request->input('mendirikan_bangunan');
        $permohonan->jika_guna_kenderaan = $request->input('jika_guna_kenderaan');
        $permohonan->jenis_kenderaan_lain = $request->input('jenis_kenderaan_lain');
        $permohonan->no_plate_kenderaan = $request->input('no_plate_kenderaan');
        $permohonan->dt_mula_niaga = $request->input('dt_mula_niaga');
        $permohonan->masa_berniaga_mula = $request->input('masa_berniaga_mula');
        
        $permohonan->kebersihan_diri = $request->input('kebersihan_diri');        
        $permohonan->kebersihan_makanan = $request->input('kebersihan_makanan');
        $permohonan->kebersihan_kawasan = $request->input('kebersihan_kawasan');
        $permohonan->laporan_kebersihan_selepas_niaga = $request->input('laporan_kebersihan_selepas_niaga');

        $permohonan->cadangan = $request->input('cadangan');
        $permohonan->tiada_halangan_syarat = $request->input('tiada_halangan_syarat');
        $permohonan->tiada_halangan_notis_dipatuhi = $request->has('tiada_halangan_notis_dipatuhi') ? $request->input('tiada_halangan_notis_dipatuhi') : null;
        $permohonan->tidak_menyokong_atas_sebab = $request->input('tidak_menyokong_atas_sebab');
        $permohonan->dirujuk_unit_penguatkuasa = $request->has('dirujuk_unit_penguatkuasa') ? $request->input('dirujuk_unit_penguatkuasa') : null;
        $permohonan->dirujuk_unit_penguatkuasa_txt = $request->input('dirujuk_unit_penguatkuasa_txt');
        
        if(auth()->user()->role == 'super'){
            $ik = User::find($request->ik_id);

        }
        else{
            $ik = $request->user();
        }

        $permohonan->pegawai_id = $ik->id;

        $permohonan->save();

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

        
        
        $folder = 'dokumens/penjaja/'.$mohon->id.'/ik/';
        
        for($i = 1; $i <= 3; $i++){
            if ($request->hasFile('gambar'.$i)) { //ada upload?
                $nama_field = 'gambar'.$i;
                $nama_field_ext = 'gambar'.$i."_ext";
                $validated = $request->validate([
                    $nama_field => 'file|mimes:jpg,png', 
                ]);
                
                // Store the file
                $path = $request->file($nama_field);
                $extension = $path->extension(); // Determine the file's extension based on the file's MIME type...
                
                $name = 'lokasi'.$i.".".$extension;

                $path->storeAs($folder, $name);

                $permohonan->$nama_field = $name;
                $permohonan->$nama_field_ext = $extension;
                $permohonan->save();

            }
        }
     
        return redirect(route('admin.papar-pemeriksaan', ['id'=>$request->permohonan_id]))->with('success', 'Borang pemeriksaan tapak telah disimpan.');
    }
    

    public function stream($id, $filename)
    {
        $permohonan = LesenPenjaja::find($id);
        Gate::authorize('keje-staff', $permohonan);

        $path = 'dokumens/penjaja/'.$id.'/ik/'.$filename;
        if (!Storage::exists($path)) return null;
        return response()->file(storage_path('app/' . $path));
    }

}