<?php

namespace Open\Controller;


use Admin\Model\DevelopersModel;
use Open\Model\OpenMessageModel;
use Admin\Model\SpendModel;
class QueryController extends CenterController
{

	public function _initialize()
	{
		
        $this->p_title = "管理中心";
        $this->p_url = U('Stats/overview');
		parent::_initialize(); // TODO: Change the autogenerated stub
	}


	public function incomesettlement($p=1){
		$this->title = "收入结算";

			if(!empty($_REQUEST['start_time']) && !empty($_REQUEST['end_time'])) {
				$map['starttime'] = array('egt',strtotime($_REQUEST['start_time']));
        $map['endtime']   = array('elt',strtotime($_REQUEST['end_time'])+86399);
			} elseif (!empty($_REQUEST['start_time']) && empty($_REQUEST['end_time'])) {
				$map['starttime'] = array('egt',strtotime($_REQUEST['start_time']));
				$map['endtime']   = array('elt',time());
			}	elseif (empty($_REQUEST['start_time']) && !empty($_REQUEST['end_time'])) {
				$map['endtime']   = array('elt',strtotime($_REQUEST['end_time'])+86399);
			}	
			
			
			/* if(!empty($_REQUEST['start_time']) && !empty($_REQUEST['end_time'])) {$map['create_time'] = array('between',[strtotime($_REQUEST['start_time']),strtotime($_REQUEST['end_time'])+86399]);}			
			
			elseif (!empty($_REQUEST['start_time']) && empty($_REQUEST['end_time'])) {$map['create_time'] = array('between',[strtotime($_REQUEST['start_time']),time()]);}			
			
			elseif (empty($_REQUEST['start_time']) && !empty($_REQUEST['end_time'])) {$map['create_time'] = array('elt',strtotime($_REQUEST['end_time'])+86399);}	
			 */
			
    	if(isset($_GET['game_id'])&&$_GET['game_id']!=""){
    		$map['game_id']=$_GET['game_id'];
    	}
    	if(isset($_GET['ti_status'])&&$_GET['ti_status']!=""){
    		$map['ti_status']=$_GET['ti_status'];
    	}
			
			empty($_REQUEST['number']) || $map['settlement_number'] = array('like','%' . $_REQUEST['number'] . '%');
			
		$map['developers']=UID;
		$data=M('settlement','tab_')
		->field('create_time,game_id,starttime,endtime,settlement_number,bind_coin_status,game_name,sum(total_money) as total_money,ratio,sum(sum_money) as sum_money,ti_status')
		->page($p,10)
		->group('create_time')
		->order('id desc')
		->where($map)
		->select();
		
		$count = M('settlement','tab_')
				->field('count( DISTINCT create_time) as count')->where($map)->find();
		
		$result['count'] = $count['count'];
		$this->assign('data',$data);
    	$this->showPage($result['count']);
		$month=$this->get_month();
		$this->assign('month',$month);
		$this->display();
	}
	private function get_month($m=5){
        $time=array();
        for ($i=0; $i <$m ; $i++) { 
            $time[]=date('Y-m',mktime(0,0,0,date('m')-$i,date('d'),date('Y')));
        }
        return $time;
    }
	public function apply_withdraw(){
        $map['create_time'] = $_POST['create_time'];
        $map['developers']  = UID;
        $with = M("withdraw","tab_"); 
        $seet = M("settlement","tab_")
        		->field("settlement_number,create_time,game_id,game_name,sum(total_money) as total_money,ratio,sum(sum_money) as sum_money,ti_status")
        		->group("create_time")
        		->where($map)
        		->find();
        $with_map['settlement_number']=$seet['settlement_number'];
        $fid = $with->where($with_map)->find();
        if($fid == null){
	        $add['settlement_number'] = $seet['settlement_number'];
	        $add['sum_money']         = $seet['sum_money'];
	        $add['promote_id']        = 0;
	        $add['promote_account']   = 0;
	        $add['developers']        = UID;
	        $add['create_time']       = time();
	        $add['start_time']		  = $seet['starttime'];
	        $add['end_time']		  = $seet['endtime'];
	        $add['status']            = 0;
	        $with->add($add);
        	M("settlement","tab_")->where($map)->save(array('ti_status'=>0));
        	M("TotalSettlement","tab_")->where('settlement_number ="'.$seet['settlement_number'].'"')->save(array('ti_status'=>0));
        	echo json_encode(array("status"=>1));  
        }elseif($fid['status']==2/*被驳回*/){
            $where['settlement_number']=$fid['settlement_number'];
            $add['status']=0;
            $with->where($where)->save($add);
            M("settlement","tab_")->where($where)->save(array('ti_status'=>0));
            echo json_encode(array("status"=>1));
        }else{
        	echo json_encode(array("status"=>0));  
        }

    }
    public function ordersearch($p=1){
	    $this->title = "订单查询";	
			
			if(!empty($_REQUEST['start_time']) && !empty($_REQUEST['end_time'])) {$map['pay_time'] = array('between',[strtotime($_REQUEST['start_time']),strtotime($_REQUEST['end_time'])+86399]);}			
			
			elseif (!empty($_REQUEST['start_time']) && empty($_REQUEST['end_time'])) {$map['pay_time'] = array('between',[strtotime($_REQUEST['start_time']),time()]);}			
			
			elseif (empty($_REQUEST['start_time']) && !empty($_REQUEST['end_time'])) {$map['pay_time'] = array('elt',strtotime($_REQUEST['end_time'])+86399);}	
			
    	if(isset($_GET['user_account'])&&$_GET['user_account']!=""){
    		$map['user_account']=array('like','%'.$_GET['user_account'].'%');
    	}
    	if(isset($_GET['game_id'])&&$_GET['game_id']!=""){
    		$map['game_id']=$_GET['game_id'];
    	}
    	if(isset($_GET['pay_way'])&&$_GET['pay_way']!=""){
    		$map['pay_way']=$_GET['pay_way'];
    	}
    	$this->assign('uid',UID);
    	$map['developers']=UID;
    	$map['s.pay_status']=1;
    	$data=M('spend','tab_')->alias("s")
        ->join("left join tab_game g on g.id = s.game_id")
        ->where($map)
        ->order('pay_time desc')
        ->page($p,10)
        ->select();
        $amount=M('spend','tab_')->alias("s")
        ->field('sum(pay_amount) as amount')
        ->join("left join tab_game g on g.id = s.game_id")
        ->where($map)
        ->order('pay_time desc')
        ->find();
        $result['count']=M('spend','tab_')->alias("s")
        ->join("left join tab_game g on g.id = s.game_id")
        ->where($map)
        ->order('pay_time desc')
        ->count();
    	$this->assign('data',$data);
    	$this->assign('amount',$amount['amount']);
    	$this->showPage($result['count']);
    	$this->display();
    }
	/**
	 * 注册
	 * @param $account
	 * @param $password
	 * @param $code
	 * author: xmy 280564871@qq.com
	 */
	public function register($account,$password,$code){
		//检查验证码
		$result = check_verify($code,2);
		if(!$result){
			$this->error("验证码错误");
		}
		//实例化注册
		$model = new DevelopersModel();
		if($result && $model->register($account,$password)){
			$model = new OpenMessageModel();
			$model->sendMsg(session("user_info.uid"),"注册成功",'恭喜您,您已成功注册开发者平台账号');
			$this->success("注册成功",U("Manage/index"));
		}else{
			$this->success("注册失败:".$model->getError());
		}
	}

	/**
	 * 登录
	 * @param $account
	 * @param $password
	 * @param $code
	 * author: xmy 280564871@qq.com
	 */
	public function login($account,$password,$code){
		//检查验证码
		$result = check_verify($code,1);
		if(!$result){
			$this->error("验证码错误");
		}
		//实例化注册
		$model = new DevelopersModel();
		$uid = $model->login($account,$password);
		if($result && $uid > 0){
			$this->success("登录成功",U("Manage/index"));
		}else{
			$this->success("登录失败:".$model->getError());
		}
	}


	/**
	 * 登出
	 * author: xmy 280564871@qq.com
	 */
	public function logout(){
		$model = new DevelopersModel();
		$model->logout();
		$this->redirect("index/index");
	}


	/**
	 * 修改密码
	 * @param $old
	 * @param $new
	 * author: xmy 280564871@qq.com
	 */
	public function alter_password($old,$new){
		$model = new DevelopersModel();
		$uid = session("user_info.uid");
		$result = $model->alterPwd($uid,$old,$new);
		if($result !== false){
			$this->success("修改成功");
		}else{
			$this->error("修改失败:".$model->getError());
		}
	}

	public function details($create_time=''){
		$map['developers'] = UID;
        $map['create_time'] = $create_time;
        $data  = M('settlement','tab_')->where($map)->select();
        $total = M('settlement','tab_')->where($map)->sum('sum_money');
        $this->assign('total',$total);
        $this->assign('list_data',$data);
        $this->display();
	}
}