<?php

namespace Admin\Controller;
use User\Api\UserApi as UserApi;
use Think\Controller;

/**
 * 用户金币提现控制器
 * @author 鹿文学
 */
class UserWithdrawController extends ThinkController {
    
    const model_name = 'WithdrawGoldCoin';

    /**
     * 金币提现列表
     */
    public function lists(){
				
				if(!empty($_REQUEST['order_number'])) {$extend['order_number']=array('like','%'.$_REQUEST['order_number'].'%');}
        if(!empty($_REQUEST['account'])) {$extend['user_account']=array('like','%'.$_REQUEST['account'].'%');}
				if(!empty($_REQUEST['timestart']) && !empty($_REQUEST['timeend'])) {$extend['create_time'] = array('between',[strtotime($_REQUEST['timestart']),strtotime($_REQUEST['timeend'])+86399]);}
				elseif (!empty($_REQUEST['timestart']) && empty($_REQUEST['timeend'])) {$extend['create_time'] = array('between',[strtotime($_REQUEST['timestart']),time()]);}
				elseif (empty($_REQUEST['timestart']) && !empty($_REQUEST['timeend'])) {$extend['create_time'] = array('elt',strtotime($_REQUEST['timeend'])+86399);}
        if(!empty($_REQUEST['alipay'])) {$extend['alipay']=array('like','%'.$_REQUEST['alipay'].'%');}
				if(is_numeric($_REQUEST['status'])) {$extend['status']=$_REQUEST['status'];}
				
        $extend['admin_status'] = 0;
        
				$this->m_title = '用户提现';
				$this->assign('commonset',M('Kuaijieicon')->where(['url'=>'UserWithdraw/lists','status'=>1])->find());
				$this->meta_title = '用户提现';
				$wgc = D(self::model_name);
				
				$data = $wgc->lists($_GET['p'],$extend);
				
				$this->assign('list_data',$data['data']);
				$this->assign('_page',$data['page']);
				
				
				$map = array('status'=>1,'admin_status'=>0);
				
				$total = null_to_0($wgc->where($map)->sum('coin'));
        $ttotal = null_to_0($wgc->where('create_time' . total(1))->where($map)->sum('coin'));
        $mtotal = null_to_0($wgc->where('create_time' . total(3))->where($map)->sum('coin'));
        $poundage = null_to_0($wgc->where($map)->sum('poundage'));

        $this->checkListOrCountAuthRestMap($map,[]);

        $this->assign('total', $total);
        $this->assign('ttotal', $ttotal);
        $this->assign('mtotal', $mtotal);
        $this->assign('poundage', $poundage);
				
				$this->display();
				
    }
		
		public function del() {
			
			$this->update_status(I('request.ids'),-2);
			
		}
		
		public function set() {
			if(IS_POST) {
				
				$_POST['status']=1;
				$_POST['name']='withdraw';
				R('Tool/saveTool');
				
			} else {
				$tool = M('tool',"tab_")->where(['name'=>'withdraw'])->find();
        if(empty($tool)){$this->error('没有此设置');}
        
				$this->assign('data',json_decode($tool['config'],true));
				
				$this->m_title = '提现设置';
				$this->meta_title = '提现设置';
				$this->assign('commonset',M('Kuaijieicon')->where(['url'=>'UserWithdraw/set','status'=>1])->find());
				
				$this->display();
			}
		}
		
		/**
     * 提现记录状态
     */
		public function update_status($ids='',$status=0){
			$ids      = $ids?$ids:I('request.ids');
			$field = 'status';
			if(empty($ids)){
					$this->error('请选择要操作的数据');
			}
			
			$map['id'] = array('in',$ids);
			
			switch ($status){
				case -2 : /* 删除 */
						$data = ['admin_status'=>1];
						$msg = array('success'=>'删除成功','error'=>'删除失败');
						break;
				case 0  : /* 禁用 */
						$data = [$field=>0,'audit_time'=>0];
						$msg = array('success'=>'禁用成功','error'=>'禁用失败');
						break;
				case 1  : /* 成功 */
						$data = [$field=>1,'audit_time'=>time()];
						$msg = array('success'=>'打款成功','error'=>'打款失败');
						break;
				case 2  : /* 驳回/拒绝 */
						$data = [$field=>2,'audit_time'=>time()];						
						$msg = array('success'=>'驳回成功','error'=>'驳回失败');
						//用户返回金币
						$withdraw = D(self::model_name)->where(['id'=>$ids])->find();
						$result = M('User','tab_')->where(['id'=>$withdraw['user_id']])->setInc('gold_coin',$withdraw['coin']);
						if(!$result){
							$this->error('操作失败');
						}
						break;
				default :
						$this->error('参数错误');
						break;
			}
			
			
			if( D(self::model_name)->where($map)->save($data)!==false ) {
					$this->success($msg['success'],'',IS_AJAX);
			}else{
					$this->error($msg['error'],'',IS_AJAX);
			}
			
			
    }


    

}