<#import "../../ftllib/spring_new.ftl" as spring />
<#import "../../ftllib/pagecom.ftl" as page />
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href='<@spring.url ""/>'/>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>菜品订货管理</title>
<link type="text/css" href="css/blue.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="javascript/jquery_easy_ui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="javascript/jquery_easy_ui/themes/icon.css">
<link type="text/css" href="css/table.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="javascript/calendar/humanity.datepick.css"/>
<link rel="stylesheet" type="text/css" href="css/form.css">
<link type='text/css' href='javascript/fullcalendar/fullcalendar.css' rel='stylesheet'  />
<link rel="stylesheet" type="text/css" href="javascript/auto/jquery.autocomplete.css" />

<script type="text/javascript" src="javascript/jquery-1.4.2.min.js" charset="utf-8"></script>
<script type="text/javascript" src="javascript/jquery_easy_ui/jquery.easyui.min.js" charset="utf-8"></script>
<script type="text/javascript" src="javascript/jquery_easy_ui/easyui-lang-zh_CN.js" charset="utf-8"></script>
<script type="text/javascript" src="javascript/jquery.form.js" charset="utf-8"></script>
<script type="text/javascript" src="javascript/jquery.utils.js" charset="utf-8"></script>
<script type='text/javascript' src='javascript/fullcalendar/fullcalendar.js'></script>
<script type='text/javascript' src='javascript/auto/jquery.autocomplete.js'></script>
<script src="javascript/json2.js"></script>
<script type='text/javascript'>
$(document).ready(function() {  
	   var height = $("#main").height() - 150;
	   $("#menu_div").height(height);
	   getMenus();
       bindEvent();  
}); 

var hasError = false;
function bindEvent()  
{   
	$("#menu_input").focus(function(){
		$(this).select();
		$(this).autocomplete(SHICH_ERP_WORDS['menus'], {
			minChars: 0,
			width: 240,
			scrollHeight: 220,
			max:50,
			matchContains: true,
			autoFill: false,
			formatItem: function(row, i, max) {
				return row.key+" "+row.name;
			},
			formatMatch: function(row, i, max) {
				return row.key+" "+row.name;
			},
			formatResult: function(row) {
				return row.name;
			}
		}).result(function(evt,data,formatted){
			$("#menu_unit").val(data.unit);
			$("#menu_id").val(data.code);
		});
	}).keydown(function(event){
		if(event.keyCode==13)
		{
			$('#menu_number').focus();
		}
	});
	$("#menu_number").focus(function(){
		$(this).select();
	}).blur(function(){
		var lastValue = $(this).val();
		hasError = false;
        if(isNaN(lastValue) || 0 > lastValue)  
        {  
           hasError = true;
           alert("请输入正确的菜品数量！");
           $('#menu_number').focus();
        }
	}).keydown(function(){
		if(event.keyCode==13)
		{
			event.keyCode=9;
		}
	});
} 

function saveChange(){
	if (confirm("确认保存？")) {
		var rows=new Array();
		$("#menu_table tr").each(function(i){
			
			var row = {};
			row['id'] = $(this).find("td:eq(0)").html();
			row['menuName'] = $(this).find("td:eq(1)").html();
			row['menuUnit'] = $(this).find("td:eq(2)").html();
			row['menuNumber'] = $(this).find("td:eq(3) :text").val();
			
			rows[rows.length] = row;
		
		});
		if (rows.length > 0) {
			var param = {"lists":JSON.stringify(rows),"command":"saveMenu"};
			$.post("<@spring.url "kc/guide/guide_menu_operate.html"/>",param,function(data){
				 var obj = eval('(' + data+ ')'); 
				if(obj.status == 1){  			
	 					
	 			}else{
	 				alert(obj.info);
	 			} 
			});
		}else {
			alert("请添加菜品信息！");
		}
	}
}
// 下一步
function nextStep(){
	if (confirm("保存当前数据,并进入下一步？")) {
		var rows=new Array();
		$("#menu_table tr").each(function(i){
			
			var row = {};
			row['id'] = $(this).find("td:eq(0)").html();
			row['menuName'] = $(this).find("td:eq(1)").html();
			row['menuUnit'] = $(this).find("td:eq(2)").html();
			row['menuNumber'] = $(this).find("td:eq(3) :text").val();
			
			rows[rows.length] = row;
		
		});
		if (rows.length > 0) {
			var param = {"lists":JSON.stringify(rows),"command":"saveMenu"};
			$.post("<@spring.url "kc/guide/guide_menu_operate.html"/>",param,function(data){
				 var obj = eval('(' + data+ ')'); 
				if(obj.status == 1){  			
	 				window.open('<@spring.url "kc/guide/guide_materiel.html"/>','_self');	
	 			}else{
	 				alert(obj.info);
	 			} 
			});
		}else {
			alert("请添加菜品信息！");
		}
	}
}

var index = 20000;
function addNewMenu(){
	var menuUnit = $("#menu_unit").val();
	var menuName = $("#menu_input").val();
	var menuId = $("#menu_id").val();
	if (menuName == null || menuName == "")
	{
		hasError = true;
		alert("请输入菜品名称！");
		$("#menu_input").focus();
		return;	
	}
	if (menuId == null || menuId == "")
	{
		hasError = true;
		alert("请输入合法的菜品名称！");
		$("#menu_input").focus();
		return;
	}
	var menuNumber = $("#menu_number").val();
	if (menuNumber == null || menuNumber == "")
	{
		hasError = true;
		alert("请输入菜品数量！")
		$("#menu_number").focus();
		return;
	}
	if (hasError) {
		alert("请填写正确的信息！");
	}else {
		index = index + 1;
	
		var tr = $("<tr align='center'></tr>");
		var td1 = $("<td>" + menuId + "</td>").appendTo(tr);
		var td2 = $("<td>" + menuName + "</td>").appendTo(tr);
		var td3 = $("<td>" + menuUnit + "</td>").appendTo(tr);
		var td4 = $("<td><input type='text' name='textfield' value='" + menuNumber + "' class='number' tabindex='" + index + "'/></td>").appendTo(tr);
		var td5 = $("<td><a href='javascript:void(0);' onclick='deleteMenu(this);'><span class='l-btn-plain icon-remove' style='float:right;padding-left:30px;height:16px;width=20px;cursor:pointer;'></span></a></td>").appendTo(tr);
		$("#menu_table").append(tr);
		
		$("#menu_input").val("");
		$("#menu_number").val("");
		$("#menu_id").val("");
		
		$("#menu_input").focus();
	}
	
}

function deleteMenu(obj){
	if (confirm("确定要删除该菜品么？")) 
	{
		var tr=obj.parentNode.parentNode;
		var param = {"command":"deleteMenu","menuId":tr.children.item(0).innerHTML};
		$.post("<@spring.url "kc/guide/guide_menu_operate.html"/>",param,function(data){
			 var obj = eval('(' + data+ ')'); 
			if(obj.status == 1){  			
 				alert("删除成功！"); 			
 			}else{
 				alert(obj.info);
 			} 
		});
		 
		var tbody=tr.parentNode; 
		tbody.removeChild(tr); 
	}
}

function allSetZero(){
	if (confirm("确定菜品数量全部置零么？"))
	{
		$(".number").val("");
	}
}

function deleteAll(){
	if (confirm("确定要删除该菜品么？")) 
	{
		var param = {"command":"deleteAllMenu"};
		$.post("<@spring.url "kc/guide/guide_menu_operate.html"/>",param,function(data){
			 var obj = eval('(' + data+ ')'); 
			if(obj.status == 1){  			
 				alert("删除成功！"); 			
 			}else{
 				alert(obj.info);
 			} 
		});
		$("#menu_table tr").each(function(i){
			$(this).remove();
		});
	}
}

function getMenus(){
	var param = {"command":"getMenus"};
	$.post("<@spring.url "kc/guide/guide_menu_operate.html"/>",param,function(data){
		if(data != null && data != ""){
			$("#menu_div").html(data);
		}
		$("#menu_table").delegate("td","keydown",function(){
			if (event.keyCode==13) {
				event.keyCode=9;
			}
		});
	});
}
</script>
	
</head>

<body>
<div id="main" style="width:99%;height:99%;border-bottom:1px solid #ccc;margin-bottom:5px;"> 
	<div style="padding:5px 5px;margin-top:5px;margin-bottom:-15px;height:15px;">
			<a href="kc/main_page.html">物流管理平台</a>&nbsp;&gt;&nbsp;菜品订货管理
	</div>
	<div style="float:right;padding:5px 5px;margin-top:-15px;">
				<a class="l-btn l-btn-plain" href="kc/guide/guide_restock_select.html">
					<span class="l-btn-left">
						<span class="l-btn-text icon-back_new" style="padding-left: 20px;">
							返回
						</span>
					</span>
				</a>
	</div>
	<div>
	<div style="padding:5px 5px;margin-top:-15px;height:40px;width:100%"><hr>
		<table width="70%" border="0" cellspacing="0" cellpadding="0" style='font-size:12px;'>
		<tr>
			<td width="210"> 
				<label>&nbsp;菜品名称：
	        		<input type="text" id="menu_input" name="1"/>
	    		</label>
	    		<input type="hidden" id="menu_id"/>
	    		<input type="hidden" id="menu_unit"/>
	    	</td>
			<td width="210">
				<label>菜品数量：
	        		<input type="text" id="menu_number" name="2"/>
	    		</label>
			</td>
			<td width="100">
				<input type="button" id="addNewMenu"  value="快速录入" onclick="addNewMenu();">
			</td>
			<td width="100">
				<input type="button" id="" value="全部置零" onclick="allSetZero();">
			</td>
			<td>
				<input type="button" id="" value="全部删除" onclick="deleteAll();">
			</td>
		</tr>
	<table>
	</div>
	<table class='blue' border='1' cellspacing='0' cellpadding='0' style='font-size:12px;width:700px;margin-top:0px;table-layout:fixed;'>
		<tr align='center' bgcolor='#fff'>
			<td width='140' height='25'>菜品编码</td>
			<td width='190'>菜品名称</td>
			<td width='140'>单位</td>
			<td width='130'>菜品数量</td>
			<td width='80'>删除</td>
			<td width='20'></td>
		</tr>
	</table>
	<div id="menu_div" style="float:left;padding:0px 0px;margin:0px auto;width:705px;overflow:auto;border:1px solid #ccc">
	</div>
	<div id="div1" style="display:block;text-align:center;width:680px;height:50;margin-top:5px;">
		<input name="button" type="button" value="保存" style="height:30px;width:100px;" onclick="saveChange();"/>
		<input name="button" type="button" value="下一步" style="height:30px;width:100px;" onclick="nextStep();"/>
	</div>
</div>
</body>
<script>
${words?default("")}
</script>
</html>
