<?php
if(!defined('APP_PATH')||!defined('I_PATH')){exit('Access Denied');}

class ld_adminuser extends controllerBase
{
	function __construct(){
		parent::__construct();
		$this->cu=syClass('ldi_admin_user');
		$this->glista = syDB('admin_group')->findAll();
		$this->per0=syDB('admin_per')->findAll(array('up'=>0,'no'=>0),' orders desc,pid ');
		$this->per1=syDB('admin_per')->findAll(array('no'=>1),' orders,pid ');
		$this->types=syClass('syclasstype');
		$this->typesdb=$this->types->type_txt();
		if(($this->sysParams('a',1)=='add'||$this->sysParams('a',1)=='edit'||$this->sysParams('a',1)=='edituser')&&$this->sysParams('run')==1){
			if($this->sysParams('pclasstype',2)){$pclasstype=','.implode(',',$this->sysParams('pclasstype',2)).',';}else{$pclasstype='';}
			$this->newrow = array(
				'auser' => $this->sysParams('auser',1),
				'apass' => md5(md5($this->sysParams("apass",1)).$this->sysParams("auser",1)),
				'aname' => $this->sysParams('aname',1),
				'amail' => $this->sysParams('amail',1),
				'atel' => $this->sysParams('atel',1),
				'level' => $this->sysParams('level'),
				'gid' => $this->sysParams('gid'),
				'pclasstype' => $pclasstype,
			);
		}
		if(($this->sysParams('a',1)=='gadd'||$this->sysParams('a',1)=='gedit')&&$this->sysParams('run')==1){
			if($this->sysParams('paction',2)){$paction=','.implode(',',$this->sysParams('paction',2)).',';}else{$paction='';}
			$this->newrow = array(
				'name' => $this->sysParams('name',1),
				'audit' => $this->sysParams('audit'),
				'paction' => $paction,
			);
		}
	}
	function index(){
		$this->toptxt='管理员管理';
		if($_SESSION['auser']['level']==1){
			$this->lists = $this->cu->findAll(null,null,'auid,auser,level,gid');
		}else{
			$this->lists = $this->cu->findAll(array('level'=>0),null,'auid,auser,level');
		}
		$this->display("adminuser.html");
	}
	function add(){
		if ($this->sysParams('run')==1){
			if(!$this->sysParams('apass',1)){message_a("密码不能为空");}
			$newVerifier=$this->cu->syVerifier($this->newrow);
				if(false == $newVerifier){	
					if($this->cu->find(array('auser'=>$this->newrow['auser']))){message_a("用户名名称已经存在");}			
					if($this->cu->create($this->newrow)){
						message_a("管理员创建成功","?c=ld_adminuser");
					}else{message_a("管理员创建失败，请重新提交");}
				}else{message_b($newVerifier);}
		}
		$this->toptxt='添加管理员';
		$this->postgo='add';
		$this->display("adminuser.html");
	}
	function edit(){
		$this->d=$this->cu->find(array('auid'=>$this->sysParams('auid')));
		if($_SESSION['auser']['level']==0 && $this->d['level']==1){message_a("无权操作超级管理员");}
		
		if ($this->sysParams('run')==1){
			$a=$this->newrow;
			if($this->sysParams('apass',1)==''){unset($a['apass']);}
				if($this->cu->update(array('auid'=>$this->d['auid']),$a)){
					message_a("管理员修改成功","?c=ld_adminuser");
				}else{message_a("管理员修改失败,请重新提交");}
		}
		$this->toptxt='修改管理员';
		$this->postgo='edit';
		$this->display("adminuser.html");
	}
	function edituser(){
		$this->d=$this->cu->find(array('auid'=>$_SESSION['auser']['auid']));
		if ($this->sysParams('run')==1){
			$a=$this->newrow;unset($a['auser'],$a['level'],$a['gid'],$a['pclasstype']);
			if($this->sysParams('apass',1)==''){unset($a['apass']);}
				if($this->cu->update(array('auid'=>$this->d['auid']),$a)){
					message_a("会员信息修改成功");
				}else{message_a("会员信息修改失败,请重新提交");}
		}
		$this->toptxt='修改会员信息';
		$this->postgo='edituser';
		$this->display("adminuser.html");
	}
	function del(){
		$this->toptxt='删除管理员';
		$this->delarray=$this->cu->find(array('auid'=>$this->sysParams('auid')));
		if($_SESSION['auser']['level']==0 && $this->delarray['level']==1){message_a("无权操作超级会员");}
		$auid=$this->delarray['auid'];
		if ($this->sysParams('run')==1){
			if($this->cu->delete(array('auid'=>$this->sysParams('auid'))))
			{message_a("管理员删除成功","?c=ld_adminuser");}else{message_a("管理员删除失败,请重新提交");}
		}
		$this->msgtitle='确定要删除管理员 <strong>['.$this->delarray['auser'].']</strong> 吗？';
		$this->msg='';
		$this->msggo='<a href="?c=ld_adminuser&a=del&run=1&auid='.$auid.'">确定删除</a><a href="?c=ld_adminuser">取消操作</a>';
		$this->display("msg.html");
	}
	function glist(){
		$this->toptxt='管理员分组管理';
		$this->lists = syDB('admin_group')->findAll();
		$this->postgo='glist';
		$this->display("adminuser.html");
	}
	function gadd(){
		if ($this->sysParams('run')==1){
			if(!$this->sysParams('name',1)){message_a("分组名称不能为空");}
			if(syDB('admin_group')->create($this->newrow)){
				message_a("管理员分组添加成功","?c=ld_adminuser&a=glist");
			}else{message_a("管理员分组添加失败，请重新提交");}
		}
		$this->toptxt='添加管理员分组';
		$this->postgo='gadd';
		$this->display("adminuser.html");
	}
	function gedit(){
		$this->d=syDB('admin_group')->find(array('gid'=>$this->sysParams('gid')));
		if ($this->sysParams('run')==1){
			if(!$this->sysParams('name',1)){message_a("分组名称不能为空");}
			if(syDB('admin_group')->update(array('gid'=>$this->sysParams('gid')),$this->newrow)){
				message_a("管理员分组修改成功","?c=ld_adminuser&a=glist");
			}else{message_a("管理员分组修改失败，请重新提交");}
		}
		$this->toptxt='修改管理员分组';
		$this->postgo='gedit';
		$this->display("adminuser.html");
	}
	function gdel(){
		$this->toptxt='删除管理员分组';
		$this->delarray=syDB('admin_group')->find(array('gid'=>$this->sysParams('gid')));
		$gid=$this->delarray['gid'];
		if ($this->sysParams('run')==1){
			if(syDB('admin_group')->delete(array('gid'=>$gid)))
			{message_a("管理员分组删除成功","?c=ld_adminuser&a=glist");}else{message_a("管理员分组删除失败,请重新提交");}
		}
		$this->msgtitle='确定要删除管理员分组 <strong>['.$this->delarray['name'].']</strong> 吗？';
		$this->msg='';
		$this->msggo='<a href="?c=ld_adminuser&a=gdel&run=1&gid='.$gid.'">确定删除</a><a href="?c=ld_adminuser&a=glist">取消操作</a>';
		$this->display("msg.html");
	}
}	