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

$total_avisos_ativos = mysqli_num_rows(mysqli_query($conexao,"SELECT * FROM avisos WHERE area = 'streaming'"));
$checar_status_aviso = mysqli_num_rows(mysqli_query($conexao,"SELECT * FROM avisos_desativados WHERE area = 'streaming' AND codigo_usuario = '".$dados_stm["porta"]."'"));
$total_avisos_exibir = ($total_avisos_ativos > $checar_status_aviso) ? $total_avisos_ativos-$checar_status_aviso : 0;

if($_POST["executar"]) {

// Conexão SSH
$ssh = new SSH();
$ssh->conectar($dados_servidor["ip"],$dados_servidor["porta_ssh"]);
$ssh->autenticar("root",code_decode($dados_servidor["senha"],"D"));

if($_POST["tipo"] == "texto") {

$resultado = @file_get_contents("http://".$dados_servidor["ip"].":555/converter-texto-audio.php?porta=".$dados_stm["porta"]."&texto=".urlencode($_POST["texto"])."&config=".urlencode($_POST["texto_voz"])."");

if($resultado == "erro") {

// Cria o sessão do status das ações executadas e redireciona.
$_SESSION["status_acao"] = '<div class="alert alert-warning text-center" role="alert">Ocorreu um erro ao tentar disparar o texto, tente novamente ou contate nosso atendimento.</div>';

header("Location: /autodj-disparar-conteudo");
exit();

}

sleep(2);

$ssh->executar('(sleep 1; echo "injetar_texto.push /home/streaming/configs/texto-'.$dados_stm["porta"].'.mp3";sleep 1) | netcat localhost '.$dados_stm["porta_telnet"].''); 

} else {

$ssh->executar('(sleep 1; echo "injetar.push /home/streaming/hora_certa/'.$dados_stm["porta"].'_'.$_POST["hora_certa_voz"].'.mp3";sleep 1) | netcat localhost '.$dados_stm["porta_telnet"].''); 

}

// Cria o sessão do status das ações executadas e redireciona.
$_SESSION["status_acao"] = '<div class="alert alert-success text-center" role="alert">O conte&uacute;do ser&aacute; disparado em 10 segundos.</div>';

header("Location: /autodj-disparar-conteudo");
exit();
}
?>
<!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>Streaming <?php echo $dados_stm["porta"]; ?></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">
  <link rel="stylesheet" href="/inc/theme.css">
  <link rel="shortcut icon" href="/img/favicon.ico">
  <script type="text/javascript" src="/inc/ajax-streaming.js"></script>
  <script type="text/javascript" src="/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">
        <?php if($dados_revenda["url_logo"]) { ?>
        <a class="navbar-brand brand-logo" href="/dashboard"><img id="logo" src="<?php echo $dados_revenda["url_logo"]; ?>" title="Painel" height="60" border="0"></a>
        <?php } else { ?>
        <a class="navbar-brand brand-logo" href="/dashboard"><span>Painel Streaming</span></a>
        <?php } ?>
        <a class="navbar-brand brand-logo-mini" href="/dashboard"><img src="/img/icones/img-icone-logo.png" title="Painel" width="32" height="32" border="0"></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 navbar-nav-right">
          <li class="nav-item nav-profile dropdown show">
            <a class="nav-link dropdown-toggle" href="#" data-toggle="dropdown" id="profileDropdown" aria-expanded="true">
              <i class="fa fa-power-off f-18 text-icones" data-toggle="tooltip" data-placement="bottom" title="<?php echo $lang['lang_botao_titulo_ligar']; ?>/<?php echo $lang['lang_botao_titulo_desligar']; ?>"></i>
            </a>
            <div class="dropdown-menu dropdown-menu-right navbar-dropdown" aria-labelledby="profileDropdown">
              <a class="dropdown-item text-success" href="javascript:ligar_streaming('<?php echo $porta_code; ?>');"> <?php echo $lang['lang_botao_titulo_ligar']; ?> Streaming</a>
              <a class="dropdown-item text-danger" href="javascript:desligar_streaming('<?php echo $porta_code; ?>');"> <?php echo $lang['lang_botao_titulo_desligar']; ?> Streaming</a>
              <div class="dropdown-divider"></div>              
              <a class="dropdown-item text-success" href="javascript:carregar_opcoes_ligar_autodj('<?php echo $porta_code; ?>');"> <?php echo $lang['lang_botao_titulo_ligar']; ?> AutoDJ</a>
              <a class="dropdown-item text-danger" href="javascript:desligar_autodj('<?php echo $porta_code; ?>');"> <?php echo $lang['lang_botao_titulo_desligar']; ?> AutoDJ</a>
            </div>
          </li>
          <li class="nav-item dropdown">
            <a class="nav-link count-indicator dropdown-toggle d-flex align-items-center justify-content-center" id="notificationDropdown" href="#" data-toggle="dropdown" aria-expanded="false">
              <i class="mdi mdi-bell f-22 text-icones" data-toggle="tooltip" data-placement="bottom" title="<?php echo $lang['lang_info_pagina_informacoes_tab_avisos']; ?>"></i>
              <?php if($total_avisos_exibir > 0) { ?><span class="count bg-success"><?php echo $total_avisos_exibir; ?></span><?php } ?>           
            </a>
            <div class="dropdown-menu dropdown-menu-right navbar-dropdown preview-list" aria-labelledby="notificationDropdown">
              <a class="dropdown-item">
                <p class="mb-0 font-weight-normal float-left"><?php echo $lang['lang_info_pagina_informacoes_tab_avisos']; ?></p>
              </a>
              <div class="dropdown-divider"></div>
              <?php carregar_avisos_streaming($conexao,$dados_stm["porta"],$dados_servidor["codigo"]); ?>
            </div>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="/sair"><i class="mdi mdi-logout f-22 text-icones" data-toggle="tooltip" data-placement="bottom" title="Sair/logout"></i></a>
          </li>
          <li>
            <button type="button" class="btn btn-info" id="status-streaming" onClick="status_streaming('<?php echo $porta_code; ?>');" style="margin: 10px;min-width: 150px; min-height: 40px"><i class="fa fa-refresh"></i></button>
          </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>

    <!-- modal -->
      <div class="modal fade" id="avisos" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title"><?php echo $lang['lang_info_pagina_informacoes_tab_avisos']; ?></h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                </div>
                <div class="modal-body" id="avisos-conteudo"><img src="/img/ajax-loader.gif" align="center" /></div>
            </div>
        </div>
      </div>
                
      <div class="modal fade" id="playlists-autodj" tabindex="-1" role="dialog" aria-hidden="true">
          <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">AutoDJ</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                </div>
                <div class="modal-body text-center" id="playlists-autodj-conteudo" style="text-align: center;"><img src="/img/ajax-loader.gif" /></div>
            </div>
          </div>
      </div>
                
      <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 class="container-fluid page-body-wrapper">      
    <?php require_once("menu.php"); ?>
      <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>Disparar Conte&uacute;do AutoDJ</header>
                  </div>
                  <div class="card-body">
                      <form method="post" action="/autodj-disparar-conteudo" name="autodj">
                      <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
                                      <tr>
                                        <td width="130" height="50" align="left" style="padding-left:5px;"><strong>Tipo Conte&uacute;do</strong></td>
                                        <td align="left">
                                          <select name="tipo" class="form-control" id="tipo" style="width:100%;" onchange="tipo_conteudo(this.value)">
                                            <option value="texto">Texto</option>
                                            <option value="hora-certa">Hora Certa</option>
                                          </select>
                                          </td>
                                      </tr>
                                      <tr id="div_texto">
                                        <td width="130" height="50" align="left" style="padding-left:5px;"><strong>Texto</strong></td>
                                        <td align="left"><textarea id="texto" name="texto" class="form-control" style="height:100px" placeholder="Max: 230 caracteres" onkeyup="contar_caracteres(this.id,'230');"></textarea><small>Caracteres restantes: <span id="total_caracteres" class="texto_padrao_pequeno">230</span></small><br><br>
                                          </td>
                                      </tr>
                                      <tr id="div_texto_voz">
                                        <td height="30" align="left" style="padding-left:5px;"><strong><?php echo $lang['lang_info_gerenciador_playlists_hora_certa_voz']; ?></strong></td>
                                        <td align="left"><select class="form-control" name="texto_voz" id="texto_voz">
                                          <optgroup label="Portugu&ecirc;s">
                                      <option value="pt-BR-Standard-A|Ricardo_Male" selected="selected">Ricardo</option>
                                            <option value="pt-BR-Standard-A|Vitoria_Female">Vitoria</option>
                                      <option value="pt-BR-Standard-A|Camila_Female">Camila</option>
                                          </optgroup>
                                          <optgroup label="Espa&ntilde;ol - US">
                                      <option value="es-US|Penelope_Female">Penelope</option>
                                            <option value="es-US|Miguel_Male">Miguel</option>
                                          </optgroup>
                                          <optgroup label="Espa&ntilde;ol - Espa&ntilde;a">
                                      <option value="es-US|Lucia_Female">Lucia</option>
                                            <option value="es-US|Enrique_Male">Enrique</option>
                                          </optgroup>
                                          <optgroup label="English">
                                      <option value="en-US|Matthew_Male">Matthew (Male)</option>
                                            <option value="en-US|Salli_Female">Salli (Female)</option>
                                          </optgroup>
                                          </select></td>
                                      </tr>
                                      <tr id="div_hora_certa_voz" style="display:none">
                                        <td height="30" align="left" style="padding-left:5px;"><strong><?php echo $lang['lang_info_gerenciador_playlists_hora_certa_voz']; ?></strong></td>
                                        <td align="left"><select name="hora_certa_voz" class="form-control" id="hora_certa_voz">
                                          <option value="masculino"><?php echo $lang['lang_info_gerenciador_playlists_hora_certa_voz_masculina']; ?></option>
                                          <option value="feminino"><?php echo $lang['lang_info_gerenciador_playlists_hora_certa_voz_feminina']; ?></option>
                                        </select></td>
                                      </tr>
                                      <tr>
                                        <td height="50" colspan="2" align="right"><input name="executar" type="hidden" id="executar" value="sim" /><button type="button" class="btn btn-info" onClick="document.autodj.submit();"><?php echo $lang['lang_botao_titulo_executar']; ?></button></td>
                                      </tr>
                                    </table>
                                    </form>
                  </div>
              </div>
            </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>
  status_streaming('<?php echo $porta_code; ?>');
  setInterval("status_streaming('<?php echo $porta_code; ?>')",60000);
  function tipo_conteudo(tipo) {

    document.getElementById('div_texto').style.display = "none";
    document.getElementById('div_texto_voz').style.display = "none";
    document.getElementById('div_hora_certa_voz').style.display = "none";

    if(tipo == "texto") {
    document.getElementById('div_texto').style.display = "table-row";
    document.getElementById('div_texto_voz').style.display = "table-row";
    } else {
    document.getElementById('div_hora_certa_voz').style.display = "table-row";
    }


  }
  </script>
</body>
</html>