<%@page import="com.capgemini.framework.common.SettingsHelper"%>
<%@page import="com.capgemini.framework.common.StringHelper" %>
<%@page import="com.capgemini.reports.database.javabean.enumeration.ReportCriteriaEnum"%>
<%@page import="com.capgemini.reports.database.manager.MngSchedulingCriteriaVal"%>
<%@page import="java.util.Map"%>
<%
    /*
     * Available request parameters:
     *  "isUpdateReport"       (boolean)             Is this a submission of a NEW report or an UPDATE?
     *  "idReport"             (long)    <Optional>  Report Id in case of an UPDATE request
     */
    boolean isDevMode            = SettingsHelper.isDevMode();
    boolean isUpdate = Boolean.valueOf(request.getParameter("isUpdateReport"));
    Long    reportID             = isUpdate ? Long.valueOf(request.getParameter("idReport")) : new Long(-1);
	        
    // Default values in each field. May contain nothing / existing values / development values
	String theDefaultGen = "";
	String theDefaultProduct = "";
	String theDefaultUnit = "";
	String theDefaultLanguage= "";
	String theDefaultPrinterType= "";

    if (isDevMode){ // Devmode default values
		theDefaultGen     = "C25";
		theDefaultProduct = "29R0";
		theDefaultUnit    = "245";
	}
    
    if( isUpdate ){ // Existing values to reload from DB
        Map<ReportCriteriaEnum, String> theFunctionalCriteriaValues = new MngSchedulingCriteriaVal().getFunctionalMap(reportID);

		theDefaultGen         = StringHelper.replaceNullByEmpty( theFunctionalCriteriaValues.get(ReportCriteriaEnum.GENERIC_ASSEMB));
		theDefaultProduct     = StringHelper.replaceNullByEmpty( theFunctionalCriteriaValues.get(ReportCriteriaEnum.PRODUCT));
		theDefaultUnit        = StringHelper.replaceNullByEmpty( theFunctionalCriteriaValues.get(ReportCriteriaEnum.UNIT));
		theDefaultLanguage    = StringHelper.replaceNullByEmpty( theFunctionalCriteriaValues.get(ReportCriteriaEnum.LANGUAGE));
		theDefaultPrinterType = StringHelper.replaceNullByEmpty( theFunctionalCriteriaValues.get(ReportCriteriaEnum.TYPE_IMPRESSION));
	}
%>
<!-- Build input radio EN / FR, as well as radio Certification / Realisation -->
<%! private String getSelectedLanguage(final String language){
        String selectedFR = "";
        String selectedEN = "";
        StringBuilder buildSelect = new StringBuilder();
        if("Francais".equals(language)){
            selectedFR ="selected=\"selected\"";
        }else{
            selectedEN ="selected=\"selected\"";
        }
        buildSelect.append("<option ").append(selectedFR).append(" value=\"Francais\">Fran&ccedil;ais</option>");
        buildSelect.append("<option ").append(selectedEN).append(" value=\"Anglais\">Anglais</option>");    
        return buildSelect.toString();
    }

    private String getPrinterType(final String printerType){
        String selectedDefCertifie = "";
        String selectedDefRealisation = "";
        StringBuilder buildSelect = new StringBuilder();
        if("Def_certifiee".equals(printerType)){
            selectedDefCertifie ="selected=\"selected\"";
        }else{
            selectedDefRealisation ="selected=\"selected\"";
        }
        buildSelect.append("<option ").append(selectedDefCertifie).append(" value=\"Def_certifiee\">D&eacute;finition certifi&eacute;e</option>");
        buildSelect.append("<option ").append(selectedDefRealisation).append(" value=\"Def_realisation\">D&eacute;finition de r&eacute;alisation</option>");    
        return buildSelect.toString();
    }
%>

<!-- HTML, will probably be imported right inside a form -->
<table cellspacing="0" cellpadding="3" border="0">
	<tr>
        <td class="tdFieldTitle required">G&eacute;n&eacute;rique* :</td>
        <td>
            <input type="text" id="generic" name="generic" value="<%= theDefaultGen %>" />
        </td>
    </tr>
	<tr>
        <td class="tdFieldTitle required">Produit* :</td>
        <td>
            <input type="text" id="product_id" name="product_id" value="<%= theDefaultProduct %>"/>
        </td>
    </tr>
	<tr>
        <td class="tdFieldTitle required">Unit* :</td>
        <td>
            <input type="text" id="unit" name="unit" value="<%= theDefaultUnit %>"/>
        </td>
    </tr>
	<tr>
        <td class="tdFieldTitle">Type d'impression :</td>
        <td>
        	<select type="text" id="type_impression" name="type_impression">
        		<% if( isUpdate ){ %>
        			<%= getPrinterType(theDefaultPrinterType) %> 
        		<% }else{ %> 
        			<option selected="selected" value="Def_certifiee">D&eacute;finition certifi&eacute;e</option>
        			<option value="Def_realisation">D&eacute;finition de r&eacute;alisation</option>		
        		<% } %>
        	</select>
        </td>
    </tr>
	<tr>
        <td class="tdFieldTitle">Langue :</td>
        <td>
        	<select type="text" id="langue" name="langue">
        		<% if( isUpdate  ){ %>
        			<%= getSelectedLanguage(theDefaultLanguage) %> 
        		<% }else{ %> 
        			<option selected="selected" value="Francais">Fran&ccedil;ais</option>
        			<option value="Anglais">Anglais</option>				
        		<% } %>
        	</select>
        </td>
    </tr>
	<tr>
		<td class="tdFieldTitle" colspan="2" style="text-align:center;">
            <br/>
			<div id="loading" style="display:none;">
				<i>Traitement en cours...</i>
                <br/>
                <img src="/kurt-web/ihm/progress.gif" vspace="6" width="129" height="20">
			</div>
		</td>
	</tr>
</table>
			
<!-- Internal functional Javascript methods (checks etc.) -->
<script>
	/* Automatic checks before sending the form */
	function checkForm() {
		var isCheckOk = false;
		
		if (isMandatoryFieldEmpty("generic")){
			parent.Dialog("Erreur", "Veuillez saisir le champ obligatoire : G&eacute;n&eacute;rique.");
		}else if (isMandatoryFieldEmpty("product_id")){
			parent.Dialog("Erreur", "Veuillez saisir le champ obligatoire : Produit.");
		}else if (isMandatoryFieldEmpty("unit")){
			parent.Dialog("Erreur", "Veuillez saisir le champ obligatoire : Unit.");
		}else if (!isNumber("unit")){
			document.getElementById("unit").focus();
			parent.Dialog("Erreur", "Vous devez saisir un entier dans le champ Unit");
		}else{
			isCheckOk = true;
		}
	
		return isCheckOk;
	}
</script>