<?php

namespace App\Core\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Config;

class OfferPreConfirmCustomerMail extends Mailable
{

    use Queueable, SerializesModels;


    public $tries = 5;
    public $timeout = 30;

    protected $param;


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

    public function build()
    {
        try {
            $params = $this->param;

            $propertyService = App::make("App\Core\Service\OfferService");

            $requestParams = [
                'offer_id' => fillOnUndefined($params, 'offer_id'),
                'property_id' => fillOnUndefined($params, 'property_id'),
            ];

            $offerDetail = $propertyService->findOffer($requestParams);
            $offerDetail = fillOnUndefined($offerDetail, 'data');

            app('translator')->setLocale($offerDetail["language"]);
            $mailSenderAddress = Config::get('app.mailSenderAddress');

            $mailParams['to'] = [];
            $mailParams['to'] = $offerDetail['email'];

            $mailParams['bcc'] = [];
            $mailParams['bcc'][] = Config::get('app.logMailAddress');


            $mailParams['property_name'] = $offerDetail['property']['name'];
            $mailParams['url'] = Config::get('app.client_server').'/offer/'.$offerDetail['offer_code'];

            $mailParams['logo'] = Config::get('app.webUrl').'/assets/img/logo/mini-logo.png';
            if(isset($offerDetail['property_brand']['logoUrl'])) {
                $mailParams['logo'] = $offerDetail['property_brand']['logoUrl'];
            }

            $description = [];
            //$description[] = '<b>'.$offerDetail['property']['name'].'</b> tarafından oluşturulan <b>'.$offerDetail['ticket_code'].'</b> kodlu teklifi onayladınız.';
            $description[] = __('api-mailing-offer_preconfirm_customer_mail-description-one', ['propertyName' => $offerDetail['property']['name'], 'code' => $offerDetail['ticket_code']]);
            //$description[] = 'Onayınız tesisimize bildirilmiştir. Tesisin teklifi onaylaması beklenmektedir, onaya istinaden size dönüş yapılacaktır.';
            $description[] = __('api-mailing-offer_preconfirm_customer_mail-description-two');

            $description = implode('<br><br>', $description);


            /*echo view('emails.offerPreConfirmCustomerMail', compact('mailParams', 'description'));
            die();*/

            return $this->from($mailSenderAddress, 'Extranetwork - ' . $mailParams['property_name'])
                ->view('emails.offerPreConfirmCustomerMail', compact('mailParams', 'description'))
                ->to($mailParams['to'])
                ->bcc($mailParams['bcc'])
                //->subject($offerDetail['ticket_code'].' Kodlu Teklifiniz Tesis Onayı Bekliyor')
                ->subject(__('api-mailing-offer_preconfirm_customer_mail-title', ['code' => $offerDetail['ticket_code']]))
                ->with(['message' => $this]);


        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            return output(['status' => -1, 'message' => $e->getMessage()]);
        }
    }

}
