<?php

namespace Controller;

use Lib\Mail\MailFun;
use Lib\Verify;
use Model\emailSql;
use Model\listsSql;


/**
 * @author:dc
 * @time 2023/2/13 11:28
 * Class Home
 * @package Controller
 */
class Home extends Base {


    /**
     * 邮件列表
     * @author:dc
     * @time 2023/2/17 14:12
     */
    public function lists(){

        // 分页 页数
        $page   =   app()->request('page',1,'intval');
        $page   =   $page ? $page : 1;

        $size   =   app()->request('size',20,'intval');
        $size   =   $size ? $size : 1;

        // 邮件文件夹的id
        $folder_id = app()->request('folder_id');

        $lists = db()->all(
            listsSql::lists(
                $this->getEmails('id'),
                $page,
                $size,
                (int) $folder_id
            )
        );

        // 总数
        $total  = db()->count(
            listsSql::listCount(
                $this->getEmails('id'),
                (int) $folder_id
            )
        );

        app()->_json(listsPage($lists,$total,$page,$size));

    }



    /**
     * 发送邮件
     * @author:dc
     * @time 2023/2/18 17:32
     */
    public function send_mail(){

        $email = $this->getEmail();

        $formData = Verify::checks([
            'nickname|'.__('nickname')  =>  ['required','max'=>50],
            'subject|'.__('subject')  =>  ['required','max'=>150],
            'body|'.__('body_email')  =>  ['required'],
            'to|'.__('to_email')  =>  ['required','array|string','email'],
            'priority|'.__('priority_email')  =>  ['in'=>[1,3,5]],
            'files|'.__('files_email')  =>  [
                'file'=>[
                    'ext'   =>  [],
                    'size'  =>  500,
                    'mine'  =>  []
                ]
            ],
            'receipt|'.__('receipt_email')  =>  []
        ],[

        ]);

        $ret = MailFun::sendEmail(
            $email['smtp'],
            $email['email'],
            base64_decode($email['password']),
            $formData['nickname']??'',
            $formData['to'],
            $formData['subject'],
            $formData['body'],
            $formData['files']??'',
            $formData['receipt']??'',
            $formData['priority']??3,
        );

        if($ret[0]===true) {
            app()->_json(['messageId'=>$ret[1]]);
        }else {
            app()->e($ret[1]);
        }
    }


    /**
     * 收到前端的同步请求操作
     * @author:dc
     * @time 2023/3/10 10:38
     */
    public function sync(){

        $where['email'] = web_request_emails();

        if(empty($where)){
            app()->e('sync_request_param_error');
        }else{
            // 查询id
            $datas = db()->all(emailSql::getValues($where,'`id`,`email`,`pwd_error`'));
            foreach ($datas as &$v){
                if(!$v['pwd_error']){
                    redis()->rPush('sync_email_lists', $v['id']);
                }
                unset($v['id']);
            }
            // 返回成功的参数值
            app()->_json($datas);
        }

    }





}