<?php
	session_start();
	include "wechat.class.php";
	include 'errCode.php';
	require_once("../Wx.Config.php");
			
	$opt = array(
		'appsecret'=>WxMainConfig::APPSECRET,//填写高级调用功能的密钥
		'appid'=>WxMainConfig::APPID	//填写高级调用功能的appid
	);
	$we = new Wechat($opt);
	$auth = $we->checkAuth();
	//var_dump($auth);
	$js_ticket = $we->getJsTicket();
	if (!$js_ticket) {
		echo "获取js_ticket失败！<br>";
		echo '错误码：'.$we->errCode;
		echo ' 错误原因：'.ErrCode::getErrText($we->errCode);
		//echo "dddddddddddddddd";
		exit;
	}
	//$url = 'https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];		//注意这里如果最初调用页面的是 http这里就不要加s否则就要加s
	$url = WxMainConfig::ServerHttpPathB.$_SERVER['REQUEST_URI'];
	
	$js_sign = $we->getJsSign($url);
	//var_dump($js_sign);
	//exit();

?>
<!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="jsp4/jquery.mobile-1.4.3.min.css">
	<script src="jsp4/jquery.js"></script>
	<script src="jsp4/jquery.mobile-1.4.3.min.js"></script>
	
	
	<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></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>

<script>
	//禁止页面上下滑动,整个页面都不能滑动了
	document.addEventListener('touchmove', function(e) {
		e.preventDefault();
	});
</script>


	
<body leftmargin="0" rightmargin="0" topmargin="0" bottommargin="0">
<?php 
		require_once("db/db.php");	
		require_once("common/common.php");
	
		$chainno=$_SESSION["ChainNofw"];
		$roomno=$_REQUEST["rmid"];
		
?>
<!--选择楼层的部分代码---------------------------->
<section data-role="page" id="pageItemList">
	<header data-role="header" data-position="fixed" data-tap-toggle="false">
		<table width="100%" height="40" border="0" cellpadding="0" cellspacing="0">
			<tr>
				<td height="40" width="45" align="right">
					<a href="roominform.php?rmid=<?php echo $roomno;?>" data-role="button" data-icon="carat-l" data-iconpos="notext" rel="external"></a>
				</td>
				<td height="40" align="center">商品扫码入账</td>
				<td height="40" width="40" ></td>
			</tr>
		</table>
	</header>
	
	<article data-role="content">
		<table width="100%" border="0" cellpadding="0" cellspacing="0" >
			<tr><td height="1" colspan="3"></td></tr>
			<tr>
				<td height="26" width="10"></td>
				<td height="26" align="left">
					<div id="SubDiv1" style="width:100%;height:100%;text-align:center;padding-top:0px;border:0px solid #fff;">
						<span style="color: #000;font-size:14px;text-shadow:none;">正在加载扫码设备。。。</span>
					</div>
				</td>
				<td height="26" width="10"></td>
			</tr>
			<tr><td height="5" colspan="3"></td></tr>
			<tr>
				<td height="50" width="10"></td>
				<td height="50" align="center">
					<!--扫描入账-->
					<div id="ScaleEnable" onclick="SubClick()" style="width:125px;height:48px;top:0px;left:0px;">
						<img src="images/saomiao3.png" style="width:100%;height:100%"/>
					</div>
				</td>
				<td height="50" width="10"></td>
			</tr>
			
			<tr><td height="8" colspan="3"></td></tr>
			<tr><td height="1" colspan="3" bgcolor="#F2E2E2"></td></tr>
			<tr><td height="10" colspan="3"></td></tr>
			
			<tr>
				<td height="20" width="10"></td>
				<td height="20" align="left">
					<div id="SubDiv2" style="width:100%;height:100%;text-align:left;padding-top:0px;border:0px solid #fff;">
						<span style="color: #000;font-size:16px;text-shadow:none;">项目：</span>
					</div>
				</td>
				<td height="20" width="10"></td>
			</tr>
			
			
			<tr>
				<td height ="30" width="10"></td>
				<td height ="30">
					<span style="color: #000;font: 12px/1.5 '微软雅黑',tahoma, Srial, helvetica, sans-serif; font-size:16px;text-shadow:none;">请选择入账数量</span>
				</td>
				<td height ="30" width="10"></td>
			</tr>
						
			<tr><td height ="10" colspan="3" align="center"></td></tr>
						
			<tr>
				<td height ="80" width="10"></td>
				<td height ="80" valign="top">
					<table width="100%" height="60" border="0" cellpadding="0" cellspacing="0">
						<tr>
							<td height="60" width="65" align="right">
								<div id="SubCode" onClick="gbClick(2,'')" style="width:55px;height:50px;text-align:center;line-height:50px;background-color:#E7E7E7;margin-left:0px;border-radius:5px;">
									<span style="color: #000;font-size:28px;text-shadow:none;">←</span>
									<input id="RowTXT" type="hidden" value="1">
								</div>
							</td>
							<td height="60" align="center">
								<div id="shuDiv" style="width:70px;height:40px;text-align:center;padding-top:10px;border:1px solid #ddd;">
									<span style="color: #000;font-size:24px;text-shadow:none;">1</span>
								</div>
							</td>
							<td height="60" width="65" align="right">
								<div id="AddCode" onClick="gbClick(1,'')" style="width:55px;height:50px;text-align:center;line-height:50px;background-color:#E7E7E7;margin-right:10px;border-radius:5px;">
									<span style="color: #000;font-size:28px;text-shadow:none;">→</span>
								</div>
							</td>
						</tr>
					</table>
				</td>
				<td height ="80" width="10"></td>
			</tr>
						
			<tr>
				<td height ="40" width="10"></td>
				<td height ="40" align="center">
					<div onClick="gbClick(3,'<?php echo $strItemNo;?>')" style="width:94%;height:40px;">
						<img id="commodity_account" src="images/save.png" style="height:38px;width:100%;" />
					</div>
				</td>
				<td height ="40" width="10"></td>
			</tr>
			
			<tr><td height ="50" colspan="3" align="center"></td></tr>
			
			<tr>
				<td height ="40" width="10"></td>
				<td height ="40" align="center">
					<div id="SubDivX1" style="width:100%;height:100%;text-align:left;padding-left:4px;padding-top:0px;border:0px solid #fff;">
						<span style="color: #000;font-size:12px;text-shadow:none;"></span>
					</div>
				</td>
				<td height ="40" width="10"></td>
			</tr>
			
		</table>

	</article>
	
</section>


<script type="text/javascript">
	var intHadLinkFlag=0;
	var strSelBarCode;
	var strGetItemNo;
	var strGetItemName;
	strSelBarCode="";
	strGetItemNo="";
	strGetItemName="";
	
	//alert('<?php echo $url;?>');
	wx.config({
			debug: false,
			appId: '<?php echo $js_sign['appid']; ?>', // 必填，公众号的唯一标识
			timestamp: <?php echo $js_sign['timestamp']; ?>, // 必填，生成签名的时间戳，切记时间戳是整数型，别加引号
			nonceStr: '<?php echo $js_sign['noncestr']; ?>', // 必填，生成签名的随机串
			signature: '<?php echo $js_sign['signature']; ?>', // 必填，签名，见附录1
			jsApiList: [
				'checkJsApi',
				'onMenuShareTimeline',
				'onMenuShareAppMessage',
				'onMenuShareQQ',
				'onMenuShareWeibo',
				'hideMenuItems',
				'showMenuItems',
				'hideAllNonBaseMenuItem',
				'showAllNonBaseMenuItem',
				'translateVoice',
				'startRecord',
				'stopRecord',
				'onRecordEnd',
				'playVoice',
				'pauseVoice',
				'stopVoice',
				'uploadVoice',
				'downloadVoice',
				'chooseImage',
				'previewImage',
				'uploadImage',
				'downloadImage',
				'getNetworkType',
				'openLocation',
				'getLocation',
				'hideOptionMenu',
				'showOptionMenu',
				'closeWindow',
				'scanQRCode',
				'chooseWXPay',
				'openProductSpecificView',
				'addCard',
				'chooseCard',
				'openCard'
			  ]
	});
	
	wx.ready(
		function () {
			// 在这里调用 API
			//alert(1);
			wx.checkJsApi(
			{
				jsApiList: ['scanQRCode'], // 需要检测的JS接口列表，所有JS接口列表见附录2,
				success: function(res) 
				{
					// 以键值对的形式返回，可用的api值true，不可用为false
					// 如：{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
					//alert(JSON.stringify(res)); //如果调用成功显示消息
					SetBarAPIState(1);
					scaleCodePage();	//自动启动扫码
					intHadLinkFlag=1;
				},
				fail: function (res)
				{
					SetBarAPIState(0);
				}
			});
		}		
	);
			

		function scaleCodePage()
		{
			wx.scanQRCode(
			{
				needResult: 1,
				scanType: ["qrCode","barCode"], // 可以指定扫二维码还是一维码，默认二者都有
				desc: 'scanQRCode desc',
				success: function (res) 
				{
					//alert(JSON.stringify(res));
					//alert(res.resultStr);
					//localStorage["wx"] = res.resultStr;
					if(res.resultStr=="")
					{
						SetBarAPIState(3);	//扫码失败
					}
					else
					{
						//显示扫码信息
						//alert(res.resultStr);
						strSelBarCode=res.resultStr;
						strSelBarCode=strSelBarCode.replace("EAN_13,","");
						//alert(strSelBarCode);
						SetBarAPIState(2);	//扫码成功
					}
				},
				cancel: function (res) 
				{
					//alert(JSON.stringify(res));
					SetBarAPIState(4);	//取消
				},
				fail: function (res) 
				{
					//alert(JSON.stringify(res));
					SetBarAPIState(3);	//扫码失败
				}
			});
		}
		
		function SubClick()
		{
			if(intHadLinkFlag>0)
			{
				scaleCodePage();
			}
		}
		
		function SetBarAPIState(intState)
		{
			if(intState==0)
			{
				//加载失败
				$("#SubDiv1").html('<span style="color: #000;font-size:15px;text-shadow:none;">加载扫码接口失败！！</span>');
			}
			else if(intState==1)
			{
				//加载成功
				$("#SubDiv1").html('<span style="color: #000;font-size:15px;text-shadow:none;">正在加载扫码设备。。。</span>');
			}
			else if(intState==2)
			{
				//扫码成功
				$("#SubDiv1").html('<span style="color: #000;font-size:15px;text-shadow:none;">扫码成功！</span>');
				
				$("#SubDivX1").html('<span style="color: #000;font-size:12px;text-shadow:none;">信息码：' + strSelBarCode + '</span>');
				//获取该条码的项目
				
				//alert(strSelBarCode);
				//return;
				//用ajax的“同步方式”调用一般处理程序
				$.ajax({
					url: "getbaritem.php?barcode=" +strSelBarCode,
					async: false,//改为同步方式
					type: "POST",
					//data: {doid:4, rmid: "<?php echo $roomno;?>" },
					success: function (RetData) 
					{
						var strret;
						strret = RetData;
						if(strret.length>15)
						{
							//alert(strret);
							//开始解析项目编号和名称
							strGetItemNo=strret.substr(0,15);
							strGetItemName=strret.substr(15);
							strGetItemNo=strGetItemNo.trim();
							strGetItemName=strGetItemName.trim();
							//alert(strGetItemNo + '+');
							//alert(strGetItemName + '+');
							$("#SubDiv2").html('<span style="color: #000;font-size:16px;text-shadow:none;">项目：' + strGetItemName + '</span>');
						}
						else
						{
							strGetItemNo="";
							strGetItemName="";
							$("#SubDiv2").html('<span style="color: #000;font-size:16px;text-shadow:none;">项目：</span>');
						}
					}
				});
			}
			else if(intState==3)
			{
				//扫码失败
				$("#SubDiv1").html('<span style="color: #000;font-size:15px;text-shadow:none;">扫码失败！</span>');
			}
			else if(intState==4)
			{
				//扫码失败
				$("#SubDiv1").html('<span style="color: #000;font-size:15px;text-shadow:none;">扫码用户取消！</span>');
			}
		}
		
	</script>
	
	<script type="text/javascript">
		//var intNowCount;
		//intNowCount=1;
		
		function gbClick(id,itmid)
		{
			var intSelCount;			
			var oTextbox1=document.getElementById("RowTXT");
			intSelCount=oTextbox1.value;
			//alert(oTextbox1.value);
			//return;
			if(id==1)
			{
				//增加商品数量 
				intSelCount++;
				if(intSelCount>99){
					intSelCount = 1;
				}
				$("#shuDiv").html('<span style="color: #000;font-size:24px;text-shadow:none;">' + intSelCount +'</span>');
				oTextbox1.value=intSelCount;
			}
			else if(id==2)
			{
				//减少商品数量
				intSelCount--;
				if(intSelCount<=0){
					intSelCount = 1;
				}
				$("#shuDiv").html('<span style="color: #000;font-size:24px;text-shadow:none;">' + intSelCount +'</span>');
				oTextbox1.value=intSelCount;
			}
			else if(id==3)
			{
				//保存入账
				if(strGetItemNo.length<1)
				{
					return;
				}
				//用ajax的“同步方式”调用一般处理程序
				$.ajax({
					url: "saveitemtoroom.php?rmid=<?php echo $roomno;?>" + "&itmno=" + strGetItemNo + "&selnum=" + intSelCount,
					async: false,//改为同步方式
					type: "POST",
					//data: {doid:4, rmid: "<?php echo $roomno;?>" },
					success: function (RetData) 
					{
						strret = RetData;
					}
				});
									
				if(strret==1)
				{
					var urlroom='roominform.php?rmid=<?php echo $roomno;?>';
					window.location.href=urlroom;
					//alert("商品入账成功!");
				}
				else
				{
					alert("商品入账失败!");
				}
			}
		}
		
	</script>
</body>
</html>
