<?php

namespace Modules\Lpj\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Mail;
use Modules\Lpj\Emails\SendPenawaranKJPPEmail;

class SendPenawaranKJPPTenderJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $kjpps;
    protected $dp1; // Tidak perlu array [0] lagi
    protected $penawaran;
    protected $permohonan;
    protected $villages;
    protected $districts;
    protected $cities;
    protected $provinces;
    protected $user;

    /**
     * Create a new job instance.
     */
    public function __construct($kjpps, $dp1, $penawaran, $permohonan, $villages, $districts, $cities, $provinces, $user)
    {
        $this->kjpps = $kjpps;
        $this->dp1 = $dp1; // Simpan keseluruhan array dp1, bukan dp1[0]
        $this->penawaran = $penawaran;
        $this->permohonan = $permohonan;
        $this->villages = $villages;
        $this->districts = $districts;
        $this->cities = $cities;
        $this->provinces = $provinces;
        $this->user = $user;
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        $email = new SendPenawaranKJPPEmail(
            $this->dp1,
            $this->penawaran,
            $this->permohonan,
            $this->villages,
            $this->districts,
            $this->cities,
            $this->provinces,
            $this->user // Kirim user ke email sebagai cc dan bcc
        );

        $email->with([
            'dp1' => $this->dp1, // Kirim seluruh array dp1 ke email
            'penawaran' => $this->penawaran,
            'permohonan' => $this->permohonan,
            'villages' => $this->villages,
            'districts' => $this->districts,
            'cities' => $this->cities,
            'provinces' => $this->provinces,
            'user' => $this->user // Kirim user ke email sebagai cc dan bcc
        ]);

        $send = Mail::to($this->kjpps)->send($email);
    }
}
