<?php

namespace app\api\controller\doctor;

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

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

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

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

        if($this->auth->idcard_status != 1){
            $this->error('请先完成实名认证');
        }

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

        //自由输入覆盖
        if(!empty($freemoney)){
            $rc_id = 0;
            $money = floatval($freemoney);
        }

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

        $min = config('site.min_takecash_money');
        $max = config('site.max_takecash_money');
        if($money < $min){
            $this->error('提现金额不能小于'.$min);
        }
        if($money > $max){
            $this->error('提现金额不能大于'.$max);
        }

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

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

        if($type == 1){
            $table_name = 'doctor_alipay';
            $account_json = Db::name($table_name)->where('doctor_id',$this->auth->id)->find();
            if(empty($account_json)){
                $this->error('未绑定对应的提现账号');
            }
        }elseif($type == 2){
            $table_name = 'doctor_bank';
            $account_json = Db::name($table_name)->where('doctor_id',$this->auth->id)->find();
            if(empty($account_json)){
                $this->error('未绑定对应的提现账号');
            }
        }elseif($type == 3){
            //微信支付
            $table_name = 'doctor_wechat';
            $account_json = Db::name($table_name)->where('doctor_id',$this->auth->id)->find();
            if(empty($account_json)){
                $this->error('未绑定对应的提现账号');
            }
        }

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

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

        $data = [
            'doctor_id' => $this->auth->id,
            '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('doctor_take_cash')->insertGetId($data);
        if(!$log_id){
            Db::rollback();
            $this->error('提现失败');
        }

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

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

    //提现记录
    public function take_cash_log(){
        $list = Db::name('doctor_take_cash')->field('id,money,get_money,type,createtime,status')->where(['doctor_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);
    }

   //////////////////////////////////////////////

    //提现配置
    public function take_cash_config(){
        $config = Db::name('take_cash_config')->order('weigh asc,id asc')->select();

        $plat_bilv = config('site.takecash_plat_bili');
        foreach($config as $key => &$val){
            $val['get_money'] = bcdiv(bcmul($val['money'],(100-$plat_bilv),2),100,2);
        }

        $data = [
            'config' => $config,
            'wallet' => model('wallet')->getwallet($this->auth->id),
            'min' => config('site.min_takecash_money'),
            'max' => config('site.max_takecash_money'),
            'plat_bilv' => $plat_bilv,
            '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.take_cash_rule'),
        ];

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


}
