<?php
// +----------------------------------------------------------------------
// | Yershop 开源网店系统
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.yershop.com All rights reserved.
// +----------------------------------------------------------------------
namespace app\seller\controller;
use think\Controller;
use think\Db;
class Cash extends Home{
    public function index(){     
        
		$map[]=["id",'=',is_login()];
        $info=Db::name("user")->where($map)->find();
	    $this->assign('info', $info);
		$this->meta_title="提现管理";
		$this->assign('meta_title', $this->meta_title);
	    return $this->fetch(); 
	}

	    public function add(){
		
		if(!session_uid()) {
		    $this->redirect( "User/login");
		}
		$money=input('money');
		
		if(!$money||$money<api("money")){
	   	    $this->error('提现金额必须不小于'.api("money"));
	   }
	   if(!input('account')){
				$this->error( "银行卡号不能为空");
	   }
		if(!input('bank')){
				$this->error( "开户行名称不能为空");
		}
        if(!input('username')){
				$this->error( "开户姓名不能为空");
		}		
		$uid=session_uid(); 
		$map["uid"]=$uid;
	   $where['id']=$uid;
	   $info=db('user')->where($where)->find();
	   if(!$info['status']){
	   	    $this->error('账号被封禁');
	   }
	   if($info["account"]<$money){
             $this->error('提现金额大于余额');
	   	
	   }	   
	   $save['account']=$info['account']-$money;
	   db('user')->where($where)->field('account')->update($save);
	   
                 $data["create_time"]=time();
				 $data["type"]=2;//提现
				 $data["uid"]=$uid;//提现费率
                 $data["money"]=$money; 
                 $data["total"]=$save['account'];
                 $data["content"]='提现';
	             db('account_log')->insert($data);
	              
	             unset($data);
		         $data["create_time"]=time();
				 $data["status"]=0;//申请中
				 $data["percent"]=api("PERCENT");//提现费率
                 $data["money"]=$money;
                 $data['uid']=$uid;  
                 $data["content"]='申请提现';
                 $data["total"]=$save['account'];
                 $percent=(float)api("PERCENT")/100;
			     $arrive_money=$money-$money*$percent;
				 $data["arrive_money"]=$arrive_money;//实际到账
				 $data["bank"]=input("bank");//银行名称
				 $data["account"]=input("account");//银行账户
				 $data["username"]=input("username");//银行开户名
				   
		        $ids=db("cash")->insertGetId($data);
		 
      if($ids){
		  $this->success("已提交审核");
	   }else{
		  $this->error("申请失败");
	   }
	  
   }
}
