<#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 type="text/css" href="css/table.css" rel="stylesheet">

<script type='text/javascript' src='dwr/interface/AjaxReportQueryService.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/jquery.utils.js" charset="utf-8"></script>
</head>
<script language="javascript"> 

$(document).ready(function(){	
	$.showAjaxAction({img:"images/control/ajaxLoader.gif"});
	
	$("#date_select").change(function(){
		var val = $(this).val();
		AjaxReportQueryService.getFixDate(val,function(data){
			if(data){	
				$("#start_date").val(data.start);
				$("#end_date").val(data.end);
			}
		});	
	});
	$("#date_select").val("currentDay");
	
	$("#my_table0 th input:checkbox").click(function(){
		$("#make_order_table tr").find("td :checkbox").attr("checked", $(this).attr("checked"));
	});
	
	$("#my_table1 th input:checkbox").click(function(){
		$("#semi_materiel_table tr").find("td :checkbox").attr("checked", $(this).attr("checked"));
	});
	
	$("#date_select").trigger("change");
	
	bindInput();
});
// 绑定输入框 上下键
function bindInput(){
	$("#page1, #page2").delegate("input", "focus", function(){
		$(this).closest("tr").css("background","#EBD3E2");
	}).delegate("input", "blur", function(){
		$(this).closest("tr").css("background","#fff");
	}).delegate("input","keydown", function(e){
		var currRow = $(this).closest("tr");
		if(e.which==40){       //down
			var row = currRow.next();
			if(row.length > 0 && row.find(":input").length <= 0){
			    row = row.next();
			}
			if(row.length > 0){
				row.find(":input").focus().select();
				currRow.css("background","#fff");
			} 
			
		}else if(e.which==38){  //up
			var row = currRow.prev();
			if(row.length > 0 && row.find(":input").length <= 0){
			    row = row.prev();
			}
			if(row.length > 0){
				row.find(":input").focus().select();
				currRow.css("background","#fff");
			} 
		}
		
	});
}
function make_orders(){
	step(0);
	var startDate = $("#start_date").val();
	var endDate = $("#end_date").val();
	
	var param = {"start_date":startDate, "end_date":endDate, "command":"make_order_list"};
	param["widths"] = $("#my_table0").widths();
	param["state"] = $("#state").val();
	
	$("#content_div0").load("<@spring.url "kc/guide/make_order_operate.html"/>",param);
}

function sum_orders(){

	var codes = "";
	$("#make_order_table tr").find("td :checked").each(function(){
		var code = $(this).closest("tr").attr("code");
		if(codes != ""){
			codes += ",";
		}
		codes += code;
	});
	if(codes == ""){
		alert("请选择要汇总的单据!");return;
	}
	$("#page0").hide();
	$("#page1").show();
	var param = {"command":"sum_semi_materiels", codes: codes};
	param["widths"]= $("#my_table1").widths();
	$("#content_div1").load("<@spring.url "kc/guide/make_order_operate.html"/>",param,function(){
		
		// 修改采购数量
		$("#semi_materiel_table").delegate(":text", "change", function(){
			var code = $(this).closest("tr").attr("code");
			var number = $(this).val();
			var param = {command:"set_semi_materiels_number", "code":code, number:number};
			$.post("<@spring.url "kc/guide/make_order_operate.html"/>", param, function(data){
				if(!data){
					alert("修改失败!");
				}
			});
		});
		
	});
}
function sum_materiels(){
	
	$("#page1").hide();
	$("#page2").show();
	var param = {command:"sum_materiels"};
	param["widths"]= $("#my_table2").widths();
	$("#content_div2").load("<@spring.url "kc/guide/make_order_operate.html"/>", param, function(){
		// 修改采购数量
		$("#materiel_table").delegate(":text", "change", function(){
			var code = $(this).closest("tr").attr("code");
			var number = $(this).val();
			var param = {command:"set_materiels_number", "code":code, number:number};
			$.post("<@spring.url "kc/guide/make_order_operate.html"/>", param, function(data){
				if(!data){
					alert("修改失败!");
				}
			});
		});
	});
}

function create_cgjh(){
	var param = {command:"create_cgjh"};
	$.post("<@spring.url "kc/guide/make_order_operate.html"/>",param, function(data){
		window.location.href = "<@spring.url "kc/buy/cg_plan_info.html?code="/>"+data;
	});
}

function step(code){
	if(code == 0){
		$("#page0").show();
		$("#page1").hide();
		$("#page2").hide();
	}else if(code == 1){
		$("#page1").show();
		$("#page0").hide();
		$("#page2").hide();
	}else if(code == 2){
		$("#page0").hide();
		$("#page1").hide();
		$("#page2").show();
	}
}
</script>
<body class="easyui-layout" >
		<div region="north" border="false" style="width:100%;line-height:40px;height:40px;padding:5px 10px;overflow:hidden;">
			<span style="float:left;">
				<label>送货日期:</label>							
				<select id="date_select">	
					<option value='' selected>全  部</option>					
					<option value='currentDay'>本  日</option>
					<option value='yesterday'>昨  日</option>
					<option value='currentWeek'>本  周</option>
					<option value='previousOneWeek'>上  周</option>	
					<option value='currentMonth'>本  月</option>
					<option value='previousOneMonth'>上  月</option>
					<option value='currentYear'>本  年</option>
					<option value='previousYear'>去  年</option>				
				</select>									
				<input class="easyui-datebox" type="text" name="start_date" id="start_date" value="${start_date?default("")}" style="width:80px;height:18px;line-height:18px;"/>
					至 						
				<input class="easyui-datebox" type="text" name="end_date" id="end_date" value="${end_date?default("")}" style="width:80px;height:18px;line-height:18px;"/>
			</span>
			<span style="margin-left:10px;float:left;">
				<label>单据状态:</label>
				<select id="state" style="width:106px;">
					<option value='' selected>全部</option>
					<option value='0' >未审核</option>
					<option value='10'>已审核</option>										
				</select>
			</span>	
			
			<a href="javascript:void(0);" class="easyui-linkbutton"  style="margin-left:15px;line-height:20px;" iconCls="icon-search" onClick="make_orders();">查 询</a>
		</div>	
					
		<div id="page0" region="center" border="false" style="overflow:auto;">
			<table id="my_table0" class="blue" style="width:100%;margin:0px;table-layout:fixed;">
				<thead>
					<tr>
						<th width="5%"><input type="checkbox"></th>
						<th width="15%">单据编码</th>
						<th width="15%">生产日期</th>
						<th width="10%">审核人</th>
						<th width="15%">审核时间</th>
						<th width="10%">状态</th>
						<th width="15%">备注</th>	
						<th width="5%"></th>				
					</tr>
				</thead>	
			</table>
			<div id="content_div0" style="width:100%;height:80%;border-bottom:1px solid #ccc;overflow-y:scroll;overflow-x:hidden;"></div>
			<div  style="padding:10px;">
				<button style="padding:4px 10px;" onClick="sum_orders();" value=""> 汇总材料 </button>
			</div>
		</div>
		
		<div id="page1" region="center" border="false" style="overflow:auto;display:none">
			<table id="my_table1" class="blue" style="width:100%;margin:0px;table-layout:fixed;">
					<thead>
						<tr>
							<th width="15%">材料编码</th>
							<th width="15%">材料名称</th>
							<th width="10%">规格</th>
							<th width="15%">单位</th>
							<th width="10%">所需半成品数量</th>
							<th width="15%">加工数量</th>
							<th width="5%"></th>				
						</tr>
					</thead>	
			</table>
			<div id="content_div1" style="width:100%;height:400px;border-bottom:1px solid #ccc;overflow-y:scroll;overflow-x:hidden;"></div>
			<div style="padding:10px;">
				<button style="padding:4px 10px;" onClick="step(0);"><<< 上一步</button>
				<button style="padding:4px 10px;" onClick="sum_materiels();">下一步 >>></button>(由半成品拆分为原材料)
			</div>
		</div>
		
		<div id="page2" region="center" border="false" style="overflow:auto;display:none">
			<table id="my_table2" class="blue" style="width:100%;margin:0px;table-layout:fixed;">
					<thead>
						<tr>
							<th width="15%">材料编码</th>
							<th width="15%">材料名称</th>
							<th width="10%">规格</th>
							<th width="15%">单位</th>
							<th width="10%">库存数量</th>
							<th width="10%">最低库存数量</th>
							<th width="10%">加工所需数量</th>
							<th width="15%">采购数量</th>
							<th width="5%"></th>				
						</tr>
					</thead>	
			</table>
			<div id="content_div2" style="width:100%;height:400px;border-bottom:1px solid #ccc;overflow-y:scroll;overflow-x:hidden;"></div>
			<div style="padding:10px;">
				<button style="padding:4px 10px;" onClick="step(1);"><<< 上一步</button>
				<button style="padding:4px 10px;" onClick="create_cgjh();">生成集中采购计划 >>></button>
			</div>
		</div>
</body>
</html>
