<?php
$bDir_Analiza = CondPrivilegio(@$s_DireitosLst, $sNomeAcesso, 'A');   // Analizar banco de dados
$bDir_Recuper = CondPrivilegio(@$s_DireitosLst, $sNomeAcesso, 'R');   // Recuperar tabelas
$sNomeIdJPrin = "iJan_Mnt_BD";
$sNomeIdJan2  = 'iJan_Mnt_Comando';   // 
$sCorBarraInf = '#E8E3CE';
// Capturar matriz de trabalho
$aListaTabelas = @$aSessao['MNT_TABELAS'];
//
//  ** PRINCIPAL **
If ( EMPTY($sSubModulo) ) {
   // Criar objeto Janela
   $oJan = New R_Janela();
   $oJan->sIdObj  = $sNomeIdJPrin;
   $oJan->nWidth  = 720;   // Largura
   $oJan->nHeight = 0;     // Altura
   $oJan->nTop    = 10;
   $oJan->nLeft   = 0;
   $oJan->nLeft  += ($nTela_W/2)-($oJan->nWidth/2);
   $oJan->sTitulo = 'Manutenção de banco de dados';
   $oJan->sJavKeyDown = "return Car_".$sNomeAcesso."1.Teclas_F(this,event);";
   //
   $sJavaTravarJ = "parent.document.getElementById('".$oJan->sIdObj."_Travar').style.visibility='visible'; parent.Disabled_In_Elems('".$oJan->sIdObj."',true);";
   $oJan->sJavOnClickF = 
       $sJavaTravarJ.
   	   "parent.document.forms.fmPMnt_BD_H.acao.value='FECHAR'; ".
   	   "parent.document.forms.fmPMnt_BD_H.submod.value=''; ".
   	   "parent.document.forms.fmPMnt_BD_H.submit();".
   	   "";
   //
   // >> Inicial
   If ( EMPTY($sAcao) ) {
      //
      $oJan->aIdsObjList = ARRAY();
	  //
      $sRet_TelaJ_HTML =
         $oJan->MontaJanHTML(True).
         '<form name="fmPMnt_BD_H" accept-charset="utf-8" method="post" action="./car_js.php" target="car_'.$sNomeAcesso.'2">'.
          '<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="ITEM" type="hidden" value="">'.
         '</form>'.
		 // Lista
         '<div style="height:5px;"></div>'.
	     '<div id="dv_MntListaTab" align="left" style="width:100%; height:380px; background:#FFFFFF; overflow:auto;">'.
          '<table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">'.
           '<tr><td align="center" valign="middle" style="color:#555555;">'.
            'Aguarde um momento...<br>'.
            '<img src="images/img_load1.gif" border="0">'.
           '</td></tr>'.
          '</table>'.
		 '</div>'.
         '<div style="height:5px;"></div>'.
		 // Barra inferior
       	 '<table width="100%" style="background-color:'.$sCorBarraInf.';">'.
		  '<tr>'.
           '<td width="8px"></td>'.
           '<td width="134px" align="right" valign="center">'.
       	     HTML_Botao( 3, 'oMnt_BDAnal', "Car_".$sNomeAcesso."1.Analizar_Tabelas();"
			              , 'Analizar'  ,'images/btimg_bd_add.png'   , 112).
		   '</td>'.
           '<td width="134px" align="right" valign="center">'.
       	     HTML_Botao( 3, 'oMnt_BDCorr', "Car_".$sNomeAcesso."1.Corrigir_Tabelas();"
			              , 'Corrigir' ,'images/btimg_ativar.png'   , 112).
		   '</td>'.
		   //'<td width="134px" align="right" valign="center">'.
       	     //HTML_Botao( 3, 'oMnt_BDCons', "Car_".$sNomeAcesso."1.Consultar_Tabelas();"
			 //             , 'Consulta MySql'  ,'images/btimg_bd_add.png'   , 142).
		   //'</td>'.

		   
		   
           '<td align="left"></td>'.
           '<td width="124px" align="right" valign="center">'.
       	     HTML_Botao( 3, 'oMnt_BDSair', $oJan->sJavOnClickF
			              , 'Sair'     ,'images/btimg_fechar.png'   , 102).
		   '</td>'.
           '<td width="4px"></td>'.
		  '</tr>'.
		 '</table>'.
         $oJan->MontaJanHTML(False);
      //
      // Java inicial
      $sRet_Javs .=
        'parent.document.getElementById("dv_MenuL_Barra").style.visibility="hidden";'.
 	    "";
      // Java posterior
      $sRet_JavsPos .=
	    "var FmH = parent.document.forms['fmPMnt_BD_H'];".
		//
        "parent.document.getElementById('oMnt_BDAnal').style.display='none';".
        "parent.document.getElementById('oMnt_BDCorr').style.display='none';".
		// teclas da janela
		"function Teclas_F( Objeto, Evento ) {".
           "vNumTecla = parent.Tecla_Apertada(Evento);".
		   "if ( vNumTecla==116) { ".           //  ** [F5] **       Não atualizar
		       //
		   "} else if ( vNumTecla==27) { ".     //  ** [ESC] **      (SAIR)
              "setTimeout( function () { ".$oJan->sJavOnClickF." } , 1);".
		   "}".
		   "return false;".
		"}".
		//
		"Puxar_Tabelas();".
		"function Puxar_Tabelas() {".
           "FmH.acao.value='PUXAR_TAB';".
           "FmH.submit();".
		"}".
		"function Analizar_Tabelas() {".
           "parent.document.getElementById('oMnt_BDAnal').style.display='none';".
           "FmH.acao.value='ANALIZAR_TAB';".
           "FmH.ITEM.value='0';".
           "FmH.submit();".
		"}".
		"function Corrigir_Tabelas() {".
           "parent.document.getElementById('oMnt_BDAnal').style.display='none';".
           "parent.document.getElementById('oMnt_BDCorr').style.display='none';".
           "FmH.acao.value='CORRIGIR_TAB';".
           "FmH.submit();".
		"}".
		//"function Consultar_Tabelas() {".
        //   "FmH.acao.value='CONSULTA_TAB';".
        //   "FmH.submit();".
		//"}".
		"";
   //
   // >> Puxar tabelas
   } ElseIf ( $sAcao=="PUXAR_TAB" ) {
	  $aListaTabelas = ARRAY();
	  $sRet_Tabela   = '';
	  $RowsT = MYSQL_QUERY("SHOW TABLES;") or die(MYSQL_ERROR());
	  While ( $RowT = MYSQL_FETCH_ARRAY($RowsT) ) {
	     $aItem = ARRAY( 0 => $RowT[0], 1 => False );
	     $aListaTabelas[] = $aItem;
		 $sRet_Tabela .=
		    $RowT[0].
			'&nbsp;&nbsp;<span id="sp_'.$RowT[0].'"></span>'.
			'<br>';
	  }
	  $sRet_Tabela =
        '<table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">'.
         '<tr>'.
		  '<td width="8px">'.
		  '</td>'.
		  '<td align="left" style="color:#555555; font-size:16px;">'.
		   $sRet_Tabela.
		  '</td>'.
		 '</tr>'.
		'</table>';
      // Java
      $sRet_Javs .=
        "parent.document.getElementById('dv_MntListaTab').innerHTML = ".JAVA_CharCode($sRet_Tabela).";".
        "parent.document.getElementById('oMnt_BDAnal').style.display='inline-block';".
		"";
   //
   // >> Analizar tabelas
   } ElseIf ( $sAcao=="ANALIZAR_TAB" ) {
      $nItem = Somente_Numeros(@$_POST['ITEM'])+0;
      If ( ISSET( $aListaTabelas[$nItem] ) ) {
	     $sNomeTabela = $aListaTabelas[$nItem][0];
	     $RowT = MYSQL_QUERY("CHECK TABLE ".$sNomeTabela." EXTENDED;") or die(MYSQL_ERROR());
	     If ( $RowT = MYSQL_FETCH_ARRAY($RowT) ) {
		    //$aListaTabelas[$nItem][1] = ( STRTOUPPER($RowT['Msg_type'])=="ERROR" );   Bloquado por André referente a alteração 3114
		    $aListaTabelas[$nItem][1] = ( STRTOUPPER($RowT['Msg_text'])<>"OK" );
		    $sRet_Conteudo = $RowT['Msg_text'];
			If ( $aListaTabelas[$nItem][1] ) {
			   $sRet_Conteudo = '<span style="color:#BB0000;">'.$sRet_Conteudo.'</span>';
			   $sRet_Javs .= ( $bDir_Recuper ? "parent.document.getElementById('oMnt_BDCorr').style.display='inline-block';" : "");
			} Else {
			   $sRet_Conteudo = '<span style="color:#669933;">'.$sRet_Conteudo.'</span>';
			}
            $sRet_Javs .= "parent.document.getElementById('sp_".$sNomeTabela."').innerHTML = ".JAVA_CharCode($sRet_Conteudo).";";
		 }
         // Java
         $sRet_Javs .=
	       "var FmH = parent.document.forms['fmPMnt_BD_H'];".
           "FmH.acao.value='ANALIZAR_TAB';".
           "FmH.ITEM.value='".($nItem+1)."';".
           "FmH.submit();".
		   "";
	  } Else {
         // Java
         $sRet_Javs .=
           "parent.document.getElementById('oMnt_BDAnal').style.display='inline-block';".
	       "";
	  }
   // >> Corrigir tabelas
   } ElseIf ( $sAcao=="CORRIGIR_TAB" ) {
      $bNaoCorrigido = False;
      ForEach ( $aListaTabelas As $aItem ) {
	     $sNomeTabela = $aItem[0];
		 $bCorrompido = $aItem[1];
		 If ( $bCorrompido ) {
	        $RowT = MYSQL_QUERY("REPAIR TABLE ".$sNomeTabela." EXTENDED;") or die(MYSQL_ERROR());
	        If ( $RowT = MYSQL_FETCH_ARRAY($RowT) ) {
		       $bCorrompido = ( STRTOUPPER($RowT['Msg_type'])=="ERROR" );
			}
		    $sRet_Conteudo = $RowT['Msg_text'];
			If ( $bCorrompido ) {
			   $sRet_Conteudo = '<span style="color:#BB0000;">->'.$sRet_Conteudo.'</span>';
			   $bNaoCorrigido = True;
			} Else {
			   $sRet_Conteudo = '<span style="color:#669933;">->'.$sRet_Conteudo.'</span>';
			}
            $sRet_Javs .= "parent.document.getElementById('sp_".$sNomeTabela."').innerHTML = ".JAVA_CharCode($sRet_Conteudo).";";
			
		 } Else {
            $sRet_Javs .= "parent.document.getElementById('sp_".$sNomeTabela."').innerHTML = '';";
		 }
	  }
      // Java
      $sRet_Javs .=
	    ( $bNaoCorrigido ? "parent.document.getElementById('oMnt_BDCorr').style.display='inline-block';" : "").
        "parent.document.getElementById('oMnt_BDAnal').style.display='inline-block';".
	    "";
   //
   // >> Consultar tabelas
   } ElseIf ( $sAcao=="CONSULTA_TAB" ) {
      $sRet_Javs .= 
	           $sJavaTravarJ.
		       "setTimeout( function () { ".
			      "var fmH = parent.document.forms['fmPMnt_BD_H'];".
				  "TargetAnt = fmH.target;".
			      "fmH.target='car_".$sNomeAcesso."3';".
		          "fmH.acao.value=''; ".
		          "fmH.submod.value='SQL_COMANDO'; ".
		          "fmH.submit();".
			      "fmH.target = TargetAnt;".
		          "fmH.submod.value='".$sSubModulo."'; ".
		       "} , 100);".
		       "";
     
   // >> Fechar janela
   } ElseIf ( $sAcao=="FECHAR" ) {
      //
      $sRet_Javs .=
         "parent.document.getElementById('dv_MenuL_Barra').style.visibility='visible';".
         "parent.RmElem('".$oJan->sIdObj."');";
   }
   //
} ElseIf ( $sSubModulo=="SQL_COMANDO" ) {  
   $bDir_Consult = True;                  
   $sNomeIdJ_Ant = $sNomeIdJPrin;          
   $sNomeIdCampo = "";   
   $sJavaExePos  = "";
   REQUIRE "./modulos/mnt_consulta_sql.inc";
   
   
}
//Salvar ou limpar matriz de trabalho
If ( ISSET($aListaTabelas) ) {
   $aSessao['MNT_TABELAS'] = $aListaTabelas;
} Else {
   If ( ISSET($aSessao['MNT_TABELAS']) ) { UNSET($aSessao['MNT_TABELAS']); }
}
//
?>