<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use addons\epay\library\Service;
use app\common\model\Wallet;
/**
 * 充值配置与充值订单
 */
class Pay extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    //vip用的
    public function vip_config(){
        $list = Db::name('payvip_config')->where('is_show',1)->order('weight asc,id asc')->select();
        $data['vipconfig'] = $list;
        $data['vip_endtime'] = model('wallet')->getWallet($this->auth->id,'vip_endtime');
        $this->success('success',$data);
    }

    //vip用的，创建订单
    public function vip_recharge(){

        $rc_id = input('rc_id',0);
        $pay_type = input('pay_type','wechat');
        $uid = $this->auth->id;

        if(!$rc_id){
            $this->error('请选择会员套餐');
        }


        //赋值money
        $recharge_config = Db::name('payvip_config')->where('id',$rc_id)->find();
        $money = $recharge_config['money'];

        if($money<=0)
        {
            $this->error('支付金额必须大于0');
        }
        if($money > 10000){
            $this->error('支付金额太大');
        }

        //创建订单
        $data = [];
        $data['status'] = 0;
        $pay_no = createUniqueNo('V',$uid);
        $data['pay_no'] = $pay_no;
        $data['money'] = $money;
        $data['payment_class'] = $pay_type;
        $data['user_id'] = $uid;
        $data['ext_info'] =  json_encode(['subject'=>'充值vip支付']);
        $data['memo'] =  '充值会员支付';
        $data['createtime'] = time();
        //$data['payment'] = 'miniapp';
        $data['payment'] = 'app';
        $orderid = Db::name('pay_order')->insertGetId($data);

        //创建回调
        $even_data = [];
        $even_data['event'] = 'success';
        $even_data['class'] = 'app\common\model\Recharge';
        $even_data['method'] = 'vippaysucc';
        $even_data['args'] = json_encode(['user_id'=>$uid,'days'=>$recharge_config['days']]);
        $even_data['pay_no'] = $pay_no;
        Db::name('pay_event')->insertGetId($even_data);

        $return = [
            'pay_no'=>$pay_no,
            'title' => '充值vip支付',
        ];

      /*  $this->success('success',$return);
    }

    public function topay(){*/

        //$openid = $this->auth->openid;
        /*$pay_no = input('pay_no');
        $orderInfo = Db::name('pay_order')->where('pay_no',$pay_no)->find();*/
        //下单
        $params = [
            'type'         => $pay_type,
            'orderid'      => $pay_no,
            'title'        => $data['memo'],
            'amount'       => $data['money'],
            //'amount'       => 0.01,
            //'method'       => 'miniapp',
            'method'       => 'app',
            //'openid'       => $openid,
            'notifyurl' => $this->request->root(true) . '/notify.php/paytype/'.$pay_type,
            'returnurl' => '',
        ];
        $res = Service::submitOrder($params);

        if($pay_type == 'wechat'){
            $this->success('success',json_decode($res,true));
        }else{
            $this->success('success',$res);
        }
    }


    //金币充值
    public function gold_config(){
        $list = Db::name('paygold_config')->where('is_show',1)->order('weight asc,id asc')->select();
        $data['goldconfig'] = $list;
        $data['gold'] = model('wallet')->getWallet($this->auth->id,'gold');
        $data['money_to_gold'] = config('site.money_to_gold');
        $this->success('success',$data);
    }

    //充值金币 创建订单
    public function gold_recharge(){

        $rc_id     = input_post('rc_id',0);
        $pay_type  = input_post('pay_type','wechat');
        $freemoney = input_post('freemoney',0);
        $uid = $this->auth->id;

        if(!$rc_id && !$freemoney){
            $this->error('请选择或填写充值金额');
        }

        //赋值money
        if($rc_id){
            $recharge_config = Db::name('paygold_config')->where('id',$rc_id)->find();
            $money = $recharge_config['money'] ?: 0;
            $gold  = $recharge_config['gold'] ?: 0;
            $first_gold     = $recharge_config['first_gold'] ?: 0;
            $first_vipdays  = $recharge_config['first_vipdays'] ?: 0;
        }

        //自由输入覆盖
        if(!empty($freemoney)){
            $rc_id = 0;
            $money = floatval($freemoney);
            $bili = config('site.money_to_gold') ?: 10;
            $gold  = bcmul($money,$bili,0);
            $first_gold = 0;
            $first_vipdays = 0;
        }

        //
        if($money<=0)
        {
            $this->error('支付金额必须大于0');
        }
        if($money > 10000){
            $this->error('支付金额太大');
        }

        //创建订单
        $data = [];
        $data['status'] = 0;
        $pay_no = createUniqueNo('P',$uid);
        $data['pay_no'] = $pay_no;
        $data['money'] = $money;
        $data['payment_class'] = $pay_type;
        $data['user_id'] = $uid;
        $data['ext_info'] =  json_encode(['subject'=>'充值金币支付']);
        $data['memo'] =  '充值金币支付';
        $data['createtime'] = time();
        $data['payment'] = 'app';
        $orderid = Db::name('pay_order')->insertGetId($data);

        //创建回调
        $even_data = [];
        $even_data['event'] = 'success';
        $even_data['class'] = 'app\common\model\Recharge';
        $even_data['method'] = 'goldpaysucc';
        $even_data['args'] = json_encode(['user_id'=>$uid,'gold'=>$gold,'money'=>$money,'pg_id'=>$rc_id,'first_gold'=>$first_gold,'first_vipdays'=>$first_vipdays]);
        $even_data['pay_no'] = $pay_no;
        Db::name('pay_event')->insertGetId($even_data);

        $return = [
            'pay_no'=>$pay_no,
            'title' => '充值金币支付',
        ];

        /*  $this->success('success',$return);
      }

      public function topay(){*/

        //$openid = $this->auth->openid;
        /*$pay_no = input('pay_no');
        $orderInfo = Db::name('pay_order')->where('pay_no',$pay_no)->find();*/
        //下单
        $params = [
            'type'         => $pay_type,
            'orderid'      => $pay_no,
            'title'        => $data['memo'],
            'amount'       => $data['money'],
            //'amount'       => 0.01,
            'method'       => 'app',
            'notifyurl' => $this->request->root(true) . '/notify.php/paytype/'.$pay_type,
            'returnurl' => '',
        ];
        $res = Service::submitOrder($params);

        if($pay_type == 'wechat'){
            $this->success('success',json_decode($res,true));
        }else{
            $this->success('success',$res);
        }
    }

}
