<?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) {

foreach($_POST["lista_playlists"] as $playlist) {
  
$verifica_playlist = mysqli_num_rows(mysqli_query($conexao,"SELECT * FROM playlists where codigo = '".code_decode($playlist,"D")."'"));
  
if($verifica_playlist == 1) {

$dados_playlist = mysqli_fetch_array(mysqli_query($conexao,"SELECT * FROM playlists where codigo = '".code_decode($playlist,"D")."'"));
  
mysqli_query($conexao,"Delete From playlists where codigo = '".$dados_playlist["codigo"]."'");
mysqli_query($conexao,"Delete From playlists_musicas where codigo_playlist = '".$dados_playlist["codigo"]."'");
mysqli_query($conexao,"Delete From playlists_agendamentos where codigo_playlist = '".$dados_playlist["codigo"]."'");

$ssh = new SSH();
$ssh->conectar($dados_servidor["ip"],$dados_servidor["porta_ssh"]);
$ssh->autenticar("root",code_decode($dados_servidor["senha"],"D"));
  
$ssh->executar("rm -fv /home/streaming/playlists/".$dados_playlist["arquivo"].";echo OK");
  
}

}

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

header("Location: /playlists");
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/ajax-streaming-playlists.js"></script>
  <script type="text/javascript" src="/inc/javascript.js"></script>
  <style type="text/css">.no-sort::after { display: none!important; }.no-sort {cursor: default!important; }</style>
</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="nova-playlist" 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_gerenciador_playlists_lista_botao_criar_playlist']; ?></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="nova-playlist-conteudo">
                                <input type="text" id="nome-nova-playlist" style="width:95%; height:30px" placeholder="<?php echo $lang['lang_info_gerenciador_playlists_lista_tabela_nome']; ?>..." />
                             </div>

                            <div class="modal-footer">
                                <button type="button" class="btn btn-primary" onclick="criar_playlist('<?php echo $porta_code; ?>');"><?php echo $lang['lang_botao_titulo_cadastrar']; ?></button>
                                <button type="button" class="btn btn-secondary" data-dismiss="modal"><?php echo $lang['lang_botao_titulo_cancelar']; ?></button>
                            </div>
                         </div>
                     </div>
                </div>
                
                <div class="modal fade" id="duplicar-playlist" 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_gerenciador_playlists_lista_duplicar']; ?> Playlist</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="duplicar-playlist-conteudo">
                                <input type="text" id="nome-duplicar-playlist" style="width:95%; height:30px" placeholder="<?php echo $lang['lang_info_gerenciador_playlists_lista_tabela_nome']; ?>..." /><input type="hidden" id="codigo_playlist_duplicar" value="" />
                             </div>

                            <div class="modal-footer">
                                <button type="button" class="btn btn-primary" onclick="duplicar_playlist(document.getElementById('codigo_playlist_duplicar').value);"><?php echo $lang['lang_info_gerenciador_playlists_lista_duplicar']; ?></button>
                                <button type="button" class="btn btn-secondary" data-dismiss="modal"><?php echo $lang['lang_botao_titulo_cancelar']; ?></button>
                            </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><?php echo $lang['lang_info_gerenciador_playlists_lista_tab_titulo']; ?></header>
                                        <div class="card-header-right">
                                            <button type="button" class="btn btn-info" onClick="$('#nova-playlist').modal('show');$('#nome-nova-playlist').focus();"><i class="fa fa-plus-circle"></i> <?php echo $lang['lang_info_gerenciador_playlists_lista_botao_criar_playlist']; ?></button> <button type="button" class="btn btn-sm btn-danger" onClick="validar_remover();"><i class="fa fa-trash"></i> Remover Marcados</button>
                                        </div>
                  </div>
                <div class="card-body">
                <form name="remover" action="/playlists" method="post">
                    <table id="data_table" class="table dataTable no-footer display responsive nowrap" role="grid" aria-describedby="data_table_info">
                                                <thead>
                                                    <tr role="row">
                                                        <th class="no-sort" tabindex="0" aria-controls="data_table" rowspan="1" colspan="1" aria-sort="ascending" align="center"></th>
                                                        <th class="sorting_asc" tabindex="0" aria-controls="data_table" rowspan="1" colspan="1" aria-sort="ascending"><?php echo $lang['lang_info_gerenciador_playlists_lista_tabela_nome']; ?></th>
                                                        <th class="sorting_asc" tabindex="0" aria-controls="data_table" rowspan="1" colspan="1" aria-sort="ascending"><?php echo $lang['lang_info_gerenciador_playlists_lista_tabela_musicas']; ?></th>
                                                        <th class="sorting_asc" tabindex="0" aria-controls="data_table" rowspan="1" colspan="1" aria-sort="ascending"><?php echo $lang['lang_info_gerenciador_playlists_lista_tabela_duracao']; ?></th>
                                                        <th class="sorting_asc" tabindex="0" aria-controls="data_table" rowspan="1" colspan="1" aria-sort="ascending"><?php echo $lang['lang_info_gerenciador_playlists_lista_tabela_hora_certa']; ?></th>
                                                        <th class="sorting_asc" tabindex="0" aria-controls="data_table" rowspan="1" colspan="1" aria-sort="ascending"><?php echo $lang['lang_info_gerenciador_playlists_lista_tabela_vinhetas_comerciais']; ?></th>
                                                        <th class="nosort sorting_disabled" tabindex="0" aria-controls="data_table" rowspan="1" colspan="1" aria-sort="ascending"><?php echo $lang['lang_info_gerenciador_playlists_lista_tabela_acao']; ?></th>
                                                    </tr>
                                                </thead>
                                                <tbody>
<?php
$sql = mysqli_query($conexao,"SELECT * FROM playlists where codigo_stm = '".$dados_stm["codigo"]."' ORDER by codigo ASC");
while ($dados_playlist = mysqli_fetch_array($sql)) {

$total_musicas = mysqli_num_rows(mysqli_query($conexao,"SELECT * FROM playlists_musicas where codigo_playlist = '".$dados_playlist["codigo"]."'"));
$duracao = mysqli_fetch_array(mysqli_query($conexao,"SELECT SUM(duracao_segundos) as total FROM playlists_musicas where codigo_playlist = '".$dados_playlist["codigo"]."'"));

$playlist_code = code_decode($dados_playlist["codigo"],"E");

$hora_certa = ($dados_playlist["hora_certa"] == "sim") ? $lang['lang_label_sim']."&nbsp;<a href='javascript:remover_hora_certa_playlist(\"".$playlist_code."\");' data-toggle='tooltip' data-placement='bottom' title='".$lang['lang_info_gerenciador_playlists_lista_acao_vinhetas_comerciais']." ".$lang['lang_info_gerenciador_playlists_lista_vinhetas_comerciais_configurar']."'><i class='fa ik-minus-circle f-20 text-yellow'></i></a>" : $lang['lang_label_nao'];

$vinhetas_comerciais = ($dados_playlist["vinhetas_comerciais"] == "sim") ? $lang['lang_label_sim']."&nbsp;<a href='javascript:remover_comerciais_playlist(\"".$playlist_code."\");' data-toggle='tooltip' data-placement='bottom' title='".$lang['lang_info_gerenciador_playlists_lista_acao_vinhetas_comerciais']." ".$lang['lang_info_gerenciador_playlists_lista_vinhetas_comerciais_configurar']."'><i class='fa ik-minus-circle f-20 text-yellow'></i></a>" : $lang['lang_label_nao'];

echo "<tr id='".$playlist_code."'>
<td height='25' align='center' scope='col'><input name='lista_playlists[]' class='marcados' type='checkbox' value='".$playlist_code."' /></td>
<td height='25' align='left' scope='col'>&nbsp;".$dados_playlist["nome"]."</td>
<td height='25' align='left' scope='col'>&nbsp;".$total_musicas."</td>
<td height='25' align='left' scope='col'>&nbsp;".gmdate("H:i:s", $duracao["total"])."</td>
<td height='25' align='left' scope='col'>&nbsp;".$hora_certa."</td>
<td height='25' align='left' scope='col'>&nbsp;".$vinhetas_comerciais."</td>
<td height='25' align='left' scope='col'>
<a href='javascript:iniciar_playlist(\"".$playlist_code."\");' data-toggle='tooltip' data-placement='bottom' title='".$lang['lang_info_gerenciador_playlists_lista_iniciar']."'><i class='fa fa-play-circle f-20 text-green'></i></a>&nbsp;
<a href='/gerenciar-playlists/".$playlist_code."' data-toggle='tooltip' data-placement='bottom' title='".$lang['lang_info_gerenciador_playlists_lista_gerenciar']."'><i class='fa fa-edit f-20 text-blue'></i></a>&nbsp;
<a href='/gerenciar-playlists-basico/".$playlist_code."' data-toggle='tooltip' data-placement='bottom' title='".$lang['lang_info_gerenciador_playlists_lista_gerenciar_basico']."'><i class='fa fa-flash f-20 text-grey'></i></a>&nbsp;
<a href='javascript:void(0);' onClick='$(\"#duplicar-playlist\").modal(\"show\");document.getElementById(\"codigo_playlist_duplicar\").value=\"".$playlist_code."\"' data-toggle='tooltip' data-placement='bottom' title='".$lang['lang_info_gerenciador_playlists_lista_duplicar']."'><i class='fa fa-copy f-20 text-blue'></i></a>&nbsp;
<a href='/gerenciar-playlists-intercalar-musicas/".$playlist_code."' data-toggle='tooltip' data-placement='bottom' title='".$lang['lang_info_gerenciador_playlists_lista_intercalar_musicas']."'><i class='fa fa-retweet f-20 text-blue'></i></a>&nbsp;
<a href='/gerenciar-playlists-hora-certa/".$playlist_code."' data-toggle='tooltip' data-placement='bottom' title='".$lang['lang_info_gerenciador_playlists_lista_acao_hora_certa']." ".$lang['lang_info_gerenciador_playlists_lista_hora_certa_configurar']."'><i class='fa fa-clock-o f-20 text-purple'></i></a>&nbsp;
<a href='/gerenciar-playlists-programas/".$playlist_code."' data-toggle='tooltip' data-placement='bottom' title='Programas'><i class='fa fa-star f-20 text-yellow'></i></a>&nbsp;
<a href='/gerenciar-playlists-programetes/".$playlist_code."' data-toggle='tooltip' data-placement='bottom' title='Programetes'><i class='fa fa-gift f-20 text-pink'></i></a>&nbsp;
<a href='/gerenciar-playlists-vinhetas-comerciais/".$playlist_code."' data-toggle='tooltip' data-placement='bottom' title='".$lang['lang_info_gerenciador_playlists_lista_acao_vinhetas_comerciais']." ".$lang['lang_info_gerenciador_playlists_lista_vinhetas_comerciais_configurar']."'><i class='fa fa-dollar f-20 text-yellow'></i></a>&nbsp;
<a href='javascript:remover_playlist(\"".$playlist_code."\");' data-toggle='tooltip' data-placement='bottom' title='".$lang['lang_info_gerenciador_playlists_lista_remover']."'><i class='fa fa-trash-o f-20 text-red'></i></a>&nbsp;";
echo "</td>
</tr>";
unset($hora_certa);
unset($vinhetas_comerciais);
}
?>
                                                </tbody>
                                            </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);
        $(document).ready(function() {

            var table = $('#data_table').DataTable({
                responsive: true,
                "lengthMenu": [[25, 50, 100, 200, -1], [25, 50, 100, 200, "Todos/All"]],
                language: {
                    "url": "//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/<?php echo $lang_sortable; ?>.json"
                },
                select: true,
                'aoColumnDefs': [{
                    'bSortable': false,
                    'aTargets': ['nosort']
                }]
            });
            $('#data_table tbody').on( 'click', 'tr', function() {
                if ( $(this).hasClass('selected') ) {
                    $(this).removeClass('selected');
                }
                else {
                    table.$('tr.selected').removeClass('selected');
                    $(this).addClass('selected');
                }
            });

        });
        function marcar_tudo(source) {
var okay=false;
var checkboxes = document.querySelectorAll('input[type="checkbox"]');
    for (var i = 0; i < checkboxes.length; i++) {
        if (checkboxes[i] != source)
            checkboxes[i].checked = source.checked;
    }
}
function validar_remover(){

    var checkboxes = document.querySelectorAll('input[type="checkbox"]');
    var total_checked = 0;

    for(var i = 0; i < checkboxes.length; i++) {
        if(checkboxes[i].checked) {
            total_checked++;
        }
    }

    if(total_checked > 0){
  document.remover.submit();
    } else {
  alert("Deve selecionar pelo menos uma playlist para remover.");
  }
}
  </script>
</body>
</html>