<?php

namespace App\Jobs;

use App\Models\Notification;
use App\Models\RenterRoom;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

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

    /**
     * Create a new job instance.
     *
     * @return void
     */
    protected $room;
    protected $remain;
    protected $dateAction;

    public function __construct($room, $remain, $dateAction)
    {
        //
	    $this->room = $room;
	    $this->remain = $remain;
	    $this->dateAction = $dateAction;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //
	    $room = $this->room;
	    $remain = $this->remain;
	    $dateAction = $this->dateAction;


	    $renters = RenterRoom::where('room_id', $room->id)->get();

	    if(empty($dateAction))
	    {
	    	$dateAction = Carbon::now()->format('m/Y');
	    }
	    foreach ($renters as $renter)
	    {
		    Notification::create( [
			    'to_user' => $renter->user_id,
			    'title'   => 'Thông báo từ itro.vn',
			    'user_id' => $renter->user_id,
			    'content' => 'Thông báo thu tiền phòng tháng '.$dateAction.': số tiền '.number_format($remain, 0, '.', '.').' đ'
		    ] );
	    }


	    $remindJob = (new RemindCharge($room, $dateAction))->delay(Carbon::now()->addDay(1));

	    dispatch($remindJob);

    }
}
