<?php

namespace addons\Pay;

use app\services\Addons;
use addons\Pay\model\PayHistory;

/**
 * 幻灯片插件
 * @author JYmusic
 */
class Pay extends Addons
{
    public $info = [
        'name'        => 'Pay',
        'title'       => '支付插件',
        'description' => '目前只有支付一个支付类型',
        'status'      => 1,
        'author'      => 'JYmusic',
        'version'     => '0.2',
    ];

    public function install()
    {
        return true;
    }

    public function uninstall()
    {
        return true;
    }

    /**
     * 幻灯片视图输出
     * @param array  $param 传入参数数组
     * @return \think\Response
     */
    public function pay($param)
    {
        if(isset($param['widget']) && $param['widget'] == 'history') {
            $limit = isset($param['limit']) ? $param['limit'] : 20;
            $model = new PayHistory();

            $map['uid'] = isset($param['user']) ? $param['user']['uid'] : UID;
            $list = $model->where($map)->paginate($limit);

            $this->assign( 'list', $list->items());
            $this->assign('page', $list->render());
            return $this->fetch('history');
        } else {
            $this->assign('out_trade_no', $this->createOrderNo());
            $this->assign('user', $param['user']);
            $this->assign('payConfig', $this->getConfig());
            return $this->fetch('widget', ['addons_param' => $param]);
        }
    }


    //生成订单号
    protected function createOrderNo() {
        $year_code = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
        return $year_code[intval(date('Y')) - 2010] .
            strtoupper(dechex(date('m'))) . date('d') .
            substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('d', rand(0, 99));
    }
}
