<?php
$dir=realpath(dirname(__FILE__)."/../");
require("{$dir}/webini.inc.php");
extract($_POST);
if(!isset($action) || !isset($mod) || $mod!='office'){
	die("Access Denied");
}
if($action=='editOffice'){//显示编辑部门界面
	if(!isset($officeId) || !is_numeric($officeId)){
		echo"参数错误";
	}else{
		$sql="select * from office where id={$officeId}";
		$row=$db->myQuery($sql,false);
		if(count($row)==0){
			echo"指定的部门不存在";
		}else{
			$row=$row[0];
			rc_require("editoffice.html",$mod);
		}
	}
}else if($action=='saveEditOffice'){
	if(!is_numeric($officeId) || isNull($officeName) || !is_numeric($parId)){
		echo"参数错误";
	}else{
		$office=md::findOffice(0,$officeId,false,false);
		if($office==false){
			echo"部门不存在";
		}else{
			$sql="select * from office where name='{$officeName}' and comId={$office[0]['comId']} and id<>{$officeId}";
			$row=$db->myQuery($sql,false);
			if(count($row)==0){
				$memo=str_replace(array("\r\n","\r","\n"),"<br />",$memo);
				$sql="update office set name='{$officeName}',parId={$parId},memory='{$memo}' where id={$officeId}";
				if($db->execSql($sql,false)==true){
					if($office[0]['parId']!==$parId){
						$resetSql="update office set sonSum=sonSum-1 where id={$office[0]['parId']};update office set sonSum=sonSum+1 where id={$parId}";
						$db->execSql($resetSql,false);
					}
					md::cacheOffice($office[0]['comId']);
					echo"新的数据已经成功保存";
				}else{
					echo "保存失败，请重试";
				}
			}else{
				echo"新的部门名称已经存在，请更改后保存！";
			}
		}
	}
}else if($action=='deleteOffice'){
	$result=true;
	if(!is_numeric($officeId)){
		echo'参数错误';
	}else{
		$office=md::findOffice($_SESSION['comId'],$officeId,false,false);
		if($office==false){
			echo"部门不存在";
		}else{
			$sql="delete from office where id={$officeId}";
			$result=$db->execSql($sql,false);
			if($result==false){
				echo"删除失败，请重试。";
			}else{
				if($office[0]['parId']!=0){
					$sql="update office set sonSum=sonSum-1 where id={$office[0]['parId']}";
					$db->execSql($sql,false);
				}
				$son=md::findSon($office[0]['comId'],$office[0]['id'],false,true);
				if(is_array($son) && count($son)){
					$idList=implode(" or ",$son);
					if($office[0]['parId']==0){
						$sql="update office set parId=0 where {$idList}";
					}else{
						$sql="update office set parId={$office[0]['parId']} where {$idList}";
					}
					$db->execSql($sql,false);
				}
				md::cacheOffice($office[0]['comId']);
				echo"已经成功删除指定的部门";
			}
		}
	}
}else if($action=='saveAddOffice'){
	$result=true;
	if(md::findCompany($_SESSION['comId'])==false){
		die("未给您分配所在的公司，或您所在的公司已经被删除，请联系管理员。");
	}else{
		$officeName=str_replace(array(" ","　"),"",$officeName);
		$parOffice=(!is_numeric($parOffice) || (int)$parOffice<1)?0:(int)$parOffice;
		$memory=mb_substr(str_replace(array("\r\n","\r","\n"),"<br />",$memory),0,200,"utf-8");
		$office=md::findOffice($_SESSION['comId'],0,false,false);
		if($office!=false){
			foreach($office as $key=>$v){
				if($v['name']==$officeName){
					die("系统中已经存在指定的部门名称[{$officeName}]，请使用其它名称！");
				}
			}
			if($parOffice>0 && md::findOffice($_SESSION['comId'],$parOffice,false,false)===false){
				die("指定的上级部门不存在，请检查并修改后重新提交！0");
			}
		}else if($parOffice!=0){
			die("指定的上级部门不存在，请检查并修改后重新提交！");
		}
		$sql="insert into office(comId,parId,sonSum,name,memory,addUser,ip) values({$_SESSION['comId']},{$parOffice},0,'{$officeName}','{$memory}','{$_SESSION['userNick']}','{$cg['ip']}')";
		$result=$db->execSql($sql,false);
		if($result===false){
			die("保存失败，请重试！");
		}else{
			if($parOffice!=0){
				$sql="update office set sonSum=sonSum+1 where id={$parOffice}";
				$db->execSql($sql,false);
			}
		}
		md::cacheOffice($_SESSION['comId']);
		echo "新的部门已经成功保存";
	}
}
?>