<%@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" type="text/css"    href="/ibase/dashboard/scm/css/scmdashboard.css"/>
 <link rel="stylesheet" type="text/css"	   href="/ibase/dashboard/scm/css/ui.dropdownchecklist.standalone.css" />
 <link rel="stylesheet" type="text/css"    href="/ibase/dashboard/scm/css/jquery-ui.css"/> 
 <script type="text/javascript"	           src="/ibase/dashboard/scm/jquery/jquery-1.6.1.min.js"></script>
 <script type="text/javascript"	           src="/ibase/dashboard/scm/jquery/jquery-ui-1.8.13.custom.min.js"></script>
 <script type="text/javascript"	           src="/ibase/dashboard/scm/jquery/ui.dropdownchecklist-1.4-min.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 = "",imgSrc ="";
	String siteCode = "";
	String locationRange="";
	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">   
 $(document).ready(function(){
 	$("#areaDropDwn").dropdownchecklist({emptyText: "For previous.", width: 100 });
  });
 
 </script>
 <script type="text/javascript">	
	  
	 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("siteCode");             
			if (siteCode == null || siteCode.trim().length() == 0 ) 
			{
			 //siteCode = userInfo.getSiteCode();
			   siteCode  = "TA387";
			      }                   
			// siteCode = "TA387";//this is static .because there is no data found
			 if(xmlString == "")
				{
				xmlString = scmdasBean.getLocPhyArea();
			 }
			//Add New Code on dated 21 Aug 2014
			
             selectedArea         = request.getParameter("areaDropDwn");
             locationRange        = request.getParameter("locationRange");	
             if(locationRange==null){
             locationRange ="";
             }
             if(selectedArea!=null && selectedArea.trim().length() != 0 )            
              { 
           	  areaStr = scmdasBean.getLocDtl(selectedArea, siteCode,locationRange);
           	  
              }
             
          //end code on dated 21Aug2014   
%>
<body>
<form method="post" action="LocationStockOccupancy.jsp"	name="LocationStockOccupancy">

	<table align="center" >
		<tr>
			<td align="center"> Location Stock Occupancy</td>
		</tr>
		<tr>
			<td align="center" >Area :<select name="areaDropDwn" id="areaDropDwn" >
					<%
						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>		
			Location Range Like:  <input type text  name="locationRange" id="locationRange" size="10"  value="<%=locationRange%>"></td>
			<td>   <input type="submit" name="showChart" value="View"/>  
		</td>
		</tr>
		<tr>
			<td>
				<div id="wrapper">
					<ul id="index_cards">
						<%
							         frmTop = 50;
							         frmLeft =50;
								
							 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>();
									List<String>  columnArray = new ArrayList<String>();
									List<String>	 columnDataList=new ArrayList();
									
									
									boolean isStkPresnt=false;
									boolean  isStkNotPresnt=false;
									String imagePath="";
									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);
																				
																				if(columnNode!=null)	
																				{
																					if(columnNode.getChildNodes() !=null)
																						{		
																							locationCodeList = columnNode.getChildNodes();
																					
																							if(locationCodeList.getLength() >0)
																								{	
																									locationNode = locationCodeList.item(0);
																									stockList    = locationNode.getChildNodes();
																									columnArray.add(columnNode.getAttributes().item(0).getNodeValue());
																									
																										if (stockList.getLength() > 0)
																											{   
																												stockPresent=true;
																												isStkPresnt=true;
columnArray.add(columnNode.getAttributes().item(0).getNodeValue());
																											} 
																										else
																									    {
																										stockNotPresent=true;
																										isStkNotPresnt=true;
columnArray.add(columnNode.getAttributes().item(0).getNodeValue());
																									    }
																								}
											                          					 }	
																					
																				}	
																		
																	}	
																						}
																		
												                        if(stockPresent && stockNotPresent)
																				{
																				 imgSrc ="/ibase/dashboard/scm/images/yellow.png";	
																				
																				}else if(stockPresent)
																				{
																				   imgSrc ="/ibase/dashboard/scm/images/green.png";	
																				}else
																				{	
																					imgSrc ="/ibase/dashboard/scm/images/white.png";
																				}
								                                        	colImg.add(imgSrc);
												}	
								 }
									
									
									
									//System.out.println("isStkPresnt ="+isStkPresnt +" and isStkNotPresnt="+isStkNotPresnt);
									 if(isStkPresnt && isStkNotPresnt)
									{
										imagePath ="/ibase/dashboard/scm/images/yellow.png";	
									
									}else if(isStkPresnt)
									{
										imagePath ="/ibase/dashboard/scm/images/green.png";	
									}else if(isStkNotPresnt)
									{	
										imagePath ="/ibase/dashboard/scm/images/white.png";
									}
							
						%>

						<li id="<%="card-"+cardID%>" style="left:<%=frmLeft%>px;top:<%=frmTop%>px;">
								 <table id='celltable'>
  						   	      <tr> 
  						   	      <td background="<%=imagePath%>"><%=rowNode.getAttributes().item(0).getNodeValue()%></td>
  						   	      <%
  						   	 columnDataList=new ArrayList();
    	  						   	 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);
    														
    														if(columnNode!=null)	
    														{
    															if(stkCtr==0)
    															{	
    															columnDataList.add(columnNode.getAttributes().item(0).getNodeValue());		
    														     }
    														}
    													}
    												}
    									}    
    						   	      for( int i=0;i<colImg.size(); i++)
  						   	    	   {
  						   	    		 %>
  									<td background="<%=colImg.get(i)%>" align="right"><%= columnDataList.get(i)%></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);
															
															if(columnNode!=null)	
															{
																if(columnNode.getChildNodes() !=null)
																	{		
																		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/yellow.png";
														  }
														 else if(stockPresent)
														  {
															   imgSrc ="/ibase/dashboard/scm/images/green.png";	
														  }
														 else if(stockNotPresent)
														  {	
																 imgSrc ="/ibase/dashboard/scm/images/white.png";
														  }
														
														 %>
															<tr id="info">
																<td background="<%=imgSrc%>" align="right"><%=stackNode.getAttributes().item(0).getNodeValue()%></td>
																<%
								
														for (int colCtr = 0; colCtr < columnList.getLength(); colCtr++) 
														{
															columnNode = columnList.item(colCtr);
															
															if(columnNode!=null)	
															{
																if(columnNode.getChildNodes() !=null)
																	{		
																		locationCodeList = columnNode.getChildNodes();
																
																		if(locationCodeList.getLength() >0)
																			{	
																				locationNode = locationCodeList.item(0);
																				stockList = locationNode.getChildNodes();
																			//	 System.out.println("stockList.getLength() in real ="+stockList.getLength());
																					if (stockList.getLength() > 0)
																						{   
										%>
										<!--<!--%=columnNode.getAttributes().item(0).getNodeValue()%--> 
										<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()%>');"> <%=locationNode.getAttributes().item(0) .getNodeValue()%> </td>
										<%
											} 
											else
										    {
										%>
										<td class="cellempty" onclick="getStockDtl();"><%=locationNode.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$
		}
	%>
</form>
<script>
  var areaCode           ="<%=selectedArea%>";
  var locRange           ="<%=locationRange%>";
  
  document.getElementById("locationRange").value=locRange;
   window.onload      = new function() { 

	    var currList   = document.getElementById("areaDropDwn");         
        for (var m = 0; m < currList.options.length; m++) 
            	 {
        	     var checkval = currList.options[m].value;
        	      if(checkval==areaCode)
    				  {
                        
        			   currList.options[m].selected = true;
    				  }
    		       }
         
    
           
      
};

</script>
</body>
</html>

