<%@ page import="ro.emid.common.constants.InformatieConstants" %>
<%@ page import="ro.emid.callcenter.obj.Informatie" %>
<%@ page import="ro.emid.common.constants.GlobalAppConstants" %>
<%@ page import="ro.emid.callcenter.db.InformatieAccessDB" %>
<%@ page import="ro.emid.common.utils.ObjectUtils" %>
<%@ page import="ro.emid.common.db.framework.IFilter" %>
<%@ page import="ro.emid.common.db.framework.Filter" %>
<%@ page import="ro.emid.common.constants.AppConstants" %>

<%@ include file="../callcenter/declaratii.jsp"%>

<%
    final Informatie informatie = new Informatie();

%>
<TABLE align=center border=0 cellPadding=5 cellSpacing=0 width="100%" class="gsaptd">
    <tr><td align="left" valign="top">
    <form name="zaforma" action="<%=GlobalAppConstants.APP_NAME%>" method="post" onsubmit="return validare()">
        <input type="hidden" name="action" value="saveInformatie">
        <input type="hidden" name="<%=InformatieConstants.ID%>" value="<%=informatie.getId()%>">
        <input type="hidden" name="<%=InformatieConstants.AUTOR%>" value="<%=user.getUsername()%>">

        <fieldset>
            <legend><b>&nbsp;Date despre client</b></legend>
            <table style="font-size: 12" width="100%" border="0" height="100%">
                <tr>
                    <td>Nume/prenume:</td>
                    <td><input class="gsapinputtext" size="80" name="<%=InformatieConstants.NUME_CLIENT%>" value="<%=AppConstants.format( informatie.getNumeClient())%>"></td>
                </tr>
                <tr>
                    <td>Telefon:</td><td> <input class="gsapinputtext" size="80" name="<%=InformatieConstants.TELEFON%>" value="<%=AppConstants.format(informatie.getTelefon())%>"></td>
                </tr>
            </table>
        </fieldset>

        <table style="font-size: 12">
            <tr><td nowrap="" valign="top">
                    Motiv1:
                    <select class="gSAPSelect" name="<%=InformatieConstants.MOTIV1%>" onchange="fillMotiv2Select()">
                        <option value="-1">Selectati:</option>
                        <%for (int i = 0; i < InformatieConstants.MOTIVE.length; i++){
                            final String motiv1 = InformatieConstants.MOTIVE[i][0]; %>
                            <option value="<%=i%>" <%=i == informatie.getMotiv1() ? "selected" : ""%> ><%=motiv1%></option>
                        <%}%>
                    </select>

                    <span id="<%=InformatieConstants.MOTIV2%>" style="display: none">
                        Motiv2:
                        <span id="select<%=InformatieConstants.MOTIV2%>">
                            <select class="gSAPSelect" name="<%=InformatieConstants.MOTIV2%>">
                            </select>
                        </span>
                        <span id="altMotiv" style="display: none">
                            <textarea rows="1" cols="50" name="<%=InformatieConstants.ALT_MOTIV%>"></textarea>
                        </span>
                    </span>
                </td>
            </tr>
        </table>
        <br>
        <div align="center">
            <input type="submit" value="Adaugare reclamatie"> &nbsp;&nbsp;
            <input type="button" value="Vizualizare Multime reclamatii" onclick="showAlteReclamatii()"> &nbsp;&nbsp;
            <input type="button" onclick="window.close()" value="Inchide Fereastra"/>
        </div>
    </form>

    <%
        String sortColumn = ObjectUtils.getStringValue(request.getParameter("sortColumn"));
        if (sortColumn == null || sortColumn.length() == 0) sortColumn = "ID";
        String sortType = ObjectUtils.getStringValue(request.getParameter("sortType"));
        if (sortType == null || sortType.length() == 0) sortType = "ASC";


        final IFilter filter = new Filter();
        filter.addOrSetCriteria("AND", "AUTOR", "=", user.getUsername());

        //for pagination{
        if (!"informatii".equals(CallcenterSessionUtils.getPaginationJsp(session))) {
            CallcenterSessionUtils.resetPaginationData(session, "informatii");
        }

        long nrLines = ObjectUtils.getLongValue(request.getParameter("nrLines"), -1);
        if (nrLines == -1) nrLines = CallcenterSessionUtils.getNrLines(session);

        long currentBeginIndex = ObjectUtils.getLongValue(request.getParameter("currentBeginIndex"), -1);
        if (currentBeginIndex == -1) currentBeginIndex = CallcenterSessionUtils.getBeginIndex( session);

        final long nrLinesTotal = InformatieAccessDB.getCount( filter);
        final String direction = ObjectUtils.getStringValue(request.getParameter("direction"));
        if ("backward".equals(direction)) {
            currentBeginIndex -= nrLines;
        } else if ("forward".equals(direction)) {
            currentBeginIndex += nrLines;
        } else if( "first".equals( direction)){
            currentBeginIndex = 1;
        } else if( "last".equals( direction)){
            currentBeginIndex =  nrLines * nrLinesTotal/nrLines + 1;
        }

        if (currentBeginIndex > nrLinesTotal && nrLinesTotal > 1)
            currentBeginIndex -= nrLines; //cazul cind se sterge ultimul element de pe ultima pagina si este singurul pe pagina
        if (currentBeginIndex < 1) currentBeginIndex = 1;

        CallcenterSessionUtils.setNrLines(session, nrLines);
        CallcenterSessionUtils.setBeginIndex(session, currentBeginIndex);
        //}for pagination

        final Informatie[] informatii = InformatieAccessDB.getAll( filter, sortColumn, sortType, currentBeginIndex, currentBeginIndex + nrLines - 1);
        final int nrLinesSelected = informatii.length;
    %>



    <fieldset>
        <legend><b>&nbsp;Alte reclamatii telefonice</b></legend>
        <TABLE id="tableAlteReclamatii" border=0 cellPadding=2 cellSpacing=1 width="100%" class="gSAPTHEAD" style="BACKGROUND-COLOR: white; margin-top: 20px ">
            <TR noSelect="true">
                <TH class="gsapthead" height=25 noWrap>&nbsp;<a href="<%=GlobalAppConstants.APP_NAME%>?action=callcenter_informatii&sortColumn=ID&sortType=<%=(sortType.equals("ASC") ? "DESC" : "ASC")%>" style="COLOR: #000000; FONT-SIZE: 13px; text-decoration: none;">Id</a>&nbsp; <% if(sortColumn.equals("ID")) if( sortType.equals("ASC")){%> <img src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/sort_up_1.gif" border=0> <%}else{%> <img src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/sort_dn_1.gif" border=0> <%}%>&nbsp;</TH>
                <TH class="gsapthead" noWrap >&nbsp;Nume Utilizator:&nbsp;</TH>
                <TH class="gsapthead" noWrap >&nbsp;<a href="<%=GlobalAppConstants.APP_NAME%>?action=callcenter_informatii&sortColumn=DATA&sortType=<%=(sortType.equals("ASC") ? "DESC" : "ASC")%>" style="COLOR: #000000; FONT-SIZE: 13px; text-decoration: none;">Data</a>&nbsp; <% if(sortColumn.equals("DATA")) if( sortType.equals("ASC")){%> <img src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/sort_up_1.gif" border=0> <%}else{%> <img src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/sort_dn_1.gif" border=0> <%}%>&nbsp;</TH>
                <TH class="gsapthead" noWrap >&nbsp;<a href="<%=GlobalAppConstants.APP_NAME%>?action=callcenter_informatii&sortColumn=NUME_CLIENT&sortType=<%=(sortType.equals("ASC") ? "DESC" : "ASC")%>" style="COLOR: #000000; FONT-SIZE: 13px; text-decoration: none;">Nume Client</a>&nbsp; <% if(sortColumn.equals("NUME_CLIENT")) if( sortType.equals("ASC")){%> <img src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/sort_up_1.gif" border=0> <%}else{%> <img src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/sort_dn_1.gif" border=0> <%}%>&nbsp;</TH>
                <TH class="gsapthead" noWrap >&nbsp;Telefon:&nbsp;</TH>
                <TH class="gsapthead" noWrap >&nbsp;Motiv reclamatie 1:&nbsp;</TH>
                <TH class="gsapthead" noWrap >&nbsp;Motiv reclamatie 2:&nbsp;</TH>
            </TR>

            <%if( informatii.length == 0 ){%>
                <tr noSelect="true"><td>Nu exista informati...</tr>
            <%}else{%>
                <%for( int i = 0; i  < informatii.length; i++){
                    final Informatie info = informatii[i]; %>
                    <tr>
                        <td align="center" <%if(sortColumn.equals("ID")){%> style="background-color:#f2efef;"<%}%> ><%=info.getId()%></td>
                        <td align="center" ><%=info.getAutor()%></td>
                        <td align="center" <%if(sortColumn.equals("DATA")){%> style="background-color:#f2efef;"<%}%>><%=AppConstants.format(info.getData())%></td>
                        <td <%if(sortColumn.equals("NUME_CLIENT")){%> style="background-color:#f2efef;"<%}%> ><%=AppConstants.format(info.getNumeClient())%></td>
                        <td><%=AppConstants.format(info.getTelefon())%></td>
                        <td><%=AppConstants.format(info.getTextMotiv1())%></td>
                        <td><%=AppConstants.format(info.getTextMotiv2())%></td>
                    </tr>
                <%}%>
                <tr class="paginationRow" noSelect="true">
                    <td colspan="8" >
                        <table width="100%" cellspacing="0" cellpadding="0">
                            <tr noSelect="true">
                                <td>
                                    
                                </td>
                                <td align="right">
                                    <form action="<%=GlobalAppConstants.APP_NAME%>" method="post">
                                        <input type="hidden" name="action" value="callcenter_informatii">
                                        <input type="hidden" name="currentBeginIndex" value="<%=currentBeginIndex%>">
                                        <input type="hidden" name="direction" value="">
                                        <input type="hidden" name="sortColumn" value="<%=sortColumn%>">
                                        <input type="hidden" name="sortType" value="<%=sortType%>">
                                        Nr. linii:
                                        <select name="nrLines" onchange="this.form.submit()" style="font-size: 10px">
                                            <option value="10"  <%=nrLines == 10 ? "selected" : ""%> >10</option>
                                            <option value="15"  <%=nrLines == 15 ? "selected" : ""%> >15</option>
                                            <option value="20"  <%=nrLines == 20 ? "selected" : ""%> >20</option>
                                            <option value="30"  <%=nrLines == 30 ? "selected" : ""%> >30</option>
                                            <option value="40"  <%=nrLines == 40 ? "selected" : ""%> >40</option>
                                            <option value="50"  <%=nrLines == 50 ? "selected" : ""%> >50</option>
                                        </select>&nbsp;&nbsp;
                                        <%=currentBeginIndex%> - <%=currentBeginIndex + nrLinesSelected - 1%> din <%=nrLinesTotal%>&nbsp;&nbsp;&nbsp;
                                        <input <%=currentBeginIndex == 1 ? "disabled" : ""%>  title="Primele <%=nrLines%> linii" type="button" onclick="this.form.direction.value='first'; this.form.submit()" style="font-size: 8px; cursor: pointer" value="&lt&lt" />
                                        <input <%=currentBeginIndex == 1 ? "disabled" : ""%>  title="Precedentele <%=nrLines%> linii" type="button" onclick="this.form.direction.value='backward'; this.form.submit()" style="font-size: 8px; cursor: pointer" value="&lt" />
                                        <input <%=currentBeginIndex + nrLinesSelected - 1 >= nrLinesTotal ? "disabled" : ""%> title="Urmatoarele <%=nrLines%> linii" type="button" onclick="this.form.direction.value='forward'; this.form.submit()" style="font-size: 8px; cursor: pointer" value="&gt" />
                                        <input <%=currentBeginIndex + nrLinesSelected - 1 >= nrLinesTotal ? "disabled" : ""%> title="Ultimele <%=nrLines%> linii" type="button" onclick="this.form.direction.value='last'; this.form.submit()" style="font-size: 8px; cursor: pointer" value="&gt&gt" />
                                    </form>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            <%}%>
        </TABLE>
    </fieldset>
</td></tr></TABLE>
<!--</td></tr></tbody></TABLE>-->
<script type="text/javascript">
    var tableReclamatii = new Table('tableAlteReclamatii', "tableReclamatii");

    function showAlteReclamatii(){
        window.open('<%=GlobalAppConstants.APP_NAME%>?action=callcenter_alteinformatii','alteReclamatii', 'top=50,left=50,width=850 height=500, status=yes,resizable=yes,dependent=yes,alwaysRaised=yes, scrollbars =yes');
    }


    var team = new Array(
            <% for( int i = 0; i < InformatieConstants.MOTIVE.length; i++){
                final String[] motive2 = InformatieConstants.MOTIVE[i];%>
                new Array(
                    new Array("Selectati", -1)
                    <% for( int j = 1; j < motive2.length; j++){ %>
                        <%=j==1 ? "," : ""%>
                        new Array("<%=motive2[j]%>", <%=j%>)<%=j < motive2.length-1 ? "," : ""%>
                    <%}%>
                    )<%=i < InformatieConstants.MOTIVE.length-1 ? "," : ""%>
            <% }%>
        );

    function fillMotiv2Select(){
        var indexMotiv1 = document.zaforma.<%=InformatieConstants.MOTIV1%>.selectedIndex - 1;
        if( indexMotiv1 >= <%=InformatieConstants.INDEX_ALT_MOTIV%> ){
            //ultimul element selectat
            //alert( $('altMotiv').innerHTML);
            Element.show('altMotiv');
            Element.hide('select<%=InformatieConstants.MOTIV2%>');
            Element.show('<%=InformatieConstants.MOTIV2%>');
        }else{
            Element.show('select<%=InformatieConstants.MOTIV2%>');
            Element.hide('altMotiv');
            fillSelectFromArray(document.zaforma.<%=InformatieConstants.MOTIV2%>, team[document.zaforma.<%=InformatieConstants.MOTIV1%>.selectedIndex-1]);

            if( document.zaforma.<%=InformatieConstants.MOTIV2%>.options.length <= 1){
                Element.hide('<%=InformatieConstants.MOTIV2%>');
            }else{
                Element.show('<%=InformatieConstants.MOTIV2%>');                
            }
        }
    }

    function validare(){
        try{
            if( document.zaforma.<%=InformatieConstants.NUME_CLIENT%>.value.trim().length == 0){
                alert( 'Introduceti nume client !');
                document.zaforma.<%=InformatieConstants.NUME_CLIENT%>.focus();
                return false;
            }
            return true;
        }catch(e){
        }
        return false;
    }
</script>

