<?php

namespace App\Jobs;

use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Http;
use App\BlastWasap;
use Carbon\Carbon;

class WhatsappPelangganSAGLAMA extends Job
{
    public $nokp;
    public $telefon;
    public $caw_name;
    public $nofoncaw;
    public $kodcaw;
    public $norujukan;
    public $tempoh;


    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($nokp, $telefon, $caw_name,$nofoncaw,$kodcaw,$norujukan,$tempoh)
    {
        $this->nokp = $nokp;
        $this->telefon = $telefon;
        $this->caw_name = $caw_name;
        $this->nofoncaw = $nofoncaw;
        $this->kodcaw = $kodcaw;
        $this->norujukan = $norujukan;
        $this->tempoh = $tempoh;


    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $current = Carbon::now();
        $current = new Carbon();
        // $mobile_no = ltrim($this->telefon, '+6'); 
        //$mobile_no = '60178916936';
        $message = 'AR-RAHN '.$this->caw_name.': MAKLUMAN, selaras penambahbaikan Sistem Ar-Rahn, Tuan/Puan diminta hadir ke premis *sebelum 15/7/2024* bagi menukar SAG dengan cara:

1. Tebus
atau
2. Tebus Gadai Semula

Hubungi '.$this->nofoncaw.' untuk maklumat lanjut.';
        if(env('APP_ENV') != 'production'){
            $mobile_no = env('ADMIN_PHONE_NUMBER');
        }
        $response = Http::get(env('WABOT_API'), [
            'number' => $mobile_no,
            'type' => 'text',
            'message' =>  $message,
            'instance_id' => env('WABOT_INSTANCES_ID'),
            'access_token' => env('WABOT_TOKEN_KEYS')
        ]);
        
        //insert log blast wasap
        $logblastwasap = new BlastWasap();
        $logblastwasap->kodcaw = $this->kodcaw;
        $logblastwasap->nokp = $this->nokp;
        $logblastwasap->notelefon = $this->telefon;
        $logblastwasap->norujukan = $this->norujukan;
        $logblastwasap->tempoh = $this->tempoh;
        $logblastwasap->status = $response;
        $logblastwasap->created_at = $current;
        $logblastwasap->updated_at = $current;
        $logblastwasap->save();        

        Log::info("WABOT - Pelanggan SAG Tawarruq Lama".$response);
        
    }
}
