<?php

namespace app\queue\redis;

use Webman\RedisQueue\Consumer;

class Email implements Consumer
{
    // 要消费的队列名
    public $queue = 'Email';

    // 连接名，对应 plugin/webman/redis-queue/redis.php 里的连接`
    public $connection = 'default';

    // 消费
    public function consume($data)
    {
        \support\Log::channel('mail')->alert("开始发送邮件");
        $config = Config('site');
        if(isset($data['config'])){
            $config = $data['config'];
            unset($data['config']);
        }
        try {
            $mail = new \Nette\Mail\Message;
            $mailer = new \Nette\Mail\SmtpMailer(
                host: $config['mail_smtp_host'],
                username: $config['mail_smtp_user'],
                password: $config['mail_smtp_pass'],
                encryption: \Nette\Mail\SmtpMailer::EncryptionSSL,
            );
            $mail->setFrom($config['mail_from'])
                ->addTo($data['email'])
                ->setSubject($data['title']);
            if($data['body']){
                $mail->setHtmlBody($data['body']);
            }else{
                $mail->setHtmlBody($this->getTemplate($data));
            }
            $mailer->send($mail);
            \support\Log::channel('mail')->alert($data['email']."邮件已经发送");
        } catch (\Throwable $th) {
            \support\Log::channel('mail')->alert('发送邮件出错1:'.$th->getMessage());
            \support\Log::channel('mail')->alert(json_encode($data));
        }
                
        // 无需反序列化
        //var_export($data); // 输出 ['to' => 'tom@gmail.com', 'content' => 'hello']
    }
    function getTemplate($data){
        $config = Config('site');
        $mail_from = $config['mail_from'];
        $str = '<div style="background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(46, 48, 50) !important; padding: 40px 8px; font-family: Poppins, serif, EmojiFont; text-align: center;height: 100%;display: flex;align-items: center;justify-content: center;" data-ogsb="rgb(239, 242, 244)"><table style="border-collapse: collapse; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(41, 41, 41) !important; max-width: 420px; margin: 0px auto; border-radius: 8px;" width="100%" role="presentation" cellspacing="0" cellpadding="40" border="0" data-ogsb="rgb(255, 255, 255)"><tbody><tr><td style="border-collapse: collapse; color: rgb(177, 180, 202) !important;" align="center" data-ogsc="rgb(89, 92, 112)"><img style="width: 96px; height: 96px; margin-top: 20px; color: rgb(177, 180, 202) !important;" title="logo" src="'.domain().'/static/img/mail_logo.png" data-imagetype="External" data-ogsc=""><div style="font-size: 24px; font-weight: 500; margin-top: 32px; color: rgb(177, 180, 202) !important;" data-ogsc="">Thanks for your request!</div><div style="font-size:12px; margin-top:8px; color:#a1a5b7">Do not share this code with anyone, even if they claim to be a Codify employee.</div><div style="padding:8px 20px; margin:32px 0; border-radius:100px; color:#00b2ff; border:2px #00b2ff dotted; display:inline-block; font-size:24px">{$code}</div><div style="margin-bottom:24px; color:#a1a5b7; font-size:12px">Valid within 15 minutes and <br>can only be used once.</div><div style="height: 1px; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(46, 48, 50) !important; width: 100%; color: rgb(177, 180, 202) !important;" data-ogsc="" data-ogsb="rgb(239, 242, 245)"></div><div style="font-size:12px; text-align:center; margin-top:24px; padding-bottom:20px; color:#a1a5b7">Need help with anything? connect <a style="color:#00b2ff" href="mailto: '.$mail_from.'" title="mailto: '.$mail_from.'" data-linkindex="0" id="LPlnk280375">support</a></div></td></tr></tbody></table></div>';
        foreach($data as $k=>$v){
            $str = str_replace('{$'.$k.'}',$v,$str);
        }
        return $str ;
    }
    // 消费失败回调
    /* 
    $package = [
        'id' => 1357277951, // 消息ID
        'time' => 1709170510, // 消息时间
        'delay' => 0, // 延迟时间
        'attempts' => 2, // 消费次数
        'queue' => 'send-mail', // 队列名
        'data' => ['to' => 'tom@gmail.com', 'content' => 'hello'], // 消息内容
        'max_attempts' => 5, // 最大重试次数
        'error' => '错误信息' // 错误信息
    ]
    */
    public function onConsumeFailure(\Throwable $e, $package)
    {
        echo "consume failure\n";
        echo $e->getMessage() . "\n";
        // 无需反序列化
        //var_export($package); 
    }
}