<?php
class adminControl extends skymvc{
	
	function __construct(){
		parent::__construct();
		$this->loadModel(array("admin","login","admin_group"));
		$this->login->checkAdminLogin();
	}
	
	public function onDefault(){
		$this->onList();
	}
	
	public function	onAdd(){
			if(!$_SESSION['ssadmin']) $this->gomsg("您没有权限");
			$id=intval(get_post('id'));
			if($id){
				$this->smarty->assign("data",$this->admin->getRow("SELECT * FROM ".table('admin')." where id='$id'  "));	
			}
			$grouplist=$this->admin_group->select(array());
			$this->smarty->assign(
				array(
					"grouplist"=>$grouplist,
				)
			);
			$this->smarty->display("admin/admin_reg.html");
	}
	public function onRegdb(){
		$username=post('username','h');
		$password=post('password','h');
		$email=post('email','h');
		$password2=post('password2','h');
		if($password!=$password2){
			$this->gomsg($this->lang['password_neq']);
		}
		if(!is_email($email)){
			$this->gomsg($this->lang['email_error']);
		}
		
		if($this->admin->selectRow(array("where"=>array('username'=>$username)))){
			$this->gomsg($this->lang['admin_is_exist']);
		}
		$data['username']=$username;
		$data['email']=$email;
		$data['salt']=$salt=rand(1000,9999);
		$data['password']=umd5($password.$salt);
		$data['group_id']=post('group_id');
		$data['siteid']=intval($_SESSION['ssadmin']['siteid']);
		$this->admin->insert($data);
		$this->gomsg($this->lang['admin_add_success']);
	}
	
	public function onEdit(){
		$id=get_post('id','i');
		$data=$this->admin->selectRow(array("where"=>array("id"=>$id)));
		if(empty($data)) $this->gomsg($this->lang['admin_no_exist'],APPADMIN."?m=admin");
		if(get('op')=='db'){
			$password=post('password');
			$password2=post('password2');
			$group_id=post('group_id','i');
			if($password){
				if($password!=$password2){
					$this->gomsg($this->lang['password_neq']);					
				}
				$newdata['password']=umd5($password.$data['salt']);
			}
			$newdata['group_id']=$group_id;
			$this->admin->update($newdata,array("id"=>$id));
			$this->gomsg($this->lang['edit_success']);
		}
		
		$grouplist=$this->admin_group->select(array());
		$this->smarty->assign(
			array(
				"grouplist"=>$grouplist,
				"data"=>$data,
			)
		);
		
		$this->smarty->display("admin/admin_edit.html");
	}
		
	public function ondel(){
			$id=intval(get('id'));
			if(!$_SESSION['ssadmin']) $this->gomsg("您没有权限");
			$this->admin->delete("   id='$id' ");
			$this->gourl();
	}
	public function	onList(){
			$grouplist=$this->admin_group->grouplist();
			$data=$this->admin->select(array("where"=>$where));
			$this->smarty->assign(
				array(
					"adminlist"=>$data,
					"grouplist"=>$grouplist
				)
			);
			$this->smarty->display("admin/admin_list.html");
	}
	
	
	 
		
	
}

?>