<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/include/taglibs.jsp"%>
<!DOCTYPE html>
<html>
<head>
<title>移库单编辑</title>
<%@ include file="/WEB-INF/include/common.jsp"%>
<script type="text/javascript" src="${ctx}/script/common/jquery.comboboxRef.js"></script>
<script type="text/javascript" src="${ctx}/script/common/jquery.combogridRef.js"></script>
<script type="text/javascript">
	var functionId = '${functionId}';
	var operate = '${requestScope.operate}';
	var inventoryLocationMoveId = '${requestScope.inventoryLocationMove.inventoryLocationMoveId}';
	var ilmi = "";
	var transferKindListJson = JSON.parse('${requestScope.transferKindListJson}');

	function transferKindFormat(value, row, index) 
	{
		for ( var i = 0; i < transferKindListJson.length; i++)
		{
			var transferKindItem = transferKindListJson[i];
			
			if (transferKindItem != null)
			{
				if (value == transferKindItem.id)
				{
					return transferKindItem.dictionaryName;
				}
			}
		}
		if (value == "0") {
			return "否";
		} else if(value == "1"){
			return "是";
		}else{
			return value;
		}
	};
	
	function formatAction(value,row) {
		//如果产品不需要扫描 则不展示SN扫描按钮
		var moveType = row.moveType;
		
		var a = '<a href="#" onclick="imeiScan(this)">IMEI列表</a> ';
		
		//不用扫描
		if (moveType == 1201 || moveType == 1203)
		{
			return null;
		}
		else
		{
			return a;
		}
	}
	
	function getRowIndex(target){
		var tr = $(target).closest('tr.datagrid-row');
		return parseInt(tr.attr('datagrid-row-index'));
	}
	
    function imeiScan(target)
    {
    	var index = getRowIndex(target);
    	
    	var rows = $("#detailList").datagrid('getRows');
    	
    	var snList = rows[index].snList;
    	
    	$('#showImei').window({
			title : 'IEMI列表',
			width : 450,
			height : 350,
			closed : false,
			cache : false,
			modal : true,
			onOpen:function() {
				document.getElementById('snListNew').value = snList;
            }
		});
    }
    
	$(function() {
		//定义页面元素
		var $tabs = $("#tabs");
		$tabs.tabs('select', "基本信息");
		var unEditableAttributes =  ${requestScope.unEditableAttributes};
		var inventoryLocationMoveData = '${ requestScope.inventoryLocationMoveData }';
		$.InventoryLocationMoveEdit = new $.AbstractEdit({
			entity : "InventoryLocationMove",
			formId : "inventoryLocationMoveForm",
			operate : operate,
			functionId : functionId,
			id : "inventoryLocationMoveId"
		});
		
		$.fn.extend($.InventoryLocationMoveEdit, {
			save : function() {
				if (this.form.form('validate') && this.validForm() ) 
				{
					if (4902 == status){
						showMsg("提示", "状态为完成,不允许修改订单");
					}
					else{
						if ("add" == this.operate){
							this.doBeforeSave();
							var postData = {
									"formData" : JSON.stringify(this.getData()),
									"updateFields" : JSON.stringify(this.changeList),
									"operate":this.operate
								};
							
							ajaxRequest(this.saveUrl, postData, this.afterSave, null, this);
						}
						else{
							if (this.changeList.length > 0)
							{
								this.doBeforeSave();
								var postData = {
										"formData" : JSON.stringify(this.getData()),
										"updateFields" : JSON.stringify(this.changeList),
										"operate":this.operate
									};
								
								ajaxRequest(this.saveUrl, postData, this.afterSave, null, this);
							}
							else
							{
								showMsg("提示", "数据未更新");
							}
						}
					}
				}
			},
			doAfterSuccessSave : function(response) {
		    	
                var temp = {'unEditableFields':['customerId','warehouseId','locationMoveStatus','locationMoveBy','locationMoveTime'],'unEditableButtons':[]};
		    	
		    	disabledAttributes($("#body"),temp);
		    	
		    	ilmi = response.data.inventoryLocationMoveId;
			}
		
		});

	    $('#locationId').combogrid({// 仓库
			url : WEBROOT + "/loadData.action?entity=location",
			idField : 'locationId',
			textField : 'locationName',
			delay : 500,
			showHeader : false,
			mode : 'remote',
			columns : [ [ {
				field : 'locationName',
				title : '储位名称',
				width : 155
			} ] ],
			onBeforeLoad : function(param) { // 此设置使控件不会自动加载数据 这里不能使用this
				param.warehouseId = $("#warehouseId").combogrid('getValue');
				
				if (!$('#locationId').combogrid('getText')) {
					return false;
				} else {
					return true;
				}
			},
			onShowPanel : function() { // 在第一次触发下拉框事件是加载数据
				if (!$(this).combogrid('getText')) {
					$(this).combogrid({
						onBeforeLoad : function(param) {
							param.warehouseId = $("#warehouseId").combogrid('getValue');
							return true;
						}
					});
				}
			},
			onSelectAfter : function(){
				 $('#itemLocation').combobox({// 储位上产品
						url : WEBROOT + "/loadData.action?entity=itemLocation",
						valueField : 'itemId',
						textField : 'itemName',
						delay : 500,
						showHeader : false,
						mode : 'remote',
						columns : [ [ {
							field : 'itemName',
							title : '商品名称',
							width : 155
						} ] ],
						onBeforeLoad : function(param) { // 此设置使控件不会自动加载数据 这里不能使用this
							param.locationId = $("#locationId").combogrid('getValue');
							param.customerId = $("#customerId").combogrid('getValue');
							
							if (!$('#itemLocation').combobox('getText')) {
								return false;
							} else {
								return true;
							}
						},
						onShowPanel : function() { // 在第一次触发下拉框事件是加载数据
							if (!$(this).combobox('getText')) {
								$(this).combobox({
									onBeforeLoad : function(param) {
										param.locationId = $("#locationId").combogrid('getValue');
										param.customerId = $("#customerId").combogrid('getValue');
										return true;
									}
								});
							}
						}
					});
			}
		});
	    
	    $('#toLocationId').combogrid({// 仓库
			url : WEBROOT + "/loadData.action?entity=location",
			idField : 'locationId',
			textField : 'locationName',
			delay : 500,
			showHeader : false,
			mode : 'remote',
			columns : [ [ {
				field : 'locationName',
				title : '储位名称',
				width : 155
			} ] ],
			onBeforeLoad : function(param) { // 此设置使控件不会自动加载数据 这里不能使用this
				param.warehouseId = $("#warehouseId").combogrid('getValue');
				
				if (!$('#toLocationId').combogrid('getText')) {
					return false;
				} else {
					return true;
				}
			},
			onShowPanel : function() { // 在第一次触发下拉框事件是加载数据
				if (!$(this).combogrid('getText')) {
					$(this).combogrid({
						onBeforeLoad : function(param) {
							param.warehouseId = $("#warehouseId").combogrid('getValue');
							return true;
						}
					});
				}
			}
		});
	    
	    $('#queryLocationName').combogrid({// 仓库
			url : WEBROOT + "/loadData.action?entity=location",
			idField : 'locationName',
			textField : 'locationName',
			delay : 500,
			showHeader : false,
			mode : 'remote',
			columns : [ [ {
				field : 'locationName',
				title : '储位名称',
				width : 155
			} ] ],
			onBeforeLoad : function(param) { // 此设置使控件不会自动加载数据 这里不能使用this
				param.warehouseId = $("#warehouseId").combogrid('getValue');
			    //param.locationCategory = 702;
				
				if (!$('#queryLocationName').combogrid('getText')) {
					return false;
				} else {
					return true;
				}
			},
			onShowPanel : function() { // 在第一次触发下拉框事件是加载数据
				if (!$(this).combogrid('getText')) {
					$(this).combogrid({
						onBeforeLoad : function(param) {
							param.warehouseId = $("#warehouseId").combogrid('getValue');
							param.locationCategory = 702;
							return true;
						}
					});
				}
			}
		});
		
		$.InventoryLocationMoveEdit.init();

		if (!$.isEmpty(${requestScope.inventoryLocationMoveData})) {
			$.InventoryLocationMoveEdit.fillData(${requestScope.inventoryLocationMoveData});
		    disabledAttributes($("#body"),unEditableAttributes);
		}

		$("#saveInventoryLocationMoveBtn").click(
				$.proxy($.InventoryLocationMoveEdit.save,
						$.InventoryLocationMoveEdit));

		//实例化实体List
		$.InventoryLocationMoveDetailEdit = new $.AbstractEdit({
			entity : "InventoryLocationMoveDetail",
			formId : "inventoryLocationMoveDetailForm",
			id : "id"
		});

		disabledAttributes($("#body1"),unEditableAttributes);
		$.InventoryLocationMoveDetailEdit.init();
		disabledAttributes($("#body1"),unEditableAttributes);
		
		var status = 4901;
		
		/**
		 * 完成转移
		 */
		$("#finishInventoryLocationMoveBtn").bind("click",function() {
			var statusFromDB = '${requestScope.inventoryLocationMove.locationMoveStatus}';
			
			if(4902 == status){
				showMsg('提示', '状态为完成,不能完成移动');
			}
			else if(4902 == statusFromDB){
				showMsg('提示', '状态为完成,不能完成移动');
			}
			else{
				ajaxRequest(WEBROOT + '/InventoryLocationMove/completeMove.action', {id : $("#inventoryLocationMoveForm").find("input[name='inventoryLocationMoveId']").val()}, function(response) {
		            if (response.code == "success") {
		                showMsg('提示', '移库成功');
		                status = 4902;
		                $("#inventoryLocationMoveForm").find("input[name='locationMoveStatus']").val(4902);
		                $("#detailList").datagrid('reload');
		                
						var data = response.data;
		                
		                $('#inventoryLocationMoveForm').form('load',{
		                	locationMoveStatus : data.locationMoveStatus,
		                	locationMoveBy : data.locationMoveBy,
		                	locationMoveTime : data.locationMoveTime
	                	});
		                
		            } else {
		                var errorDesc = response.errorDesc;
		                showMsg('移库失败', errorDesc);
		            };
		        });
			}
		});
	});
</script>
<script type="text/javascript"
	src="${ctx}/script/wms/inventory/inventoryLocationMove.js"></script>
</head>
<body style="overflow-y: hidden" fit="true" scroll="no" >
	<div id="tabs" class="easyui-tabs" fit="true" >
		<div title="基本信息" id="body">
			<form id="inventoryLocationMoveForm" method="post">
				<input type="hidden" name="inventoryLocationMoveId" value="${inventoryLocationMove.inventoryLocationMoveId}"></input>
				<input type="hidden" name="deleteFlag" id="deleteFlag"></input>
				<table style="width: 100%">
					<tr>
						<a href="#" icon="icon-save" id="saveInventoryLocationMoveBtn"
							class="easyui-linkbutton">保存</a>
						<a href="#" icon="" id="finishInventoryLocationMoveBtn"
							class="easyui-linkbutton">完成移动</a>
					</tr>

					<tr>
						<td><div class="star">*</div>仓库:</td>
						<td>
						    <s:select list="#request.warehouseList" listKey="warehouseId" listValue="warehouseName" cssClass="easyui-combobox" cssStyle="width: 155px" name="warehouseId" id="warehouseId" theme="simple" data-options="required: true"></s:select>
						</td>
						
						<td><div class="star">*</div>货主:</td>
						<td>
						    <s:select list="#request.customerList" listKey="customerId" listValue="customerName" cssClass="easyui-combobox" cssStyle="width: 155px" name="customerId" id="customerId" theme="simple" data-options="required: true"></s:select>
						</td>
						
						<td>状态:</td>
						<td>
						    <select id="locationMoveStatus" class="easyui-combobox" size="8" name="locationMoveStatus" style="width:160px" data-options="required:true" disabled="disabled">
								<option value="4901" selected="selected">待处理</option>
								<c:forEach var="item" items="${locationMoveStatusList}">
									<option value="${item.id}">${item.dictionaryName}</option>
								</c:forEach>
						    </select>
					    </td>
					</tr>
					<tr>
						<td>处理人:</td>
						<td><input class="easyui-validatebox" name="locationMoveBy" disabled="disabled"></input>
						</td>

						<td>转移时间:</td>
						<td><input class="easyui-datebox" name="locationMoveTime" disabled="disabled"></input>
						</td>

						<td>备注:</td>
						<td><textarea class="easyui-validatebox" name="description"
								style="width: 155px"></textarea>
						</td>
					</tr>
				</table>
			</form>

			<div id="detailTabs" class="easyui-tabs" fit="false">
				<div title="详情明细">
					<div id="toolbar" class="toolbar">
					<table>
						<tr>
							<td><a id="addDetailBtn" href="#" class="easyui-linkbutton"
								data-options="iconCls:'icon-add'">新增</a></td>
							<td><a id="removeDetailBtn" href="#" class="easyui-linkbutton"
								data-options="iconCls:'icon-remove'">删除</a></td>
						</tr>
					</table>
					</div>

					<form id="searchForm" action="#" method="post">
						<table id="detailList" data-options="fitColumns:true">
							<thead>
								<tr>
									<th data-options="field:'id',hidden:true">主键</th>
									<th data-options="field:'ck',checkbox:true">编码</th>
									<th data-options="field:'relationedCustomerName',align:'center',width:100">货主关系人</th>
									<th data-options="field:'locationName',align:'center',width:100">原储位</th>
									<th data-options="field:'itemCode',align:'center',width:100">产品代码</th>
									<th data-options="field:'itemName',align:'center',width:100">产品名称</th>
									<th data-options="field:'normalFlag',align:'center',width:100">产品形态</th>
									<th data-options="field:'locationMoveQuantity',align:'center',width:100">移动数量</th>
									<th data-options="field:'toLocationName',align:'center',width:100">至储位</th>
									<th data-options="field:'transferKind',align:'center',width:100,formatter:transferKindFormat">全部转移</th>
									<th data-options="field:'moveReason',align:'center',width:100">移库原因</th>
									<th data-options="field:'snList',align:'center',width:100,formatter:formatAction">IMEI列表</th>
								</tr>
							</thead>
						</table>
					</form>
				</div>
			</div>
		</div>
	</div>

	<div id="popWindow" data-options="" style="margin: 0px; padding: 0px; overflow: auto" >
		<%@ include file="/WEB-INF/pages/wms/inventoryLocationMove/inventoryLocationMoveDetailEdit.jsp"%>
	</div>
</body>
</html>
</html>