<?php
class adminClassAction extends Action
{
	
	public function publicstoreAfter($table, $rows)
	{
		$group	= m('group')->getall('1=1 order by `sort`', '`id`,`name`');
		return array('group'=>$group);
	}

	public function publicbeforesave($table, $cans, $id)
	{
		$user 		= $cans['user'];
		$groupid 	= $cans['groupname'];
		$msg  = '';	
		if(m($table)->rows("`user`='$user' and `id`<>'$id'")>0)$msg ='用户名['.$user.']已存在';
		$rows = array();
		
		if($msg == ''){
			$did  = $cans['deptid'];
			$sup  = $cans['superid'];
			
			$rows = $this->getuninf($did, $sup);
		}
		$rows['groupname'] = $this->rock->post('groupnamess');
		$arr = array('msg'=>$msg,'rows'=>$rows);
		return $arr;
	}
	
	public function publicaftersave($table, $cans, $id)
	{
		m($table)->record(array('superman'=>$cans['name']), "`superid`='$id'");
		$this->savegroup($id, $this->rock->post('groupnamePost'));
	}
	
	private function getuninf($did, $sup)
	{
		$deptpath 	= $this->db->getpval($this->T('dept'), 'pid', 'id', $did, '],[');
		$deptname	= $this->db->getmou($this->T('dept'), 'name', "`id`='$did'");
		$supername	= '';
		
		$superpath	= '';
		if(!$this->rock->isempt($sup)){
			$sua = explode(',', $sup);
			foreach($sua as $suas){
				$sss1 	= $this->db->getpval($this->T('admin'), 'superid', 'id' ,$suas, '],[');
				if($sss1 != '')$superpath.=',['.$sss1.']';
				$sss2	= $this->db->getmou($this->T('admin'), 'name', "`id`='$suas'");
				if(!$this->rock->isempt($sss2))$supername.=','.$sss2;
			}
			if($superpath!='')$superpath=substr($superpath,1);
			if($supername!='')$supername=substr($supername,1);
		}
		$rows['deptpath'] 	= $this->rock->strformat('[?0]', $deptpath);
		$rows['superpath'] 	= $superpath;
		$rows['deptname'] 	= $deptname;
		$rows['superman'] 	= $supername;
		
		return $rows;
	}
	
	public function updatedataAjax()
	{
		$rows	= $this->db->getall("select id,name,deptid,superid,deptpath,superpath,deptname,superman,groupname from `[Q]admin` where `status`=1 order by `sort`");
		$total	= $this->db->count;
		$cl		= 0;
		foreach($rows as $k=>$rs){
			$nrs	= $this->getuninf($rs['deptid'], $rs['superid']);
			if($nrs['deptpath'] != $rs['deptpath'] || $nrs['deptname'] != $rs['deptname'] || $nrs['superpath'] != $rs['superpath'] || $nrs['superman'] != $rs['superman']){
				$this->db->record($this->T('admin'), $nrs, "`id`='".$rs['id']."'");
				$cl++;
			}
		}
		echo '总'.$total.'条记录,更新了'.$cl.'条';
	}
	
	
	
	public function getugroupAjax()
	{
		$id	= (int)$this->rock->request('id');
		$ugs	= '[0]';
		$rows	= m('sjoin')->getall("`type`='ug' and `mid`='$id'",'sid');
		foreach($rows as $k=>$rs)$ugs.=',['.$rs['sid'].']';
		echo $ugs;
	}
	
	public function savegroupAjax()
	{
		$id		= (int)$this->rock->post('id');
		$s		= $this->rock->post('s');
		$this->savegroup($id, $s);
	}
	
	private function savegroup($id, $s)
	{
		$this->db->delete($this->T('sjoin'), "`type`='ug' and `mid`='$id'");
		if(!$this->rock->isempt($s))$this->db->insert($this->T('sjoin'), '`type`,`mid`,`sid`,`indate`', "select 'ug','$id',`id`,'$this->now' from `[Q]group` where `id` in($s)", true);
	}
	
}