<?php
namespace Admin\Controller;
use Think\Controller;
class XiaozuController extends AdminController {
	public function _initialize(){
		parent::_initialize();        
		if(!in_array(session('admin.job'),array(3,4,5)))
			$this->error('权限不足，无法访问此页面');
	}
	public function seetuijian(){
		if(I('nickname')){
			$f=M('admin')->where(array('nickname'=>I('nickname'),'xiaozu'=>session('admin.xiaozu')))->find();
			if(!$f){
				$this->error('未找到此人');
			}
			$m=M('admin');
			$where['tuijian']=I('nickname');
			$p=getpage($m, $where, 15);
			$user=$m->where($where)->order('zctime 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,'xiaozu'=>session('admin.xiaozu')))->find();
			if(!$f)
				$this->error('未找到此人');
			if($f['job']){
				switch ($f['job']) {
					case '1':
					$nick=M('admin')->where(array('xiaozu'=>$f['xiaozu']))->field('nickname')->select();
					$nickname=i_array_column($nick, 'nickname');
					$where['tuijian']=array('in',$nickname);
					break;
					case '3':
					$nick=M('admin')->where(array('xiaozu'=>$f['xiaozu']))->field('nickname')->select();
					$nickname=i_array_column($nick, 'nickname');
					$where['tuijian']=array('in',$nickname);
					break;
					case '6':
					$nick=M('admin')->where(array('xiaodui'=>$f['xiaodui']))->field('nickname')->select();
					$nickname=i_array_column($nick, 'nickname');
					$where['tuijian']=array('in',$nickname);
					break;
					case '7':
					$where['tuijian']=$f['nickname'];
					break;
					default:
					break;
				}
			}
			if(I('type')){
				switch (I('type')) {
					case 'today':
					$where['zctime']=array('EGT',strtotime(date('Y-m-d', time())));
					break;
					case 'todayg':
					$where['zctime']=array('EGT',strtotime(date('Y-m-d', time())));
					$where['type']=array('EQ','2');
					break;
					case '7day':
					$where['zctime']=array('EGT',strtotime(date('Y-m-d',strtotime("-7 days"))));
					break;
					case '1mon':
					$where['zctime']=array('EGT',strtotime(date('Y-m-d',strtotime("-30 days"))));
					break;
					default:
					break;
				}
			}
			if(I('jh')){
				switch (I('jh')) {
					case 'yes':
					$where['type']=1;
					break;
					case 'no':
					$where['type']=0;
					break;
					case 'sj':
					$where['type']=2;
					break;
					default:
					break;
				}
			}
			$m=M('admin');
			$p=getpage($m, $where, 15);
			$user=$m->where($where)->order('zctime DESC')->select();
			$this->id=$id;
			$this->p=$p->show();
			$this->user=$user;
			$this->display();
		}
	}

	public function seejiedai(){
		if(I('nickname')){
			$f=M('admin')->where(array('nickname'=>I('nickname'),'xiaozu'=>session('admin.xiaozu')))->find();
			$m=M('admin');
			$where['jiedai']=I('nickname');
			$p=getpage($m, $where, 15);
			$user=$m->where($where)->order('jhtime 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(!$f)
				$this->error('未找到此人');
			if($f['job']){
				switch ($f['job']) {
					case '1':
					$nick=M('admin')->where(array('xiaozu'=>$f['xiaozu'],'xiaozu'=>session('admin.xiaozu')))->field('nickname')->select();
					$nickname=i_array_column($nick, 'nickname');
					$where['jiedai']=array('in',$nickname);
					break;
					case '4':
					$nick=M('admin')->where(array('xiaozu'=>$f['xiaozu']))->field('nickname')->select();
					$nickname=i_array_column($nick, 'nickname');
					$where['jiedai']=array('in',$nickname);
					break;
					case '8':
					$where['jiedai']=$f['nickname'];
					break;
					default:
					break;
				}
			}
			if(I('type')){
				switch (I('type')) {
					case 'today':
					$where['jhtime']=array('EGT',strtotime(date('Y-m-d', time())));
					break;
					case '7day':
					$where['jhtime']=array('EGT',strtotime(date('Y-m-d',strtotime("-7 days"))));
					break;
					case '1mon':
					$where['jhtime']=array('EGT',strtotime(date('Y-m-d',strtotime("-30 days"))));
					break;
					default:
					break;
				}
			}
			if(I('jh')){
				switch (I('jh')) {
					case 'yes':
					$where['type']=1;
					break;
					case 'sj':
					$where['type']=2;
					break;
					default:
					break;
				}
			}
			$m=M('admin');
			$p=getpage($m, $where, 15);
			$user=$m->where($where)->order('jhtime DESC')->select();
			$this->id=$id;
			$this->p=$p->show();
			$this->user=$user;
			$this->display();
		}
	}
	public function seejipin(){
		if(I('nickname')){
			$f=M('admin')->where(array('nickname'=>I('nickname'),'xiaozu'=>session('admin.xiaozu')))->find();
			$m=M('admin');
			$where['jiedai']=I('nickname');
			$p=getpage($m, $where, 15);
			$user=$m->where($where)->order('jhtime 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,'xiaozu'=>session('admin.xiaozu')))->find();
			if(!$id)
				$this->error('未找到此人');
			if($f['job']){
				switch ($f['job']) {
					case '10':
					$nick=M('admin')->where(array('xiaozu'=>$f['xiaozu']))->field('nickname')->select();
					$nickname=i_array_column($nick, 'nickname');
					$where['jiedai']=array('in',$nickname);
					break;
					case '11':
					$nick=M('admin')->where(array('xiaozu'=>$f['xiaozu']))->field('nickname')->select();
					$nickname=i_array_column($nick, 'nickname');
					$where['jiedai']=array('in',$nickname);
					break;
					case '12':
					$where['jiedai']=$f['nickname'];
					break;
					default:
					break;
				}
			}
			if(I('type')){
				switch (I('type')) {
					case 'today':
					$where['jhtime']=array('EGT',strtotime(date('Y-m-d', time())));
					break;
					case '7day':
					$where['jhtime']=array('EGT',strtotime(date('Y-m-d',strtotime("-7 days"))));
					break;
					case '1mon':
					$where['jhtime']=array('EGT',strtotime(date('Y-m-d',strtotime("-30 days"))));
					break;
					default:
					break;
				}
			}
			if(I('jh')){
				switch (I('jh')) {
					case 'yes':
					$where['type']=1;
					break;
					case 'sj':
					$where['type']=2;
					break;
					default:
					break;
				}
			}
			$m=M('admin');
			$p=getpage($m, $where, 15);
			$user=$m->where($where)->order('jhtime DESC')->select();
			$this->id=$id;
			$this->p=$p->show();
			$this->user=$user;
			$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();
		}
	}

	public function see(){
		if(IS_POST){
			$s=M('admin')->where(array('id'=>I('userid')))->field('type,nickname,shenhe')->find();
			if(!$s)
				$this->error('未找到此用户');
			$nickname=$s['nickname'];
			if($s['type']<1&&$s['shenhe']==NULL)
				$this->error('该用户未通过审核且未激活不能进行此操作');
			$data=array(
				'bumen'=>session('admin.bumen'),
				'tuandui'=>session('admin.tuandui'),
				'xiaozu'=>session('admin.xiaozu'),
				'xiaodui'=>I('xiaodui'),
				'job'=>I('job')
				);
			$isok=isok($data);

			if($isok!==true)
				$this->error($isok);
			switch (I('type')) {
				case 'add':
				$data['rhtime']=time();
				$note='会员:'.$nickname.'加入'.btzd(I('tuandui'))." ".btzd(I('xiaozu'))." ".btzd(I('xiaodui')).",职务：".job(I('job'))."by ".session('admin.nickname');
				break;
				case 'edit':
				$note='会员:'.$nickname.'职务调整'.btzd(I('tuandui'))." ".btzd(I('xiaozu'))." ".btzd(I('xiaodui')).",职务：".job(I('job'))."by ".session('admin.nickname');
				break;
				default:
				$this->error('参数错误');
				break;
			}
			if(M('admin')->where(array('id'=>I('userid')))->save($data)){
				wlog($note,I('userid'),I('nickname'));
				$this->success('修改信息成功！');
			}else{
				$this->success(C('MSG'));
			}
		}else{
			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['xiaozu']!=session('admin.xiaozu')&&$info['job']!=0)
				$this->error('不属于本组');
			if($info['job']<session('admin.job')&&$info['job']!=0)
				$this->error('无权查看');
			$this->u=$info;
			$this->job=getauth(session('admin'),1);
			$this->display();
		}
	}
	public function index(){
		$this->user=M('admin')->where(array('id'=>session('admin.id')))->select();
		$this->display();
	}
	public function xiaodui(){
		$id=I('id');
		if(empty($id)) $this->error('参数错误！');
		$content=M('admin')->where(array('xiaozu'=>$id,'job'=>6))->select();

		if(!$content){
			$content=M('admin')->where(array('xiaozu'=>$id,'job'=>array('in','8,9')))->select();
		}
		$this->user=$content;
		$this->display();
	}
	public function tlist(){
		$id=I('id');
		if(empty($id)) $this->error('参数错误！');
		$content=M('admin')->where(array('xiaodui'=>$id,'job'=>7))->select();
		$this->user=$content;
		$this->display();
	}
	public function tuijian(){
		if(!I('nickname')){
			$this->error('参数错误！');
		}
		switch (I('type')) {
			case 'no':
			$where['type']=0;
			break;
			case 'yes':
			$where['type']=1;
			break;
			default:
			break;
		}
		$where['tuijian']=I('nickname');
		$m=M('admin');
		$p=getpage($m, $where, 10);
		$user=$m->where($where)->select();
		$this->nickname=I('nickname');
		$this->p=$p->show();
		$this->user=$user;
		$this->display();
	}

	public function tongguo(){
		$id=I('get.id');
		$nickname=I('get.nickname');
		if(!$id&&is_numeric($id))
			$this->error('参数错误!');
		$f=M('admin')->where(array('id'=>$id))->field('shiming')->find();
		if($f['shiming']!=1)
			$this->error('该用户未提交或已经通过实名');
		if(M('admin')->where(array('id'=>$id))->setField('shiming',2)){
			wlog('会员 '.$nickname." 实名认证通过，by ".session('admin.nickname'),$id,$nickname);
			$this->success('对用户实名认证通过成功');
		}else{
			$this->error(C('MSG'));
		}
	}

	public function butongguo(){
		$id=I('get.id');
		$nickname=I('get.nickname');
		if(!$id&&is_numeric($id))
			$this->error('参数错误!');
		$f=M('admin')->where(array('id'=>$id))->field('shiming')->find();
		if($f['shiming']!=1)
			$this->error('该用户未提交或已经通过实名');
		if(M('admin')->where(array('id'=>$id))->setField('shiming',0)){
			wlog('会员 '.$nickname." 实名认证没能通过(请按照规定重新上传资料)，by ".session('admin.nickname'),$id,$nickname);
			$this->success('对用户实名认证驳回成功');
		}else{
			$this->error(C('MSG'));
		}
	}

	public function fengjin(){
		$id=I('get.id');
		$nickname=I('get.nickname');
		if(!$id&&is_numeric($id))
			$this->error('参数错误!');
		$f=M('admin')->where(array('id'=>$id))->field('nickname,state,xiaozu')->find();
		if($f['state']==0)
			$this->error('此用户已经被封禁了');
		if($f['xiaozu']!=session('admin.xiaozu'))
			$this->error('此用户不是本组成员，无权限封禁');
		if(M('admin')->where(array('id'=>$id))->setField('state',0)){
			wlog('会员 '.$f['nickname']." 被封禁 账号无法使用，by ".session('admin.nickname'),$id,$f['nickname']);
			$this->success('对用户封禁成功');
		}else{
			$this->error(C('MSG'));
		}
	}

	public function jiefeng(){
		$id=I('get.id');
		$nickname=I('get.nickname');
		if(!$id&&is_numeric($id))
			$this->error('参数错误!');
		$f=M('admin')->where(array('id'=>$id))->field('nickname,state,xiaozu')->find();
		if($f['state']==1)
			$this->error('此用户为正常状态，不需要解封');
		if($f['xiaozu']!=session('admin.xiaozu'))
			$this->error('此用户不是本队成员，无权限封禁');
		if(M('admin')->where(array('id'=>$id))->setField('state',1)){
			wlog('会员 '.$f['nickname']." 封禁解除 账号恢复正常，by ".session('admin.nickname'),$id,$f['nickname']);
			$this->success('对用户解封成功');
		}else{
			$this->error(C('MSG'));
		}
	}

	public function kouchu(){
    $id=I('id');
    if(IS_POST){
        $gold=intval(I('gold'));
        $liyou=I('liyou');
        if(strlen($liyou)>45||strlen($liyou)<12)
            exit('理由要4-15个汉字之间');
        $f=M('admin')->where(array('id'=>$id))->field('id,nickname,gold')->find();
        if(!$f)
            exit('未找到此人');
        if($f['gold']<$gold)
            exit('此人金币不够你的扣除');
        if(M('admin')->where(array('id'=>$id))->setDec('gold',$gold)){
            $add=array('uid'=>$id,'type'=>0,'note'=>'会员 '.$f['nickname'].' 金币减少'.$gold.',原因:'.$liyou.',by '.session('admin.nickname'),'time'=>time());
            M('admin_goldlogs')->add($add);
            exit('扣除金币成功！');
        }else{
            exit(C('MSG'));
        }
    }else{
        $f=M('admin')->where(array('id'=>$id))->field('id,nickname,gold')->find();
        if(!$f)
            exit('未找到此人');
        $this->id=$id;
        $this->display();
    }
}
}