<%@page import="java.rmi.RemoteException"%>
<%@page import="ibase.webitm.utility.ITMException"%>
<%@page contentType="text/html"%>
<%@page import="javax.xml.parsers.DocumentBuilderFactory,javax.xml.parsers.DocumentBuilder,org.w3c.dom.*"%>
<%@page import="ibase.utility.CommonConstants"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="javax.naming.Context"%>
<%@page import="org.w3c.dom.Node"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="org.w3c.dom.NodeList"%>
<%@page import="ibase.webitm.utility.GenericUtility"%>
<%@page import="ibase.dashboard.scm.bean.*"%>
<jsp:useBean id="scmdasBean" scope="request" class="ibase.dashboard.scm.bean.LocationStockOccuBean"/>
<html>
<head>
<link rel="stylesheet" href="/ibase/dashboard/scm/css/style.css" type="text/css" />
<link href="/ibase/jquery/css/jquery-ui.css" rel="stylesheet"	type="text/css" /> 
<script src="/ibase/jquery/js/jquery.min.js"></script>
<script src="/ibase/jquery/js/jquery-ui.min.js"></script>
<script src="/ibase/jquery/js/jquery-1.9.1.js"></script> 
<script src="/ibase/jquery/js/jquery-ui.js"></script>

<%!GenericUtility genericUtility = GenericUtility.getInstance();
	String xmlString = "";
	String objName = "";
	Document dom = null;
	NodeList dtlList = null;
	Node parentNode = null;
	NodeList childNodeList = null;
	Node currDetail = null;	
	String childNodeName = "", locPhyArea = "", areaDtlString = "", selectedArea = "", siteCode = " ",imgSrc ="";
	NodeList rowList = null, stackList = null, columnList = null;
	boolean stockNotPresent;
	boolean stockPresent;
	Node childNode = null;
	Node rowNode = null, stackNode = null, columnNode = null;
	String areaStr = "";
	NodeList locationCodeList = null;
	Node locationNode = null;
	NodeList stockList = null;
	int frmTop;
	int frmLeft;
%>	
<%
	response.setHeader("Expires", "Sat, 6 May 1995 12:00:00 GMT");
	response.setHeader("Cache-Control", "no-store, no-cache,must-revalidate");
	response.addHeader("Cache-Control", "post-check=0, pre-check=0");
	response.setHeader("Pragma", "no-cache");
	int ctr = 0;
	
%>

<script type="text/javascript">
	function getDtl() 
	{
		 var selectedArea = document.getElementById("areaDropDwn").value;
		  window.location.replace("LocationStockOccupancy.jsp?name=" +selectedArea);
		 <% selectedArea = request.getParameter("name");
		 if(scmdasBean !=null )
		 {
			 if (selectedArea == null || selectedArea == "") 
			    {
					selectedArea = scmdasBean.getSelectedArea();
			    }
			areaStr = scmdasBean.getLocDtl(selectedArea, siteCode);
			
		 }
		 %>
	}
	function setAreaSelected() 
	{
		var selectedArea ="<%=request.getParameter("name")%>";
		var ddl = document.getElementById("areaDropDwn");
		var opts = ddl.options.length;
		for ( var i = 0; i < opts; i++) 
		{
			if (ddl.options[i].value == selectedArea)
			{
				ddl.options[i].selected = true;
				break;
			}
	     } 
     }
	  
	 function getStockDtl(location,row,stack,column)
     {
	        var newTable,startTag,endTag;		  
	             	 startTag="<TABLE id='parentStkTbl' class='parentstktbl' ><TBODY>"
	        	 endTag="</TBODY></TABLE>"
                 newTable=startTag;			  
	
	       <%
	       areaStr = areaStr.replaceAll("\"", "'");
	      %>
	     data="<%=areaStr%>";
	     
	    if (window.DOMParser) 
	      {
	  		parser = new DOMParser();
	  		xmlDoc = parser.parseFromString(data, "text/xml");
	  	} else
	  	{
	  		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	  		xmlDoc.async = false;
	  		xmlDoc.loadXML(data);
	  	} 
	      if (xmlDoc != null && xmlDoc != "")
	    		{
	    	  x=xmlDoc.getElementsByTagName("loc_phy_area")[0]
            	  locPhyRowList=x.childNodes;
	    	  if(locPhyRowList !=null)
	    		   {
	    		    for (i = 0; i < locPhyRowList.length; i++) 
 				  {
	    			      locPhyRow = locPhyRowList[i];
	    			      if(locPhyRowList[i].attributes !=null)
	    			    	 { 
	    			    	    rowValue=locPhyRowList[i].attributes.getNamedItem("row").nodeValue;
	    			         
	    			    	    if(rowValue==row)
		    			    		 {
		    			    	      
			    			    	     locPhyStackList=locPhyRow.childNodes;
				    			      
			    			    	      for (j = 0; j < locPhyStackList.length; j++) 
						    				{
					    			    	  locPhyStack=locPhyStackList[j];
	    			    	 
						    			    	  if(locPhyStackList[j].attributes !=null)
						    			    	  { 
						    			                    stackValue=locPhyStackList[j].attributes.getNamedItem("stack").nodeValue;
							    			               	if(stackValue ==stack)
							    			               		{
								    			                  
									    			               	locPhyColList=locPhyStack.childNodes;
										    			    	       for (k = 0; k < locPhyColList.length; k++) 
													    				{
										    			    	          locPhyCol=locPhyColList[k];
										    			    	          colValue=locPhyCol.getAttribute("col");
													    			    	          
													    			    	          if(colValue==column)
													    			    	          {
										    			    	                     
													    			    	          locationCodeList=locPhyCol.childNodes;
													    			    	                
													    			    	                 for (l = 0; l <locationCodeList.length; l++) 
																 					    				{
															    			    	                	     locationCode=locationCodeList[l];
															    			    	                	     locationCodeValue=locationCode.getAttribute("lcode");
															    			    	                	     if(locationCodeValue==location)
																							    			    	      {
																							    			    	            stockList= locationCode.childNodes;
																				    			    	                	     if(stockList !=null)
																				    			    	                	    {	 
																						    			    	                	      for (m = 0; m<stockList.length; m++) 
																									 					    				{
																						    			    	                	    	  stock=stockList[m];
																						    			    	                	    	  stockDtl=stock.childNodes;
																												    			    	          
																						    			    	                	    	  newTable+="<TR>";
																						    			    	                	    	  
																						    			    	                	    	  newTable+="<TD>";
																						    			    	                	    	  newTable+="<TABLE id='stockTbl' class='stocktbl'>";
																						    			    	                	    	  
																						    			    	                	    	  for (n = 0; n<stockDtl.length; n++) 
																																 					 {
																						    			    	                	    		  if(n==0)
																											    			    	        		{ 
																						    			    	                	    	  		  newTable+="<TR>";
																						    			    	                	    		      newTable+="<TD>Item Code :"+stockDtl[n].childNodes[0].nodeValue +"</TD>";
																											    			    	        		
																											    			    	        		}
																						    			    	                	    		     
																						    			    	                	    		     
																						    			    	                	    		   if(n==2)
																						    			    	                	    			  {
																						    			    	                	    			   newTable+="<TR>";
																						    			    	                	    			   newTable+="<TD>Lot No :"+stockDtl[n].childNodes[0].nodeValue +"</TD> ";
																						    			    	                	    			   newTable+="</TR>";
																						    			    	                	    			  } 

																						    			    	                	    		   if(n==1)
																											    			    	        		 { 
																						    			    	                	    		     newTable+="<TD>Description :"+stockDtl[n].childNodes[0].nodeValue+"</TD>"; 
																						    			    	                	    		     newTable+="</TR>";
																											    			    	        		 }
																						    			    	                	    		   
																						    			    	                	    		   if(n==3)
																						    			    	                	    			  {
																						    			    	                	    			   newTable+="<TR>";
																						    			    	                	    			   newTable+="<TD>Lot Serial :"+stockDtl[n].childNodes[0].nodeValue +"</TD> ";
																						    			    	                	    			   newTable+="</TR>";
																						    			    	                	    			  }
																						    			    	                	    		   
																						    			    	                	    		   if(n==4)
																						    			    	                	    			  {
																						    			    	                	    			   newTable+="<TR>";
																						    			    	                	    			   newTable+="<TD > Quantity :"+stockDtl[n].childNodes[0].nodeValue +"</TD> ";
																						    			    	                	    			 
																						    			    	                	    			  }
																						    			    	                	    		   if(n==5)
																						    			    	                	    			  {
																						    			    	                	    			   newTable+="<TD >Hold Quantity :"+stockDtl[n].childNodes[0].nodeValue +"</TD> ";
																						    			    	                	    			   newTable+="</TR>";
																						    			    	                	    			  }
																						    			    	                	    		  
																						    			    	                	    		   if(n==6)
																						    			    	                	    			  {
																						    			    	                	    			   newTable+="<TR>";
																						    			    	                	    			   newTable+="<TD>Allocated Qty :"+stockDtl[n].childNodes[0].nodeValue +"</TD> ";
																						    			    	                	    			   newTable+="</TR>";
																						    			    	                	    			  }
																						    			    	                	    		  
																						    			    	                	    		   if(n==7)
																						    			    	                	    			  {
																						    			    	                	    			   newTable+="<TR>";
																						    			    	                	    			   newTable+="<TD>Mfg. Date :"+stockDtl[n].childNodes[0].nodeValue +"</TD> ";
																						    			    	                	    			  }
																						    			    	                	    		   if(n==8)
																						    			    	                	    			  {
																						    			    	                	    			   newTable+="<TD>Expiry Date :"+stockDtl[n].childNodes[0].nodeValue +"</TD> ";
																						    			    	                	    			   newTable+="</TR>";
																						    			    	                	    			  }
																						    			    	                	    		   if(n==9)
																						    			    	                	    			  {
																						    			    	                	    			   newTable+="</TR>";
																						    			    	                	    			   newTable+="<TD>Retest Date :"+stockDtl[n].childNodes[0].nodeValue +"</TD> ";
																						    			    	                	    			   newTable+="</TR>";
																						    			    	                	    			  }
																												    			    	        		}
																						    			    	                	    	  
																						    			    	                	    	          newTable+="</TABLE>";
																						    			    	                	    	          newTable+="</TD>";
																												    			    	        	  newTable+="</TR>";


																													    			    	        	  if(m< stockList.length-1)
																													    			    	        	  { 
																												    			    	        	       newTable+="<TR> <TD> &nbsp;</TD></TR>";
																													    			    	        	  }
																																 					 }
										 					    				                                                             }
							    			    	                	                                                          }
		 					    				                                                                         }
																 					    				}
													    			    	                 }
													    			    	          
													    			    	}
										    			    	       
				    				                             }	
							    			               	
							    			               	}  
						    			    	  
		    				                    }	
				    			        } 
 				  
	    			    	 }
 				}
	    		}  
	      newTable+=endTag;
	    document.getElementById('stocktableDiv').innerHTML=newTable;
	    $("#stocktableDiv").dialog({width:425,height:350}); 
	    $("#FieldOnFocus").attr('value',this.id); 
	    $('#stocktableDiv').css({
	    	  width: '425px',
	    	});
     	  
		   if(location==null)
  	   {
			 $('#stocktableDiv').dialog('close');
		   }
 
        

	    
     }	

  </script>
</head>
<%
	try {
		
			//ibase.utility.UserInfoBean userInfo = (ibase.utility.UserInfoBean) session.getAttribute("USER_INFO");
			//String user    = userInfo.getLoginCode();
            siteCode       = request.getParameter("SITE_CODE");
			if (siteCode == null || siteCode.trim().length() == 0 ) 
			{
				  //siteCode = userInfo.getSiteCode();
			      }                   
			 siteCode = "TA821";//this is static .because there is no data found
			 if(xmlString == "")
				{
				xmlString = scmdasBean.getLocPhyArea();
							
			       }
			
%>
<body>
	<table >
		<tr>
			<td align="right"> Location Stock Occupancy</td>
		</tr>
		<tr>
			<td align="right" ><label> Area :</label> <select name="areaDropDwn" id="areaDropDwn" onchange="getDtl();" >
					<%
						if (xmlString != null && xmlString.trim().length() > 0) 
						{
								dom = genericUtility.parseString(xmlString);
								if (dom != null)
								{
									dtlList = dom.getElementsByTagName("Detail");
									for (int cntr = 0; cntr < dtlList.getLength(); cntr++) 
									{
										parentNode = dtlList.item(cntr);
										childNodeList = parentNode.getChildNodes();

										for (int nodCtr = 0; nodCtr < childNodeList .getLength(); nodCtr++) 
										{
											currDetail = childNodeList.item(nodCtr);
											childNodeName = currDetail.getNodeName();
											if (childNodeName.trim().equals("loc_phy_area"))
											{
												//locPhyArea = currDetail.getFirstChild().getNodeValue();
												locPhyArea = currDetail.getTextContent();

												if (locPhyArea != "")
												{
													%>
													<option><%=locPhyArea%>
													</option>
													<%
												}
											}
										}

									}
								}
							}
					%>
			</select></td>
			
			<td></td>
		</tr>
		<%
		   if(scmdasBean!=null)
		   {
		      if (selectedArea == null || selectedArea == "") 
			    {
					selectedArea = scmdasBean.getSelectedArea();
				}
    				areaStr = scmdasBean.getLocDtl(selectedArea, siteCode);
		   }
		%>

		<tr>
			<td>
				<div id="wrapper">
					<ul id="index_cards">
						<%
							         frmTop = 50;
							          frmLeft =20;
								
							        if(areaStr !=null && areaStr.length() >0)
							        {
							        dom = genericUtility.parseString(areaStr);
								    rowList = dom.getElementsByTagName("loc_phy_row");
								for (int rowCtr = 0; rowCtr < rowList.getLength(); rowCtr++) 
								{
									rowNode   = rowList.item(rowCtr);
									stackList = rowNode.getChildNodes();
									int cardID = rowCtr + 1;
									List<String>  colImg = new ArrayList<String>();
									
									
									if(stackList.item(0).getChildNodes()!=null)
									{
							
									   for(int totlCol=0; totlCol<stackList.item(0).getChildNodes().getLength(); totlCol++ )
											         	{
										                   stockNotPresent=false;														
									                       stockPresent=false;
												for(int stkCtr = 0; stkCtr < stackList.getLength(); stkCtr++) 
																	{
																	    	stackNode = stackList.item(stkCtr);
																		    columnList = stackNode.getChildNodes();
																		    
																		if (columnList.getLength() > 0) 
																		{
																				columnNode = columnList.item(totlCol);
																				locationCodeList = columnNode.getChildNodes();
																				if(locationCodeList.getLength() >0)
																				{	
																				locationNode = locationCodeList.item(0);
																				stockList = locationNode.getChildNodes();
																					if (stockList.getLength() > 0)
																						{   
																							stockPresent=true;
																						} 
																					else
																					    {
																						stockNotPresent=true;
																			}
										                                 }	
																		
																	}	
																						}
																		
												                        if(stockPresent && stockNotPresent)
																				{
																				 imgSrc ="/ibase/dashboard/scm/images/redgreen.png";	
																				
																				}else if(stockPresent)
																				{
																				   imgSrc ="/ibase/dashboard/scm/images/green.png";	
																				}else
																				{	
																					imgSrc ="/ibase/dashboard/scm/images/red.png";
																				}
								                                        	colImg.add(imgSrc);
												}	
								 }
						%>

						<li id="<%="card-"+cardID%>" style="left:<%=frmLeft%>px;top:<%=frmTop%>px;">
								 <table id='celltable'>
  						   	      <tr> 
  						   	      <td><%=rowNode.getAttributes().item(0).getNodeValue()%></td>
  						   	      <%
  						   	       for(String imgSource :colImg)
						   	    	   {
						   	    		 %> 
						   	    	<td background="<%=imgSource%>"></td> 
						   	    		  <%
						   	    	    } %>
  						   	      </tr> 
									<%
										for (int stkCtr = 0; stkCtr < stackList.getLength(); stkCtr++) 
										{
												    	stackNode = stackList.item(stkCtr);
													    columnList = stackNode.getChildNodes();
													    stockNotPresent=false;														
													     stockPresent=false;
													if (columnList.getLength() > 0) 
													{

														for (int colCtr = 0; colCtr < columnList.getLength(); colCtr++) 
														{
														
															columnNode = columnList.item(colCtr);
															locationCodeList = columnNode.getChildNodes();
															locationNode = locationCodeList.item(0);
															stockList = locationNode.getChildNodes();
																if (stockList.getLength() > 0)
																	{   
																		stockPresent=true;
																	} 
																else
																    {
																		stockNotPresent=true;
																	}
														}
														
														if(stockPresent && stockNotPresent)
															{
															 imgSrc ="/ibase/dashboard/scm/images/redgreen.png";	
															}else if(stockPresent)
															{
															   imgSrc ="/ibase/dashboard/scm/images/green.png";	
															}else
															{	
																imgSrc ="/ibase/dashboard/scm/images/red.png";
															}
														
									%>
									<tr id="info">
										<td  background="<%=imgSrc%>"><%=stackNode.getAttributes().item(0).getNodeValue()%></td>
										<%
														for (int colCtr = 0; colCtr < columnList.getLength(); colCtr++) 
														{
															columnNode = columnList.item(colCtr);
															locationCodeList = columnNode.getChildNodes();
															locationNode = locationCodeList.item(0);
															stockList = locationNode.getChildNodes();
															if (stockList.getLength() > 0)
															{   
																stockPresent=true;
										%>
										<td class="itemcell" onclick="getStockDtl('<%=locationNode.getAttributes().item(0) .getNodeValue()%>','<%=rowNode.getAttributes().item(0).getNodeValue()%>','<%=stackNode.getAttributes().item(0).getNodeValue()%>','<%=columnNode.getAttributes().item(0).getNodeValue()%>');"> <%=columnNode.getAttributes().item(0).getNodeValue()%> </td>
										<%
											} 
											else
										    {
												stockNotPresent=true;
										%>
										<td class="cellempty" onclick="getStockDtl();"><%=columnNode.getAttributes().item(0).getNodeValue()%>
										</td >
										<%
											}
										}
													}
										%>
									</tr>
									<%
										}
									%>
								</table>
						</li>
						<%
							        frmLeft+= 25;
									frmTop+= 25;
								}
								}
						%>
					</ul>
		        </div>	
			</td>
			<td>
				<div id="stocktableDiv" width="400px">

				</div>
			</td>
		</tr>
	</table>
	
	<%
		} catch (RemoteException ex) {
			System.out .println("Exception : LocationStockOccupancey  sMessageArguments(String,wizardBean) :" + ex); //$NON-NLS-1$

		} catch (ITMException ex) {
			System.out .println("Exception : LocationStockOccupancey  sMessageArguments(String,wizardBean) :" + ex); //$NON-NLS-1$

		} catch (Exception ex) {
			System.out .println("Exception : LocationStockOccupancey  sMessageArguments(String,wizardBean) :" + ex); //$NON-NLS-1$
		}
	%>

	<script>
		window.onload = setAreaSelected;
	</script>

</body>
</html>

