<?php


namespace Heanup\App\Api\Controller\User;


use Heanup\App\Api\Package\Database\Mail;
use Heanup\App\Api\RestfulController;
use Heanup\Frame\Helper;
use Heanup\Library\Vestacp;
use ProfiCloS\VestaCP\ClientException;
use ProfiCloS\VestaCP\ProcessException;

class User extends RestfulController
{

    /**
     * Get Method|获取数据
     * @return void
     */
    protected function getData()
    {
        $domains=[
          'qq-mail.ga',
          'qq-mail.gq',
          'qq-mail.ml',
          'qq-mail.tk',
        ];
        $domain = $domains[rand(0,count($domains)-1)];
        $email = Helper::getStrongCode(6);
        $password = md5(uniqid());
        $user_key=$this->getRequest()->string("a",$_SESSION['user_key']);
        $data = compact("domain", "email", "password","user_key");
        $data['expired_time'] = date("Y-m-d H:i:s", time() + 7200);
        $data['add_time'] = date("Y-m-d H:i:s");
        $exist = Mail::getOne(['email' => $email, 'domain' => $domain, 'status' => 1]);
        if (!$exist) {
            try {
                $result = Vestacp::getInstance()->getModuleMail("admin")->addAccount($domain, $email, $password);
                if ($result) {
                    Mail::insert($data);
                    $this->render();
                }
            } catch (ClientException $e) {
                $this->showError(400, $e->getMessage());
            } catch (ProcessException $e) {
                $this->showError(400, $e->getMessage());
            }
        }
        $this->showSuccess();
    }

    /**
     * Post Method|新增数据
     * @return void
     */
    protected function postData()
    {
        // TODO: 需要实现 postData() 方法.
    }

    /**
     * Put Method|更新数据
     * @return void
     */
    protected function putData()
    {
        // TODO: 需要实现 putData() 方法.
    }

    /**
     * Delete Method|删除数据
     * @return void
     */
    protected function deleteData()
    {
        // TODO: 需要实现 deleteData() 方法.
    }
}