<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
	</head>
	<body>
		<div class="top"></div>
		<div class="center">
			<h3 id="movePopuWin">
				<a href="javascript:;" title="关闭" class="message_close"
					id="closePopuWin"></a>
				<span>编辑设备信息</span>
			</h3>
			<div class="popuWinMessage_main">
				<ul>
					<li style="text-align: center;">
						<span id="eqInfoMsg" style="color: red;">&nbsp;</span>
					</li>
				</ul>
				<form id="eqInfoForm"
					action="${pageContext.request.contextPath}/manager/vechileAction_updateBasicInfo.action"
					method="POST">
					<table width="100%" border="0" style="line-height: 35px;">

						<s:if test="#parameters['p_eqName'][0] == 'true'">
							<tr>
								<td
									style="width: 100px; text-align: right; padding-right: 15px;">
									设备名称:
								</td>
								<td>
									<input name="form.carName" />
								</td>
							</tr>
						</s:if>
						<s:if test="#parameters['sim_power'][0] == 'true'">
							<tr id="eq_sim">
								<td
									style="width: 100px; text-align: right; padding-right: 15px;">
									SIM卡号:
								</td>
								<td>
									<input name="form.sim" />
								</td>
							</tr>
						</s:if>
						<s:if test="#parameters['gprs_power'][0] == 'true'">
							<tr id="eq_gprs">
								<td
									style="width: 100px; text-align: right; padding-right: 15px;">
									设备ID:
								</td>
								<td>
									<input name="form.gprs" />
								</td>
							</tr>
						</s:if>
						<s:if test="#parameters['number_power'][0] == 'true'">
							<tr>
								<td
									style="width: 100px; text-align: right; padding-right: 15px;">
									数据号码:
								</td>
								<td>
									<input name="form.number" />
								</td>
							</tr>
						</s:if>
						<s:if test="#parameters['voice_power'][0] == 'true'">
							<tr>
								<td
									style="width: 100px; text-align: right; padding-right: 15px;">
									语音号码:
								</td>
								<td>
									<input name="form.voice" />
								</td>
							</tr>
						</s:if>
						<s:if test="#parameters['remark'][0] == 'true'">
							<tr id="eq_remark">
								<td
									style="width: 100px; text-align: right; padding-right: 15px;">
									备注:
								</td>
								<td>
									<textarea name="form.remark" rows="6" cols="30">

										</textarea>
								</td>
							</tr>
						</s:if>
						<!--  -->
					</table>
					<input name="form.id" value="${param.vehicleId}" type="hidden" />
					<input id="saveEqInfoFormBtn" type="button" value="保存"
						style="width: 80px; height: 30px; margin-left: 115px; margin-top: 20px;" />
				</form>
			</div>
		</div>
		<div class="bot"></div>
		<script>
			
				var curEqInfo_eqId = 0;
				var curEqInfo_form = null;
				var curEqInfo_carName_form_input = null;
				var curEqInfo_sim_form_input = null;
				var curEqInfo_gprs_form_input = null;
				var curEqInfo_number_form_input = null;
				var curEqInfo_voice_form_input = null;
				var curEqInfo_remark_form_textarea = null;
				var eqInfo_form_msg_lable = null;
				var save_eqInfo_form_btn = null
				
			
				function initformVar(){
					curEqInfo_form = $("#eqInfoForm");
					curEqInfo_carName_form_input = curEqInfo_form.find("input[name='form.carName']");
					curEqInfo_sim_form_input = curEqInfo_form.find("input[name='form.sim']");
					curEqInfo_gprs_form_input = curEqInfo_form.find("input[name='form.gprs']");	
					curEqInfo_number_form_input = curEqInfo_form.find("input[name='form.number']");	
					curEqInfo_voice_form_input = curEqInfo_form.find("input[name='form.voice']");		
					curEqInfo_remark_form_textarea = curEqInfo_form.find("textarea[name='form.remark']");				
					curEqInfo_eqId = parseInt(curEqInfo_form.find("input[name='form.id']").val());
					eqInfo_form_msg_lable = $("#eqInfoMsg");
					save_eqInfo_form_btn = $("#saveEqInfoFormBtn");
				}
				
				function formaSsignment(curEqInfo_eqId){
					//console.info(JSON.stringify(equipmentObject[curEqInfo_eqId]));
					if(equipmentObject[curEqInfo_eqId]){
						//console.log(equipmentObject[curEqInfo_eqId]);
						curEqInfo_carName_form_input.length>0 && curEqInfo_carName_form_input.val( $.trim( equipmentObject[curEqInfo_eqId].vehicleName.split("(")[0] ) );
						curEqInfo_sim_form_input.length>0 && curEqInfo_sim_form_input.val( $.trim( equipmentObject[curEqInfo_eqId].simCode ) );
						curEqInfo_gprs_form_input.length>0 && curEqInfo_gprs_form_input.val( $.trim( equipmentObject[curEqInfo_eqId].gprsCode ) );
						
						//console.log(carList);
						//alert(curEqInfo_eqId);
						for ( var i = 0; i < carList.data.length; i++) {
							if (carList.data[i].id == curEqInfo_eqId){
								curEqInfo_remark_form_textarea.length >0 && curEqInfo_remark_form_textarea.val( $.trim( carList.data[i].remark ) );
								curEqInfo_number_form_input.length>0 && curEqInfo_number_form_input.val( $.trim( carList.data[i].dataCode ) );
								curEqInfo_voice_form_input.length>0 && curEqInfo_voice_form_input.val( $.trim( carList.data[i].voiceCode ) );
							}
						}
					}
				}
				
				function initEqInfoFormBtnEvent(){
					$("#saveEqInfoFormBtn").click(function(){
						submitEqInfoForm();
					});
				}
				
				function submitEqInfoForm(){
					
					if(!eqInfoFormDataIsValid()) return;
										
					save_eqInfo_form_btn.attr("disabled","disabled");//关闭按钮									
					submitForm("eqInfoForm",eqInfoFormCallBack);	//提交表单
					
				}
				
				function eqInfoFormDataIsValid(){
				
					eqInfo_form_msg_lable.html("&nbsp;");
					
					var reg,value;
					
					
					if(curEqInfo_carName_form_input.length>0){
						value = $.trim(curEqInfo_carName_form_input.val());
						if(value.length<=0){
							eqInfo_form_msg_lable.html("*设备名称不能为空");
							return false;
						}
					}
					
					if(curEqInfo_sim_form_input.length>0){
						reg=/^[A-Za-z0-9]{5,13}$/;
						value = $.trim(curEqInfo_sim_form_input.val());
						if(!reg.test(value)){
							eqInfo_form_msg_lable.html("*SIM卡号无效:(5-13)位数字或字母");
							return false;
						}
					}
					
					if(curEqInfo_gprs_form_input.length>0){
						reg=/^[A-Za-z0-9]{3,40}$/;
						value = $.trim(curEqInfo_gprs_form_input.val());
						if(!reg.test(value)){
							eqInfo_form_msg_lable.html("*设备ID无效:(3-40)位数字或字母");
							return false;
						}
					}
					
					return true;
					
				}
				
				var eqInfoFormCallBack = function(json){
					console.info(JSON.stringify(json));	
					if(json.flag){
						updateEqData(curEqInfo_eqId);
						$("#closePopuWin").click();//修改成功后关闭弹出框
					}else{
						
						if(json.statecode && json.statecode == RESPONSE_CODE_NO_LOGIN ) outSys();//未登录状态跳转到登录页面
						
						eqInfo_form_msg_lable.html(parseErrorMsg(json));
						save_eqInfo_form_btn.removeAttr("disabled");
						
					}
				}
				
				function updateEqData(EqId){
								
					updateEquipmentObject(EqId);//更新equipmentObject对象					
					//更新MainMap页面的设备集合
					updateMapData(EqId);
					updateTreeData(EqId);//更新树形车牌号码
					
				}
				
				function updateEquipmentObject(EqId){
					if(equipmentObject[EqId]){
						equipmentObject[curEqInfo_eqId].vehicleName = curEqInfo_carName_form_input.length>0 &&  $.trim( curEqInfo_carName_form_input.val() );
						equipmentObject[curEqInfo_eqId].simCode = curEqInfo_sim_form_input.length>0 && $.trim( curEqInfo_sim_form_input.val() );
					    equipmentObject[curEqInfo_eqId].gprsCode = curEqInfo_gprs_form_input.length>0 && $.trim( curEqInfo_gprs_form_input.val() );
						equipmentObject[curEqInfo_eqId].number = curEqInfo_number_form_input.length>0 && $.trim( curEqInfo_number_form_input.val() );
						equipmentObject[curEqInfo_eqId].voice = curEqInfo_voice_form_input.length>0 && $.trim( curEqInfo_voice_form_input.val() );
					}
				}
				
				function updateMapData(EqId){
					updateMarkerData(EqId);
				}
				
				function updateTreeData(EqId){
					 var eqNode = null;
					 if(!curEqInfo_carName_form_input.length>0) return;
					 $.each(equipmentMap, function (groupId, eqs) {  								
						eqNode = $("#eqname_"+groupId+"_"+EqId);						
						if(eqNode){
							eqNode.text(equipmentObject[EqId].vehicleName+(equipmentObject[EqId].aliases?( "(" +equipmentObject[EqId].aliases+ ")" ):""));
						}
					 });
				}
				
				function parseErrorMsg(json){
					var msg = "";
					if(json.statecode){
						if( json.statecode == RESPONSE_CODE_SYSTEM_ERROR ){
							msg = "*系统异常请稍后重试";
						}
					}else{
						msg = json.msg;
					}
					return msg;
				}
				
				(function(){
					window.parent.initVisualPower();//初始化属性可见
					if(!window.parent.eqVisualPower.sim){
						$("#eq_sim").hide();
					}
					if(!window.parent.eqVisualPower.gprs){
						$("#eq_gprs").hide();
					}
					if(!window.parent.eqVisualPower.remark){
						$("#eq_remark").hide();
					}
					initformVar();
					formaSsignment(curEqInfo_eqId);
					
					initEqInfoFormBtnEvent();
				
				})();
				
	
			</script>
	</body>
</html>
