<?php

// Criar objeto Janela
$oJanelaPrinc = New R_Janela();
$oJanelaPrinc->sIdObj  = "iJan_LstCadCli"; 
$oJanelaPrinc->nTop    = 5;
$oJanelaPrinc->nLeft   = 20;
$oJanelaPrinc->nWidth  = 880;   // Largura
$oJanelaPrinc->nHeight = 0;     // Altura
$oJanelaPrinc->nLeft  += ($nTela_W/2)-($oJanelaPrinc->nWidth/2);
$oJanelaPrinc->sTitulo = 'Clientes';
$oJanelaPrinc->sJavKeyDown = "return Car_".$sNomeAcesso."10.Teclas_F(this,event);";
//
$sJavaTravarJ = "parent.document.getElementById('".$oJanelaPrinc->sIdObj."_Travar').style.visibility='visible'; parent.Disabled_In_Elems('".$oJanelaPrinc->sIdObj."',true);";
$oJanelaPrinc->sJavOnClickF = 
    $sJavaTravarJ.
	"parent.document.forms.fmPCadCli.acao.value='FECHAR'; ".
	"parent.document.forms.fmPCadCli.Cli_PChave.value=''; ".
	"parent.document.forms.fmPCadCli.Cli_NumCam.value=''; ".
	"parent.document.forms.fmPCadCli.submit();".
	"";
//
//
// Criar objeto GRID
$oTabelaRegs = New Pad_GridPesq();
$oTabelaRegs->sNomeTabela  = 'cad_cliente';
$oTabelaRegs->sIdObjTabela = 'iQdr_ListaCli';
$oTabelaRegs->GLstCampos( 'CLI_CODIGOI,Codigo,68px,STRZERO=8;'.
                          'CLI_NOMECP,Nome;'.
						  'TEL_NUMERO,Telefone,90px,TEL;'.
						  'CLI_ENDLOG,Endereço,220px;'.
						  'CLI_ENDNUM,Numero,50px;'.
						  'CID_NOMECD,Cidade,140px;'
						 );
$oTabelaRegs->sCampoIndice = 'CLI_CODIGOI';
$oTabelaRegs->nWidth  = $oJanelaPrinc->nWidth;
$oTabelaRegs->nHeight = 350;    // Altura
$oTabelaRegs->sCampoOnClick  = "Car_".$sNomeAcesso."10.P_Sel(<NUMCAM>); Car_".$sNomeAcesso."10.P_Atu();";
$oTabelaRegs->sLinhaOnClick  = "Car_".$sNomeAcesso."10.PRow_Sel(<QTDCOL>,<SELLIN>);";
$oTabelaRegs->sLinhaOnDblClk = "Car_".$sNomeAcesso."10.Pesq_Seleciona();".
							   "";
$oTabelaRegs->sJavSelectReg = "sSelIndiceC = S_Indice;".
	                          "if (parseInt(sSelIndiceC)>0) {".
	                              "parent.document.getElementById('CadCCli_BT_Sel').style.display='inline-block';".
							  "}".
                              'parent.document.getElementById("iCadCli_PChave").focus();';
//
// MONTAR LISTA DE CADASTROS
If ( $sAcao=="LISTAR" ) {
   If ($bDir_Consult) {
      $sP_PChave = @$_REQUEST['Cli_PChave'];
	  $nP_NumCam = @$_REQUEST['Cli_NumCam']+0;
	  $nTip_Busca = @$_REQUEST['Tip_Busca'];
	  // quando digitar exe: /joao na tela de consulta cliente 
	  If ( ISSET($aSessao['Pesq_Chave']) ){
         $sP_PChave = $aSessao['Pesq_Chave'];
         UNSET($aSessao['Pesq_Chave']);
      }
	  // Montar condição para a pesquisa
      $sWhereSQL = '';
	  $sOrderSQL = '';
      If ( $nP_NumCam==0 ) { $nP_NumCam=2; }
	  $aLstCampo = EXPLODE(',',@$oTabelaRegs->aLstCampos[$nP_NumCam-1]);
      If ( STRLEN(@$aLstCampo[0])>0 ) {
	  
         If ( STRLEN($sP_PChave)>0 ) {
			If ($nTip_Busca == 1) {
			   $sWhereSQL .= 'AND '.$aLstCampo[0].' LIKE "'.$sP_PChave.'%" ';
			}
			If ($nTip_Busca == 2) {
			   $sWhereSQL .= 'AND '.$aLstCampo[0].' LIKE "%'.$sP_PChave.'%" ';
			}				
	     }
		 $sOrderSQL = 'ORDER BY '.$aLstCampo[0].' ASC ';
      }
	  $sWhereSQL .=' AND CLI_TRAREG=0 ';
	  
	  $oTabelaRegs->sBDQuery =
            /*
				"SELECT cli.*,".
			    "COALESCE(TEL_NUMERO,'') AS TEL_NUMERO,".
				"COALESCE(CID_NOMECD,'') AS CID_NOMECD ".
		    "FROM cad_cliente as cli ".
            "LEFT JOIN cad_telefones AS tel ON tel.TEL_CODCAD = cli.CLI_CODIGOI ".
            "LEFT JOIN cad_cidade    AS cid ON cid.CID_CODIGO = cli.CLI_CODCID ".
            "WHERE COALESCE(tel.TEL_TIPCAD,1)=1 ".
	        $sWhereSQL.
            "GROUP BY CLI_CODIGOI ".
			$sOrderSQL.
			";";*/
			
			"SELECT cli.CLI_CODIGOI,".
                 "cli.CLI_NOMECP,".
                 "cli.CLI_DTACAD,".
                 "cli.CLI_ENDLOG,".
                 "cli.CLI_ENDBAI,".
                 "cli.CLI_ENDNUM,".
                 "cli.CLI_ENDCPL,".
                 "cli.CLI_CODCID,".
                 "cli.CLI_ENDCEP,".
                 "cli.CLI_PRZVAL,".
                 "cli.CLI_OBS001,".
                 "cli.CLI_OBS002,".
                 "cli.CLI_TRAREG,".
                 "tel.TEL_NUMERO,".
                 "tel.TEL_CODCAD,".
                 "tel.TEL_TIPCAD,".
                 "COALESCE(TEL_NUMERO,'') AS TEL_NUMERO, ".
                 "COALESCE(CID_NOMECD,'') AS CID_NOMECD ".
         "FROM cad_cliente AS cli ".
              "LEFT JOIN cad_cidade AS cid ON cid.CID_CODIGO = cli.CLI_CODCID ".
              "LEFT JOIN cad_telefones AS tel ON tel.TEL_CODCAD = cli.CLI_CODIGOI AND ".
                                                "COALESCE(tel.TEL_TIPCAD,1)=1 ".     		 
         "WHERE 1 = 1 ".$sWhereSQL.
		 
         "GROUP BY CLI_CODIGOI ".
         $sOrderSQL.
         ";";
		 
		 
         // ANTES ESTAVA ASSIM ( WILIAM YABUTA TINHA USADO INNER JOIN PROBLEMA OASIS) HAROLDO CIENTE
         //"INNER JOIN cad_cidade AS cid ON cid.CID_CODIGO = cli.CLI_CODCID ".
         //"INNER JOIN cad_telefones AS tel ON tel.TEL_CODCAD = cli.CLI_CODIGOI ".
         //"WHERE COALESCE(tel.TEL_TIPCAD,1)=1 ".
		 //	 $sWhereSQL.
         //"GROUP BY CLI_CODIGOI ".
         //$sOrderSQL.
         //";";
	  //
      // Tabela
      $sRet_TelaJ_HTML = $oTabelaRegs->MontaTabelaHTML();
      // Java
      $sRet_Javs .=
         "var iQdr_ListaCli = parent.document.getElementById('".$oTabelaRegs->sIdObjTabela."');".
	     'iQdr_ListaCli.innerHTML = '.JAVA_CharCode($sRet_TelaJ_HTML).';'.
	     '';
      // Java posterior
      $sRet_JavsPos .=
         'parent.Car_'.$sNomeAcesso.'10.'.$oTabelaRegs->sNomeTabela.'_Rowsel = 1;'.
         'parent.Car_'.$sNomeAcesso.'10.P_Sel('.$nP_NumCam.');'.
	     '';
      $sRet_TelaJ_HTML = '';
   }
//
// FECHAR
} ElseIf ( $sAcao=="FECHAR" ) {
   $sRet_Javs .=
	  "parent.document.getElementById('".$sNomeIdJ_Ant."_Travar').style.visibility='hidden';".
	  "parent.Disabled_In_Elems('".$sNomeIdJ_Ant."',false);".
	  $sJavaExePos.
	  'parent.document.getElementById("'.$sNomeIdCampo.'").focus();'.
      "parent.RmElem('".$oJanelaPrinc->sIdObj."');";
//
// JANELA TELA PRINCIPAL
} Else {
   //
   $oJanelaPrinc->aIdsObjList = ARRAY( 'iCadCli_PChave', 'iTip_Busca' );
   // Tela HTML
   $sRet_TelaJ_HTML =
	  $oJanelaPrinc->MontaJanHTML(True).
      '<form name="fmPCadCli" accept-charset="utf-8" method="post" action="./car_js.php" target="car_'.$sNomeAcesso.'11">'.
       '<input name="mod" type="hidden" value="'.$sModulo.'">'.
       '<input name="submod" type="hidden" value="'.$sSubModulo.'">'.
       '<input name="acao" type="hidden" value="">'.
	   '<input name="nomeacs" type="hidden" value="'.$sNomeAcesso.'">'.
	   '<input name="Cli_PChave" type="hidden" value="">'.
	   '<input name="Cli_NumCam" type="hidden" value="">'.
	   '<input name="Cli_Codigo" type="hidden" value="">'.
	   '<input name="Tip_Busca" type="hidden" value="">'.
    '</form>'.
   	  '<table width="100%"></tr>'.
   	   '<td width="4px" height="10px" align="left">&nbsp;</td>'.
   	   '<td align="left" width="70px">'.
   	    'Procurar por: '.
   	   '</td>'.
   	   '<td align="left">'.
   	    '<input id="iCadCli_PChave" type="text" maxlength="30" style="width:70%; height:24px; background:#FFFBEC; border-width: 1px; border-style: solid; border-color: #0099FF;">'.
        '&nbsp;&nbsp;'.         	   
		'Tipo Busca:&nbsp;&nbsp;'.
		'<select id="iTip_Busca" onchange="Car_'.$sNomeAcesso.'10.P_Atu();" style="border-width: 1px; border-style: solid; border-color: #0099FF;margin-top:-20px;height:20px; ">'.
				 '<option value="1">Normal</option>'.				  
			     '<option value="2" selected>Contida</option>'.
		'</select>'.
   	   '</td>'.
   	   '<td align="right" width="130px">'.
   	   HTML_Botao( 3, 'CadCli_BT_Pro'  , 'Car_'.$sNomeAcesso.'10.P_Atu();'
	                , 'Procurar (F10)' ,'images/btimg_lupa.png',120).
   	   '</td>'.
   	   '<td width="4px" align="left">&nbsp;</td>'.
   	  '</tr></table>'.
	  $oTabelaRegs->MontaDivHtml().
   	  '<table width="100%"></tr>'.
   	   '<td width="4px" height="40px" align="left">&nbsp;</td>'.
   	   '</td>'.
   	   '<td>'.
   	    HTML_Botao( 3, 'CadCCli_BT_Sel',   'Car_'.$sNomeAcesso.'10.Pesq_Seleciona();'
		             , 'Seleciona (ENTER)','images/btimg_ativar.png').'&nbsp;&nbsp;'.
   	   '</td>'.
   	   '<td width="120px" align="left">'.
   	    HTML_Botao( 3, 'CadCCli_BT_Fch'   , $oJanelaPrinc->sJavOnClickF
                     , 'Sair (ESC)'       ,'images/btimg_fechar.png').
   	   '</td>'.
   	  '</tr></table>'.
	  $oJanelaPrinc->MontaJanHTML(False);
   //
   $sRet_Javs .=
	  "parent.document.getElementById('".$sNomeIdJ_Ant."_Travar').style.visibility='visible';".
	  "parent.Disabled_In_Elems('".$sNomeIdJ_Ant."',true);".
	  'parent.document.getElementById("dv_MenuL_Barra").style.visibility="hidden";'.
      "var sSelIndiceC='';".
      "function P_Atu() {".
        "sSelIndiceC='';".
	    $oTabelaRegs->MontaJavaAgu().
	    "parent.document.getElementById('CadCCli_BT_Sel').style.display='none';".
     	"parent.document.forms.fmPCadCli.acao.value='LISTAR'; ".
     	"parent.document.forms.fmPCadCli.Cli_PChave.value=parent.document.getElementById('iCadCli_PChave').value; ".
     	"parent.document.forms.fmPCadCli.Cli_NumCam.value=nPesq_CampoSel+''; ".
		"parent.document.forms.fmPCadCli.Tip_Busca.value = parent.document.getElementById('iTip_Busca').value;".
     	"setTimeout( function() { parent.document.forms.fmPCadCli.submit(); }, 100);".
      '}'.
	  'function Pesq_Seleciona() {'.
	    'parent.document.getElementById("'.$sNomeIdCampo.'").value = sSelIndiceC;'.
	    $oJanelaPrinc->sJavOnClickF.
	  '}'.
      $oTabelaRegs->MontaJavaPri();
      '';
   // Java posterior
   $sRet_JavsPos .=
	  "P_Atu();".
	  'parent.document.getElementById("iCadCli_PChave").focus();'.
	  //
	  "function Teclas_F( Objeto, Evento ) {".
         "vNumTecla = parent.Tecla_Apertada(Evento);".
	     "if ( vNumTecla==116) { ".        //  ** [F5] **         Não atualizar
             //
	     "} else if ( vNumTecla==121) { ". //  ** [F10] **        Procurar
	         "P_Atu();".
	     "} else if ( vNumTecla==27) { ".  //  ** [ESC] **        (SAIR)
              "setTimeout( parent.document.getElementById('CadCCli_BT_Fch').onclick , 100);".
	     "} else if ( vNumTecla==13) { ". //   ** [ENTER] **      Selecionar
              "if ( parent.document.getElementById('CadCCli_BT_Sel').style.display == 'none') {".
			     "P_Atu();". 
                 "parent.document.getElementById('iCadCli_PChave').value='';".
			  "} else {".
                   "setTimeout( parent.document.getElementById('CadCCli_BT_Sel').onclick , 100);".
			  "}".
		 "} else {".
	          "P_TeclasPress_fm(Evento);".
		      "return true;".
		 "}".
		 "return false;".
	  '}'.
	  //
	  "";
}
?>