<#import "../../ftllib/spring_new.ftl" as spring />
<#import "../../ftllib/pagecom.ftl" as page />
<#import "../../ftllib/frame.ftl" as frame />
<!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=utf-8">
<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 rel="stylesheet" type="text/css" href="javascript/auto/jquery.autocomplete.css" />
<link rel="stylesheet" type="text/css" href="javascript/calendar/humanity.datepick.css"/>
<link type="text/css" href="css/table.css" rel="stylesheet">
<style>	
	#model_div div{margin:2px 5px;padding:3px 0px;border-bottom:1px dotted #cccccc;cursor:pointer;}	
	.red_row {font-weight:bold;color:red;}	
	.blue_row {font-weight:bold;color:blue;}
</style>
<script language="javascript" type="text/javascript" src="javascript/pic_change.js"></script>
<script type='text/javascript' src='dwr/interface/AjaxCgydService.js'></script>
<script type='text/javascript' src='dwr/interface/AjaxCgshService.js'></script>
<script type='text/javascript' src='dwr/interface/AjaxQqpsService.js'></script>
<script type='text/javascript' src='dwr/interface/AjaxCheckService.js'></script>
<script type='text/javascript' src='dwr/engine.js'></script>
<script type='text/javascript' src='dwr/util.js'></script>
<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/auto/jquery.autocomplete.js'></script>
<script type='text/javascript' src='javascript/tools/jquery.qtip-1.0.0-rc3.min.js'></script>
<script type='text/javascript' src="javascript/jquery.utils.js"></script>
<script type='text/javascript' src="javascript/convert.js"></script>
<script type='text/javascript' src="javascript/calendar/jquery.datepick.pack.js"></script>
<script type='text/javascript' src="javascript/calendar/jquery.datepick-zh-CN.js" charset="utf-8"></script>


</head>
<script language="javascript">
function disableKeys(eve){
	var ev = (document.all) ? window.event : eve;
	var evCode = (document.all) ? ev.keyCode : ev.which;
	var srcElement = (document.all) ? ev.srcElement : ev.target;
    if(srcElement.type != "textarea" && srcElement.type != "text" || (srcElement.type=="text" && srcElement.readOnly)){
		if (evCode == 8){return false;}
	}
}
(document.all) ? (document.onkeydown = disableKeys) : (document.onkeypress = disableKeys);



var TYPE = "${target?default("")}";
//当前项
$_current = false;
$_select = false;
//检查ajax执行结果
function CHECK_ERROR(a,b){
	if(b){
		if(a.error){alert("操作失败:"+a.error_msg);return false;}
	}else{		
		if(a!= null && a.substring(0,7)=="[error]"){alert("操作失败:"+a.substring(7));return false;}
	}
	return true;
}
//重设页面布局.
function resize(){
	var page_width = $("#page").width();
	var page_height = $("#page").height();
	$("#materiel_body").maximizeHeight({margin:20});
	$("#materiel_table_div").maximizeHeight({margin:10});	
}

function bind_table(){
	//点击数量栏
	$("#materiel_table input[type=text]")
	.focus(function(){
		$(this).select();
		var $tr = $(this).closest("tr");
		if($_select){$_select.removeClass("selected");}
		$_select = $tr;
		$tr.addClass("selected");
	}).change(function(){
		var $tr = $(this).closest("tr");
		var param = {"command":"set_number"};
		param["materiel_code"] = $tr.attr("materiel_code");
		param["id"] = $tr.attr("model_id");
		param["number"] = $(this).val();
		set_row_state($tr,$(this).val());									
		$.post("<@spring.url "kc/guide/model_operate.html"/>",param,function(data){if(!CHECK_ERROR(data)){return false;}});					
	}).keydown(function(){
		if(event.keyCode==13){
			event.keyCode=9;
		}
	});
}
function set_row_state($tr,val){
	var lowLimit = T_float($tr.find(".limit").attr("low"),0.0);
	var highLimit = T_float($tr.find(".limit").attr("high"),0.0);
	var kcNumber = T_float($tr.find(".kc_number").text(),0.0);
	var number = T_float(val,0.0);
	if(kcNumber+number<lowLimit){		
		$tr.find(".limit").removeClass("blue_row").addClass("red_row");
	}else if(kcNumber+number>highLimit){
		$tr.find(".limit").removeClass("red_row").addClass("blue_row");
	}else{
		$tr.find(".limit").removeClass("blue_row").removeClass("red_row");
	}
}

function create(type){
	var model_id = $("#model_code").combo("getValue");
	if(model_id == ""){
		alert("请选择模板并设置数据！");
		return;
	}
	if(!confirm("确定要按当前模板数据生成单据吗？")){
		return;
	}
	var remark = $("#span_model").find(":text").val();	
 	if(type=="qqps"){
	 	AjaxQqpsService.createByModel(model_id,remark,function(data){
	 		if(data){	 			
	 			AjaxQqpsService.saveModelLog(data,$(document.body).html());
	  			alert('请求配送单生成成功！');
	  			location.href = "<@spring.url "kc/mdqp/qqps_order_info.html?code="/>"+data;
	  		}else{
	  			alert("生成请求配送单失败！");
	  		}
	  	});
  	}else if(type=="cgyd"){
	 	AjaxCgydService.createByModel(model_id,function(data){
	 		if(data=="error_number"){
	  			alert("生成采购单失败：请输入实需数量.");
	  			return;
	  		}
	  		alert('采购订单生成成功！');
	  		location.href = "<@spring.url "kc/buy/cgyd_order_info.html?code="/>"+data;
	  	});
  	}else if(type=="cgsh"){
	 	AjaxCgshService.createByModel(model_id,function(data){
	 		if(data=="error_number"){
	  			alert("生成采购单失败：请输入实需数量.");
	  			return;
	  		}
	  		alert('采购收货单生成成功！');
	  		location.href = "<@spring.url "kc/buy/cgsh_order_info.html?code="/>"+data;
	  	});
  	}

}

function load_materiels(model_id){	
	if(!model_id || model_id==""){
		return;
	}
	var param = {"command":"load_materiel","type":TYPE,"id":model_id};
	param["days"] = $("#days").val();
	param["days_type"] = $("#header_div").find(":radio[name='days_type']:checked").val();
	param["history"] = $("#history").val();
	param["history_type"] = $("#header_div").find(":radio[name='history_type']:checked").val();	
	param["widths"] = $("#materiel_table_header").widths();
	$("#materiel_table_div").load("<@spring.url "kc/guide/model_operate.html"/>",param,function(data){
		if(!CHECK_ERROR(data)){return false;}
		bind_table();
	});
}

$(document.body).ready(function(){
	resize();		
	$.showAjaxAction({img:"images/control/ajaxLoader.gif"});	
	$("#model_code").combobox({
		onSelect:function(node){
			var id=node.value;
			load_materiels(id);
		}
	});	
	
	$("#history").datepick({
		multiSelect: 7, 
		multiSeparator: ',',
		monthsToShow: 2,
		monthsOffset:1,
		onClose:function(datas){
			var model_id = $("#model_code").combo("getValue");
			load_materiels(model_id);
		}
	});
	$("#days").datepick({
		multiSelect: 7, 
		multiSeparator: ',',
		monthsToShow: 2,
		monthsOffset:1,
		onClose:function(datas){
			var model_id = $("#model_code").combo("getValue");
			load_materiels(model_id);
		}
	});	
	$("#header_div").find(":radio").click(function(){
		var model_id = $("#model_code").combo("getValue");
		load_materiels(model_id);
	});
});
</script>
<body class="easyui-layout">
<div id="header_div" region="north" border="false" style="height:130px;width:99%;padding:0px 5px;overflow:hidden;">
<@page.bodyTop>
	<@page.bodyTopLink>
		<@page.link "物流管理平台","kc/main_page.html"/>&nbsp;&gt;&nbsp;单据模板
	</@page.bodyTopLink>
	<@page.bodyTopImage>
	<@page.topImageButton "back","history.go(-1);"/>
	</@page.bodyTopImage>
</@page.bodyTop>
	<span style="float:left;width:90%;height:26px;" id="span_model">
		<label>选择模板:</label>						
		<select id="model_code"  class="easyui-combobox" style="width:140px;" panelHeight="400" panelWidth="200" required="true"/>
			${modelOptions?default("")}
		</select>	
		<#if target=="qqps">
			<a id="create" href="javascript:void(0);" onclick="javascript:create('qqps');" class="easyui-linkbutton" icon="icon-ok" style="margin-left:20px;">生成请求配送单</a>
		</#if>
		<#if target=="cgmb">
			<a id="create" href="javascript:void(0);" onclick="javascript:create('cgyd');" class="easyui-linkbutton" icon="icon-ok" style="margin-left:20px;">生成采购订单</a>
			<a id="create" href="javascript:void(0);" onclick="javascript:create('cgsh');" class="easyui-linkbutton" icon="icon-ok" style="margin-left:20px;">生成采购收货单</a>
		</#if>	
	</span>	
	<span style="margin-top:6px;float:left;width:90%;height:26px;">
		<label style="font-size:12px;float:left;">历史订货:</label>
		<input type="text" id="history" style="margin-left:5px;width:400px;border:0px;border-bottom:1px solid #A4BED4;"/>
		<input name="history_type" type="radio" value="avg" checked/>平均 
		<input name="history_type" type="radio" value="sum"/>汇总		
	</span>
	<span style="margin-top:6px;float:left;width:90%;height:26px;">
		<label style="font-size:12px;float:left;">销售参考:</label>
		<input type="text" id="days" style="margin-left:5px;width:400px;border:0px;border-bottom:1px solid #A4BED4;"/>
		<input name="days_type" type="radio" value="avg" checked/>平均 
		<input name="days_type" type="radio" value="sum"/>汇总		
	</span>
</div>
<div id="body_div" region="center" border="false" style="overflow:auto;">	
		<div id="materiel_body" style="width:98%; height:99%;margin-left:0px;border-top:1px solid #AACCEE;">
    		<table id="materiel_table_header" class="blue" style="margin:0px;table-layout:fixed;width:100%;">
    			<tr id="head1"> 
    				<th width='20%'>材料名称</th>
					<th width='15%'>规格</th>
					<th width='8%'>单位</th>
					<th width='15%'>库存下限/上限</th>
					<th width='10%'>现有库存</th>
					<th width='10%'>销售参考</th>
					<th width='10%'>历史订货</th>
					<th width='10%'>订货数</th>
					<th width='20'></th>	
    			</tr>
    		</table>
    		<div id="materiel_table_div" style="width:100%;overflow-y:scroll;overflow-x:hidden;text-align:left;height:400px;border-bottom:1px solid #AACCEE;">
    		</div>
    	</div>
</div>
<div region="south"  border="false" style="height:30px;">
</div>

</body>
</html>
