<%@ page import="ro.emid.common.types.Reclamatie" %>
<%@ page import="ro.emid.common.constants.ReclamatieConstants" %>
<%@ page import="ro.emid.common.utils.ObjectUtils" %>
<%@ page import="ro.emid.common.utils.pagination.PaginationController" %>
<%@ page import="ro.emid.common.db.NomAccessDB" %>
<%@ page import="ro.emid.common.constants.NomConstants" %>
<%@ page import="ro.emid.common.types.Nomenclator" %>
<%@ page import="ro.emid.common.utils.pagination.PaginationData" %>
<%@ page import="ro.emid.callcenter.util.CallcenterHelper" %>
<%@ page import="ro.emid.callcenter.i10n.Configuration" %>
<%@ page import="ro.emid.common.components.sugest.base.SugestConstants" %>
<%@ page import="ro.emid.common.constants.AppConstants" %>
<%@ page import="java.sql.Date" %>
<%@ page import="java.util.List" %>
<%@ page import="ro.emid.common.utils.sort.SortData" %>
<%@ page import="ro.emid.common.utils.sort.SortController" %>
<%@ taglib prefix="emid" tagdir="/WEB-INF/tags" %>
<%
        /**
         * @see ro.emid.callcenter.action.CautareReclamatiiAction
         */
    final boolean showReclamatii = request.getParameter("show") != null;

    final int tip = ObjectUtils.getIntValue(request.getParameter("tip"), -1);

    Date data = ObjectUtils.getDateValue(request.getParameter("data"), AppConstants.FORMAT_DATA);
    if (data == null) data = new Date(System.currentTimeMillis());

    final long codReclamatie = ObjectUtils.getLongValue(request.getParameter("codReclamatie"), -1);
    final long idDeranjament = ObjectUtils.getLongValue(request.getParameter("idDeranjament"), -1);
    final int codCir = ObjectUtils.getIntValue(request.getParameter("codCir"), -1);
    final int continutSesisizare1 = ObjectUtils.getIntValue(request.getParameter("sesizare1"), -1);


    final String localitate = ObjectUtils.getStringValue(request.getParameter(ReclamatieConstants.MUNICIPIU)).trim();
    final String localitate2 = ObjectUtils.getStringValue(request.getParameter(ReclamatieConstants.LOCALITATE2)).trim();
    final String tipArtera = ObjectUtils.getStringValue(request.getParameter(ReclamatieConstants.TIP_ARTERA)).trim();
    final String strada = ObjectUtils.getStringValue(request.getParameter(ReclamatieConstants.STRADA)).trim();
    final String nr = ObjectUtils.getStringValue(request.getParameter(ReclamatieConstants.NR)).trim();
    final String bloc = ObjectUtils.getStringValue(request.getParameter(ReclamatieConstants.BLOC)).trim();
    final String scara = ObjectUtils.getStringValue(request.getParameter(ReclamatieConstants.SCARA)).trim();
    final String ap = ObjectUtils.getStringValue(request.getParameter(ReclamatieConstants.AP)).trim();
    final String numeClient = ObjectUtils.getStringValue(request.getParameter(ReclamatieConstants.NUME_CLIENT)).trim();
    final String denumireAgent = ObjectUtils.getStringValue(request.getParameter(ReclamatieConstants.DENUMIRE_AGENT)).trim();
    final boolean arhiva = ObjectUtils.getIntValue( request.getParameter( "arhiva"), 0) == 1;


    final SortData sd = (SortData) request.getAttribute("sd");
    final PaginationData pd = (PaginationData) request.getAttribute("pd");
    final List<Reclamatie> reclamatii = (List<Reclamatie>) request.getAttribute("reclamatii");

    Configuration configuration = CallcenterHelper.getConfiguration();
%>

<table align=center border=0 cellPadding=5 cellSpacing=0 width="100%" class="gsaptd">
<tr><td align="left" valign="top">
<form name="zaforma" action="<%=AppConstants.APP_NAME%>?action=cautare-reclamatii" method="post" style="margin: 0;">
    <input type="hidden" name="show" value="yup">

    <fieldset>
    <legend><b>&nbsp;Criterii de cautare</b></legend>

<table width="100%"  height="100%">
<tr height="20">
    <td valign="bottom">
        <div>
            Reclamatia a fost emisa cel mai devreme in data de: <emid:datefield name="data" value="<%=data%>"/>
        </div>
    </td>
</tr>
<tr>
    <td>
        <table style="font-size: 12px; " >
            <tr>
                <td style="padding-left:5px">
                    <input <%=tip==1 ? "checked" : ""%> type="radio" name="tip" value="1" id="tip1" onclick="onClickTip(this)"> <label for="tip1">COD RECLAMATIE</label><br>
                    &nbsp;<input <%=tip !=1 ? "disabled" : ""%> type="text" name="codReclamatie" class="gsapinputtext" value="<%=tip == 1 ? String.valueOf(codReclamatie) : ""%>" >
                </td>
                <td style="padding-left:5px">
                    <input <%=tip==2 ? "checked" : ""%> type="radio" name="tip" value="2" id="tip2" onclick="onClickTip(this)"> <label for="tip2">ID DERANJAMENT</label><br>
                    &nbsp;<input <%=tip !=2 ? "disabled" : ""%> type="text" name="idDeranjament" class="gsapinputtext" value="<%=tip == 2 ? String.valueOf(idDeranjament) : ""%>" >
                </td>
                <td style="padding-left:5px">
                    <%if(AppConstants.CIR_ACTIV){%>
                        <input <%=tip==3 ? "checked" : ""%> type="radio" name="tip" value="3" id="tip3" onclick="onClickTip(this)"> <label for="tip3">CIR ALOCAT</label><br>
                        <% final Nomenclator[] ciruri = NomAccessDB.getAllNom(NomConstants.NOM_CIR); %>
                        &nbsp;<select name="codCir" class="gSAPSelect" <%=tip !=3 ? "disabled" : ""%>>
                            <option value="-1">Selectati:</option>
                            <% for (int i = 0; i < ciruri.length; i++) { %>
                                <option value="<%=ciruri[i].getCod()%>" <%=ciruri[i].getCod() == codCir ? "selected" : ""%>  ><%=ciruri[i].getDescriere()%></option>
                            <%} %>
                        </select>
                    <%}%>
                </td>
                <td style="padding-left:5px">
                    <input <%=tip==4 ? "checked" : ""%> type="radio" name="tip" value="4" id="tip4" onclick="onClickTip(this)"> <label for="tip4">CONTINUT SESIZARE 1</label><br>
                    &nbsp;<select name="sesizare1" class="gSAPSelect" <%=tip != 4 ? "disabled" : ""%>>
                        <option value="-1">Selectati:</option>
                        <%for (int i = 0; i < ReclamatieConstants.SESIZARI.length; i++){
                            final String sesizare1 = ReclamatieConstants.SESIZARI[i][0]; %>
                            <option value="<%=i%>" <%=i == continutSesisizare1 ? "selected" : ""%> ><%=sesizare1%></option>
                        <%}%>
                    </select>
                </td>
            </tr>
            <tr>
                <td colspan="9" style="padding-left:5px">
                    <input <%=tip==5 ? "checked" : ""%>  type="radio" name="tip" value="5" id="tip5" onclick="onClickTip(this)">
                    <label for="tip5">ADRESA CLIENT</label>

                    &nbsp;&nbsp;&nbsp;&nbsp;
                    <input type="checkbox" value="1" name="arhiva" id="arhiva" <%=arhiva ? "checked" : ""%>><label for="arhiva">Cautare in arhiva</label>
                    <br>
                    <div id="trAdresa" style=" border-color: gray; border-style:solid;border-width:1px;padding:5px;<% if( tip != 5){%> display: none<%}%>    ">
                        <table style="margin-left: 15px" border="0" cellspacing="0">
                            <tr>
                                <td colspan="2"><%=configuration.getLabelLocalitate1()%></td>
                                <td colspan="2"><%=configuration.getLabelLocalitate2()%></td>
                                <td colspan="2">Denumire arteră</td>
                                <td colspan="2"></td>
                            </tr>
                            <tr>
                                <td colspan="2">
                                    <input type="hidden" id="<%=ReclamatieConstants.COD_MUNICIPIU%>" name="<%=ReclamatieConstants.COD_MUNICIPIU%>" value="" />
                                    <input class="gsapinputtext" style="width: 97%" id="<%=ReclamatieConstants.MUNICIPIU%>" name="<%=ReclamatieConstants.MUNICIPIU%>" value="<%=AppConstants.format( localitate)%>" />
                                </td>
                                <td colspan="2">
                                    <input type="hidden" id="<%=ReclamatieConstants.COD_LOCAL%>" name="<%=ReclamatieConstants.COD_LOCAL%>" value="" />
                                    <input class="gsapinputtext" style="width: 150px" type=text id="<%=ReclamatieConstants.LOCALITATE2%>" name="<%=ReclamatieConstants.LOCALITATE2%>" value="" sugestWidth="250" />
                                </td>
                                <td colspan="2">
                                    <input type="hidden" name="<%=ReclamatieConstants.COD_STRADA%>" id="<%=ReclamatieConstants.COD_STRADA%>"  value="-1">
                                    <input class="gsapinputtext" style="width: 160px" name="<%=ReclamatieConstants.STRADA%>" id="<%=ReclamatieConstants.STRADA%>" value="<%=AppConstants.format( strada)%>">
                                </td>
                                <td colspan="2">
                                    <input class="gsapinputtext" style="width: 100px" type=text name="<%=ReclamatieConstants.TIP_ARTERA%>" id="<%=ReclamatieConstants.TIP_ARTERA%>" onchange="onChangeTipArtera()"  >
                                </td>
                            </tr>
                            <tr>
                                <td width="10">Nr</td>
                                <td>Bloc</td>
                                <td>Sc.</td>
                                <td>Ap.</td>
                                <td>Judet</td>
                            </tr>
                            <tr>
                                <td><input onfocus="validateForNumar()" class="gsapinputtext" type="text" name="<%=ReclamatieConstants.NR%>" id="<%=ReclamatieConstants.NR%>"></td>
                                <td><input class="gsapinputtext" type="text" name="<%=ReclamatieConstants.BLOC%>" size="5" value="<%=AppConstants.format( bloc)%>"></td>
                                <td><input class="gsapinputtext" type="text" name="<%=ReclamatieConstants.SCARA%>" size="5" value="<%=AppConstants.format( scara)%>"></td>
                                <td><input class="gsapinputtext" type="text" name="<%=ReclamatieConstants.AP%>" size="5" value="<%=AppConstants.format( ap)%>"></td>
                                <td><input class="gsapinputtext" type="text" size="15" value="" readonly="true" id="<%=ReclamatieConstants.JUDET%>" name="<%=ReclamatieConstants.JUDET%>"/></td>
                            </tr>
                            <tr>
                                <td colspan="2">Nume Prenume Client / Denumire agent economic</td>
                                <!--<td>Den. Agent Economic</td>-->
                            </tr>
                            <tr>
                                <td colspan="2"><input class="gsapinputtext" type="text" style="width: 97%" name="<%=ReclamatieConstants.NUME_CLIENT%>" value="<%=AppConstants.format( numeClient)%>"></td>
                                <%--<td colspan="2"><input class="gsapinputtext" type="text" size="30" name="<%=ReclamatieConstants.DENUMIRE_AGENT%>" value="<%=AppConstants.format( denumireAgent)%>"></td>--%>
                            </tr>
                        </table>

                    </div>
                </td>
            </tr>
        </table>
    </td>
</tr>
<tr>
    <td align="center">
        <input type="submit" value="Căutare"> &nbsp;&nbsp;&nbsp;
        <input type="button" value="Reset" onclick="resetFiltru()"> &nbsp;&nbsp;&nbsp;
        <button onclick="window.close()">Închide</button>
    </td>
</tr>
</table>

</fieldset>

<% if( showReclamatii){%>
<fieldset>
    <legend><b>&nbsp;Lista reclamatii gasite</b></legend>
    <table id="tableReclamatii" border=0 cellPadding=2 cellSpacing=1 width="100%" class="table gSAPTHEAD" style="BACKGROUND-COLOR: white">
        <thead>
            <tr>
                <th class="gsapthead" height=25 nowrap title="Informatie de stare">&nbsp;-&nbsp;</th>
                <th class="gsapthead" title="Cod reclamatie" nowrap><%=SortController.printSortableColumn("Cod", "ID", sd)%></th>
                <th class="gsapthead" title="Data inregistrare reclamatie" nowrap><%=SortController.printSortableColumn("Data", "DATA", sd)%></th>
                <th class="gsapthead" title="Nume reclamant" nowrap ><%=SortController.printSortableColumn("Nume client", "NUME_CLIENT", sd)%></th>
                <th class="gsapthead" title="Localitate reclamant" nowrap ><%=SortController.printSortableColumn("Localitate", "LOCALITATE", sd)%></th>
                <th class="gsapthead" title="Sat reclamant" nowrap ><%=SortController.printSortableColumn("Sat", "LOCALITATE2", sd)%></th>
                <th class="gsapthead" title="Tip artera" nowrap ><%=SortController.printSortableColumn("Tip artera", "TIP_ARTERA", sd)%></th>
                <th class="gsapthead" title="Denumire artera" nowrap ><%=SortController.printSortableColumn("Den. artera", "STRADA", sd)%></th>
                <th class="gsapthead" title="Nr" nowrap ><%=SortController.printSortableColumn("Nr.", "NR", sd)%></th>
                <th class="gsapthead" title="Bl" nowrap ><%=SortController.printSortableColumn("Bl.", "BLOC", sd)%></th>
                <th class="gsapthead" title="Sc" nowrap ><%=SortController.printSortableColumn("Sc.", "SCARA", sd)%></th>
                <th class="gsapthead" title="Ap" nowrap ><%=SortController.printSortableColumn("Ap.", "AP", sd)%></th>
                <th class="gsapthead" title="Sesizarea depusa de reclamant" nowrap ><%=SortController.printSortableColumn("Sesizare", "CONTINUT_SESIZARE1 ASC, CONTINUT_SESIZARE2", sd)%></th>
            </tr>
        </thead>
        <tbody>
            <%if( reclamatii == null || reclamatii.size() == 0){%>
                <tr><td class="no-data" colspan="15">Nu s-au gasit reclamatii dupa criteriile specificate</td></tr>
            <%}else for(final Reclamatie reclamatie : reclamatii){ %>
                <tr ondblclick="showDateReclamatie(<%=reclamatie.getId()%>)">
                    <td nowrap align="center" title="<%=reclamatie.isArhivata() ? "Reclamatie Arhivata" : reclamatie.isRetransmisa() ? "Reclamatie Retransmisa" : ""%>">
                        <%=reclamatie.isArhivata() ? "A" : reclamatie.isRetransmisa() ? "R" : "-"%>
                    </td>
                    <td nowrap align="center" <%=ReclamatieConstants.DB_REP_LOC_CONF_ZONA_OPERATOR.equals(reclamatie.getObservatii() != null ? reclamatie.getObservatii():"")?"style=\"color:red\" title=\"Atentie! Reclamatie arondata pe C.E. conform zona operator, va rugam sa alocati localitatea " + reclamatie.getLocalitate() + " in cadrul modulului Localitati/Centre\" ":""%>><%=reclamatie.getNiceNumarReclamatie()%></td>
                    <td nowrap align="center"><%=AppConstants.format(reclamatie.getData())%></td>
                    <td nowrap><%=reclamatie.getNumeClient()%></td>
                    <td nowrap><%=AppConstants.format(reclamatie.getLocalitate())%></td>
                    <td nowrap><%=AppConstants.format(reclamatie.getLocalitate2())%></td>
                    <td nowrap><%=AppConstants.format(reclamatie.getTipArtera())%></td>
                    <td nowrap><%=AppConstants.format(reclamatie.getStrada())%></td>
                    <td nowrap align="right"><%=AppConstants.format(reclamatie.getNr())%></td>
                    <td nowrap align="right"><%=AppConstants.format(reclamatie.getBloc())%></td>
                    <td nowrap align="right"><%=AppConstants.format(reclamatie.getScara())%></td>
                    <td nowrap align="right"><%=AppConstants.format(reclamatie.getAp())%></td>
                    <td nowrap><%=ReclamatieConstants.getContinutSesizare( reclamatie, " ")%></td>
                </tr>
            <%}%>
        </tbody>
        <tfoot>
            <tr class="paginationRow">
                <td colspan="27">
                    <%=SortController.initSort(sd)%>
                    <%=PaginationController.printPaginationData(pd)%>
                </td>
            </tr>
        </tfoot>
    </TABLE>
</fieldset>

</form>

<script type="text/javascript">
    var tableReclamatii = new Table('tableReclamatii', "tableReclamatii");
</script>
<%}%>

</td></tr></TABLE>

<script type="text/javascript">
    function onClickTip(oRadio){
        var tip = oRadio.value;
        disableAll();
        switch ( tip){
            case "1" : document.zaforma.codReclamatie.disabled = false; break;
            case "2" : document.zaforma.idDeranjament.disabled = false; break;
            case "3" : try{ document.zaforma.codCir.disabled = false; }catch(e){} break;
            case "4" : document.zaforma.sesizare1.disabled = false; break;
            case "5" : $('trAdresa').style.display = '';  break;
        }
    }

    function disableAll(){
        document.zaforma.codReclamatie.disabled = true;
        document.zaforma.idDeranjament.disabled = true;

        try{
            document.zaforma.codCir.disabled = true;
        }catch(e){}
        
        document.zaforma.sesizare1.disabled = true;
        $('trAdresa').style.display = 'none';
    }

    function resetFiltru(){
        var elements = Form.getElements(document.zaforma);
        for( var i = 0; i < elements.length; i++){
            var el = elements[i];
            if( el.type == "checkbox") el.checked =  false;
            else if( el.type == "text") el.value = "";
            else if( el.type == "radio") el.checked = false;
        }

        var selects = document.zaforma.getElementsByTagName("select");
        for( var i = 0; i < selects.length; i++){
            var selectu = selects[i];
            selectu.selectedIndex = 0;
        }
    }

    function setStradaData(codStrada, tipArtera, denumire){
        $('<%=ReclamatieConstants.COD_STRADA%>').value = codStrada;
        $('<%=ReclamatieConstants.TIP_ARTERA%>').value = tipArtera;
        $('<%=ReclamatieConstants.STRADA%>').value = denumire;
    }

    function loadSugests(){
            //municipiu sugest
            municipiuAjaxDataProvider = new AjaxDataProvider( function(){
                return "<%=AppConstants.APP_NAME%>?action=suggest-localitate&<%=SugestConstants.SUGEST_Q_QUERY%>=";
            });


            var municipiuSelectat = null;

            new InputSugest( $("<%=ReclamatieConstants.MUNICIPIU%>"), $("<%=ReclamatieConstants.COD_MUNICIPIU%>"), municipiuAjaxDataProvider, {
                onSelect : function( item){
                    municipiuSelectat = item;

                    $("<%=ReclamatieConstants.MUNICIPIU%>").value = item.denMunicipiu;
                    $('<%=ReclamatieConstants.COD_LOCAL%>').value ='';
                    $('<%=ReclamatieConstants.LOCALITATE2%>').value ='';
                    $('<%=ReclamatieConstants.STRADA%>').value ='';
                    $('<%=ReclamatieConstants.TIP_ARTERA%>').value ='';

                    $('<%=ReclamatieConstants.JUDET%>').value = item.denJudet;

                    if( item.countCopii == 1 ){
                        //completam denumirea si codul localitatii daca este doar una singura
                        $("<%=ReclamatieConstants.LOCALITATE2%>").value = item.denCopil;
                        $("<%=ReclamatieConstants.COD_LOCAL%>").value = item.codCopil;

                        //setam focusul direct pe strada
                        $('<%=ReclamatieConstants.STRADA%>').value = '';
                        $('<%=ReclamatieConstants.TIP_ARTERA%>').value = '';
                        $('<%=ReclamatieConstants.STRADA%>').focus();
                    } if (item.countCopii == 0){
                        //setam focusul direct pe strada
                        $('<%=ReclamatieConstants.STRADA%>').value = '';
                        $('<%=ReclamatieConstants.TIP_ARTERA%>').value = '';
                        $('<%=ReclamatieConstants.STRADA%>').focus();
                    }else{
                        $('<%=ReclamatieConstants.LOCALITATE2%>').focus();
                    }
                }
            });

        //localitate sugest
        var localitate2AjaxDataProvider = new AjaxDataProvider( function(){
            return "<%=AppConstants.APP_NAME%>?action=suggest-localitate2"
                    +"&mun=" + $("<%=ReclamatieConstants.MUNICIPIU%>").value
                    +"&codMun=" + $("<%=ReclamatieConstants.COD_MUNICIPIU%>").value
                    + "&<%=SugestConstants.SUGEST_Q_QUERY%>=";
        });

        new InputSugest( $("<%=ReclamatieConstants.LOCALITATE2%>"), $("<%=ReclamatieConstants.COD_LOCAL%>"), localitate2AjaxDataProvider, {
            onSelect : function( item){
                $('<%=ReclamatieConstants.LOCALITATE2%>').value = item.denLocalitate;
                $('<%=ReclamatieConstants.COD_MUNICIPIU%>').value = item.codMunicipiu;
                $('<%=ReclamatieConstants.MUNICIPIU%>').value = item.denMunicipiu;
                $('<%=ReclamatieConstants.JUDET%>').value = item.denJudet;

                $('<%=ReclamatieConstants.STRADA%>').value = '';
                $('<%=ReclamatieConstants.TIP_ARTERA%>').value = '';
                $('<%=ReclamatieConstants.STRADA%>').focus();
            }
        });


        //tip artera sugest
        var tipArteraAjaxDataProvider = new AjaxDataProvider( function(){
            return "<%=AppConstants.APP_NAME%>?action=suggest-tip-artera&<%=SugestConstants.SUGEST_Q_QUERY%>=";
        });

        var tipArteraAjaxSugest = new InputSugest( $("<%=ReclamatieConstants.TIP_ARTERA%>"), null, tipArteraAjaxDataProvider, {
            onSelect : function( item){
                //item.key;
                //item.value;
                $('<%=ReclamatieConstants.STRADA%>').value ='';
                $('<%=ReclamatieConstants.STRADA%>').focus();
            }
        });


        //strada sugest
        var stradaAjaxDataProvider = new AjaxDataProvider( function(){
            var nrCopiiLocalitate1 = municipiuSelectat != null ? municipiuSelectat.countCopii : 0;
            var codMun = $('<%=ReclamatieConstants.COD_MUNICIPIU%>').value;
            var mun = $('<%=ReclamatieConstants.MUNICIPIU%>').value;
            var codLoc = $('<%=ReclamatieConstants.COD_LOCAL%>').value;
            var loc = $('<%=ReclamatieConstants.LOCALITATE2%>').value;
            var tipArtera = $('<%=ReclamatieConstants.TIP_ARTERA%>').value;

            if( mun != null  && mun.trim().length > 0)
                return "<%=AppConstants.APP_NAME%>?action=suggest-artera"
                        + "&codLocalitate=" + codMun
                        + "&localitate=" + mun
                        + "&codLocalitate2=" + codLoc
                        + "&localitate2=" + loc
                        + "&tipArtera=" + tipArtera
                        + "&<%=SugestConstants.SUGEST_Q_QUERY%>=";
            else return null;
        });

        new InputSugest( $("<%=ReclamatieConstants.STRADA%>"), $("<%=ReclamatieConstants.COD_STRADA%>"), stradaAjaxDataProvider, {
            onSelect : function( item){
                setStradaData( item.cod, item.tipArtera, item.denumire);
                $('<%=ReclamatieConstants.NR%>').focus();
                $('<%=ReclamatieConstants.NR%>').value='';

            }
        });
    }

    function validateForNumar(){
        if( document.zaforma.<%=ReclamatieConstants.MUNICIPIU%>.value.length == 0){
            alert( 'Selectati o localitate !');
            document.zaforma.<%=ReclamatieConstants.MUNICIPIU%>.focus();
            return false;
        }

        if( document.zaforma.<%=ReclamatieConstants.STRADA%>.value.length == 0){
            alert( 'Selectati o strada !');
            document.zaforma.<%=ReclamatieConstants.STRADA%>.focus();
            return false;
        }
        return true;
    }

    function onChangeTipArtera(){
        document.zaforma.<%=ReclamatieConstants.COD_STRADA%>.value = '-1';
        document.zaforma.<%=ReclamatieConstants.STRADA%>.value = '';
    }

    function cautare(){
        document.zaforma.submit();
    }


    setTimeout("loadSugests()", 1000);
</script>

