<?php
class deptClassAction extends Action
{
	
	public function publicaftersave($table, $cans, $id)
	{
		m('admin')->record(array('deptname'=>$cans['name']), "`deptid`='$id'");
	}
	
	public function getdeptAjax()
	{
		$type 	= $this->request('changetype');
		$val 	= $this->request('val');
		$pid	= 0;
		$rows	= $this->getdept($pid, $type);
		echo json_encode(array(
			'root'	=>'.',
			'children'=>$rows
		));
	}
	
	private function getdept($pid, $type)
	{
		$sql	= $this->stringformat('select `id`,`name` from `?0` where `pid`=?1', array($this->T('dept'), $pid));
		$arr	= $this->db->getall($sql);
		$rows	= array();
		foreach($arr as $k=>$rs){
			$children		= $this->getdept($rs['id'], $type);
			$uchek			= $this->rock->contain($type, 'check');
			if($this->rock->contain($type, 'user')){
				$sql	= $this->stringformat('select `id`,`name`,`gender`,`ranking`,`deptname` from `?0` where `deptid`=?1 and `status`=1 and `state`<>5 order by `sort`', array($this->T('admin'), $rs['id']));			
				$usarr	= $this->db->getall($sql);
				foreach($usarr as $k1=>$urs){
					$usarr[$k1]['leaf'] = true;
					$usarr[$k1]['uid']  = $urs['id'];
					$usarr[$k1]['id']   = 'u'.$urs['id'];
					$usarr[$k1]['type'] = 'u';
					$usarr[$k1]['icon'] = 'mode/icons/user.png';
					if($urs['gender']=='女')$usarr[$k1]['icon'] = 'mode/icons/user_female.png';
					if($uchek)$usarr[$k1]['checked']=false;
				}
				$children= array_merge($children, $usarr);
			}
			if($this->rock->contain($type, 'dept')){
				if($uchek)$ars['checked']=false;
			}
			$ars['children']=$children;
			$ars['name'] 	= $rs['name'];
			$ars['id'] 		= 'd'.$rs['id'];
			$ars['did'] 	= $rs['id'];
			$ars['type'] 	= 'd';
			$rows[]			= $ars;
		}
		return $rows;
	}
	
}