<?php

namespace App\Jobs;

use App\Models\MoneyDetail;
use App\Models\MoneyInfo;
use App\Models\RenterRoom;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

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

    /**
     * Create a new job instance.
     *
     * @return void
     */

    protected $moneyInfoId;

//    public function onConnection($connection)
//    {
//        $this->connection = 'redis';
//        return $this;
//    }

    public function __construct($moneyInfoId)
    {
        //
        $this->moneyInfoId = $moneyInfoId;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //
        $moneyInfoId = $this->moneyInfoId;
        $moneyInfo = MoneyInfo::find($moneyInfoId);
        if (!$moneyInfo) {
            return;
        }

        $room = $moneyInfo->room;
        if (!$room) {
            return;
        }

        $details = MoneyDetail::query()->where('money_info_id', $moneyInfoId)->get();
        if ($details->count() <= 0) {
            return;
        }

        $email = $moneyInfo->contract->email;
        //$email = 'huynt57@gmail.com';


        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            return;
        }
        \Mail::send('frontend3.mail.mail_remind_renter_money', compact('moneyInfo', 'details'), function ($message) use ($moneyInfo, $email) {
            $message->from('support@itro.vn', 'iTro.vn');
            $message->subject('Thông báo đóng tiền hóa đơn ' . ' ' . $moneyInfo->name . ', tháng ' . $moneyInfo->date_action->format('m/Y') . ' - iTro.vn');
            $message->to($email);
            //$message->to('huynt57@gmail.com');
        });

    }
}
