<%@ page language="java" import="java.util.*"  contentType="text/html;charset=UTF-8"%> 
<%@ page import="com.chengxusheji.po.Sell" %>
<%@ page import="com.chengxusheji.po.Customer" %>
<%@ page import="com.chengxusheji.po.Product" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    List<Sell> sellList = (List<Sell>)request.getAttribute("sellList");
    //获取所有的customerObj信息
    List<Customer> customerList = (List<Customer>)request.getAttribute("customerList");
    //获取所有的productObj信息
    List<Product> productList = (List<Product>)request.getAttribute("productList");
    int currentPage =  (Integer)request.getAttribute("currentPage"); //当前页
    int totalPage =   (Integer)request.getAttribute("totalPage");  //一共多少页
    int recordNumber =   (Integer)request.getAttribute("recordNumber");  //一共多少记录
    Product productObj = (Product)request.getAttribute("productObj");
    Customer customerObj = (Customer)request.getAttribute("customerObj");
    String sellDate = (String)request.getAttribute("sellDate"); //销售日期查询关键字
    String payWay = (String)request.getAttribute("payWay"); //支付方式查询关键字
    String logistics = (String)request.getAttribute("logistics"); //物流公司查询关键字
    String wayNumber = (String)request.getAttribute("wayNumber"); //运单号查询关键字
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1 , user-scalable=no">
<title>销售查询</title>
<link href="<%=basePath %>plugins/bootstrap.css" rel="stylesheet">
<link href="<%=basePath %>plugins/bootstrap-dashen.css" rel="stylesheet">
<link href="<%=basePath %>plugins/font-awesome.css" rel="stylesheet">
<link href="<%=basePath %>plugins/animate.css" rel="stylesheet">
<link href="<%=basePath %>plugins/bootstrap-datetimepicker.min.css" rel="stylesheet" media="screen">
</head>
<body style="margin-top:70px;">
<div class="container">
<jsp:include page="../header.jsp"></jsp:include>
	<div class="row"> 
		<div class="col-md-9 wow fadeInDown" data-wow-duration="0.5s">
			<div>
				<!-- Nav tabs -->
				<ul class="nav nav-tabs" role="tablist">
			    	<li><a href="<%=basePath %>index.jsp">首页</a></li>
			    	<li role="presentation" class="active"><a href="#sellListPanel" aria-controls="sellListPanel" role="tab" data-toggle="tab">销售列表</a></li>
			    	<li role="presentation" ><a href="<%=basePath %>Sell/sell_frontAdd.jsp" style="display:none;">添加销售</a></li>
				</ul>
			  	<!-- Tab panes -->
			  	<div class="tab-content">
				    <div role="tabpanel" class="tab-pane active" id="sellListPanel">
				    		<div class="row">
				    			<div class="col-md-12 top5">
				    				<div class="table-responsive">
				    				<table class="table table-condensed table-hover">
				    					<tr class="success bold"><td>序号</td><td>销售id</td><td>销售产品</td><td>销售客户</td><td>销售单价</td><td>销售数量</td><td>销售日期</td><td>支付方式</td><td>物流公司</td><td>运单号</td><td>操作</td></tr>
				    					<% 
				    						/*计算起始序号*/
				    	            		int startIndex = (currentPage -1) * 5;
				    	            		/*遍历记录*/
				    	            		for(int i=0;i<sellList.size();i++) {
					    	            		int currentIndex = startIndex + i + 1; //当前记录的序号
					    	            		Sell sell = sellList.get(i); //获取到销售对象
 										%>
 										<tr>
 											<td><%=currentIndex %></td>
 											<td><%=sell.getSellId() %></td>
 											<td><%=sell.getProductObj().getProductName() %></td>
 											<td><%=sell.getCustomerObj().getCustomerName() %></td>
 											<td><%=sell.getPrice() %></td>
 											<td><%=sell.getSellCount() %></td>
 											<td><%=sell.getSellDate() %></td>
 											<td><%=sell.getPayWay() %></td>
 											<td><%=sell.getLogistics() %></td>
 											<td><%=sell.getWayNumber() %></td>
 											<td>
 												<a href="<%=basePath  %>Sell/<%=sell.getSellId() %>/frontshow"><i class="fa fa-info"></i>&nbsp;查看</a>&nbsp;
 												<a href="#" onclick="sellEdit('<%=sell.getSellId() %>');" style="display:none;"><i class="fa fa-pencil fa-fw"></i>编辑</a>&nbsp;
 												<a href="#" onclick="sellDelete('<%=sell.getSellId() %>');" style="display:none;"><i class="fa fa-trash-o fa-fw"></i>删除</a>
 											</td> 
 										</tr>
 										<%}%>
				    				</table>
				    				</div>
				    			</div>
				    		</div>

				    		<div class="row">
					            <div class="col-md-12">
						            <nav class="pull-left">
						                <ul class="pagination">
						                    <li><a href="#" onclick="GoToPage(<%=currentPage-1 %>,<%=totalPage %>);" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>
						                     <%
						                    	int startPage = currentPage - 5;
						                    	int endPage = currentPage + 5;
						                    	if(startPage < 1) startPage=1;
						                    	if(endPage > totalPage) endPage = totalPage;
						                    	for(int i=startPage;i<=endPage;i++) {
						                    %>
						                    <li class="<%= currentPage==i?"active":"" %>"><a href="#"  onclick="GoToPage(<%=i %>,<%=totalPage %>);"><%=i %></a></li>
						                    <%  } %> 
						                    <li><a href="#" onclick="GoToPage(<%=currentPage+1 %>,<%=totalPage %>);"><span aria-hidden="true">&raquo;</span></a></li>
						                </ul>
						            </nav>
						            <div class="pull-right" style="line-height:75px;" >共有<%=recordNumber %>条记录，当前第 <%=currentPage %>/<%=totalPage %> 页</div>
					            </div>
				            </div> 
				    </div>
				</div>
			</div>
		</div>
	<div class="col-md-3 wow fadeInRight">
		<div class="page-header">
    		<h1>销售查询</h1>
		</div>
		<form name="sellQueryForm" id="sellQueryForm" action="<%=basePath %>Sell/frontlist" class="mar_t15">
            <div class="form-group">
            	<label for="productObj_productId">销售产品：</label>
                <select id="productObj_productId" name="productObj.productId" class="form-control">
                	<option value="0">不限制</option>
	 				<%
	 				for(Product productTemp:productList) {
	 					String selected = "";
 					if(productObj!=null && productObj.getProductId()!=null && productObj.getProductId().intValue()==productTemp.getProductId().intValue())
 						selected = "selected";
	 				%>
 				 <option value="<%=productTemp.getProductId() %>" <%=selected %>><%=productTemp.getProductName() %></option>
	 				<%
	 				}
	 				%>
 			</select>
            </div>
            <div class="form-group">
            	<label for="customerObj_customerId">销售客户：</label>
                <select id="customerObj_customerId" name="customerObj.customerId" class="form-control">
                	<option value="0">不限制</option>
	 				<%
	 				for(Customer customerTemp:customerList) {
	 					String selected = "";
 					if(customerObj!=null && customerObj.getCustomerId()!=null && customerObj.getCustomerId().intValue()==customerTemp.getCustomerId().intValue())
 						selected = "selected";
	 				%>
 				 <option value="<%=customerTemp.getCustomerId() %>" <%=selected %>><%=customerTemp.getCustomerName() %></option>
	 				<%
	 				}
	 				%>
 			</select>
            </div>
			<div class="form-group">
				<label for="sellDate">销售日期:</label>
				<input type="text" id="sellDate" name="sellDate" class="form-control"  placeholder="请选择销售日期" value="<%=sellDate %>" onclick="SelectDate(this,'yyyy-MM-dd')" />
			</div>
			<div class="form-group">
				<label for="payWay">支付方式:</label>
				<input type="text" id="payWay" name="payWay" value="<%=payWay %>" class="form-control" placeholder="请输入支付方式">
			</div>






			<div class="form-group">
				<label for="logistics">物流公司:</label>
				<input type="text" id="logistics" name="logistics" value="<%=logistics %>" class="form-control" placeholder="请输入物流公司">
			</div>






			<div class="form-group">
				<label for="wayNumber">运单号:</label>
				<input type="text" id="wayNumber" name="wayNumber" value="<%=wayNumber %>" class="form-control" placeholder="请输入运单号">
			</div>






            <input type=hidden name=currentPage value="<%=currentPage %>" />
            <button type="submit" class="btn btn-primary">查询</button>
        </form>
	</div>

		</div>
	</div> 
<div id="sellEditDialog" class="modal fade" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title"><i class="fa fa-edit"></i>&nbsp;销售信息编辑</h4>
      </div>
      <div class="modal-body" style="height:450px; overflow: scroll;">
      	<form class="form-horizontal" name="sellEditForm" id="sellEditForm" enctype="multipart/form-data" method="post"  class="mar_t15">
		  <div class="form-group">
			 <label for="sell_sellId_edit" class="col-md-3 text-right">销售id:</label>
			 <div class="col-md-9"> 
			 	<input type="text" id="sell_sellId_edit" name="sell.sellId" class="form-control" placeholder="请输入销售id" readOnly>
			 </div>
		  </div> 
		  <div class="form-group">
		  	 <label for="sell_productObj_productId_edit" class="col-md-3 text-right">销售产品:</label>
		  	 <div class="col-md-9">
			    <select id="sell_productObj_productId_edit" name="sell.productObj.productId" class="form-control">
			    </select>
		  	 </div>
		  </div>
		  <div class="form-group">
		  	 <label for="sell_customerObj_customerId_edit" class="col-md-3 text-right">销售客户:</label>
		  	 <div class="col-md-9">
			    <select id="sell_customerObj_customerId_edit" name="sell.customerObj.customerId" class="form-control">
			    </select>
		  	 </div>
		  </div>
		  <div class="form-group">
		  	 <label for="sell_price_edit" class="col-md-3 text-right">销售单价:</label>
		  	 <div class="col-md-9">
			    <input type="text" id="sell_price_edit" name="sell.price" class="form-control" placeholder="请输入销售单价">
			 </div>
		  </div>
		  <div class="form-group">
		  	 <label for="sell_sellCount_edit" class="col-md-3 text-right">销售数量:</label>
		  	 <div class="col-md-9">
			    <input type="text" id="sell_sellCount_edit" name="sell.sellCount" class="form-control" placeholder="请输入销售数量">
			 </div>
		  </div>
		  <div class="form-group">
		  	 <label for="sell_sellDate_edit" class="col-md-3 text-right">销售日期:</label>
		  	 <div class="col-md-9">
                <div class="input-group date sell_sellDate_edit col-md-12" data-link-field="sell_sellDate_edit"  data-link-format="yyyy-mm-dd">
                    <input class="form-control" id="sell_sellDate_edit" name="sell.sellDate" size="16" type="text" value="" placeholder="请选择销售日期" readonly>
                    <span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>
                    <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
                </div>
		  	 </div>
		  </div>
		  <div class="form-group">
		  	 <label for="sell_payWay_edit" class="col-md-3 text-right">支付方式:</label>
		  	 <div class="col-md-9">
			    <input type="text" id="sell_payWay_edit" name="sell.payWay" class="form-control" placeholder="请输入支付方式">
			 </div>
		  </div>
		  <div class="form-group">
		  	 <label for="sell_logistics_edit" class="col-md-3 text-right">物流公司:</label>
		  	 <div class="col-md-9">
			    <input type="text" id="sell_logistics_edit" name="sell.logistics" class="form-control" placeholder="请输入物流公司">
			 </div>
		  </div>
		  <div class="form-group">
		  	 <label for="sell_wayNumber_edit" class="col-md-3 text-right">运单号:</label>
		  	 <div class="col-md-9">
			    <input type="text" id="sell_wayNumber_edit" name="sell.wayNumber" class="form-control" placeholder="请输入运单号">
			 </div>
		  </div>
		  <div class="form-group">
		  	 <label for="sell_sellMemo_edit" class="col-md-3 text-right">销售备注:</label>
		  	 <div class="col-md-9">
			    <textarea id="sell_sellMemo_edit" name="sell.sellMemo" rows="8" class="form-control" placeholder="请输入销售备注"></textarea>
			 </div>
		  </div>
		</form> 
	    <style>#sellEditForm .form-group {margin-bottom:5px;}  </style>
      </div>
      <div class="modal-footer"> 
      	<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
      	<button type="button" class="btn btn-primary" onclick="ajaxSellModify();">提交</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<jsp:include page="../footer.jsp"></jsp:include> 
<script src="<%=basePath %>plugins/jquery.min.js"></script>
<script src="<%=basePath %>plugins/bootstrap.js"></script>
<script src="<%=basePath %>plugins/wow.min.js"></script>
<script src="<%=basePath %>plugins/bootstrap-datetimepicker.min.js"></script>
<script src="<%=basePath %>plugins/locales/bootstrap-datetimepicker.zh-CN.js"></script>
<script type="text/javascript" src="<%=basePath %>js/jsdate.js"></script>
<script>
var basePath = "<%=basePath%>";
/*跳转到查询结果的某页*/
function GoToPage(currentPage,totalPage) {
    if(currentPage==0) return;
    if(currentPage>totalPage) return;
    document.sellQueryForm.currentPage.value = currentPage;
    document.sellQueryForm.submit();
}

/*可以直接跳转到某页*/
function changepage(totalPage)
{
    var pageValue=document.sellQueryForm.pageValue.value;
    if(pageValue>totalPage) {
        alert('你输入的页码超出了总页数!');
        return ;
    }
    document.sellQueryForm.currentPage.value = pageValue;
    documentsellQueryForm.submit();
}

/*弹出修改销售界面并初始化数据*/
function sellEdit(sellId) {
	$.ajax({
		url :  basePath + "Sell/" + sellId + "/update",
		type : "get",
		dataType: "json",
		success : function (sell, response, status) {
			if (sell) {
				$("#sell_sellId_edit").val(sell.sellId);
				$.ajax({
					url: basePath + "Product/listAll",
					type: "get",
					success: function(products,response,status) { 
						$("#sell_productObj_productId_edit").empty();
						var html="";
		        		$(products).each(function(i,product){
		        			html += "<option value='" + product.productId + "'>" + product.productName + "</option>";
		        		});
		        		$("#sell_productObj_productId_edit").html(html);
		        		$("#sell_productObj_productId_edit").val(sell.productObjPri);
					}
				});
				$.ajax({
					url: basePath + "Customer/listAll",
					type: "get",
					success: function(customers,response,status) { 
						$("#sell_customerObj_customerId_edit").empty();
						var html="";
		        		$(customers).each(function(i,customer){
		        			html += "<option value='" + customer.customerId + "'>" + customer.customerName + "</option>";
		        		});
		        		$("#sell_customerObj_customerId_edit").html(html);
		        		$("#sell_customerObj_customerId_edit").val(sell.customerObjPri);
					}
				});
				$("#sell_price_edit").val(sell.price);
				$("#sell_sellCount_edit").val(sell.sellCount);
				$("#sell_sellDate_edit").val(sell.sellDate);
				$("#sell_payWay_edit").val(sell.payWay);
				$("#sell_logistics_edit").val(sell.logistics);
				$("#sell_wayNumber_edit").val(sell.wayNumber);
				$("#sell_sellMemo_edit").val(sell.sellMemo);
				$('#sellEditDialog').modal('show');
			} else {
				alert("获取信息失败！");
			}
		}
	});
}

/*删除销售信息*/
function sellDelete(sellId) {
	if(confirm("确认删除这个记录")) {
		$.ajax({
			type : "POST",
			url : basePath + "Sell/deletes",
			data : {
				sellIds : sellId,
			},
			success : function (obj) {
				if (obj.success) {
					alert("删除成功");
					$("#sellQueryForm").submit();
					//location.href= basePath + "Sell/frontlist";
				}
				else 
					alert(obj.message);
			},
		});
	}
}

/*ajax方式提交销售信息表单给服务器端修改*/
function ajaxSellModify() {
	$.ajax({
		url :  basePath + "Sell/" + $("#sell_sellId_edit").val() + "/update",
		type : "post",
		dataType: "json",
		data: new FormData($("#sellEditForm")[0]),
		success : function (obj, response, status) {
            if(obj.success){
                alert("信息修改成功！");
                $("#sellQueryForm").submit();
            }else{
                alert(obj.message);
            } 
		},
		processData: false,
		contentType: false,
	});
}

$(function(){
	/*小屏幕导航点击关闭菜单*/
    $('.navbar-collapse a').click(function(){
        $('.navbar-collapse').collapse('hide');
    });
    new WOW().init();

    /*销售日期组件*/
    $('.sell_sellDate_edit').datetimepicker({
    	language:  'zh-CN',  //语言
    	format: 'yyyy-mm-dd',
    	minView: 2,
    	weekStart: 1,
    	todayBtn:  1,
    	autoclose: 1,
    	minuteStep: 1,
    	todayHighlight: 1,
    	startView: 2,
    	forceParse: 0
    });
})
</script>
</body>
</html>

