<?php
namespace app\api\controller;
use app\common\controller\Api;
use app\common\model\OrderLevel;
use app\common\model\UserGroup;
use liuniu\repositories\LevelRepository;
use liuniu\UtilService;
use think\Request;

class Level extends Api
{

    protected $noNeedRight = ['*'];
    public function ify()
    {
        $this->success('获取成功',UserGroup::where('cid',$this->cid)->where('status','normal')->where("pay_price",'>',0)->select());
    }
    public function create(Request $request)
    {
        $where = UtilService::postMore(
            [
                ['cid',$this->cid],
                ['user_id',$this->auth->getUserinfo()['id']],
                ['level_id',0],
                ['body',''],
                ['pay_type',0],
                ['pay_price',0],
                ['from','weixin'],
            ],$request
        );
        $where1 = $where;
        unset($where1['from']);
        $where1['order_id'] = OrderLevel::getNewOrderId();
        $where1['createtime'] = time();
        $order = OrderLevel::order_create($where1);
        if(!$order) $this->error(OrderLevel::getErrorInfo());
        $orderId = $order['order_id'];
        $info = compact('orderId');
        if ($orderId) {
            $orderInfo = OrderLevel::where('order_id', $orderId)->find();
            if (!$orderInfo || !isset($orderInfo['paid']))  $this->error('支付订单不存在!');
            if ($orderInfo['paid']) $this->error('支付已支付!');
            try {
                if ($where['from'] == 'routine') {
                    $jsConfig = LevelRepository::jsPay($this->cid,$orderId); //创建订单jspay
                } else if ($where['from'] == 'weixinh5') {
                    $jsConfig = LevelRepository::h5Pay($this->cid,$orderId);
                } else {
                    $jsConfig = LevelRepository::wxPay($this->cid,$orderId);
                }
            } catch (\Exception $e) {
                return $this->error( $e->getMessage());
            }
            $info['jsConfig'] = $jsConfig;

            return $this->success('订单创建成功',$info);

        } else $this->error(OrderLevel::getErrorInfo());
    }
    public function mylst(Request  $request)
    {
        $where = UtilService::getMore(
            [
                ['page',1],
                ['limit',10],
                ['cid',$this->cid],
                ['user_id',$this->auth->getUserinfo()['id']],
                ['paid',-1],
            ],$request
        );
        $this->success('获取成功',OrderLevel::lst($where));
    }
    /**
     * 订单支付
     * @param Request $request
     * @return mixed
     */
    public function pay(Request $request)
    {
        list($uni, $paytype, $from) = UtilService::postMore([
            ['uni', ''],
            ['paytype', '0'],
            ['from', 'weixin']
        ], $request, true);
        if (!$uni)  $this->error('参数错误!');
        $order = LevelRepository::where('cid',$this->cid)->where('order_id',$uni)->find();
        if (!$order)
            $this->error('订单不存在!');
        if ($order['paid'])
            $this->error('该订单已支付!');
        $order['pay_type'] = $paytype; //重新支付选择支付方式
        switch ($order['pay_type']) {
            case '0':
                try {
                    if ($from == 'routine') {
                        $jsConfig = LevelRepository::jsPay($this->cid,$order); //订单列表发起支付
                    } else if ($from == 'weixinh5') {
                        $jsConfig = LevelRepository::h5Pay($this->cid,$order);
                    } else {
                        $jsConfig = LevelRepository::wxPay($this->cid,$order);
                    }
                } catch (\Exception $e) {
                    $this->error($e->getMessage());
                }
                $this->success('获取成功',$jsConfig);
                break;
        }
        return $this->error('支付方式错误');
    }
}