<?php
/**
 * 公众号管理
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
 
namespace Admin\Controller;
class MemberPublicController extends BaseController{
    public function _initialize(){
		parent::_initialize();
        $this->wx_type=strtoarray('1:订阅号,2:认证订阅号,3:服务号,4:认证服务号');
    }
    public function _before_index(){
        if(!IS_ROOT){
            $_GET['uid']=$this->admin_info['id'];
        }
    }
	public function index(){
        if(!IS_ROOT){
			$map['adminid'] = $this->admin_info['id'];
        	$list = M('MemberPublic')->where($map)->field(true)->select(); 
		}else{
			$list = M('MemberPublic')->field(true)->select();	
		}
		$this->assign ( 'list', $list );
		$this->display ();
    }
    public function _before_add(){
        $this->checkpublicnum();
    }
    public function _before_insert(){
        $this->checkpublicnum();
    }
    public function _before_update(){
        if($_POST['adminid']!=$this->admin_info['id'])$this->error(L('DO_ERROR'));
    }
    public function change(){
        $id=I('id');
        if(empty($id))$this->error(L('DO_ERROR'));
        $map['id']=$id;
        $map['adminid']=$this->admin_info['id'];
        $mod=M('MemberPublic');
        $data['status']=1;
		$map['updatetime']=time();
        $result=$mod->where($map)->save($data);
        
        $map['id']=array('neq',$id);
        $data['status']=0;
        $mod->where($map)->save($data);
        if($result!==false){
            get_token();
            $this->success(L('DO_OK'));
        }else{
            $this->error(L('DO_ERROR'));
        }
        
    }
    private function checkpublicnum(){        
        $token=I('post.token');
        if(!empty($token)){
            $map=array();
            $map['token']=$token;
			$num=M('MemberPublic')->where($map)->count();
            if($num)$this->error('token重复请重新添加!');
        }        
        if($this->admin_info['id']==1)return true;
        $map=array();
        $map['adminid']=$this->admin_info['id'];
        $num=M('MemberPublic')->where($map)->count();
        $limit_num=C('WX_PUBLIC_NUM');
        if($num>=$limit_num){
            $this->error('每个用户只能添加'.$limit_num.'个公众号!');
        }
    }
	public function search(){
		$token=I('token');
		$map['sn'] = I('sn');
		$list = M('SnCode')->where($map)->field(true)->select(); 
		$this->assign ( 'list', $list );
        $this->assign ( 'token', $token );
     	$this->display ();  
    }
	function set_use() {
		$map ['id'] = I ( 'id' );
		$map ['token'] = I ( 'token' );
		$data = M ( 'SnCode' )->where ( $map )->find ();
		if (! $data) {
			$this->error ( '数据不存在' );
		}
		if ($data ['is_use']) {
			$data ['is_use'] = 0;
			$data ['use_time'] = '';
		} else {
			$data ['is_use'] = 1;
			$data ['use_time'] = time ();
		}
		$res = M ( 'SnCode' )->where ( $map )->save ( $data );
		if ($res) {
			$this->success ( '设置成功',U('Admin/MemberPublic/search',array('sn'=>$data['sn'])) );
		} else {
			$this->error ( '设置失败' );
		}
	}
}