<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 提现
 */
class Withdraw extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    //提现配置
    public function config(){

        $data = [
            'agentjewel' => model('wallet')->getwallet($this->auth->id,'agentjewel'),
            'user_bank' => Db::name('user_bank')->where('user_id',$this->auth->id)->find(),
            'user_alipay' => Db::name('user_alipay')->where('user_id',$this->auth->id)->find(),
            'remark' => config('site.withdraw_rule'),

            'min' => config('site.withdraw_min_price'),
            'plat_bilv' => config('site.withdrawal_plat_bili'),
            'agentjewel_to_money' => config('site.agentjewel_to_money'),
        ];

        $this->success('success',$data);
    }

    //提现
    public function withdraw(){
        $freemoney = input('freemoney',0);
        $type = input('type',1);

        if(!$freemoney){
            $this->error('请填写金额');
        }

        if (!in_array($type,[1,2])) {
            $this->error('未知的提现类型');
        }

        //验证提现类型
        if(!$this->user_auth_limit()){
            $this->error('请先完成实名认证');
        }


        //自由输入覆盖
        $agentjewel = floatval($freemoney);

        //
        if($agentjewel<=0)
        {
            $this->error('金额必须大于0');
        }

        $min = config('site.withdraw_min_price');
        if($agentjewel < $min){
            $this->error('提现金额不能小于'.$min);
        }

        $check = Db::name('withdraw')->where(['user_id'=>$this->auth->id,'status'=>0])->find();
        if($check){
            $this->error('您已经申请了提现,请等待审核');
        }

        $user_money = model('wallet')->getwallet($this->auth->id,'agentjewel');
        if($agentjewel > $user_money){
            $this->error('提现金额不能大于可提现余额');
        }

        if($type == 1){
            $table_name = 'user_alipay';
            $account_json = Db::name($table_name)->where('user_id',$this->auth->id)->find();
            if(empty($account_json)){
                $this->error('未绑定对应的提现账号');
            }
        }else{
            $table_name = 'user_bank';
            $account_json = Db::name($table_name)->where('user_id',$this->auth->id)->find();
            if(empty($account_json)){
                $this->error('未绑定对应的提现账号');
            }
        }

        //除汇率,得人民币
        $huilv = config('agentjewel_to_money') ?: 10;
        $money = bcdiv($agentjewel,$huilv,2);

        //平台手续费
        $plat_bilv = config('site.withdrawal_plat_bili');
        $plat_money = bcdiv(bcmul($money,$plat_bilv,2),100,2);

        //减去手续费,得实得金额
        $get_money = bcsub($money,$plat_money,2);



        $data = [
            'user_id'    => $this->auth->id,
            'agentjewel' => $agentjewel,
            'huilv'      => $huilv,
            'money'      => $money,
            'plat_bilv'  => $plat_bilv,
            'plat_money' => $plat_money,
            'get_money'  => $get_money,
            'type'       => $type,
            'acount_json'=> json_encode($account_json),
            'createtime' => time(),
            'updatetime' => time(),
            'status'     => 0,
        ];
        Db::startTrans();

        $log_id = Db::name('withdraw')->insertGetId($data);
        if(!$log_id){
            Db::rollback();
            $this->error('提现失败');
        }

        //扣除money
        $rs_wallet = model('Wallet')->lockChangeAccountRemain($this->auth->id,'agentjewel',-$agentjewel,55,'提现(审核中)','withdraw',$log_id);
        if($rs_wallet['status']===false)
        {
            Db::rollback();
            $this->error($rs_wallet['msg']);
        }

        Db::commit();

        $this->success('申请成功请等待审核');
    }

    //提现记录
    /*public function withdraw_log(){
        $list = Db::name('withdraw')->field('id,money,type,createtime')->where(['user_id'=>$this->auth->id])->autopage()->select();
        foreach($list as $key => &$val){
            $val['remark'] = '';

            if($val['type'] == 1){
                $val['remark'] = '支付宝提现';
            }elseif($val['type'] == 2){
                $val['remark'] = '银行卡提现';
            }else{
                $val['remark'] = '微信提现';
            }
        }

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

}