<?php
require_once("inc/protecao-admin.php");

if($_POST) {

if(empty($_POST["ftp_servidor"]) or empty($_POST["ftp_usuario"]) or empty($_POST["ftp_senha"])) {
$_SESSION["status_acao"] = "<div class='alert alert-danger text-center' role='alert'>".lang_info_campos_vazios."</div>";
header("Location: ".$_SERVER['HTTP_REFERER']."");
exit();
}

$dados_config_bkp_atual = mysqli_fetch_array(mysqli_query($conexao,"SELECT * FROM configuracoes_backup"));

if(empty($dados_config_bkp_atual['ftp_servidor'])) {

mysqli_query($conexao,"INSERT INTO configuracoes_backup (ftp_servidor,ftp_usuario,ftp_senha,bkp_arquivos,bkp_mysql,bkp_mysql_stats) VALUES ('".$_POST["ftp_servidor"]."','".$_POST["ftp_usuario"]."','".$_POST["ftp_senha"]."','".$_POST["bkp_arquivos"]."','".$_POST["bkp_mysql"]."','".$_POST["bkp_mysql_stats"]."')") or die("Erro: ".mysqli_error($conexao));

} else {

mysqli_query($conexao,"Update configuracoes_backup set ftp_servidor = '".$_POST["ftp_servidor"]."', ftp_usuario = '".$_POST["ftp_usuario"]."', ftp_senha = '".$_POST["ftp_senha"]."', bkp_arquivos = '".$_POST["bkp_arquivos"]."', bkp_mysql = '".$_POST["bkp_mysql"]."', bkp_mysql_stats = '".$_POST["bkp_mysql_stats"]."'") or die("Erro: ".mysqli_error($conexao));

}

// Cria o sessão do status das ações executadas e redireciona.
$_SESSION["status_acao"] = "<div class='alert alert-success text-center' role='alert'>Backup configurado com sucesso.</div>";

header("Location: /admin/admin-backup");
exit();
}

$dados_config_bkp = mysqli_fetch_array(mysqli_query($conexao,"SELECT * FROM configuracoes_backup"));
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <title>Administra&ccedil;&atilde;o</title>
  <link rel="stylesheet" href="/inc/materialdesignicons.min.css">
  <link rel="stylesheet" href="/inc/font-awesome.min.css">
  <link rel="stylesheet" href="/inc/vendor.bundle.base.css">
  <link rel="stylesheet" href="/inc/vendor.bundle.addons.css">  
  <?php if($dados_revenda["tema_dark"] == "sim") { ?>
  <link rel="stylesheet" href="/inc/theme-dark.css">
  <?php } else { ?>
  <link rel="stylesheet" href="/inc/theme.css">
  <?php } ?>
  <link rel="shortcut icon" href="/img/favicon.ico" />
  <script type="text/javascript" src="/admin/inc/ajax.js"></script>
  <script type="text/javascript" src="/admin/inc/javascript.js"></script>
</head>
<body>
  <div class="container-scroller">
    <nav class="navbar col-lg-12 col-12 p-0 fixed-top d-flex flex-row">
      <div class="text-center navbar-brand-wrapper d-flex align-items-center justify-content-center">
        <a class="navbar-brand brand-logo" href="/dashboard"><span>Administra&ccedil;&atilde;o</span></a>
        <a class="navbar-brand brand-logo-mini" href="/dashboard"><i class="fa fa-lock text-yellow"></i></a>
      </div>
      <div class="navbar-menu-wrapper d-flex align-items-center justify-content-end justify-content-lg-start">
        <button class="navbar-toggler navbar-toggler align-self-center" type="button" data-toggle="minimize">
          <span class="mdi mdi-menu text-icones"></span>
        </button>
        <ul class="navbar-nav mr-lg-2">
          <li class="nav-item nav-search d-none d-lg-block">
            <div class="input-group">
              <div class="input-group-prepend">
                <span class="input-group-text" id="search">
                  <i class="mdi mdi-magnify"></i>
                </span>
              </div>
              <input type="text" class="form-control" id="busca_chave" placeholder="Busca" aria-label="search" aria-describedby="search">
            </div>
          </li>
        </ul>
        <ul class="navbar-nav navbar-nav-right">
          <li class="nav-item">
            <a class="nav-link" href="/admin/sair"><i class="mdi mdi-logout f-22 text-icones" data-toggle="tooltip" data-placement="bottom" title="Sair/logout"></i></a>
          </li>
        </ul>
        <button class="navbar-toggler navbar-toggler-right d-lg-none align-self-center" type="button" data-toggle="offcanvas">
          <span class="mdi mdi-menu text-icones"></span>
        </button>
      </div>
    </nav>
    <div class="container-fluid page-body-wrapper">
      <nav class="sidebar sidebar-offcanvas" id="sidebar">
        <ul class="nav">
          <li class="nav-item">
            <a class="nav-link" href="/admin/admin-streamings">
              <i class="fa fa-headphones text-icones"></i>
              <span class="menu-title"><?php echo lang_menu_streaming; ?></span>
            </a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="/admin/admin-revendas">
              <i class="fa fa-users text-icones"></i>
              <span class="menu-title"><?php echo lang_menu_revenda; ?></span>
            </a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="/admin/admin-servidores">
              <i class="fa fa-tasks text-icones"></i>
              <span class="menu-title"><?php echo lang_menu_servidor; ?></span>
            </a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="/admin/admin-avisos">
              <i class="fa fa-info-circle text-icones"></i>
              <span class="menu-title"><?php echo lang_menu_avisos; ?></span>
            </a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="/admin/admin-tutoriais">
              <i class="fa fa-life-bouy text-icones"></i>
              <span class="menu-title"><?php echo lang_menu_tutoriais; ?></span>
            </a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="/admin/admin-apps">
              <i class="fa fa-android text-icones"></i>
              <span class="menu-title"><?php echo lang_menu_apps; ?></span>
            </a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="/admin/admin-logs-acoes">
              <i class="fa fa-file-text-o text-icones"></i>
              <span class="menu-title"><?php echo lang_menu_logs_acoes; ?></span>
            </a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="/admin/admin-backup">
              <i class="fa fa-shield text-icones"></i>
              <span class="menu-title">Backup</span>
            </a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="/admin/admin-estatisticas">
              <i class="fa fa-bar-chart-o text-icones"></i>
              <span class="menu-title"><?php echo lang_menu_estatisticas; ?></span>
            </a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="/admin/admin-configuracoes">
              <i class="fa fa-cog text-icones"></i>
              <span class="menu-title"><?php echo lang_menu_configuracoes; ?></span>
            </a>
          </li>   
        </ul>
      </nav>
      <!-- partial -->
      <div class="main-panel">
        <div class="content-wrapper">
          <div id="local-status-acoes"><?php echo $_SESSION['status_acao'];unset($_SESSION['status_acao']); ?></div>
          <div class="row">            

            <div class="col-lg-12">
              <div class="card">
                  <div class="card-header">
                    <header>Backup</header>
                  </div>
                <div class="card-body">
                     <form method="post" action="/admin/admin-backup" name="configurar">
                                            <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
                                              <tr>
                                                <td width="200" height="50" align="left" style="padding-left:5px;">Servidor FTP</td>
                                                <td align="left"><input name="ftp_servidor" type="text" class="form-control" id="ftp_servidor" required="required" style="width:100%;" value="<?php echo $dados_config_bkp["ftp_servidor"]; ?>" /></td>
                                              </tr>
                                              <tr>
                                                <td height="50" align="left" style="padding-left:5px;">Usuario FTP</td>
                                                <td align="left"><input name="ftp_usuario" type="text" class="form-control" id="ftp_usuario" required="required" style="width:100%;" value="<?php echo $dados_config_bkp["ftp_usuario"]; ?>" /></td>
                                              </tr>
                                              <tr>
                                                <td height="50" align="left" style="padding-left:5px;">Senha FTP</td>
                                                <td align="left"><input name="ftp_senha" type="password" class="form-control" id="ftp_senha" required="required" style="width:100%;" value="<?php echo $dados_config_bkp["ftp_senha"]; ?>" /></td>
                                              </tr>
                                              <tr>
                                                <td height="50" align="left" style="padding-left:5px;">Backup Arquivos</td>
                                                <td align="left" class="texto_padrao"><input name="bkp_arquivos" id="bkp_arquivos" type="checkbox" value="sim" <?php if($dados_config_bkp["bkp_arquivos"] == "sim") { echo 'checked="checked"';} ?> /></td>
                                              </tr>
                                              <tr>
                                                <td height="50" align="left" style="padding-left:5px;">Backup MySQL</td>
                                                <td align="left" class="texto_padrao"><input name="bkp_mysql" id="bkp_mysql" type="checkbox" value="sim" <?php if($dados_config_bkp["bkp_mysql"] == "sim") { echo 'checked="checked"';} ?> /></td>
                                              </tr>
                                              <tr>
                                                <td height="50" align="left" style="padding-left:5px;">Backup MySQL Estatisticas</td>
                                                <td align="left" class="texto_padrao"><input name="bkp_mysql_stats" id="bkp_mysql_stats" type="checkbox" value="sim" <?php if($dados_config_bkp["bkp_mysql_stats"] == "sim") { echo 'checked="checked"';} ?> /></td>
                                              </tr><tr>
                                            <td height="50" colspan="2" align="right"><button type="button" class="btn btn-info" onClick="document.configurar.submit();"><?php echo lang_botao_titulo_configurar; ?></button></td>
                                          </tr>
                                            </table>
                                        </form>
                </div>
              </div>
            </div>

            <div class="col-lg-12">
              <div class="card">
                  <div class="card-header">
                    <header>Cron</header>
                  </div>
                <div class="card-body" style="padding-bottom: 20px; padding-top: 20px">
                     <input class="form-control" onclick="this.select();" value="0 1 * * * /usr/bin/php /home/painelvideo/public_html/robots/backup-painel-mysql.php" />
                </div>
              </div>
            </div>

            <div class="col-lg-12">
              <div class="card">
                  <div class="card-header">
                    <header>Logs</header>
                  </div>
                <div class="card-body">
                     <?php echo nl2br($dados_config_bkp["log"]); ?>
                </div>
              </div>
            </div>

          </div>
        </div>
      </div>
      <!-- modal -->                
        <div class="modal fade" id="log-sistema" tabindex="-1" role="dialog" aria-hidden="true">
          <div class="modal-dialog modal-dialog-centered" role="document">
              <div class="modal-content">
                  <div class="modal-body" id="log-sistema-conteudo" style="text-align:center"><img src="/img/ajax-loader.gif" /></div>
              </div>
          </div>
        </div>

    </div>
  </div>
  <script src="/inc/vendor.bundle.base.js"></script>
  <script src="/inc/vendor.bundle.addons.js"></script>
  <script src="/inc/theme.js"></script>
  <script>
  $('#busca_chave').keypress(function (e) {
    if (e.which == 13) {
       window.location = "/admin/admin-streamings/resultado/"+document.getElementById("busca_chave").value;
    }
  }); 
  </script>
</body>
</html>