<?php

namespace App\Http\Controllers;

use App\Models\BilPelbagaiApi;
use App\Models\LesenPenjaja;
use Illuminate\Support\Facades\Http;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class BilPelbagaiController extends Controller
{
    //
    protected  $client_key = 'MPJBT';
    protected  $host_epbt = 'epbt.mbip.gov.my';
     
    public function doNothing(Request $request){


        return response()->json([
            'status' => 'ok',
            'success' => true,
            'message' => 'Bil berjaya dijana',
            'data'    => 'contoh no bil'
        ]);
        
    }

    public function janaBil(Request $request)
    {
        $url_api = 'http://'.$this->host_epbt.'/appsepbtkompaun_ws/registerbilpel/registerbilpelDetails';

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


        try {

            //The JSON data.
    
            $customer = [
                [	
                    'cust_id' => $lesen_penjaja->user->nokp,
                    'full_name' => $lesen_penjaja->user->name,
                    'cust_type' => 'S',
                    'add_1' => $lesen_penjaja->user->alamat,
                    'add_2' => $lesen_penjaja->user->alamat2,
                    'poscode' => $lesen_penjaja->user->poskod,
                    'city' => $lesen_penjaja->user->bandar,
                    'state' => $lesen_penjaja->user->negeri,
                    'phone_num' => $lesen_penjaja->user->notelefon,
                    'citizenship' => 1,
                    'race' => $lesen_penjaja->user->short_bangsa(),
                    'gender' => $lesen_penjaja->user->short_jantina()
                ]
            ];
    
            $bil = [
                [
                    'desc' => $request->keterangan,
                    'dept' => 'Jabatan Pelesenan',
                    'pay_status' => 0,
                    'ent_opr' => $user->login_epbt,
                    'ent_date' => date('Y-m-d H:i:s')
                ]
            ];
    
    
            $bilitem = 	[
                [
                    'itemno' => 1,
                    'itemdesc' => $request->keterangan,
                    'itemprice' => $request->amaun,
                    'gst_type' => 'I',
					'gst_code' => 'SR',
                    'cr_code' => 'H72407',
                    'dr_code' => 'H72407',
                    'cost_center_code' => '101005',
                    'pay_status' => 0,
                ]
            ];
    
    
            $jsonData = [
                'customer' => $customer,
                'bil' => $bil,
                'bilitem' => $bilitem,
                'client_type' => 'K',
                'client_key' =>  $this->client_key
            ];

           // dd($url_api, $this->client_key, $jsonData);

            $response = Http::withHeaders([
                'Content-Type' => 'application/json',
            ])->post($url_api, $jsonData);

            if ($response->failed()) {
                return response()->json([
                    'success' => false,
                    'message' => 'API tidak respon.'
                ], 500);
            }

            $obj = $response->object(); // macam json_decode
            
            if (!($obj->status ?? false)) {
                return response()->json([
                    'success' => false,
                    'message' => 'API return status = false; '.$obj->return_param,
                ], 400);
            }

            // Simpan dalam database
            $bil = BilPelbagaiApi::create([
                'user_id'          => $user->id,
                'lesen_penjaja_id'       => $request->lesen_penjaja_id, 
                'tag'          => $obj->tag,
                'status'       => $obj->status,
                'return_param' => $obj->return_param,
                'data_dihantar' => $jsonData,
                'url_dihantar' => $url_api,
                'no_akaun_bil' => $obj->accountNo,
            ]);

            return response()->json([
                'status' => 'ok',
                'success' => true,
                'message' => 'Bil berjaya dijana',
                'data'    => $bil
            ]);


        } catch (\Exception $e) {

            return response()->json([
                'success' => false,
                'message' => $e->getMessage()
            ], 500);

        }
    }
}
