<?php
/*
	[Office 515158] (C) 2009-2012 天生创想 Inc.
	$Id: mod_department 1209087 2012-01-08 08:58:28Z baiwei.jiang $
*/

(!defined('IN_TOA') || !defined('IN_ADMIN')) && exit('Access Denied!');
get_key("department_");
empty($do) && $do = 'list';
if ($do == 'list') {
	include_once('template/department.php');

}elseif ($do == 'add') {
	if($_POST['view']!=''){
		$did = getGP('id','P','int');
		$name = check_str(getGP('name','P'));
		if($did!=''){
			$persno=getGP('persnoid','P','int');
			$persno1=getGP('persno1id','P','int');
			$persno2=getGP('persno2id','P','int');
			$persno3=getGP('persno3id','P','int');
			$department = array(
				'name' => $name,
				'persno' => $persno,
				'persno1' => $persno1,
				'persno2' => $persno2,
				'persno3' => $persno3
			);
			update_db('department',$department, array('id' => $did));
		}else{
			$father=getGP('fatherid','P','int');
			if(getGP('fathers','P','int')==1){
				$father=0;
			}
			$persno=getGP('addpersnoid','P','int');
			$persno1=getGP('addpersno1id','P','int');
			$persno2=getGP('addpersno2id','P','int');
			$persno3=getGP('addpersno3id','P','int');
			$department = array(
				'name' => $name,
				'persno' => $persno,
				'persno1' => $persno1,
				'persno2' => $persno2,
				'persno3' => $persno3,
				'father' => $father,
				'jituanid' =>$_USER->jituanid
			);
			insert_db('department',$department);
		}
		show_msg('部门信息操作成功！', 'admin.php?ac='.$ac.'&fileurl='.$fileurl.'&datte='.get_date('YmdHis',PHP_TIME));
	}else{
		$did = getGP('did','G','int');
		if($did!=''){
			$user = $db->fetch_one_array("SELECT * FROM ".DB_TABLEPRE."department  WHERE did = '$did' and jituanid='".$_USER->jituanid."'");
			$_title['name']='编辑';
		}else{ 
			$_title['name']='发布';
		}
		include_once('template/departmentadd.php');
	}
}elseif ($do== 'update') {
	if(getGP('depid','G','int')==''){
		echo 'id';
		exit;
	}
	$rsfno = $db->fetch_one_array("SELECT * FROM ".DB_TABLEPRE."department where father='".getGP('depid','G','int')."' and jituanid='".$_USER->jituanid."' ORDER BY id asc limit 0,1");
	if($rsfno['id']!=''){
		echo 'false';
		exit;
	}
	$db->query("DELETE FROM ".DB_TABLEPRE."department WHERE id = '".getGP('depid','G','int')."' and jituanid='".$_USER->jituanid."'");
	echo 'true';
	exit;
	
}elseif($do=='ajax'){
	$rsfno = $db->fetch_one_array("SELECT * FROM ".DB_TABLEPRE."department where id='".$_GET['depid']."' and jituanid='".$_USER->jituanid."' ORDER BY id asc limit 0,1");
	if($rsfno['id']!=''){
		echo $rsfno['name'].','.$rsfno['persno'].','.$rsfno['persno1'].','.$rsfno['persno2'].','.$rsfno['persno3'].','.get_realname($rsfno['persno']).','.get_realname($rsfno['persno1']).','.get_realname($rsfno['persno2']).','.get_realname($rsfno['persno3']);
	}
	exit;
}
function _dep_user($father){
	global $db;
	global $_USER;
	if($father!=''){
		$query = $db->query("SELECT name,id FROM ".DB_TABLEPRE."department where father='".$father."' and jituanid='".$_USER->jituanid."' ORDER BY id Asc");
		while ($row = $db->fetch_array($query)) {
			echo ','.chr(13).chr(10).'{ id:'.trim($row['id']).', pId:'.$father.', name:"'.trim($row['name']).'"}';
			$rsfno = $db->fetch_one_array("SELECT id FROM ".DB_TABLEPRE."department where father='".$row['id']."' and jituanid='".$_USER->jituanid."' ORDER BY id asc limit 0,1");
			if($rsfno['id']!=''){
				_dep_user(trim($row['id']));
			}
		}
	}
}?>