<#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 src="javascript/jquery.utils.js" charset="utf-8"></script>
<script src="javascript/json2.js"></script>
<script type='text/javascript'>
$(document).ready(function() {  
	   var height = $("#main").height() - 180;
	   $("#div_materiel").height(height);
	   
	   $.showAjaxAction({img:"images/control/ajaxLoader.gif"});
	  
	   getMateriels();
}); 

var hasError = false;
function bindEvent()  
{   
	$("#materiel_table :text").focus(function(){
		$(this).select();
	}).blur(function(){
		var value = $(this).val();
		if (isNaN(value) || value < 0) {
			hasError = true;
			alert("请输入正确的采购数量！");
			$(this).focus();
			return;
		}else {
			hasError = false;
		}
		
		var old_value = $(this).attr("old_value");		
		if (old_value != value) {
			$(this).css("color","red");
			$(this).attr("class","modified");
		}else {
			$(this).attr("class","");
		}
	}).keydown(function(){
		if(event.keyCode==13)
		{
			event.keyCode=9;
		}
	});
} 

function saveChange(){
	
	if (!hasError) {
		var rows=new Array();
		$(".modified").each(function(){
			var row = {"materielCode":this.id,"number":$(this).val()};
			rows[rows.length] = row;
		});
		if (rows.length > 0) {
			
			var param = {"lists":JSON.stringify(rows),"command":"setBuyNumber"};
			
			$.post("<@spring.url "kc/guide/guide_materiel_operate.html"/>",param,function(data){
				
				var obj = eval('(' + data+ ')'); 
				
				if(obj.status == 1){  		
					alert("保存成功！"); 				 		
	 			}else{
	 				alert(obj.info);
	 				return;
	 			} 
			});
		}
	}else {
		alert("请输入正确的折扣值！");
		return;
	}
	
}

function getMateriels(){
	var param = {"command":"getMateriels","buyType":$("#buyType").val()};
	$.post("<@spring.url "kc/guide/guide_materiel_operate.html"/>",param,function(data){
		if(data != null && data != ""){
			$("#div_materiel").html(data);
		}
		bindEvent();
	});
}

function checkMenu(){
	if (confirm("该操作会重置采购数量，确定要执行该操作吗？")) {
		window.open('<@spring.url "kc/guide/guide_menu.html"/>','_self');
	}
}

function checkMateriel(){
	if ($("#materiel_table tr").size() == 0) {
		alert("没有材料不能生产单据！");
		return false;
	}
	return true;
}
function createQpd(){
	if (confirm("确认保存并生成请配单吗？")) {
		
		if (checkMateriel()) {
			saveChange();
		
			var param = {"command":"createQpd"};
			$.post("<@spring.url "kc/guide/guide_materiel_operate.html"/>",param,function(data){
				
				var obj = eval('(' + data+ ')'); 
				
				if(obj.status == 1){  		
					
					alert("请配单生成成功！"); 		
					$("#checkQp").attr("href", "kc/mdqp/qqps_order_info.html?code=" + obj.info + "").show();	
				
				}else{
					alert(obj.info);
				} 
			});
		}
	}
}

function createCgjh(){
	if (confirm("确认保存并生成采购计划吗？")) {
		
		if (checkMateriel()) {
			saveChange();
		
			var param = {"command":"createCgjh"};
			$.post("<@spring.url "kc/guide/guide_materiel_operate.html"/>",param,function(data){
				
				var obj = eval('(' + data+ ')'); 
				
				if(obj.status == 1){  		
					
					alert("采购计划生成成功！"); 
					$("#checkCg").attr("href", "kc/buy/cg_plan_info.html?code=" + obj.info + "").show();				 		
	 			
	 			}else{
	 				alert(obj.info);
	 			} 
			});
		}
	}
}
</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 id="div2" style="float:left;padding:5px 5px;margin-top:5px;height:20px;width:70%"><hr>
		<label>采购方式：
	        <select id="buyType">
	          <<option  value="" selected>全部</option>
	          <option value="0">自采购</option>
	          <option value="1">总部生产</option>
	          <option value="3">集中订货</option>
	          <option value="4">集中采购</option>
	        </select>
	    </label>&nbsp;&nbsp;
	    <input type="button" value="查 询" onclick="getMateriels();" style="height:23px;width:60px;"/>
	     <a id='checkQp' style='padding-left:50px;font-size:12px;color:red;display:none'>【点此查看请求配送单】</a>
	     <a id='checkCg' style='padding-left:50px;font-size:12px;color:red;display:none'>【点此查看采购计划】</a>
	
	<table class='blue' style='table-layout:fixed;margin-top:2px;width:2px;height:15px'>
		<tr class=''>
		<td width='150'>材料名称</td>
		<td width='100'>规格</td>
		<td width='100'>单位</td>
		<td width='100'>采购模式</td>
		<td width='100'>预定数量</td>
		<td width='100'>现有库存</td>
		<td width='100'>采购数量</td>
		<td width='23'></td>
		</tr>
	</table>
	<div id="div_materiel" style="width:780px;overflow:auto;border:1px solid #ccc"></div>
	<br />
	<div id="div1" style="display:block;text-align:center;width:680px;height:50px;">
		<input name="button" type="button" value="上一步：查看菜品" style="height:40px;width:120px;" onclick="checkMenu();"/>
		<input name="button" type="button" value="生成请配单" style="height:40px;width:120px;" onclick="createQpd();"/>
		<input name="button" type="button" value="生成采购计划" style="height:40px;width:120px;" onclick="createCgjh();"/>
	</div>
	</div>
</div>
</body>
</html>
