<?php

namespace App\Mail;

use App\Order;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class PaymentMethodChangedUser extends Mailable implements ShouldQueue
{
    use Queueable, SerializesModels;

    public $order;
    public $newPaymentMethod;
    public $paymentLink;

    /**
     * Create a new message instance.
     *
     * @param Order $order
     * @param int $newPaymentMethod (0: bank transfer, 1: credit card)
     * @param string|null $paymentLink
     * @return void
     */
    public function __construct(Order $order, $newPaymentMethod, $paymentLink = null)
    {
        $this->onConnection(config('queue.default'));
        $this->onQueue(config('queue.connections.redis.queue'));
        
        $this->order = $order;
        $this->newPaymentMethod = $newPaymentMethod;
        $this->paymentLink = $paymentLink;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        $template = ($this->newPaymentMethod == 1) ? 'payment_method_change_to_credit' : 'payment_method_change_to_bank';
        
        return $this->from(config('mail.from.address'), config('mail.from.name'))
            ->replyTo(config('mail.from.address'), config('mail.from.name'))
            ->subject('お支払い方法変更のご案内 - 注文番号: ' . $this->order->code)
            ->view('emails.' . $template)
            ->with([
                'order' => $this->order,
                'paymentLink' => $this->paymentLink
            ]);
    }
}