<?php
	session_start();
	ini_set('session.gc_maxlifetime', 99999999); //设置时间 永不过期
	
	require_once("db/db.php");	
	require_once("common/common.php");
				
	$strRoomNo=trim($_REQUEST["rmid"]);
	$intDoTypeID=$_REQUEST["doid"];
	$strWXEndDay=$_REQUEST["wxendday"];
	$strWeiXiuText=$_REQUEST["wxtext"];
	
	$strUserName=trim($_SESSION['usernamefw']);
	$strChainNo=trim($_SESSION["ChainNofw"]);
	
	//$strWeiXiuText=iconv('utf-8','gb2312',$strWeiXiuText1);
	//$strUserName=iconv('utf-8','gb2312',$strUserName1);
	
	$strRetID="0";
	
	if($intDoTypeID==2)
	{
		//设置维修房完成
		$lngWXListNo=0;
		
		//1.先判断当前是否维修房
		$sql=sprintf("SELECT ListNo FROM TR_Room_Fault_List WHERE Chain_No='%s' And Room_No='%s' And BillState=0",$strChainNo,$strRoomNo);
		$result = querySql($sql);
		if($row = fetch_array($result))
		{
			$lngWXListNo=$row['ListNo'];
		}
		freeResult($result);
		
		if($lngWXListNo>0)
		{
			$sql=sprintf("UPDATE TR_Room_Fault_List SET BillState=1,EndTime=GetDate() WHERE Chain_No='%s' And Room_No='%s' And BillState=0",$strChainNo,$strRoomNo);
			executeSql($sql);
			//$result = querySql($sql);
			//freeResult($result);

			$sql=sprintf("EXECUTE TR_SetRoomState '%s',5,0,'%s','','%s','维修完成',0,1",$strRoomNo,$strUserName,$strChainNo);
			executeSql($sql);
			//$result = querySql($sql);
			//freeResult($result);
		}
		$strRetID="1";	//成功
	}
	else if($intDoTypeID==1)
	{
		//更改维修房内容
		//1.判断当前是否是维修房
		$lngWXListNo=0;
		
		$sql=sprintf("SELECT ListNo FROM TR_Room_Fault_List WHERE Chain_No='%s' And Room_No='%s' And BillState=0",$strChainNo,$strRoomNo);
		$result = querySql($sql);
		if($row = fetch_array($result))
		{
			$lngWXListNo=$row['ListNo'];
		}
		freeResult($result);
		
		if($lngWXListNo>0)
		{
			//2.如果是修改维修内容
			$sql=sprintf("UPDATE TR_Room_Fault_List SET EndTime='%s',Remarks='%s' WHERE ListNo=%s",$strWXEndDay,$strWeiXiuText,$lngWXListNo);
			executeSql($sql);
		}
		else
		{
			//3.如果是新建维修房
			//3.1.得到当前客房的状态
			$intOldRoomState=0;
			$strRoomType='';
			$sql=sprintf("SELECT a.Room_State,b.Room_Type FROM TR_Room_Inform a,TR_Room_Type b WHERE a.TypeListNo=b.ListNo And a.Room_No='%s' AND b.Chain_No='%s'",$strRoomNo,$strChainNo);
			$result = querySql($sql);
			if($row = fetch_array($result))
			{
				$intOldRoomState=$row['Room_State'];
				$strRoomType=$row['Room_Type'];
			}
			freeResult($result);
			
			//3.2.开始添加维修计划
			$sql=sprintf("INSERT INTO TR_Room_Fault_List(Chain_No,TradeTime,TradeMan,Room_No,Room_Type,StartTime,EndTime,BillState,OldRoomState,Remarks)
						VALUES('%s',GETDATE(),'%s','%s','%s',GetDate(),'%s',0,%s,'%s')"
						,$strChainNo,$strUserName,$strRoomNo,$strRoomType,$strWXEndDay,$intOldRoomState,$strWeiXiuText);
			if(executeSql($sql))
			{
				//3.3.更改客房状态
				$strWXRemarks=iconv('utf-8','gb2312','设置维修房');
				$sql=sprintf("EXECUTE TR_SetRoomState '%s',6,2,'%s','','%s','%s',0,0",$strRoomNo,$strUserName,$strChainNo,$strWXRemarks);
				executeSql($sql);
			}
		}
				
		$strRetID="1";	//成功
	}
	
	closeDb();
	echo $strRetID;

?>


