<?php
	session_start();
	header("Content-type:text/html;charset=gb2312");
	//判断是否登录系统		
	if($_SESSION["main_viploginedsk"]!="1" |$_SESSION["main_vipnosk"]=="")
	{
		//没有登录开始启动登录
		require_once("logina.php");	
	}
?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="gb2312">	
	<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
	<meta name="apple-mobile-web-app-capable" content="1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="format-detection" content="telephone=0">
	<meta name="apple-touch-fullscreen" content="1">

	<title>微信订房</title>
	
    <link rel="stylesheet" href="css/mycss.css"> 
    <link rel="stylesheet" href="jsp5/jquery.mobile-1.4.5.min.css">
    
	<script src="jsp5/jquery.js"></script>
  	<script src="jsp5/jquery.mobile-1.4.5.min.js"></script>
    <script src="jsp5/jquery-1.8.3.min.js"></script>

	<!--<link rel="stylesheet" href="jsp5/jquery.mobile-1.4.5.min.css">-->
	<!--<script src="jsp4/jquery.mobile-1.4.3.min.js"></script>-->
	
	<script language="JavaScript"> 
		//不允许进行后退
		javascript:window.history.forward(0); 
	</script>
    
</head>

<script>	
	//隐藏维信右上角菜单
	function onBridgeReady(){
		WeixinJSBridge.call('hideOptionMenu');
	}

	if (typeof WeixinJSBridge == "undefined")
	{
		if( document.addEventListener )
		{
			document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
		}
		else if (document.attachEvent)
		{
			document.attachEvent('WeixinJSBridgeReady', onBridgeReady); 
			document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
		}
	}
	else
	{
		onBridgeReady();
	}
</script>

<?php
	require_once("db/db.php");	
	require_once("common/common.php");
	require_once("../Wx.Config.php");	//只加载一次
	
	$strret=trim($_REQUEST['stext']);
	$strorderno=trim($_REQUEST['tbno']);	//订单号
	
	//得到该订单的订房数量和房型以及已选房数量
		
	$strdoid=trim($_REQUEST['doid']);		//1是历史订单,否则我的订单
	
	$strsitedchainno='';
	
	$sql=sprintf("SELECT Convert(varchar(10),a.CheckInTime,120)as checkintime,Convert(varchar(10),a.CheckOutTime,120)as checkouttime,
					b.Room_Type,b.Room_Count,a.CheckDays,Convert(varchar(5),a.WillTime,8)as willtime,a.Chain_No,
					a.Order_State,DATEPART(weekday,a.CheckInTime)AS weekday1,DATEPART(weekday,a.CheckOutTime) AS weekday2 FROM TR_Order_Inform a,
					TR_Book_RoomType b WHERE a.Order_No=b.Order_No AND a.Order_No='%s';",$strorderno);
				
	$result = querySql($sql);
	$row = fetch_array($result);

	$strcheckinday = $row['checkintime'];			//入住时间
	$strcheckoutday = $row['checkouttime'];			//离店日期
	$intcheckdays = $row['CheckDays'];				//住房天数
	$strwilltime = $row['willtime'];				//保留时间
	$strroomtype = $row['Room_Type'];				//房型
	$introomcount = $row['Room_Count'];				//订房数
	$strsitedchainno = trim($row['Chain_No']);			//入住分店
	
	//$intweekday1 = $row['weekday1'];
	//$intweekday2 = $row['weekday2'];
	$strremarks = $row['Remarks'];
	$intorderstate = $row['Order_State'];		//预订0,入住1,取消2,完成3
	
	$fltallval=$introomcount * $fltroomprice;
	
	//释放数据库
	freeResult($result);	
	//关闭数据库
	//closeDb();	
				
	//得到已选房数量	
	$introomselcount=0;	//已选房数量
	$sql=sprintf("SELECT COUNT(ListNo)AS SelCount FROM TR_Order_Room_List WHERE Order_No='%s'",$strorderno);
	$result = querySql($sql);
	$row = fetch_array($result);
	$introomselcount = trim($row['SelCount']);
	
	freeResult($result);
								
	//关闭数据库
	closeDb();	
	
	//本次可以选择房间数
	$intNowCanSelCount = $introomcount - $introomselcount;
	
?>

<body leftmargin="0" rightmargin="0" topmargin="0" bottommargin="0">
<!--第一个页面的内容实现---------------------------->
<section data-role="page" id="hispage1">
	<header data-role="header" data-position="fixed" data-tap-toggle="false" style="border-top-width:0px;border-bottom-width:0px;">
        <table width="100%" height="45" border="0" cellpadding="0" cellspacing="0" bgcolor="<?php echo WxMainConfig::colHeadBkColorBook;?>">
		   	<tr>
				<td height="45" width="30" align="center"></td>
				<td height="45" align="center" valign="middle">
	                <span id="MsgTxt" style="color:<?php echo WxMainConfig::colHeadTextColorBook;?>;font: 12px/1.5 '微软雅黑',tahoma, Srial, helvetica, sans-serif; font-size:19px;text-shadow:none;">自助选房</span>
               	</td>
				<td height="45" width="30" valign="middle"></td>
		  	</tr>
		</table>
	</header>
		
	<article data-role="none">
		<table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#ffffff">
			<tr>
				<td height="4" width="15"></td>
				<td height="4" align="left"></td>
				<td height="4" width="15"></td>
			</tr>
			
            <?php
            	
				$strselroomno='';
				$strrlistno='0';
				$introomstate=0;
				$introwindex=0;
				$strroomstate='';
				$intfloor=0;
				
				$sql=sprintf("SELECT a.ListNo,b.Room_No,Room_State,b.Room_Floor FROM dbo.TRF_GetFreeRoomNoList('%s','%s','%s','%s')a,TR_Room_Inform b,TR_Room_Type c
								WHERE b.ListNo=a.ListNo AND b.TypeListNo=c.ListNo And c.Chain_No='%s'",$strsitedchainno,$strcheckinday,$strcheckoutday,$strroomtype,$strsitedchainno);

				$result = querySql($sql);
											
				while($row = fetch_array($result))
				{
					$strselroomno=$row['Room_No'];
					$strrlistno=$row['ListNo'];
					$introomstate=$row['Room_State'];
					$intfloor=$row['Room_Floor'];
			
					$introwindex = $introwindex + 1;
					
					$strroomstate='';
					//状态:0空房,1预订房,2在住房,3钟点房,4自用房,5退客房,6维修房,7锁房
					switch($introomstate)
					{
					  case 1:
					  	$strroomstate='当前状态：预订排房';
					    break;
					  case 2:
					  	$strroomstate='当前状态：客人在住';
						break;
					  case 3:
					  	$strroomstate='当前状态：钟点房在住';
						break;
					  case 4:
					  	$strroomstate='当前状态：内部用房';
						break;
					  case 6:
					  	$strroomstate='当前状态：客房维修中';
						break;
					  case 7:
					  	$strroomstate='当前状态：临时保留';
						break;
					}

					
			?>
			
            <tr>
				<td height="50" width="15"></td>
				<td height="50" align="left">
                	
                    <!---显示房间信息列表--> 
                    <div id="cmdroomsuit" onClick="myroomclick(<?php echo $introwindex;?>);" >
                        <table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                                <td height="49" width="90"> 
                                    <span style="color: #000;font: 12px/1.5 '微软雅黑',tahoma, Srial, helvetica, sans-serif; font-size:18px;"><?php echo $strselroomno;?></span> 
           							<input id="check<?php echo $introwindex;?>" name="check<?php echo $introwindex;?>" value="0" type="hidden" />
           							<input id="roomno<?php echo $introwindex;?>" name="roomno<?php echo $introwindex;?>" value="<?php echo $strselroomno;?>" type="hidden" />
           							<input id="selpos<?php echo $introwindex;?>" name="selpos<?php echo $introwindex;?>" value="0" type="hidden" />
                                    
                                </td>
                                <td height="49"> 
									<table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">
                                    	<tr><td width="100%" height="12%"></td></tr>
                                    	<tr><td width="100%" height="35%">
                                        	<span style="color: #D7C4C4;font: 12px/1.5 '微软雅黑',tahoma, Srial, helvetica, sans-serif; font-size:12px;">所属楼层：<?php echo $intfloor;?>层</span>
                                         	</td></tr>
                                    	<tr><td width="100%" height="38%">
                                        	<span style="color: #D7C4C4;font: 12px/1.5 '微软雅黑',tahoma, Srial, helvetica, sans-serif; font-size:12px;"><?php echo $strroomstate;?></span> 
                                            </td></tr>
                                       	<tr><td width="100%" height="15%"></td></tr>
                                    </table>
                                </td>
                                <td height="49" width="40" valign="middle"> 
                                	<img id="imgroom<?php echo $introwindex;?>" style="width:35px;height:35px;border:0px; padding-top:5px;" src="images/check0.png" />
                                </td>
                            </tr>
                            
                            <tr>
                                <td height="1" width="90" bgcolor="#F2E2E2"></td>
	                            <td height="1" bgcolor="#F2E2E2"></td>
                                <td height="1" width="40" bgcolor="#F2E2E2"></td>
                            </tr>
                        </table>
                    </div>
                </td>
				<td height="50" width="15"></td>
			</tr>
            
            <?php 
				}
				
				//释放数据库
				freeResult($result);	
				//关闭数据库
				closeDb();	
			?>
			<tr>
				<td height="1" width="15" bgcolor="#F2E2E2"></td>
				<td height="1" align="left" bgcolor="#F2E2E2"></td>
				<td height="1" width="15" bgcolor="#F2E2E2"></td>
			</tr>
		</table>
			
	</article>
    
	<footer data-role="footer" data-position="fixed" data-tap-toggle="false" style="border-top:0px solid #F2E2E2;border-bottom:0px;border-left:0px solid #F2E2E2;border-right:0px solid #F2E2E2;">
		<table width="100%" height="10" border="0" cellpadding="0" cellspacing="0" bgcolor="#ffffff">
        	<tr height="1" valign="top">
            	<td height="1" width="50%" bgcolor="#F2E2E2"></td>
                <td height="1" width="50%" bgcolor="#F2E2E2"></td>
            </tr>
            
            <tr height="35" valign="top" bgcolor="#F4EFEF">
            	<td height="35" valign="middle" colspan="2">
                	<table width="100%" border="0" cellpadding="0" cellspacing="0">
                    	<tr>
                        	<td height="35" width="50%" valign="middle">
                            	<span style="color:#CD0013 ;font: 12px/1.5 '微软雅黑',tahoma, Srial, helvetica, sans-serif; font-size:14px;"> &nbsp; &nbsp; 预订 <?php echo $introomcount;?> 间,已选房 <?php echo $introomselcount;?> 间</span>
                            </td>
                        	<td height="35" width="20%" valign="middle">
                              	<label id="label1" style="color: #AA9595;font: 12px/1.5 '微软雅黑',tahoma, Srial, helvetica, sans-serif; font-size:12px;"> 已选 0 间</label>
                            </td>
							<td height="35" width="30%" valign="middle">
								<label id="label2" style="color: #AA9595;font: 12px/1.5 '微软雅黑',tahoma, Srial, helvetica, sans-serif; font-size:12px;">还有 <?php echo $intNowCanSelCount;?> 间未选</label>
                            </td>
                         </tr>
                    </table>
                </td>
            </tr>
            
            <tr height="1" valign="top">
            	<td height="1" width="50%" bgcolor="#F2E2E2"></td>
                <td height="1" width="50%" bgcolor="#F2E2E2"></td>
            </tr>
            
			<tr>
				<td height="100%" width="50%" bgcolor="#ffffff">
						<div onClick="window.location = 'orderview.php?tbno=<?php echo $strorderno; ?>&doid=0';" style="width:100%; height:45px; background-color:#D7AA28;">
                            <table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
                                <tr><td width="100%" height="100%" align="center">
                                    <span style="color:#fff ;font: 12px/1.5 '微软雅黑',tahoma, Srial, helvetica, sans-serif; font-size:16px;text-shadow:none;">返 回</span>                            	
                                </td></tr>
                            </table>
                        </div>				
				</td>
				<td height="100%"  width="50%">
                	<!--<a href="#popupParis2" data-rel="popup" data-position-to="window" data-transition="fade">-->
                        <div id="bmtSendOk" onClick="bmtSendOkClick()" style="width:100%; height:45px; background-color:#CD0013;">
                            <table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
                                <tr><td width="100%" height="100%" align="center">
                                    <span style="color:#fff ;font: 12px/1.5 '微软雅黑',tahoma, Srial, helvetica, sans-serif; font-size:16px;text-shadow:none;">确定选房</span>                            	
                                </td></tr>
                            </table>
                     	</div>
              		<!--</a>-->
				</td>
			</tr>
		</table>						
	</footer>
    
    
    
    <script type="text/javascript">
		//订房总数(本次允许选择的房间数）
		var intBookRoomCount=<?php echo $intNowCanSelCount;?>;
		//已选择房数
		var intHadSelCount=0;
		var intAllRoomCount=<?php echo $introwindex;?>;
		
		//点击房间时自动显示图片的方式	
		function myroomclick(rmid)
		{
			if(intBookRoomCount<1)
			{
				return;
			}
			var imgname = 'imgroom' + rmid.toString();
			var checkname = '#check' + rmid.toString();
			var checkpos = '#selpos' + rmid.toString();
			
			//得到当前选择的状态
			var checked = $(checkname).val();
			
			//alert(checked.toString());
			
			//得到图ID
			var img = document.getElementById(imgname);
			
			if(checked==0)
			{
				img.src="images/check1.png";
				$(checkname).val(1);
								
				//判断是否大于了订房数
				if(intHadSelCount>=intBookRoomCount)
				{
					//将第一个选择房间的勾选撤销掉
					var inttemp2;
					for(var i=1;i<	intAllRoomCount+1;i++)
					{
						inttemp2=$('#selpos' + i.toString()).val();	
						if(inttemp2>0)
						{
							if(inttemp2<=intHadSelCount)
							{
								if(inttemp2==1)
								{
								 	document.getElementById( 'imgroom' + i.toString()).src="images/check0.png";
									$('#check' + i.toString()).val(0);
								}
								$('#selpos' + i.toString()).val(inttemp2-1);
							}
						}
						
					}
				}
				else
				{
					//设置选择房间数+1
					intHadSelCount = intHadSelCount +1;
				}
				//设置当前房间为第N个选择的
				$(checkpos).val(intHadSelCount);
			}
			else
			{
				img.src="images/check0.png";
				$(checkname).val(0);

				//alert(intHadSelCount.toString());
				
				var intdelval;	//要删除的流水号
				intdelval=$(checkpos).val();
				
				var inttemp1;
				//将当前选择的房间设置为未选择
				for(var i=1;i<	intAllRoomCount+1;i++)
				{
					inttemp1=$('#selpos' + i.toString()).val();	
					if(inttemp1>0)
					{
						if(intdelval==inttemp1)
						{
							$('#selpos' + i.toString()).val(0);			
						}
						else if(intdelval<=inttemp1)
						{
							$('#selpos' + i.toString()).val(inttemp1-1);	
						}
					}
					
				}
								
				//设置选择房间数-1
				intHadSelCount = intHadSelCount -1;
				
			}
			
			//alert(intAllRoomCount.toString());
			//显示所有房间数
			/*
			var strtemp='';
			for(var j=1;j<	(intAllRoomCount+1);j++)
			{
					inttemp1=$('#selpos' + j.toString()).val();	
					if(inttemp1>0)
					{
						strtemp = strtemp + '*' + inttemp1.toString();
					}
					
			}
			
			alert(strtemp);
			*/
			
			//判断当前选择的房间数是否大于预定数
			$("#label1").text("已选 " + intHadSelCount.toString() + ' 间');
			$("#label2").text("还有 " + (intBookRoomCount-intHadSelCount).toString() + ' 间未选');
				
		}
		
		//相应提交按钮事件处理部分
		function bmtSendOkClick()
		{
			//得到当前选择的房间信息
			var strrooms='';
			var inttemp=0;
			for(var i=1;i<	(intAllRoomCount+1);i++)
			{
				inttemp = $('#check' + i.toString()).val();
				if(inttemp>0)
				{
					//的到房号				
					strrooms = strrooms + ',' + $('#roomno' + i.toString()).val();
				}		
			}
			
			if(strrooms.length>0)
			{
				strrooms = strrooms.substring(1);
				
				//alert(strrooms);
				
				//开始提交PHP页面执行数据库操作流程
				var strurl = 'roomselsave.php?tbno=<?php echo $strorderno;?>&roms=' + strrooms;
				
				$.get(strurl ,
					function(data)
					{
						//alert("Data Loaded: " + data);
						if(data=='1')
						{
							//执行成功，返回到订单页面	
							window.location.href="orderview.php?tbno=<?php echo $strorderno; ?>&doid=0";
						}
					//	else if(data==2)
					//	{
					//		alert("Data Loaded: " + data);
					//	}
						else
						{
							alert("选房失败，代码：" + substr(data,0,1));
						}
					}
				);
				
			}
			else
			{
				alert('请选择要入住的房间！');
			}
				
		}
	</script>

</section>

</body>
	
</html>