<%
/******************************************************************************
 * Copyright (c) 2000-2005 e-Deal
 *
 * e-Deal S.A.
 * 55 rue Brillat Savarin
 * 75013 Paris
 * France
 *
 * T: +33 (0)1 53 80 80 30
 * F: +33 (0)1 73 01 69 77
 * http://www.e-deal.com
 * 
 * La diffusion de ce code source sous quelque forme que ce soit sans
 * l'autorisation de E-DEAL est interdite.
 *
 * Vous êtes autorisés à modifier ce code source uniquement pour votre usage
 * propre et sous réserve que les mentions de copyright demeurent intactes.
 *
 * Ce code est fourni en l'état. Aucune garantie d'aucune sorte, explicite ou
 * implicite n'est donnée. En aucun cas E-DEAL ne pourra être tenu pour
 * responsable des dommages pouvant résulter de l'utilisation de ce code
 * source.
 ******************************************************************************/
%><%@ page language="java"%>
<%@ page import="com.edeal.frontline.navigation.*" %>
<%@ page import="com.edeal.frontline.*" %>
<%@ page import="java.util.*" %>
<%@taglib uri="http://www.e-deal.com/taglib/fl" prefix="fl"%>
<%@ page import="java.text.DecimalFormat" %>
<%
final org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory.getLog(getClass());
FlRequest flRequest = new FlRequest(request);
FlContext context = flRequest.getContext();
DataDictionary dico = context.getDataDictionary();

DecimalFormat df = FlLocale.getInstance(session).getDecimalFormat("#,##0.##");

Vector list = (Vector) request.getAttribute("ListSeg");
Vector listDive = (Vector) request.getAttribute("ListDive");

	if (listDive == null) listDive = new Vector();
	if (list == null) list = new Vector();

	String pDive = Utils.getRequestParameter(context, request, "dive");
	
	// search criteria
	String sc = flRequest.getRequestParameterOrAttribute("sc");
	// object uri
	String ou = flRequest.getRequestParameterOrAttribute("ou");
	// id
	
	Map searchCriteria = new Hashtable();
	if (sc != null) {
		searchCriteria = WebUtils.parseQueryString(sc);
	}
	
	request.setAttribute("selectedTab", "1");
	
	Vector dive = null;
	if (pDive != null) {
		dive = Utils.explodeList(pDive, ";");
	}
	
	Vector divingAxes = new Vector();	
	boolean calcFirst = "1".equals(Utils.getRequestParameter(context, request, "calcFirst"));
	
	Vector axesCount = new Vector();
%>

<link href="<fl:webapp/>/css/smalltabs_css.jsp" rel="stylesheet" type="text/css">
<script>

<%-- THIS FUNCTION MAKES US DIVE ALONG THE GIVEN SEGMENT --%>
function dive(id) {
	var loc = this.location.href;
		if (loc.indexOf("dive") > -1) {
			this.location.href += ';'+id;
		}else {
			this.location += '&dive='+id;
		}
}

<%-- THIS FUNCTION MAKES US GO BACK TO THE GIVEN SEQUENCE OF SEGMENTS --%>
function upDive(dive) {
	var loc = this.location.href;
	loc = loc.substr(0, loc.indexOf('dive=') + 5) + dive;
	this.location = loc;
}

<%-- THIS UNSELECTS ALL SELECTED SEGMENTS --%>
function doReinit() {
	var loc = this.location.href;
	loc = loc.substr(0, loc.indexOf('dive='));
	this.location = loc;
}

<%-- THIS CREATES A POPULATION FOR THE CURRENT SELECTION OF SEGMENTS --%>
function doNewPopulation() {
	this.parent.location = '<%=flRequest.computeURL("/create_axis_population.fl?initSegments="+pDive)%>';
}

function doSearch() {
	var myQuery = "";
	var myForm = document.forms[0];
	
	if (myForm.dive!=null){
		<% if (pDive != null){%>
		myQuery += 'dive=<%=pDive%>';
		<%}%>
	}
	
	if (myQuery == "") {
		myForm.action = '<fl:link url="/list_axis.fl"/>';
	} else {
		myForm.action = '<fl:link url="/list_axis.fl?1=1"/>' + '&'+myQuery;		
	}				
		
	myForm.submit();
}
</script>


<body class="object_body" style="border:none;overflow: auto;left:0px; width: 100%; top:0px; height: 100%">
<fl:bigTabs objectTitle="<%= Utils.getMessage(session, \"L64D8\", false, true, \"Liste des segmentations\") %>" ou="<%=\"/list_axis.fl\"%>" criteriaTitle="<%= Utils.getMessage(session, \"L64D7\", \"Recherche segmentation\") %>" listTitle="<%= Utils.getMessage(session, \"L64D8\", false, true, \"Liste des segmentations\") %>" lu="/list_axis.fl" cu="/list_axis_criteria.fl">
<form action="javascript:doSearch()" method="post">
	<input type="hidden" name="dive" value="<%= pDive !=  null ? pDive : "" %>">
</form>

<%-- IF THERE ARE DIVING SEGMENTS --%>
<% if (dive != null && dive.size() > 0) {  %>
<table border="0" width="100%" class="container">
<%-- SHOW TOOLBAR FOR NEW SEGMENT (ONLY IF SEVERAL AXIS SELECTED) AND NEW POP--%>
	<tr class="fieldline">
		<td colspan="<%= dive.size() %>" style="width: 30%">&nbsp;</td>
		<td class="dataValue" style="text-align: right" colspan="3">
			<% if (false && dive.size() > 1) { %>
			<a href="javascript:doNewAxis()"><fl:getMessage code="L630A" alt="Axe"/> +</a>
			|
			<% } %>
			<a href="javascript:doNewPopulation()"><fl:getMessage code="F000C" alt="Population"/> +</a>
			|
			<a href="javascript:doReinit()"><fl:getMessage code="L630E" alt="Déselectionner tout"/></a>
		</td>
	</tr>
</table>
<%-- HEADERS--%>
<table border="0" width="100%" class="container">
	<tr class="fieldline">		
		<td class="titreInterneSmalltabs" colspan="<%= dive.size() + 4 %>" width="100%"><fl:getMessage code="L630B" alt="Segments sélectionnés"/></td>		
	</tr>
	<%-- tr class="ligne_separation_couleur"><td colspan="<%= dive.size() + 3 %>"></td></tr--%>
	<tr class="fieldline">
		<td colspan="<%=dive.size()%>" style="width: 30%;" class="headerDataGrid"></td>
		<td style="width: 25%;" class="headerDataGrid">
			<fl:getMessage code="F2025" alt="Nom"/>
		</td>
		<td style="width: 25%;" class="headerDataGrid">
			<fl:getMessage code="F609A" alt="Volume"/>
		</td>
		<td style="width: 20%;" class="headerDataGrid">
			<fl:getMessage code="F60CC" alt="PRP"/>
		</td>
	</tr>	
	
<%-- FOR EVERY DIVING AXIS, LOOK FOR THE DISPLAY INFO IN THE LISTSUMMARY RESULT --%>	
	<% 
	
	String upDive = ""; //STRING TO CONSTRUCT TO GO BACK UP
	Vector mySegments1 = new Vector();
	//FOR EACH SEGMENT, CALCULATE VOLUME AND PRP, AND DISPLAY INFO
	for (int i=0 ; dive != null && i < dive.size() ; i++) {
			upDive += (i!=0 ? ";" : "") + dive.elementAt(i);
			for (int j=0 ; j<listDive.size() ; j++) {
				Hashtable currSeg = (Hashtable) listDive.elementAt(j);
				String segID = (String)currSeg.get("SegID");
				String axeID = (String)currSeg.get("SegAxeID");
					
				divingAxes.add(axeID);
				String segCode = (String)currSeg.get("SegCode");
				String segLabel = (String)currSeg.get("SegRefValue:RefTe1");
				String axeCode = (String)currSeg.get("SegAxeID:AxeCode");
				String axeName = (String)currSeg.get("SegAxeID:AxeName");
				if (segID.equals(dive.elementAt(i))) { 
					mySegments1.add(segID);
					Vector countSegments1 = SegmentBean.countSegment(context, mySegments1);
					Number volume1 = null;
					Number prp1 = null;
					if (countSegments1 != null) {
						if (countSegments1.elementAt(0)!=null) {
							volume1 = (Number)countSegments1.elementAt(0);
						}
						if (countSegments1.elementAt(1) != null) {
							prp1 = (Number)countSegments1.elementAt(1);
						}
					}
			%>
	<tr class="tabSimpleFdBleuImportant">
		
		<% int spacerNB = 0;
			for (int k=0 ; k < i ; k++) { %>
		<td style="width: 2%;">&nbsp;</td>
		<% 		spacerNB = k;
			} %>
		<td colspan="<%= dive.size() + 3-i %>"  style="width: 97%;">
			<A HREF="<%=flRequest.computeURL("/read_axis.fl?id="+axeID)%>" target="_blank"><img src="<fl:webapp/>/icons/ico/zoom.gif" border="0" style="vertical-align: middle;"></A>
				&nbsp;<%= Utils.formatToWeb(axeCode, false)%> - <%= Utils.formatToWeb(axeName, false)%>
		</td>
	</tr>
	<tr class="tabSimpleFdBlanc">
		
		<td style="width: 30%;" colspan="<%= dive.size() %>">
			<% if (currSeg.get("SegAttID") != null) { %>
			<a href="javascript:upDive('<%= upDive %>')">
				<img src="<fl:webapp/>/download?id=<%= currSeg.get("SegAttID") %>" border="0" alt="">
			</a>
			<% } else { 
				String color = null;
				try { color = (String)dico.getRefDisp(session, "Col", "Te2", (String)currSeg.get("SegColor")); }
				catch (Exception e) {}					
			%>
			<a href="javascript:upDive('<%= upDive %>')" style="font-size:10pt ;font-weight: bold<%= color != null ? ";color: " + color : ""%>">
				<%= Utils.formatToWeb(segCode, false) %>
			</a>
			<% } %>
		</td>
		<td style="width: 25%;"><a href="javascript:upDive('<%= upDive %>')"><%= Utils.formatToWeb(segLabel, false) %></a></td>	
		<td style="width: 25%;text-align: right;"><%= Utils.formatToWeb(volume1, false) %></td>
		<td style="width: 20%;text-align: right;"><%= new Double(Double.MIN_VALUE).equals(prp1) ? "&nbsp;" : df.format(prp1) %></td>
	</tr>
			<% 		break;	//IF WE HAVE FOUND THE SEGMENT WE WANT, GO TO NEXT
				} //END OF IF RIGHT SEGMENT
			} //END OF FOR (J)
	 } //END OF FOR (I) 
	 %>
<%-- JUMP A LINE --%>
		<%--<tr><td class="transparent" colspan="<%= dive.size() + 3 %>"><hr noshade></hr></td></tr>--%>
		<%--<tr><td class="transparent" colspan="4"><img src="<fl:webapp/>/icons/spacer.gif" width="3" height="2"></td></tr>
		<tr><td colspan="4"><img src="<fl:webapp/>/icons/spacer.gif" height="1"></td></tr>
		<tr><td class="transparent" colspan="4"><img src="<fl:webapp/>/icons/spacer.gif" width="3" height="2"></td></tr>--%>
</table>
<% } //END OF IF THERE ARE DIVING SEGMENT

if (list != null && list.size() > 0) {
%>
		<%-- FOR EACH SEGMENT --%>
		<% boolean firstOne = true;
		boolean allInDive = true;
		String prevAxeID = "";
		
		int compteur = 0;
		for (int j=0; j<list.size();j++) {
					
			Hashtable currSeg = (Hashtable) list.elementAt(j);
			String segID = (String)currSeg.get("SegID");
			
			//IF IT ISN'T A DIVING SEGMENT, DISPLAY INFO
			if (dive == null || !dive.contains(segID)) {	
				String segCode = (String)currSeg.get("SegCode");
				String axeID = (String)currSeg.get("SegAxeID");
				//IF WE ARE ALREADY DIVING ALONG THIS AXIS, SKIP
				if (divingAxes.contains(axeID)) {
					continue;
				}else{
					allInDive = false;
				}
					
				String segLabel = (String)currSeg.get("SegRefValue:RefTe1");
				String axeCode = (String)currSeg.get("SegAxeID:AxeCode");
				String axeName = (String)currSeg.get("SegAxeID:AxeName");
			
				if (firstOne) {
%>
		<%-- REMAINING SEGMENT HEADER (IF AT LEAST ONE) --%>
<table class="container" width="100%">	
<tr class="ligne_separation"><td></td></tr>	
		<tr class="fieldline">			
			<% if (dive != null && dive.size() > 0) { %>
			<td class="titreInterneSmalltabs" colspan="4"><fl:getMessage code="L630C" alt="Segments restants"/></td>
			<% } else { %>
			<% } %>
		</tr>
		<tr class="fieldline">
			<td style="width: 30%;" class="headerDataGrid"></td>
			<td style="width: 25%;" class="headerDataGrid">
				<fl:getMessage code="F2025" alt="Nom"/>
			</td>
			<td style="width: 25%;" class="headerDataGrid">
				<fl:getMessage code="F609A" alt="Volume"/>
			</td>
			<td style="width: 20%;" class="headerDataGrid">
				<fl:getMessage code="F60CC" alt="PRP"/>
			</td>
		</tr>	
<%				
					firstOne = false;
				}
				//IF FIRST SEGMENT IN AXIS, SHOW AXIS INFO
				if (!prevAxeID.equals(axeID)) { %>
		<tr class="tabSimpleFdBleuImportant">
			<td colspan="4">
				<A HREF="<%=flRequest.computeURL("/read_axis.fl?id="+axeID)%>"><img src="<fl:webapp/>/icons/ico/zoom.gif" border="0" style="vertical-align:middle;"></A>
					&nbsp;<%= Utils.formatToWeb(axeCode, false)%> - <%= Utils.formatToWeb(axeName, false)%>
			</td>
			
		</tr>
					
				<%	prevAxeID = axeID;
				}
				Number volume = null;
				Number prp = null;
				//IF THERE ARE DIVING AXIS OR IF FORCE CALC IS CHECKED, CALCULATE
				if (dive != null || calcFirst) {
					Vector mySegments = null;
					if (dive == null) {
						mySegments = new Vector();
					} else {
						mySegments = new Vector(dive);
					}
					mySegments.addElement(segID);
					Vector countSegments = SegmentBean.countSegment(context, mySegments);
					if (countSegments != null) {
						if (countSegments.elementAt(0)!=null) {
							volume = (Number)countSegments.elementAt(0);
						}
						if (countSegments.elementAt(1) != null) {
							prp = (Number)countSegments.elementAt(1);
						}
					}
				//OTHERWISE, TAKE PRE-CALCULATED VALUES
				} else {
					if (currSeg.get("SegCount") != null) {
						volume = (Number)currSeg.get("SegCount");
					}
					if (currSeg.get("SegPRP") != null) {
						prp = (Number)currSeg.get("SegPRP");
					}
				}				
		%>
			<tr class="<%= (j%2 == 0) ? "tabSimpleFdBlanc" : "tabSimpleFdBleu" %>">
				<td nowrap style="width: 20%;">
					<% if (currSeg.get("SegAttID") != null) { %>
					<a href="javascript:dive('<%= currSeg.get("SegID")%>')">
						<img src="<fl:webapp/>/download?id=<%= currSeg.get("SegAttID") %>" border="0" alt="">
					</a>
					<% } else { 
						String color = null;
						try { color = (String)dico.getRefDisp(session, "Col", "Te2", (String)currSeg.get("SegColor")); }
						catch (Exception e) {}					
					%>
					<a href="javascript:dive('<%= segID %>')" style="font-size: 10pt;font-weight: bold<%= color != null ? ";color: " + color : ""%>">
						<%= Utils.formatToWeb(segCode, false) %>
					</a>
					<% } %>
				</td>
				<td nowrap style="width: 20%;">
					<a href="javascript:dive('<%= currSeg.get("SegID")%>')"><%= Utils.formatToWeb(segLabel, false)%></a>
				</td>
				<td nowrap style="width: 20%;text-align: right;">
						<%= Utils.formatToWeb(volume, false) %>
				</td>
				<td nowrap style="width: 1%;text-align: right;">
						<%= (prp == null || new Double(Double.MIN_VALUE).equals(prp)) ? "&nbsp;" : df.format(prp) %>
				</td>
			</tr>
<%			} //END OF IF IT ISN'T A DIVING SEGMENT 
		} //END OF FOR EACH SEGMENT
			
%>
 <%
	if (!allInDive) {
 %>
 </table><%}//Il faut ne pas le mettre si il n'existe pas de segment restants%>
<%	}else { %>
<br>
<span class="alertbandeau"><fl:getMessage code="L6309" alt="Aucun segment ne correspond à vos critères de recherche."/></span>

<%	} %>
</fl:bigTabs>
</body>
