<%@ page import="com.fenalco.utilities.lists.ListView" %>
<%@ page import="com.fenalco.entities.User" %>
<%@ page import="com.fenalco.entities.DataAccess"%>
<%@ page import="com.fenalco.entities.Country"%>
<%@ page import="com.fenalco.entities.Province"%>
<%@ page import="com.fenalco.entities.City"%>
<%@ page import="com.fenalco.entities.Company"%>
<%@ page import="java.util.Hashtable"%>
<%@ page import="java.util.Enumeration"%>
<%@ page import="java.util.Vector"%>
<%
    String firstRow = ( String )request.getAttribute( "firstRow" );
    String lastRow = ( String )request.getAttribute( "lastRow" );
    String total = ( String )request.getAttribute( "total" );
    String page1 = ( String )request.getAttribute( "page" );
    String pages = ( String )request.getAttribute( "pages" );
    int pageResults = Integer.valueOf(request.getAttribute( "pageResults" ).toString()).intValue();
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel='stylesheet' style='text/css' href='../css/tablas.css'>
<title>Listado de empresas</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#ffffff" background="../images/sysmanager/fondo1.gif" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" onload='showMessage("<%=request.getAttribute("message")%>")'>

<script language='javascript' type='text/javascript' src='../js/library.js'></script>
<script>

    function showMessage(message){
        if(message!=null && message!=""){
            window.alert(message);
        }
        return true;
    }

     function submitCombo(comboName){
                document.companyList.combo.value=comboName;
                document.companyList.action = "listaempresas";
                document.companyList.submit();
                return true;
     }
     
    function showCompany(id){
           var left = (screen.availWidth-515)/2;
	   var top = (screen.availHeight-550)/2;
           window.open('/administrador/verempresa?key='+id,'','WIDTH=515,HEIGHT=550,left='+left+',top='+top+', SCROLLBARS=yes');
    }

</script>


<table border="0" cellpadding="0" cellspacing="0" width="780">
        <%
            String helpId = "3";
        %>
        <%@ include file="encabezado.jsp" %>
</table>

<table width="760">
	<tr>
            <td width="152" valign='top'>
                    <%@ include file="menuadministrador.jsp" %>
            </td>
            <td valign='top'>
                <%
                    String tittle = "Empresas";
                %>
                <%@ include file="/titulo.jsp" %>

                <form name="companyList" method="post" action="/administrador/actualizarempresas">
                <table width="560" border="0" cellpadding="0" cellspacing="0" bgcolor="#E8E8F4" align="center">
                    <!--<tr><td align="center"><%//= request.getAttribute("message")%></td></tr>-->
                    <tr>
                        <td width="16" align="left" valign="top"><img src="images/bus1.gif" width="16" height="14"></td>
                        <td width="529">&nbsp;</td>
                        <td width="15" align="right" valign="top"><img src="images/bus3.gif" width="15" height="14"></td>
                    </tr>

                    <tr>
                        <td width='50%'>
                            <table>
                                <tr>
                                    <td>&nbsp;&nbsp;Pa&iacute;s&nbsp;</td>
                                    <td>
                                        <select name="country" onChange="submitCombo(this.name)">
                                        <%
                                        Vector countriesList = (Vector)request.getAttribute("countriesList");
                                        Enumeration en = countriesList.elements();
                                        String selected = "";
                                        int temp = Integer.parseInt(request.getAttribute("countryId").toString());
                                        for(int i = 0 ; i < countriesList.size() ; i++){
                                            int key = ((Country)en.nextElement()).getId();
                                            int temp2 = key;

                                            if(temp == temp2){
                                                selected = "selected";
                                            }else{
                                                selected = "";
                                            }   
                                        %>
                                            <option value="<%=key%>" <%=selected%>><%=((Country)countriesList.elementAt(i)).getName()%> </option>
                                        <%
                                        }
                                        %>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td>&nbsp;&nbsp;Departamento&nbsp;</td>
                                    <td>
                                        <select name="province" onChange="submitCombo(this.name)">
                                            <option value="-1" selected>Todos</option>
                                        <%
                                        Vector provincesList = (Vector)request.getAttribute("provincesList");
                                        en = provincesList.elements();
                                        selected = "";
                                        temp = Integer.parseInt(request.getAttribute("provinceId").toString());
                                        for(int i = 0 ; i < provincesList.size() ; i++){
                                            int key = ((Province)en.nextElement()).getId();
                                            int temp2 = key;

                                            if(temp == temp2){
                                                selected = "selected";
                                            }else{
                                                selected = "";
                                            }   
                                        %>
                                            <option value="<%=key%>" <%=selected%>><%=((Province)provincesList.elementAt(i)).getName()%> </option>
                                        <%
                                        }
                                        %>
                                        </select>
                                    </td>
                                </tr>
                                <tr>    
                                    <td>&nbsp;&nbsp;Ciudad&nbsp;</td>
                                    <td>
                                        <select name="city" <!--onChange="submitCombo(this.name)"-->>
                                            <option value="-1" selected>Todas</option>
                                        <%
                                        Vector citiesList = (Vector)request.getAttribute("citiesList");
                                        en = citiesList.elements();
                                        selected = "";
                                        temp = Integer.parseInt(request.getAttribute("cityId").toString());
                                        for(int i = 0 ; i < citiesList.size() ; i++){
                                            int key = ((City)en.nextElement()).getId();
                                            int temp2 = key;

                                            if(temp == temp2){
                                                selected = "selected";
                                            }else{
                                                selected = "";
                                            }   
                                        %>
                                            <option value="<%=key%>" <%=selected%>><%=((City)citiesList.elementAt(i)).getName()%> </option>
                                        <%
                                        }
                                        %>
                                        </select>
                                    </td>
                                </tr>
                            </table>
                        </td>
                        
                        <td valign="top">
                            <table>
                                <tr>
                                    <td align="left">Tipo de empresa&nbsp;</td>
                                    <td>
                                        <% 
                                            Hashtable companyTypes = DataAccess.getCompanyTypes();
                                            Enumeration typesKeys=companyTypes.keys();
                                            String s=null;
                                            String cType="";
                                        %>
                                            <select name="companyType" <!--onchange="submitCombo('this.name')"-->>
                                                <option value="-1" selected >Todas</option>
                                        <% 
                                            while(typesKeys.hasMoreElements()){
                                                Object object1 = typesKeys.nextElement();
                                                s=object1.toString();
                                                Object object2 = companyTypes.get(new Integer(s));
                                                String typeName=object2.toString();
                                                cType="1";
                                                if(request.getAttribute("companyType")!=null){
                                                    cType=(String)request.getAttribute("companyType");
                                                }
                                                if(cType.equals(s)){ %>
                                                    <option value="<%= s %>" selected ><%= typeName %></option>
                                                <%}else if(!typeName.equals("Fenalco")){%>
                                                    <option value="<%= s %>"><%= typeName %></option>
                                                <%}
                                            }
                                        %>
                                            </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td>Nombre&nbsp;</td>
                                    <td><input type="text" name='name' size='15' value='<%=(String)request.getAttribute("name")%>'></td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                    
                    <tr>
                        <td align="left" valign="bottom"><img src="images/bus2.gif" width="16" height="15"></td>
                        <td>&nbsp;</td>
                        <td align="right" valign="bottom"><img src="images/bus4.gif" width="15" height="15"></td>
                    </tr>
                </table>

                <table align="center">
                    <tr>
                        <td>
                            <jsp:include page="/general/paginador.jsp" flush="true">
                                <jsp:param name="page" value="<%=page1%>" />
                                <jsp:param name="pages" value="<%=pages%>" />
                                <jsp:param name="pageResults" value="<%=pageResults%>" />
                            </jsp:include>
                        </td>
                    </tr>    
                    <tr>
                        <td>
                            <jsp:include page="/general/resultados.jsp" flush="true">
                                <jsp:param name="firstRow" value="<%=firstRow%>" />
                                <jsp:param name="lastRow" value="<%=lastRow%>" />
                                <jsp:param name="total" value="<%=total%>" />
                            </jsp:include>
                        </td>
                    </tr>    
                </table>


                <%
                    //tittle = "Empresas";
                %>
                <%//@ include file="/titulo.jsp" %>
                <table align="center" cellspacing='2'>
                    <tr><td colspan='5' height='10'></td></tr>
                    <tr class='tableheader'>
                        <!-- Encabezado del listado -->
                        <td align="center"><font color="white">Nombre Comercial</font></td>
                        <td align="center"><font color="white">Tel&eacute;fono</font></td>
                        <td align="center"><font color="white">Estado</font></td>
                        <% if(cType.equals("2")){%>
                            <td align="center"><font color="white">Editar</font></td>
                            <td align="center"><font color="white">Minoristas<br>Exclusivos</font></td>
                        <% }else if(cType.equals("1")){ %>
                            <td align="center"><font color="white">Editar</font></td>
                            <td align="center"><font color="white">Establecimientos</font></td>
                        <%
                           }%>

                        <td align="center"><font color="white">Ver</font></td>
                        <td align="center"><font color="white">Eliminar</font></td>
                    </tr>
                    <% 
                        ListView view = (ListView)request.getAttribute("view");
                        String data;
                        int count = 0;
                        while(view.move()){
                            count++;
                            int i=0;
                            String id="";
                            String status="";

                            String color = "";
                            if(count % 2 == 0){
                                color = "#EFEFEF";
                            }else{
                                color = "FFFFF";
                            }
                    %>
                            <tr bgcolor='<%=color%>'>
                            <% while((data=(String)view.nextElement()) != null){
                                    if(i>0 && i!=3/*&& cType.equals("1")) || (i>2 && cType.equals("2"))*/ ){
                            %>
                                        <td><%= data %></td>
                                <%  }else if(i==3){ 
                                        status=data;
                                    }else if(i==0){
                                        id=data;
                                    }
                                    i++;
                               }
                            %>
                                    <!-- Columnas para las opciones -->

                                    <% Hashtable companyStatus = DataAccess.getCompanyStatus();
                                    Enumeration keys=companyStatus.keys();
                                    String temp2=null;
                                    %>
                                    <td align="center"><select name="<%="status"+id%>" onchange="operationType.value='changeStatus';companyList.action='/administrador/actualizarempresas?key=<%=id%>';companyList.submit()">
                                    <% while(keys.hasMoreElements()){
                                        Object object1 = keys.nextElement();
                                        temp2=object1.toString();
                                        Object object2 = companyStatus.get(new Integer(temp2));
                                        String statusName=object2.toString();
                                        if(status.equals(temp2)){%>
                                            <option value="<%= temp2 %>" selected><%= statusName %></option>
                                        <%}else{%>
                                            <option value="<%= temp2 %>"><%= statusName %></option>
                                        <%}
                                       }%>
                                    </select></td>
                                    <!--<td align="center"><a href="/administrador/actualizarempresas?key=<%= id %>&operationType=<%=cType.equals("2")?"updateSupplier":"updateShopKeeper"%>"><img  name="update" src="/images/buttons/sysmanager/actualizar.gif" border="0" alt="Editar"></a></td>-->
                                    <% if(cType.equals("2")){%>
                                        <td align="center"><a href="/administrador/actualizarempresas?key=<%= id %>&operationType=updateSupplier"><img  name="update" src="/images/buttons/sysmanager/actualizar.gif" border="0" alt="Editar"></a></td>
                                        <td align="center"><a href="/administrador/actualizarempresas?key=<%= id %>&operationType=setStores"><img  name="update" src="/images/buttons/sysmanager/actualizar.gif" border="0" alt="Establecer minoristas"></a></td>
                                    <% }else if(cType.equals("1")){ %>
                                        <td align="center"><a href="/administrador/actualizarempresas?key=<%= id %>&operationType=updateShopKeeper"><img  name="update" src="/images/buttons/sysmanager/actualizar.gif" border="0" alt="Editar"></a></td>
                                        <td align="center"><a href="/administrador/listaestablecimientos?key=<%= id %>"><img  name="update" src="/images/buttons/sysmanager/actualizar.gif" border="0" alt="Establecimientos"></a></td>
                                    <% }%>
                                    <!--<td align="center"><a href="#"><img  name="view" src="/images/buttons/sysmanager/detalle.gif"  border="0" alt="Ver" onClick="window.open('/administrador/verempresa?key=<%= id %>','','HEIGHT=500,WIDTH=515, SCROLLBARS=yes')"></a></td>-->
                                    <td align="center"><a href="javascript: showCompany(<%=id%>)"><img  name="view" src="/images/buttons/sysmanager/detalle.gif"  border="0" alt="Ver"></a></td>
                                    <td align="center"><input name="item" type="radio" value="<%= id %>"></td> 
                            </tr>
                        <%
                            }
                        %>
                            <tr>
                                <td colspan='7'>
                                    <jsp:include page="/general/resultados.jsp" flush="true">
                                        <jsp:param name="firstRow" value="<%=firstRow%>" />
                                        <jsp:param name="lastRow" value="<%=lastRow%>" />
                                        <jsp:param name="total" value="<%=total%>" />
                                    </jsp:include>
                                </td>
                            </tr>    
                        
                        <%
                            if( count == 0 ){
                        %>
                                <tr>
                                    <td colspan='6' align="center">
                                        <strong>No se encontraron empresas</strong>
                                    </td>
                                </tr>
                        <%
                            }else{
                                int colspan = 0;
                                if(cType.equals("-1")){
                                    colspan=4;
                                }else if(cType.equals("1")){
                                    colspan=6;
                                }else if(cType.equals("2")){
                                    colspan=6;
                                }
                        %>
                                 <tr>
                                     <td colspan="<%=colspan%>"></td>
                                     <td align="center"><input name="delete" type="submit" value="Eliminar" onClick="operationType.value='delete';return window.confirm('Está a punto de eliminar registros del sistema, desea continuar?');"><td>
                                 </tr>
                         <%
                            }
                         %>
                </table>	 

                <input name="operationType" type="hidden" value="">
                <input type="hidden" name="combo">

                </form>
            </td>
	</tr>
</table>

<table width="760">
	<tr>
		<td><%@ include file="/piepagina.jsp" %></td>
	</tr>
</table>

</body>
</html>
// “Copyright (C) <2008> Multilateral Investment Fund of the Inter-American Development Bank.
// Este archivo es parte de <ICT4BUS-PVL>
// < ICT4BUS-PVL > es software libre: usted puede redistribuirlo y/o modificarlo bajo los términos de la Licencia Pública General GNU publicada por la Fundación para el Software Libre, ya sea la versión 3 de la Licencia, o (a su elección) cualquier versión posterior.
// < ICT4BUS-PVL > se distribuye con la esperanza de que sea útil, pero SIN GARANTÍA ALGUNA; ni siquiera la garantía implícita MERCANTIL o de APTITUD PARA UN PROPÓSITO DETERMINADO. 
// Consulte los detalles de la Licencia Pública General GNU para obtener una información más detallada. 
// Debería haber recibido una copia de la Licencia Pública General GNU junto a este programa. En caso contrario, consulte http://www.gnu.org/licenses/.”







