<?php
	session_start();
	
	require_once("db/db.php");	
	require_once("common/common.php");
	
	//设置统一语言格式
	header("Content-type: text/html; charset=gb2312"); 

	$strChainNo=trim($_REQUEST['chainno']);	
	$strBListNo=trim($_REQUEST['rblistno']);
	$strRomListNo=trim($_REQUEST['rmlistno']);
	$strNewPrice1=trim($_REQUEST['newprice']);
	$strChangeText1=trim($_REQUEST['changetxt']);
		
	$strNewPrice= iconv('utf-8','gb2312',$strNewPrice1);
	$strChangeText= iconv('utf-8','gb2312',$strChangeText1);
		
	if(strlen($strBListNo)<1)
	{
		$strBListNo='0';
	}
	if(strlen($strRomListNo)<1)
	{
		$strRomListNo='0';
	}
	
	$strTradeMan=trim($_SESSION['username']);
	
	//1.判断当前客房状态
	$intBillState=0;
	$sql=sprintf("SELECT Room_BillState FROM TR_Order_Room_List WHERE ListNo=%s",$strBListNo);
	$result = querySql($sql);		
	if($row = fetch_array($result))
	{	
		$intBillState=$row['Room_BillState'];
	}
	freeResult($result);
	if($intBillState<>2)
	{
		//该客房为非在住状态，不能继续换房!
		echo "*2*";
		exit;
	}
	
	//2.判断目标房间的状态 状态:0空房,1预订房,2在住房,3钟点房,4自用房,5退客房,6维修房,7锁房
	$intBillState=0;
	$strNewRoomNo='';
	$sql=sprintf("SELECT Room_State,Room_No FROM TR_Room_Inform WHERE ListNo=%s",$strRomListNo);
	$result = querySql($sql);		
	if($row = fetch_array($result))
	{	
		$intBillState=$row['Room_State'];
		$strNewRoomNo=$row['Room_No'];
	}
	freeResult($result);
	if($intBillState<>0)
	{
		//目标房间已被占用，不能继续换房
		echo "*3*";
		exit;
	}
	
	
	//3.开始提交换房确认	
	$sql=sprintf("EXECUTE TR_ChangeRoomSave '%s',%s,'%s','%s',%s,'%s',1",$strChainNo,$strBListNo,$strTradeMan,$strNewRoomNo,$strNewPrice,$strChangeText);
    $result = querySql($sql);
	
	echo '*1*';	//成功

?>


