<?php

namespace app\adminapi\controller\v1\notification\sms;

use app\models\system\SystemConfig;
use crmeb\exceptions\AdminException;
use think\facade\Route;
use app\adminapi\controller\AuthController;
use crmeb\services\{
    sms\Sms, FormBuilder, UtilService
};

/**
 * 短信购买
 * Class SmsPay
 * @package app\admin\controller\sms
 */
class SmsPay extends AuthController
{
    /**
     * @var Sms
     */
    protected $smsHandle;

    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->smsHandle = new Sms('yunxin', [
            'sms_account' => sys_config('sms_account'),
            'sms_token' => sys_config('sms_token'),
            'site_url' => sys_config('site_url')
        ]);
        if (!$this->smsHandle->isLogin()) {
            throw new AdminException('请先填写短息配置');
        }
    }

    /**
     *  获取账号信息
     */
    public function number()
    {
        $countInfo = $this->smsHandle->count();
        if ($countInfo['status'] == 400) return $this->fail($countInfo['msg']);
        $info['account'] = SystemConfig::getConfigValue('sms_account');
        $info['number'] = $countInfo['data']['number'];
        $info['send_total'] = $countInfo['data']['send_total'];
        return $this->success($info);
    }

    /**
     *  获取支付套餐
     */
    public function price()
    {
        list($page, $limit) = UtilService::getMore([
            ['page', 1],
            ['limit', 20],
        ], null, true);
        $mealInfo = $this->smsHandle->meal($page, $limit);
        if ($mealInfo['status'] == 400) return $this->fail($mealInfo['msg']);
        return $this->success($mealInfo['data']);
    }

    /**
     * 获取支付码
     */
    public function pay()
    {
        list($payType, $mealId, $price) = UtilService::postMore([
            ['payType', 'weixin'],
            ['mealId', 0],
            ['price', 0],
        ], null, true);
        $payInfo = $this->smsHandle->pay($payType, $mealId, $price, $this->adminId);
        if ($payInfo['status'] == 400) return $this->fail($payInfo['msg']);
        return $this->success($payInfo['data']);
    }
}