<%@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 theDefaultGenMont = "";
	String theDefaultProduct = "";
	String theDefaultUnit = "";
	String theDefaultDate = "";
		
    if (isDevMode){ // Devmode default values
		theDefaultGen     = "E24";
		theDefaultGenMont = "24E";
		theDefaultProduct = "30E0";
		theDefaultUnit    = "20";
		theDefaultDate    = "01-01-2015";
	}

    if( isUpdate){ // Existing values to reload from DB
        Map<ReportCriteriaEnum, String> theFunctionalCriteriaValues = new MngSchedulingCriteriaVal().getFunctionalMap(reportID);

		theDefaultGen     = StringHelper.replaceNullByEmpty( theFunctionalCriteriaValues.get(ReportCriteriaEnum.GENERIC));
		theDefaultGenMont = StringHelper.replaceNullByEmpty( theFunctionalCriteriaValues.get(ReportCriteriaEnum.GENERIC_ASSEMB));  
		theDefaultProduct = StringHelper.replaceNullByEmpty( theFunctionalCriteriaValues.get(ReportCriteriaEnum.PRODUCT));
		theDefaultUnit    = StringHelper.replaceNullByEmpty( theFunctionalCriteriaValues.get(ReportCriteriaEnum.UNIT));
		theDefaultDate    = StringHelper.replaceNullByEmpty( theFunctionalCriteriaValues.get(ReportCriteriaEnum.EFFECTIVITY_DATE));
 
	}
%>

<!-- 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 &quot;Vue Technique&quot;* :</td>
        <td>
            <input type="text" id="generic" name="generic" value="<%=theDefaultGen%>" />
        </td>
    </tr>
    <tr>
        <td class="tdFieldTitle required">G&eacute;n&eacute;rique &quot;Vue montage&quot;* :</td>
        <td>
            <input type="text" id="generic_assemb" name="generic_assemb" value="<%=theDefaultGenMont%>" />
        </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">Date d'effectivit&eacute; :</td>
        <td>
            <input id="datepicker" type="text" name="datepicker" style="cursor: hand;" value="<%=theDefaultDate%>"/>
            <div class="flow-element">
                <div id="container">
                </div>
            </div>
        </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("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 (isMandatoryFieldEmpty("generic")) {
			parent.Dialog("Erreur", "Veuillez saisir le champ obligatoire : G&eacute;n&eacute;rique.");
		} else if (isMandatoryFieldEmpty("generic_assemb")) {
			parent.Dialog("Erreur", "Veuillez saisir le champ obligatoire : G&eacute;n&eacute;rique Montage.");
		} 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;
	}
	/* Initialize date picker */
	initPicker('datepicker', 'container');
</script>