<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends AdminController {
	/**
	 * [index description]
	 * @method   index
	 * @Author   Amen[233713821@qq.com]
	 * @DateTime 2016-05-29T10:18:54+0800
	 * @return   [type]                   [description]
	 */
	public function index(){
		$this->u=session('admin');
		$this->display();
	}

	/**
	 * [index description]
	 * @method   index
	 * @Author   Amen[233713821@qq.com]
	 * @DateTime 2016-05-29T10:18:54+0800
	 * @return   [type]                   [description]
	 */
	public function regedit(){
		$this->display();
	}
	public function yeji(){

		$this->user=session('admin');
		$this->display();
	}

	public function seepeixun(){
		if(I('nickname')){
			$f=M('admin')->where(array('nickname'=>I('nickname'),'xiaozu'=>session('admin.xiaozu')))->find();
			$m=M('admin');
			$where['peixun']=I('nickname');
			$p=getpage($m, $where, 15);
			$user=$m->where($where)->order('sjtime DESC')->select();
			$this->id=$f['id'];
			$this->p=$p->show();
			$this->user=$user;
			$this->display();
		}else{
			$id=I('id',0,'int')?I('id'):$this->error('参数错误');
			$f=M('admin')->where(array('id'=>$id))->find();
			if(!$id)
				$this->error('未找到此人');
			if($f['job']){
				switch ($f['job']) {
					case '2':
					$nick=M('admin')->where(array('xiaozu'=>$f['xiaozu'],'xiaozu'=>session('admin.xiaozu')))->field('nickname')->select();
					$nickname=i_array_column($nick, 'nickname');
					$where['peixun']=array('in',$nickname);
					break;
					case '5':
					$nick=M('admin')->where(array('xiaozu'=>$f['xiaozu']))->field('nickname')->select();
					$nickname=i_array_column($nick, 'nickname');
					$where['peixun']=array('in',$nickname);
					break;
					case '9':
					$where['peixun']=$f['nickname'];
					break;
					default:
					break;
				}
			}
			if(I('type')){
				switch (I('type')) {
					case 'today':
					$where['sjtime']=array('EGT',strtotime(date('Y-m-d', time())));
					break;
					case '7day':
					$where['sjtime']=array('EGT',strtotime(date('Y-m-d',strtotime("-7 days"))));
					break;
					case '1mon':
					$where['sjtime']=array('EGT',strtotime(date('Y-m-d',strtotime("-30 days"))));
					break;
					default:
					break;
				}
			}
			$m=M('admin');
			$p=getpage($m, $where, 15);
			$user=$m->where($where)->order('sjtime DESC')->select();
			$this->id=$id;
			$this->p=$p->show();
			$this->user=$user;
			$this->display();
		}
	}
	

	/**
	 * [top description]
	 * @method   top
	 * @Author   Amen[233713821@qq.com]
	 * @DateTime 2016-05-24T13:03:52+0800
	 * @return   [type]                   [description]
	 */
	public function top(){
		$this->u=session('admin');
		$this->display();
	}
	/**
	 * [sider description]
	 * @method   sider
	 * @Author   Amen[233713821@qq.com]
	 * @DateTime 2016-05-29T10:19:04+0800
	 * @return   [type]                   [description]
	 */
	public function sider(){
		$this->display();
	}
	/**
	 * [main description]
	 * @method   main
	 * @Author   Amen[233713821@qq.com]
	 * @DateTime 2016-05-29T10:19:08+0800
	 * @return   [type]                   [description]
	 */
	public function main(){
		$this->display();
	}
	/**
	 * [search 新入会人员查询]
	 * @method   search
	 * @Author   Amen[233713821@qq.com]
	 * @DateTime 2016-05-29T10:19:13+0800
	 * @return   [type]                   [description]
	 */
	public function search(){
		if(I('get.nickname')){
			$where['nickname']=I('get.nickname');
		}
		if(I('get.truename')){
			$where['truename']=I('get.truename');
		}
		if(I('get.and')&&I('get.andv')){
			switch (I('get.and')) {
				case '1':
				$where['phone']=I('get.andv');
				break;
				case '2':
				$where['qq']=I('get.andv');
				break;
				case "3":
				$where['qt']=I('get.andv');
				break;
				default:
				break;
			}
		}
		if(!I('get.nickname')&&!I('get.truename')&&!I('get.and')&&!I('get.type')){
			// 只能查询未入会的人员
			$where['job']=0;
		}
		
		if(I('get.type')){
			switch (I('get.type')) {
				case 'no':
				$where['job']=0;
				break;
				case 'yes':
				switch (session('admin.job')) {
					case '1':
					case '2':
					$where['tuandui']=session('admin.tuandui');	
					break;
					case '3':
					case '4':
					case '5':
					$where['xiaozu']=session('admin.xiaozu');
					break;
					case '6':
					$where['xiaodui']=session('admin.xiaodui');	
					default:
					break;
				}
				break;
				default:
				break;
			}
		}
		$m=M('admin');
		$p=getpage($m, $where, 15);
		$user=$m->where($where)->order('zctime DESC')->select();

		$this->p=$p->show();
		$this->user=$user;
		$this->display();
	}

	public function see(){
		if(!I('get.id')||!is_numeric(I('get.id')))
			$this->error('页面不存在！');
		if(!$info=M('admin')->where(array('id'=>I('get.id')))->find())
			$this->error('没有这个人');
		if($info['job']!=0)
			$this->error('该用户已有职务，无法查看');
		$this->u=$info;
		$this->display();
	}

	public function rhsh(){
        $id=I('id',0,int) ? I('id'):$this->error('参数错误');
        $f=M('admin')->where(array('id'=>$id))->field('shenhe')->find();
        if($f['shenhe']!=NULL){
            $this->error('该用户已经通过,不需要重复提交');
        }
        if(M('admin')->where(array('id'=>$id))->save(array('shenhe'=>session('admin.nickname')))){
            $this->success('操作成功');
        }else{
            $this->success(C('MSG'));
        }
	}
	
	/**
	 * [handle 表单AJAX验证]
	 * @method   handle
	 * @Author   Amen[233713821@qq.com]
	 * @DateTime 2016-05-23T15:07:49+0800
	 * @return   [type]                   [description]
	 */
	public function handle(){
		if(!IS_AJAX) $this->error('页面不存在！');
		$name=I('name');
		$param=I('param');
		switch ($name) {
			case 'nickname':
			if(nickname($param)){
				$r=array('info'=>"通过信息验证！",'status'=>"y");
			}else{
				$r=array('info'=>"验证失败！昵称被占用或非法",'status'=>"x");
			}
			break;
			case 'tuijian':
			if(xuanchuan($param)){ 
				$r=array('info'=>"通过信息验证！",'status'=>"y");
			}else{
				$r=array('info'=>"验证失败！推广人不存在",'status'=>"x");
			}
			break;
			case 'cash':
			if(cash($param)){
				$r=array('info'=>yuji($param),'status'=>"y");
			}else{
				$r=array('info'=>"余额不足",'status'=>"x");
			}
			break;
			case 'pass':
			if(M('admin')->where(array('password'=>md5($param),'id'=>session('user.id')))->find()){
				$r=array('info'=>"通过验证！",'status'=>"y");
			}else{
				$r=array('info'=>"旧密码错误",'status'=>"x");
			}
			break;
			case 'anquan':
			$pwd=M('admin')->where(array('id'=>session('user.id')))->find();
			if($pwd['password']!=md5($param)){
				$r=array('info'=>"通过验证！",'status'=>"y");
			}else{
				$r=array('info'=>'密码和安全码不可相同','status'=>"x");
			}
			break;
			case 'aqpwd':
			if(M('admin')->where(array('anquan'=>md5($param),'id'=>session('user.id')))->find()){
				$r=array('info'=>"通过验证！",'status'=>"y");
			}else{
				$r=array('info'=>"旧提现密码错误",'status'=>"x");
			}
			break;
			case 'tixian':
			if(M('admin')->where(array('anquan'=>md5($param),'id'=>session('user.id')))->find()){
				$r=array('info'=>"通过验证！",'status'=>"y");
			}else{
				$r=array('info'=>"提现密码错误",'status'=>"x");
			}
			break;
			case 'jihuoma':
			$card = M('card_pt')->where(array('card'=>$param))->find();
			if($card){
				if ($card['shiyong'] == 1)
				{
					$r=array('info'=>"该激活码已被使用",'status'=>"x");
				}
				else{
					$r=array('info'=>"通过验证！",'status'=>"y");
				}
			}else{
				$r=array('info'=>"该激活码不存在",'status'=>"x");
			}
			break;
			default:
			$r=array('info'=>"未定义",'status'=>"x");
			break;
		}
		$this->ajaxReturn($r);
	}

	/**
	 * [reg 用户注册第一步]
	 * @method   reg
	 * @Author   Amen[233713821@qq.com]
	 * @DateTime 2016-05-23T16:23:01+0800
	 * @return   [type]                   [description]
	 */
	public function reg(){
		// 如果有SESSION reg 跳转至注册第二步
		if(session('reg')) $this->redirect('reg2');
		// 如果用户有提交信息
		if(IS_POST){
			// 是否同意会员协议
			//if(I(agreement)!='y') $this->error('必须同意并遵守会员协议!');
			// 接收POST过来的值

			$User = D('Admin'); 	// 实例化User对象
			$data =	$User->create();

			if (!$User->create()) {
                // 如果创建失败 表示验证没有通过 输出错误提示信息
				$this->error($User->getError());
			} else {
				if($id=$User->add()){
					M('admin')->where(array('id'=>$id))->setInc('type',1);

					M('admin')->where(array('id'=>$id))->save(['jiedai'=>session('admin.nickname'),'password'=>md5(I('phone'))]);

					//设置激活码
					$jihuoma=I('jihuoma');
					M('card_pt')->where(array('card'=>$jihuoma))->save(['shiyong'=>'1','syr'=>I('nickname'),'jiedai'=>session('admin.nickname')]);
					// 给推荐人计数
					// 获得推荐人ID
					if($tuijian=M('admin')->where(array('nickname'=>I('tuijian')))->find()){
						M('admin')->where(array('id'=>$tuijian['id']))->setInc('tuijianjs',1);
						M('admin_goldlogs')->add(array('uid'=>$tuijian['id'],'type'=>2,'note'=>'用户:'.I('nickname').' 注册 未激活 金币增加0'.'，by Admin','time'=>time()));
						// tuijianjs($tuijian['id']);
					}
					// $wherewxdz['id']=array('neq',$tuijian['id']);
					if($wxduizhang=M('admin')->where(array('xiaodui'=>$tuijian['xiaodui'],'job'=>6,))->field('id')->find()){
						// M('admin')->where(array('id'=>$wxduizhang['id']))->setInc('tuijianjs',1);
						M('admin_goldlogs')->add(array('uid'=>$wxduizhang['id'],'type'=>2,'note'=>'用户:'.I('nickname').' 注册 未激活 金币增加0 推荐人:'.I('tuijian').'，by Admin','time'=>time()));
						// tuijianjs($wxduizhang['id']);
					}
					if($wxzuzhang=M('admin')->where(array('xiaozu'=>$tuijian['xiaozu'],'job'=>3))->field('id')->find()){
						// M('admin')->where(array('id'=>$wxzuzhang['id']))->setInc('tuijianjs',1);
						M('admin_goldlogs')->add(array('uid'=>$wxzuzhang['id'],'type'=>2,'note'=>'用户:'.I('nickname').' 注册 未激活 金币增加0 推荐人:'.I('tuijian').'，by Admin','time'=>time()));
						// tuijianjs($wxzuzhang['id']);
					}
					if($tuanzhang=M('admin')->where(array('tuandui'=>$tuijian['tuandui'],'job'=>1))->field('id')->find()){
						// M('admin')->where(array('id'=>$tuanzhang['id']))->setInc('tuijianjs',1);
						M('admin_goldlogs')->add(array('uid'=>$tuanzhang['id'],'type'=>2,'note'=>'用户:'.I('nickname').' 注册 未激活 金币增加0 推荐人:'.I('tuijian').'，by Admin','time'=>time()));
						// tuijianjs($tuanzhang['id']);
					}
					record("1");					
					$this->display('success');
				}else{
					$this->error(C('MSG'));
				}
			}
		}else{

			$this->display();
			// $key=makekey(I('get.key'),1);

			// if(!$key){
			// 	$this->display('error');
			// }else{
			// 	$this->assign('key',$key)->display();
			// }
		}
	}

}