<?php
namespace member\controller;
class Cash extends Member {
    public function index(){
    	$this->display();
    }
/**
 * 106云盘自动发卡平台网盘系统
 * ============================================================================
 * 版权所有 2016-2028 扎兰屯市姝妍计算机服务有限公司，并保留所有权利。
 * 网站地址: http://www.106ym.com
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 */
    
    public function lists(){
    	
		
    	$where = array();
    	$where['uid'] = session("user_auth.id");
    	$type = I('type',false);
    	$card = I('card',false);
    	if($type){
    		$where['status'] = $type;
    	}
    	if($card){
    		$where['cash_card'] = array('like','%'.$card.'%');
    	}
    	$list = M("cash")->where($where)->select();
    	$type = $this->get_status_all('status',33);
    	$this->list = $list;
    	$this->type = $type;
    	$this->sql=M('cash')->getLastsql();
    	$this->display();
    }
    
    public function get_status_all($field,$model){
		$extra=M('attribute')->where(array('model_id'=>$model,'name'=>$field))->getField('extra');
		$extra=parse_config_attr($extra);
		return $extra;
	}
	
	//添加提现记录
	public function add(){
		if(C('CASH_SWITCH')==2){
		    $this->error(C('CASH_CLOSE'));
		}
		$uid=session("user_auth.id");
		
		
		$userconfig=userconfig_get($uid);
		if(empty($userconfig['member_idcard']) || empty($userconfig['member_truename']) || empty($userconfig['member_alipay'])){
		    $this->error('请先完善个人收款信息');
		}
		
		if(!$uid){
		    $this->error('登录超时');
		}
		if(C('CASH_MIN')>I('amount')){
		    $this->error('最小提现金额为：'.C('CASH_MIN'));
		}
		
		//可提现金额
		$amount_input=I('amount');
		$time=strtotime("-".C('MONEY_JIESUANZHOUQI')." day");
		$time=mktime(23,59,59,date('m',$time),date('d',$time),date('Y',$time));
		$amount=M('money')->where(array('uid'=>$uid,'ctime'=>array('elt',$time)))->sum('price');
		$money=M('money')->where(array('uid'=>$uid))->sum('price');
		if($amount_input>$money){
		    $this->error('您的可提现金额为：'.$money);
		}
		//输入金额判断
		
		if($amount_input>$amount){
		    $this->error('您的可提现金额为：'.$amount);
		}else{
			$amount=$amount_input;
		}
		//手续费
		$cash_price=sprintf("%.2f",$amount*$userconfig['wangpan_rate']);
		$cash_amount=sprintf("%.2f",$amount-$cash_price);
		
		$data_cash=array(
			'uid'=>$uid,
			'type'=>1,
			'no'=>date('YmdHis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT),
			'cash_amount'=>$cash_amount,
			'pay_way'=>1,
			'cash_price'=>$cash_price,
			'cash_card'=>$userconfig['member_alipay'],
			'ctime'=>time(),
			'status'=>0,
			'amount'=>$amount,
			'truename'=>$userconfig['member_truename'],
		);
		$m=D('pub/Cash');
		if($m->create($data_cash)){
		    $m->add();
			$this->success('提现申请提交成，我们将尽快处理');
		}else{
			$this->error($m->getError());
		}
		
		
	}
}