<?php
namespace System\Controller;
use Think\Controller;
class IndexController extends Controller {
	public function _initialize(){
        // if(is_ip()) die('该IP被未被管理员设置信任，不可访问');
		if (!session('?s')) 
			$this->redirect('Public/login');
	}

	public function index(){
		$this->display();
	}
	public function top(){
		$this->display();
	}
	public function main(){
		$time = time();
        $today=strtotime(date('Y-m-d 00:00:00'));
        $t=array();$a=array();
        $t['t']=M('admin')->where(array('zctime'=>array('egt',$today)))->count();
        $t['s']=M('admin')->where(array('sjtime'=>array('egt',$today)))->count();
        $a['t']=M('admin')->where('zctime is not null')->count();
        $a['s']=M('admin')->where('sjtime is not null')->count();


        $this->t=$t;
        $this->a=$a;

        $this->display();
    }
    public function newso(){
        if(I('get.nickname')){
            $where['nickname']=I('get.nickname');
        }
        if(I('get.qq')){
            $where['truename']=I('get.qq');
        }
        if(I('get.peixun')){
            $where['peixun']=I('get.peixun');
        }
        if(I('get.tuijian')){
            $where['tuijian']=I('get.tuijian');
        }
        if(I('get.type')){
            switch (I('get.type')) {
                case '1':
                break;
                case '2':
                $where['job']=0;
                break;
                case '3':
                $where['job']=array('GT',0);
                break;
                default:
                break;
            }
        }
        if(!I('get.nickname')&&!I('get.qq')&&!I('get.peixun')&&!I('get.tuijian')&&!I('get.type')){
            $where['job']=0;
        }
        $m=M('admin');
        $p=getpage($m, $where, 10);
        $user=$m->where($where)->order('zctime DESC')->select();
        $this->p=$p->show();
        $this->user=$user;
        $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'=>'System'))){
            $this->success('操作成功');
        }else{
            $this->success(C('MSG'));
        }
    }
    public function see(){
        if(IS_POST){
            $s=M('admin')->where(array('id'=>I('userid')))->field('type,shenhe')->find();
            if($s['type']<1&&$s['shenhe']==NULL)
                $this->error('该用户未通过审核且未激活不能进行此操作');
            $data=array(
                'bumen'=>I('bumen'),
                'tuandui'=>I('tuandui'),
                'xiaozu'=>I('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='会员:'.I('nickname').'加入'.btzd(I('tuandui'))." ".btzd(I('xiaozu'))." ".btzd(I('xiaodui')).",职务：".job(I('job'))."by System";
                break;
                case 'edit':
                $note='会员:'.I('nickname').'职务调整'.btzd(I('tuandui'))." ".btzd(I('xiaozu'))." ".btzd(I('xiaodui')).",职务：".job(I('job'))."by System";
                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('没有这个人');
			
	
            $this->u=$info;
            $job['bumen']=M('admin_group')->where('pid=0')->select();
            $job['tuandui']=M('admin_group')->where(array('pid'=>array('in',"1,2")))->select();
            $job['xiaozu']=M('admin_group')->where(array('gname'=>array('like',"%组")))->select();
            $job['xiaodui']=M('admin_group')->where(array('gname'=>array('like',"%队")))->select();
            $job['job']=M('auth_group')->select();
            $this->job=$job;
            $history['gold']=M('cash')->where(array('uid'=>I('get.id')))->sum('gold');
            $history['rmb']=M('cash')->where(array('uid'=>I('get.id')))->sum('rmb');
            $this->history=$history;
            $where['uid']=I('get.id');
            $where['type']=array('in','0,1');
            $this->list=M('admin_goldlogs')->where($where)->order('time DESC')->select();

            $this->sys_list=M('admin_logs')->where(array('uid'=>session('user.id')))->order('time DESC')->select();
            $this->display();
        }
    }
    public function gettuandui(){
        if(!IS_AJAX)
            E('页面不存在');
        if(I('bumen')){
            $job=M('admin_group')->where(array('pid'=>I('bumen')))->select();
            if($job){
                foreach ($job as $k => $v) {
                    $back[$k]['value']=$v['id'];
                    $back[$k]['name']=$v['gname'];
                }
            }
            if($back){
                $data=array('state'=>"success",'data'=>$back);
            }
            $this->ajaxReturn($data);
        }
    }
    public function getxiaozu(){
        if(!IS_AJAX)
            E('页面不存在');
        if(I('tuandui')){
            $job=M('admin_group')->where(array('pid'=>I('tuandui')))->select();
            if($job){
                foreach ($job as $k => $v) {
                    $back[$k]['value']=$v['id'];
                    $back[$k]['name']=$v['gname'];
                }
            }
            if($back){
                $data=array('state'=>"success",'dataxiaozu'=>$back);
            }
            $this->ajaxReturn($data);
        }
    }
    public function getxiaodui(){
        if(!IS_AJAX)
            E('页面不存在');
        if(I('xiaozu')){
            $job=M('admin_group')->where(array('pid'=>I('xiaozu')))->select();
            if($job){
                foreach ($job as $k => $v) {
                    $back[$k]['value']=$v['id'];
                    $back[$k]['name']=$v['gname'];
                }
            }
            if($back){
                $data=array('state'=>"success",'dataxiaodui'=>$back);
            }else{
                $data=array('state'=>"success",'dataxiaodui'=>array(0=>array('value'=>0,'name'=>'没有小队')));
            }
            $this->ajaxReturn($data);
        }
    }
    public function tuandui(){
        // $yi=M('admin_group')->where('id=1')->find();
        $result = M('admin_group')->order('gname ASC')->select();
        $this->s=createTree($result,$parentid=0);


        $this->display();
    }

    public function tuanduiadd(){
        $id=I('get.id');
        if(empty($id)) $this->error('参数错误！');
        if(IS_POST){
            $gname=I('post.gname');
            $add=array('pid'=>$id,'gname'=>$gname);
           
            if($nid=M('admin_group')->add($add))
                // 下级增加一个表示
                M()->execute("update `snow_admin_group` set `auth`=CONCAT(`auth`,',$nid') where `id`=$id");
            $this->success('增加成功！');
        }else{
            if(!$f=M('admin_group')->where(array('id'=>$id))->find())
                $this->error('错误！没有找到该团/组/队');
            $this->assign('add',$f)->display();
        }
    }

    public function tuanduiedit(){
        $id=I('get.id');
        if(empty($id)) $this->error('参数错误！');
        if(IS_POST){
            $gname=I('post.gname');
            M('admin_group')->where(array('id'=>$id))->setField('gname',$gname);
            $this->success('修改成功！');
        }else{
            if(!$f=M('admin_group')->where(array('id'=>$id))->find())
                $this->error('错误！没有找到该团队/组/队');
            $this->assign('edit',$f)->display();
        }
    }

    public function tuanduisee(){
        $id=I('id');
        if(empty($id)) $this->error('参数错误！');
        $content=M('admin')->where(array('tuandui'=>$id,'job'=>array('in','3,4,5')))->select();
        $this->user=$content;
        $this->display();
    }

    public function xiaozusee(){
        $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 xiaoduisee(){
        $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 setwage(){
        if(IS_POST){
            $cwage=I('cwage');
            foreach ($cwage as $k => $v) {
                M('auth_group')->where(array('id'=>$k))->setField('cwage',$v);
            }
            $pwage=I('pwage');
            foreach ($pwage as $k => $v) {
                M('auth_group')->where(array('id'=>$k))->setField('pwage',$v);
            }
            $xwage=I('xwage');
            foreach ($xwage as $k => $v) {
                M('auth_group')->where(array('id'=>$k))->setField('xwage',$v);
            }
            $this->success('设置工资成功！');
        }else{
            $this->wage=M('auth_group')->field('id,title,cwage,pwage,xwage')->select();
            $this->display();            
        }
    }

    public function cash(){
        if(I('get.id')){
            $map['id']=I('get.id');
        }
        if(I('get.nickname')){
            $map['nickname']=I('get.nickname');
        }
        if(!I('get.id')&&!I('get.nickname')&&I('get.type')){
            $type=I('get.type');
            switch ($type) {
                case 1:
                $map['shenhe']=0;
                $map['chuli']=0;
                break;
                case 2:
                $map['shenhe']=1;
                $map['chuli']=0;
                break;
                case 3:
                $map['shenhe']=1;
                $map['chuli']=1;
                break;
                default:
                $map['shenhe']=0;
                $map['chuli']=0;
                break;
            }
        }
        $m=M('cash');
        $p=getpage($m, $map, 10);
        $cash=$m->where($map)->order('sqtime DESC')->select();
        $this->p=$p->show();
        $this->cash=$cash;
        $this->display();
    }

    public function cashshenhe(){
        $id=I('get.id');
        $l=M('cash')->where(array('id'=>$id))->find();
        if(!$l){
            $this->error('未找到此条提现记录');
        }
        if(!$id&&is_numeric($id))
            $this->error('参数错误!');
        $save=array(
            'shenhe'=>1,
            'shtime'=>time(),
            );
        if(M('cash')->where(array('id'=>$id))->save($save)){
            wlog('会员 '.$l['nickname'].'ID为'.$id.'的提现申请通过审核，by System',$l['uid'],$l['nickname']);
            $this->success('审核成功！');
        }else{
            $this->error(C('MSG'));
        }
    }
    public function cashchuli(){
        $id=I('get.id');
        if(!$id&&is_numeric($id))
            $this->error('参数错误!');
        if(!$l=M('cash')->where(array('id'=>$id,'shenhe'=>1))->find()){
            $this->error('此条记录还未进行审核');
        }
        $save=array(
            'chuli'=>1,
            'cltime'=>time(),
            );
        if(M('cash')->where(array('id'=>$id))->save($save)){
           wlog('会员 '.$l['nickname'].'ID为'.$id.'的提现申请已确认转账 注意查收，by System',$l['uid'],$l['nickname']);
           $this->success('提交成功！');
       }else{
        $this->error(C('MSG'));
    }
}

// public function card(){
//     $this->list=makecard(1000);
//     $this->display();
// }

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('state')->find();
    if($f['state']==0)
        $this->error('此用户已经被封禁了');
    if(M('admin')->where(array('id'=>$id))->setField('state',0)){
        wlog('会员 '.$nickname." 被封禁 账号无法使用，by System",$id,$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('state')->find();
    if($f['state']==1)
        $this->error('此用户为正常状态，不需要解封');
    if(M('admin')->where(array('id'=>$id))->setField('state',1)){
        wlog('会员 '.$nickname." 封禁解除 账号恢复正常，by System",$id,$nickname);
        $this->success('对用户解封成功');
    }else{
        $this->error(C('MSG'));
    }
}

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 System",$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 System",$id,$nickname);
        $this->success('对用户实名认证驳回成功');
    }else{
        $this->error(C('MSG'));
    }
}

public function seetuijian(){
    if(I('nickname')){
        $f=M('admin')->where(array('nickname'=>I('nickname')))->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))->find();
        if(!$f)
            $this->error('未找到此人');
        if($f['job']){
            switch ($f['job']) {
                case '1':
                $nick=M('admin')->where(array('tuandui'=>$f['tuandui']))->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 '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')))->find();
        if(!$f)
            $this->error('未找到此人');
        $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('tuandui'=>$f['tuandui']))->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')))->find();
        if(!$f)
            $this->error('未找到此人');
        $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 '10':
                $nick=M('admin')->where(array('tuandui'=>$f['tuandui']))->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')))->find();
        if(!$f)
            $this->error('未找到此人');
        $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(!$f)
            $this->error('未找到此人');
        if($f['job']){
            switch ($f['job']) {
                case '2':
                $nick=M('admin')->where(array('tuandui'=>$f['tuandui']))->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 yichu(){
    $id=I('get.id');
    $nickname=I('get.nickname');
    if(!$id&&is_numeric($id))
        $this->error('参数错误!');
    $f=M('admin')->where(array('id'=>$id))->field('nickname,tuandui')->find();
    $save=array('bumen'=>0,'tuandui'=>0,'xiaozu'=>0,'xiaodui'=>0,'job'=>0);
    if(M('admin')->where(array('id'=>$id))->save($save)){
        wlog('会员 '.$f['nickname']." 被移除团队，by System",$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 System','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();
    }
}

public function jiangli(){
    $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(M('admin')->where(array('id'=>$id))->setInc('gold',$gold)){
            $add=array('uid'=>$id,'type'=>1,'note'=>'会员 '.$f['nickname'].' 金币增加'.$gold.',原因:'.$liyou.',by System','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();
    }
}

public function editap(){
    if(IS_POST){
        $f=M('sss')->where('id=1')->find();
        if($f['password']!=md5(I('password'))){
            $this->error('旧密码错误！');
        }
        $rules = array(
     array('account','require','账户必须'), //默认情况下用正则进行验证
     array('account','/^[\@A-Za-z0-9\!\#\$\%\^\&\*\.\~]{1,10}$/','账户1-10个字符'),
     array('cnewp','newp','确认密码不正确',0,'confirm'), // 验证确认密码是否和密码一致
     array('newp','/^[\@A-Za-z0-9\!\#\$\%\^\&\*\.\~]{6,20}$/','密码格式5-20个',), // 自定义函数验证密码格式
     );
        $save=array('account'=>I('account'),'password'=>md5(I('newp')));
        $User = M("sss"); // 实例化User对象
        if (!$User->validate($rules)->create()){
     // 如果创建失败 表示验证没有通过 输出错误提示信息
           exit($User->getError());
       }else{
        if($User->where(array('id'=>1))->save($save)){
            $this->success('修改账户信息成功！');
        }else{
            $this->error(C('MSG'));
        }
    }
}else{
    $this->s=M('sss')->where('id=1')->find();
    $this->display();
}
}

public function logout(){
    session('s',null);
    $this->redirect('Public/login');
}

public function card_new(){
    $pt['js']=M('card_pt')->Count('id');
    $pt['syjs']=M('card_pt')->where(array('shiyong'=>1))->count('id');
    // echo (makecard_new(10));
    $jp['js']=M('card_jp')->Count('id');
    $jp['syjs']=M('card_jp')->where(array('shiyong'=>1))->count('id');
    $this->pt=$pt;
    $this->jp=$jp;
    $this->display();
}

public function ajax_card(){
    $type=I('type',0,int);
    $sl=I('sl',100,int);
    $m=makecard_new($sl,$type);
    if($m){
       // $url="http://s.51jianzhi.info/Public/card/".$m;
	    $url="http://www.chuangshen1314.com/Public/card/".$m;
        echo '卡密生成成功：<a href="'.U('index/download',array('field'=>$m)).'" target="_blank">下载到本地</a> | <a href="'.$url.'" target="_blank">在线打开</a>';
    }else{
       echo '<span style="color:red">生成卡密失败！请联系QQ：233713821</span>'; 
   }
}


public function ajax_getcard(){
    // sleep(10);
    $card=I('card');
    $pt=M('card_pt')->where(array('card'=>$card))->find();
    $jp=M('card_jp')->where(array('card'=>$card))->find();
    if($pt){
        $e1="查询结果：卡密类型(<span style='color:red'>普通</span>) 使用状况(";
        if($pt['shiyong']){
            $e2="<span style='color:red'>已使用</span>) ";
            $e3="使用人(<span style='color:red'>".$pt['syr']."</span>) ";
            if($pt['jiedai']){
                // $e4="推荐:(<span style='color:red'>".$pt['waixuan']."</span>) ";
                $e5="用途(<span style='color:red'>激活</span>) 接待人(<span style='color:red'>".$pt['jiedai']."</span>)";
            }elseif($pt['peixun']){
                $e5="用途(<span style='color:red'>升级</span>) 培训人(<span style='color:red'>".$pt['peixun']."</span>)";
            }else{
                $e5="用途(<span style='color:red'>未知</span>) 此卡密出错！";
            }
        }else{
            $e2="<span style='color:green'>未使用</span>) ";
        }
        echo $e1.$e2.$e3.$e4.$e5;
    }elseif ($jp) {
        $e1="查询结果：卡密类型(<span style='color:red'>急聘</span>) 使用状况(";
        if($jp['shiyong']){
            $e2="<span style='color:red'>已使用</span>) ";
            $e3="使用人(<span style='color:red'>".$jp['syr']."</span>) ";
            $e4="推荐:(<span style='color:red'>".$jp['waixuan']."</span>) ";
            $e5="急聘人(<span style='color:red'>".$jp['jiedai']."</span>)";
        }else{
            $e2="<span style='color:green'>未使用</span>) ";
        }
        echo $e1.$e2.$e3.$e4.$e5;
    }else{
        echo '<span style="color:red">此卡密不存在</span>';
    }
}
public function download(){
    if(!I('field'))
        $this->error('参数错误');
    $file_name="./Public/card/".I('field');//需要下载的文件
    $file_name=iconv("utf-8","gb2312","$file_name");
$fp=fopen($file_name,"r+");//下载文件必须先要将文件打开，写入内存
if(!file_exists($file_name)){//判断文件是否存在
echo "文件不存在"; //如果不存在
exit(); //直接退出
} //如果存在，继续执行下载
$file_size=filesize($file_name);//判断文件大小
//返回的文件
Header("Content-type: application/txt");
//按照字节格式返回
Header("Accept-Ranges: bytes");
//返回文件大小
Header("Accept-Length: ".$file_size);
//弹出客户端对话框，对应的文件名
Header("Content-Disposition: attachment; filename=".I('field'));
//防止服务器瞬时压力增大，分段读取
$buffer=1024;
while(!feof($fp)){
    $file_data=fread($fp,$buffer);
    echo $file_data;
}
//关闭文件
fclose($fp);
}

public function setvideo(){
    if(IS_POST){
        if(M('video')->where('id=1')->save(array('p'=>I('p'),'t'=>time()))){
            $this->success('设置视频观看密码成功!');
        }else{
            $this->error(C('MSG'));
        }
    }else{
        $this->v=M('video')->where('id=1')->find();
        $this->display();
    }
}

public function chongzhi(){
    $id=I('get.id');
    $nickname=I('get.nickname');
    if(!$id&&is_numeric($id))
        $this->error('参数错误!');
    $f=M('admin')->where(array('id'=>$id))->field('id')->find();
    if(!$f)
        $this->error('未找到该用户');
    $save['password']=md5('z778899');
    $save['anquan']=md5('z778899');
    if(M('admin')->where(array('id'=>$id))->save($save)){
        wlog('会员 '.$nickname." 密码和提现密码成功重置为z778899,请及时修改密码和提现密码，by System",$id,$nickname);
        $this->success('重置密码和提现密码成功成功');
    }else{
        $this->error(C('MSG'));
    }
}

public function editpay(){
	$id=I('id');
    if(IS_POST){
        if(M('admin')->where(array('id'=>$id))->setField('alipay',I('alipay'))){
            exit('修改支付宝账号成功');
        }else{
            exit(C('MSG'));
        }
    }else{
     $f=M('admin')->where(array('id'=>$id))->field('id,alipay')->find();
        if(!$f)
            exit('未找到此人');
        $this->value=$f;
        $this->display();
    }

}


public function editqq(){
    $id=I('id');
    if(IS_POST){
        if(M('admin')->where(array('id'=>$id))->setField('qq',I('qq'))){
            exit('修改QQ账号成功');
        }else{
            exit(C('MSG'));
        }
    }else{
     $f=M('admin')->where(array('id'=>$id))->field('id,qq')->find();
        if(!$f)
            exit('未找到此人');
        $this->value=$f;
        $this->display();
    }

}

public function task(){
        if(I('get.nickname')){
            $where['pbr']=I('get.nickname');
        }
        if(I('get.task_id')){
            $where['id']=I('get.task_id');
        }
   
        if(!I('get.nickname')&&!I('get.task_id')){
            $where = null;
        }

        $m=M('task');
        $p=getpage($m, $where, 10);
        $task=$m->where($where)->order('id DESC')->select();
        $this->p=$p->show();
        $this->task=$task;

        $this->display();
    }
    public function task_edit(){
        if(IS_POST){
            $data['id'] = I('post.id');
            $data['total'] = I('post.total');

            $res = M('task')->save($data);
            if($res ===false)
                $this->error('失败');
            else
                $this->success('成功');
        }else{
            $id = I('get.id');
            $info = M('task')->where("id = $id")->find();
            $this->info = $info;
            $this->display();
        }
       
    }
    public function task_list(){
        $id = I('get.id'); 
        $task_info = M('task')->where("id = $id")->find();
        $this->task_info = $task_info;
        $m = M('admin_task');
        $where = "task_id = $id";
        $p=getpage($m, $where, 10);
        $task=$m->where($where)->order('id DESC')->select();
        $this->p=$p->show();
        $this->task = $task;
        $this->display();
    }

    public function card_managept(){
        if(I('get.card')){
            $where['card']=I('get.card');
        }
        if(I('get.syr')){
            $where['syr']=I('get.syr');
        }
        if(I('get.type')){
            switch (I('get.type')) {
                case '1':
                break;
                case '2':
                $where['shiyong']=array(array('eq',0),array('exp',' is null'), 'or') ;
                break;
                case '3':
                $where['shiyong']=array('eq',1);
                break;
                default:
                break;
            }
        }
       
        $m=M('card_pt');
        $p=getpage($m, $where, 10);
        $user=$m->where($where)->select();
        $this->p=$p->show();
        $this->cardList=$user;
        $this->display();
    }

    public function deleteuser(){
        if(IS_GET){
            $id=I('id');

            if(M('admin')->where(array('id'=>$id))->delete()){

                $this->success("删除成功");
            }else{
                $this->error(C('MSG'));
            }
        }
    }

    public function cancelptcard(){
        if(IS_GET){
            $id=I('id');

            $cardgj = M('card_pt')->where(array('id'=>$id))->find();

            $param = array('shiyong'=>null,'syr'=>null,'jiedai'=>null);
            if(M('card_pt')->where(array('id'=>$id))->save($param)){

                M('admin')->where(array('nickname'=>$cardgj['syr']))->delete();

                $this->success("撤销成功");
            }else{
                $this->error(C('MSG'));
            }
        }
    }

    public function card_managegj(){
        if(I('get.card')){
            $where['card']=I('get.card');
        }
        if(I('get.syr')){
            $where['syr']=I('get.syr');
        }
        if(I('get.type')){
            switch (I('get.type')) {
                case '1':
                break;
                case '2':
                $where['shiyong']=array(array('eq',0),array('exp',' is null'), 'or') ;
                break;
                case '3':
                $where['shiyong']=array('eq',1);
                break;
                default:
                break;
            }
        }
       
        $m=M('card_jp');
        $p=getpage($m, $where, 10);
        $user=$m->where($where)->select();
        $this->p=$p->show();
        $this->cardList=$user;
        $this->display();
    }

    public function cancelgjcard(){
        if(IS_GET){
            $id=I('id');

            $cardgj = M('card_jp')->where(array('id'=>$id))->find();

            $param = array('shiyong'=>null,'syr'=>null,'jiedai'=>null);
            if(M('card_jp')->where(array('id'=>$id))->save($param)){

                $param2 = array('peixun'=>null,'type'=>1);
                M('admin')->where(array('nickname'=>$cardgj['syr']))->save($param2);

                $this->success("撤销成功");
            }else{
                $this->error(C('MSG'));
            }
        }
    }

    /**
	 * [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();
			// }
		}
    }
    /**
	 * [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{
                if('管理员' == $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);
	}
}
