<%@page import="java.util.*, java.text.SimpleDateFormat, fr.gouv.finances.dgfip.xemelios.web.imports.*, fr.gouv.finances.dgfip.utils.Pair"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %>
<c:set var="codeEcran" scope="request"><bean:message key="imports.codeEcran"/></c:set>
<c:set var="titreEcran" scope="request"><bean:message key="imports.titreEcran"/></c:set>
<%@ include file="/WEB-INF/jsp/include/entete.jsp" %>
<%
    Import ip = (Import)request.getAttribute("import");
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
%>
<style type="text/css">
    .import-detail {
        margin-top: 12px;
        margin-left: 12px;
        border-collapse: separate;
        background-color: white;
        padding: 1px;
        spacing: 1px;
        width: 720px;
        border-spacing: 1px;
    }
    .right { text-align: right; }
    .center { text-align: center; }
    th.import {
        background-color: #5988E9;
        color: #FFFFFF;
        font-weight: bold;
        font-size: 12px;
        text-align: left;
        vertical-align: baseline;
    }
    td.import {
        font-size: 12px;
        background-color: #9AB0DC;
        margin-left: 3px;
        vertical-align: baseline;
    }
    td.new {
        font-size: 12px;
    }
    p { font-size: 12px;}
</style>
<script src="<%=request.getContextPath()%>/js/getHttpObject.js" language="javascript" ></script>
<script type="text/javascript">
    function fileTypeChanged() {
        select = document.getElementById("fileType");
        docId = select.options[select.options.selectedIndex].value;
        var httpObject;

        /* Compilation conditionnelle d'IE */
        /*@cc_on
        @if (@_jscript_version >= 5)
        try {
                httpObject = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
                try {
                        httpObject = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (E) {
                        httpObject = false;
                }
        }
        @else
        xmlhttp = false;
        @end @*/

        if (!httpObject && typeof XMLHttpRequest != 'undefined') {
            try {
                httpObject = new XMLHttpRequest();
            } catch (e) {
                httpObject = false;
            }
        }

        if (httpObject) {
            httpObject.onreadystatechange=function() {
                var text="";
                if (httpObject.readyState == 4) {
                    if (httpObject.status == 200) {
                        response=httpObject.responseXML;
//                            alert(response);
                        collectiviteSelect = document.getElementById("collectivite");
                        list = response.getElementsByTagName("collectivite");
                        for(i=0;i<list.length;i++) {
                            node=list.item(i);
                            collectiviteCode = node.getAttribute("value");
                            collectiviteLib = node.firstChild.nodeValue;
                            collectiviteSelect.options[collectiviteSelect.options.length]=new Option(collectiviteLib,collectiviteCode);
                        }
                    }
                }
            }
        }
        httpObject.open("POST","<%=request.getContextPath()%>/getCollectivites.do",true);
        httpObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        httpObject.send("docId="+docId);
    }
    function collectiviteChanged() {
        select = document.getElementById("collectivite");
        codeCollectivite = select.options[select.options.selectedIndex].value;
        if(codeCollectivite!="---") {
            libelleCollectivite = select.options[select.options.selectedIndex].text;
            document.getElementById("collectiviteCode").value=codeCollectivite;
            document.getElementById("collectiviteLib").value=libelleCollectivite;
            // TODO: ajax call to populate budget list if exists
            budgetSelect = document.getElementById("budget");
            if(budgetSelect=="undefined") return;
            budgetSelect.options.length=0;
            budgetSelect.options[0]=new Option("<bean:message key="import-detail.selectBudget"/>","---");
            var httpObject;

            /* Compilation conditionnelle d'IE */
            /*@cc_on
            @if (@_jscript_version >= 5)
            try {
                    httpObject = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                    try {
                            httpObject = new ActiveXObject("Microsoft.XMLHTTP");
                    } catch (E) {
                            httpObject = false;
                    }
            }
            @else
            xmlhttp = false;
            @end @*/

            if (!httpObject && typeof XMLHttpRequest != 'undefined') {
                try {
                    httpObject = new XMLHttpRequest();
                } catch (e) {
                    httpObject = false;
                }
            }

            if (httpObject) {
                httpObject.onreadystatechange=function() {
                    var text="";
                    if (httpObject.readyState == 4) {
                        if (httpObject.status == 200) {
                            response=httpObject.responseXML;
//                            alert(response);
                            budgetSelect = document.getElementById("budget");
                            list = response.getElementsByTagName("budget");
                            for(i=0;i<list.length;i++) {
                                node=list.item(i);
                                budgetCode = node.getAttribute("value");
                                budgetLib = node.firstChild.nodeValue;
                                budgetSelect.options[budgetSelect.options.length]=new Option(budgetLib,budgetCode);
                            }
                        }
                    }
                }
            }
            httpObject.open("POST","<%=request.getContextPath()%>/getBudgets.do",true);
            httpObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
            httpObject.send("docId=<%=((ip.getFileType()!=null&&ip.getFileType().length()>0)?ip.getFileType():"\"+document.getElementById('fileType').options[document.getElementById('fileType').options.selectedIndex].value+\"")%>&collectivite="+codeCollectivite);
        }
    }
    function budgetChanged() {
        select = document.getElementById("budget");
        codeBudget = select.options[select.options.selectedIndex].value;
        if(codeBudget!="---") {
            libelleBudget = select.options[select.options.selectedIndex].text;
            document.getElementById("budgetCode").value=codeBudget;
            document.getElementById("budgetLib").value=libelleBudget;
        }
    }
</script>
<form name="submitMe" action="<%=request.getContextPath()%>/saveImportDetails.do" method="POST">
    <input type="hidden" name="importId" value="<%=ip.getImportId()%>"/>
<table class="import-detail">
    <!--colgroup>
        <col width="35%"/>
        <col width="65%"/>
    </colgroup-->
    <tbody>
        <tr>
            <th class="import"><bean:message key="import-detail.file"/></th>
            <td class="import"><%=ip.getFileName()%></td>
        </tr>
        <tr>
            <th class="import"><bean:message key="import-detail.fileType"/></th>
            <td class="import">
                <% if(/*ip.getFileType()!=null && ip.getFileType().length()>0*/ false) {
                    List<Pair> fileTypes = (List<Pair>)request.getAttribute("fileTypes");
                for(Pair fileType:fileTypes) {
                    if(fileType.key.equals(ip.getFileType())) { %><%=fileType.libelle%>
                <% }
                 }
                } else { %>
                <select class="zoneSaisissable" id="fileType" name="fileType" onchange="javascript:fileTypeChanged();">
                    <% if(ip.getFileType()==null || ip.getFileType().length()>0) { %><option value="---"><bean:message key="import-detail.selectFileType"/></option><% } %>
                <% List<Pair> fileTypes = (List<Pair>)request.getAttribute("fileTypes");
                for(Pair fileType:fileTypes) { %>
                <option value="<%=fileType.key%>"<%=(fileType.key.equals(ip.getFileType())?" selected=\"selected\"":"")%>><%=fileType.libelle%></option>
                <% } %>
                    </select>
                <% } %>
            </td>
        </tr>
        <tr>
            <th class="import"><bean:message key="import-detail.collectivite"/></th>
            <td class="import">
                <% if(/*ip.getCollectiviteId()!=null && ip.getCollectiviteLib()!=null && ip.getCollectiviteId().length()>0 && ip.getCollectiviteLib().length()>0*/ false) { %><%=ip.getCollectiviteLib()%>
                <% } else { %>
                <table style="margin-left: 3px;">
                    <tr>
                        <td><bean:message key="import-detail.existing-collectivite"/></td>
                        <td>
                            <select class="zoneSaisissable" name="collectivite" id="collectivite" onchange="javascript:collectiviteChanged()">
                    <%if(ip.getCollectiviteId()==null || ip.getCollectiviteLib()==null || ip.getCollectiviteId().length()==0 || ip.getCollectiviteLib().length()==0) {%>
                    <option value="---"><bean:message key="import-detail.selectCollectivite"/></option><% } %>
                    <%if(request.getAttribute("collectivites")!=null) for(Pair p:(List<Pair>)request.getAttribute("collectivites")) { %>
                    <option value="<%=p.key%>"<%= (p.key.equals(ip.getCollectiviteId())?" selected=\"selected\"":"") %>><%=p.libelle%></option><% } %>
                        </select></td>
                </tr>
                <tr>
                    <td><bean:message key="import-detail.codeCollectivite"/></td>
                    <td><input type="text" name="collectiviteCode" class="zoneSaisissable" id="collectiviteCode"/></td>
                </tr>
                <tr>
                    <td><bean:message key="import-detail.libCollectivite"/></td>
                    <td><input name="collectiviteLib" class="zoneSaisissable" id="collectiviteLib" size="30"/></td>
                </tr>
                </table>
                <% } %>
            </td>
        </tr>
        <tr>
            <th class="import">Budget</th>
            <td class="import">
                <% if(/*ip.getBudgetId()!=null && ip.getBudgetId().length()>0 && ip.getBudgetLib()!=null && ip.getBudgetLib().length()>0*/ false) { %><%=ip.getBudgetLib()%><% }
    else { %>
    <table style="margin-left: 3px;">
        <tr>
            <td><bean:message key="import-detail.existing-budget"/></td>
            <td><select name="budget" id="budget" class="zoneSaisissable" onchange="javascript:budgetChanged();">
                <% if(ip.getBudgetId()==null || ip.getBudgetId().length()==0 || ip.getBudgetLib()==null || ip.getBudgetLib().length()==0) { %><option value="---"><bean:message key="import-detail.selectBudget"/></option><% } %>
                <% if(request.getAttribute("budgets")!=null) for(Pair p:(Collection<Pair>)request.getAttribute("budgets")) { %>
                <option value="<%=p.key%>"<%= (p.key.equals(ip.getBudgetId())?" selected=\"selected\"":"")%>><%=p.libelle%></option>
                <% } %>
            </select></td>
        </tr>
        <tr>
            <td><bean:message key="import-detail.codeBudget"/></td>
            <td><input name="budgetCode" type="text" class="zoneSaisissable" id="budgetCode"/></td>
        </tr>
        <tr>
            <td><bean:message key="import-detail.libBudget"/></td>
            <td><input name="budgetLib" type="text" class="zoneSaisissable" id="budgetLib" size="30"/></td>
        </tr>
    </table> <% } %>
            </td>
        </tr>
    </tbody>
</table>
<input type="submit" value="Enregistrer" class="boutonStandardImpair"/>
</form>
<script type="text/javascript">
    <%if(request.getAttribute("message")!=null) { %>
        showDialog('<%=request.getAttribute("message")%>');
    <% } %>
</script>
<%@ include file="/WEB-INF/jsp/include/pied.jsp" %>