<?php
namespace App\Mail;

use Storage;
use App\User;
use App\Services\Credit;
use App\Services\Invoice;
use App\Models\UserCredit;
use App\Models\ShoppingOrder;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class MailCredit extends Mailable
{
    use Queueable, SerializesModels;

    protected $user_credit;

    public $subject;


    public function __construct(UserCredit $user_credit)
    {
        $this->user_credit = $user_credit;
        $this->subject = 'Gutschrift auf Grüne Seele';
        
    }
    
    public function build()
    {
        $title =  __('email.credit_title');
        $copy1line = __('email.credit_copy1line');

        $filename = Credit::getFilename($this->user_credit);   
        $path = Credit::getDownloadPath($this->user_credit);
        if (!Storage::disk('public')->exists($path)) {
            return;
        }
        $file = Storage::disk('public')->path($path);
        $mime = Storage::disk('public')->mimeType($path);

        return $this->view('emails.blank')->with([
            'title' => $title,
            'copy1line' =>  $copy1line,
        ])->attach($file,[
            'as' => $filename,
            'mime' => $mime,
        ]); // attach  file;
    }
}