<?php
#######################CHECK ERROR ###################################

ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
error_reporting(E_ALL); 
######################################################################



$db = new SQLite3('./api/.boxbr_config.db');
$db->exec('CREATE TABLE IF NOT EXISTS config(id INTEGER PRIMARY KEY NOT NULL, Title TEXT, url TEXT, CreateDate TIMESTAMP, linktype TEXT)');
$rowschk = $db->query('SELECT COUNT(*) as count FROM config');
$rowcount = $rowschk->fetchArray();
$numRowsicons = $rowcount['count'];
if ($numRowsicons == 0) {
	$db->exec('INSERT INTO config(Title,url,CreateDate,linktype) VALUES(\'LOGO\',\'default\',\'' . time() . '\',\'0\')');
	$db->exec('INSERT INTO config(Title,url,CreateDate,linktype) VALUES(\'BACK\',\'default\',\'' . time() . '\',\'0\')');
	$db->exec('INSERT INTO config(Title,url,CreateDate,linktype) VALUES(\'VIDEO\',\'default\',\'' . time() . '\',\'0\')');
}


if (!file_exists('config')) {
	mkdir('config', 511, true);
}
if (isset($_GET['delete'])) {
	$db->exec('UPDATE config SET url=\'default\', CreateDate=\'' . time() . '\',' . "\n\t\t\t\t\t\t" . 'linktype=\'0\'' . "\n\t\t\t\t\t\t" . '  WHERE ' . "\n\t\t\t\t\t\t\t" . '  id=\'' . $_GET['delete'] . '\'');
$db->close();

		$msg = '?messageE=Clean item file..';
	header("Location: {$base_file}" . $msg);
	exit();
}

if (isset($_GET['remove_Al1'])) {
	//$db->exec('UPDATE icons SET image=\'default\',CreateDate=\'' . time() . '\',type=\'0\'');
	
		$db->exec("DELETE FROM config");
$db->close();
		$msg = '?messageE=All clean itens file..';
	header("Location: {$base_file}" . $msg);
	exit();
}

include 'includes/header.php';
echo '<div class="modal fade" id="confirm-delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">' . "\n";
echo '    <div class="modal-dialog">' . "\n";
echo '        <div class="modal-content">' . "\n";
echo '            <div class="modal-header">' . "\n";
echo '                <h2>Confirm</h2>' . "\n";
echo '            </div>' . "\n";
echo '            <div class="modal-body">' . "\n";
echo '                Você realmente deseja excluir?' . "\n";
echo '            </div>' . "\n";
echo '            <div class="modal-footer">' . "\n";
echo '                <button type="button" class="btn btn-default" data-dismiss="modal">Não</button>' . "\n";
echo '                <a class="btn btn-danger btn-ok">Sim</a>' . "\n";
echo '            </div>' . "\n";
echo '        </div>' . "\n";
echo '    </div>' . "\n";
echo '</div>' . "\n";
echo '        <div>';
echo '    <div class="container-fluid">' . "\n";

if (isset($_GET['succes'])) {
	echo $msg_delete;
}

if (isset($_GET['success'])) {
	echo $msg_success;
}

if (isset($_GET['message'])) {
	echo '<div class="alert alert-danger bg-primary py-3" id="flash-msg"><h4 align="center" class="m-0 font-weight-bold text-white"><i class="icon fa fa-check"></i> '.$_GET['message'].'</h4></div>';
}
if (isset($_GET['messageA'])) {
	echo '<div class="alert alert-danger bg-primary py-3" id="flash-msg"><h4 align="center" class="m-0 font-weight-bold text-white"><i class="icon fa fa-check"></i> '.$_GET['messageA'].'</h4></div>';
}
if (isset($_GET['messageE'])) {
	echo '<div class="alert alert-danger bg-danger py-3" id="flash-msg"><h4 align="center" class="m-0 font-weight-bold text-white"><i class="icon fa fa-times"></i> '.$_GET['messageE'].'</h4></div>';
}
if (isset($_GET['messageS'])) {
	echo '<div class="alert alert-danger bg-success py-3" id="flash-msg"><h4 align="center" class="m-0 font-weight-bold text-white"><i class="icon fa fa-check"></i> '.$_GET['messageS'].'</h4></div>';
}
echo '          <div class="row">' . "\n";
echo "\n";
echo '            <!-- First Column -->' . "\n";
echo '            <div class="col-lg-12">' . "\n";
echo "\n";
echo '              <!-- Custom codes -->' . "\n";
echo '                <div class="card text-text-primary">' . "\n";
echo '                <div class="card-header bg-primary py-3">' . "\n";
echo '                <center><h4 align="center" class="m-0 text-white"><i class="fas fa fa-cog" style="font-size:20px" aria-hidden="true">&nbsp;&nbsp;</i> Configuração da Launcher</h4>
						</center>' . "\n";
echo '            </div>' . "\n";

echo '                <div class="card-body">' . "\n";
echo '                            <a button class="btn btn-danger btn-icon-split" href="./launcher.php?remove_Al1">' . "\n";
echo '                            <span class="icon text-white-50"><i class="fas fa-minus" ></i></span><span class="text"> LIMPAR TODOS</span>' . "\n";
echo '                            </button></a>' . "\n";
echo '    ' . "\t\t\t\t\t" . '<hr>';
echo "\t\t\t\t\t\t" . '<div class="table-responsive">';
echo "\t\t\t\t\t\t\t" . '<table id="Dtable" class="table" style="width:100%">';
echo "\t\t\t\t\t\t\t" . '<thead>';
echo "\t\t\t" . '<tr >';
echo "\t\t\t" . '<th style = "display:none" >Index</th>';
echo '                  <th>Titulo</th>' . "\n";
echo '                  <th>Prever</th>' . "\n";
echo '                  <th>Link Url</th>' . "\n";
echo '                  <th>Criado em</th>' . "\n";
echo "\t\t\t\t" . '<th>Editar</th>' . "\n";
echo "\t\t\t\t" . '<th>Deletar</th>' . "\n";
echo "\t\t\t\t" . '</tr>';
echo "\t\t\t\t" . '</thead>';
echo "\t\t\t\t" . '<tbody >';

$db = new SQLite3('./api/.boxbr_config.db');
$res1 = $db->query('SELECT * FROM config');
while ($row = $res1->fetchArray()) {
	$id = $row['id'];
	$Title = $row['Title'];
	$url = $row['url'];

	if ($Title == 'LOGO' && $url !== 'default') {
		$image = $url;
	}elseif ($Title == 'BACK' && $url !== 'default') {
		$image = $url;
	}elseif ($Title == 'VIDEO' && $url !== 'default') {
		$image = './img/VOD.png';
	}elseif (empty($image)) {
	    $url = 'Sem item gravado';
		$image = './img/noimg.jpg';
	}
	
	$CreateDate = date('D d-m-y H:i:s', $row['CreateDate']);
	echo "\t\t\t\t\t" . ' <tr >';
	echo "\t\t\t\t\t" . ' <td style = "display:none">' . $id . '</td>';
	echo "\t\t\t\t" . '<td>' . $Title . '</td>' . "\n";
	if ($Title == 'VIDEO' && $url !== 'default') {
	echo '<td><video controls="" autoplay="" name="media" width="150"> <source src="'.$url .'" type="video/mp4"><img src="' . $image . '" title="does not support the video" alt="does not support the video" height=80 width=150></img></video></td>' . "\n";
}else{
	echo "\t\t\t\t" . '<td><img src="' . $image . '" alt="' . $image . '" title="' . $image . '" height=80 width=150></img></td>' . "\n";}
	
	echo "\t\t\t\t" . '<td title="'.$url.'">Passe o mouse para ver</td>' . "\n";
								
	echo "\t\t\t\t" . '<td>' . $CreateDate . '</td>' . "\n";
	echo "\t\t\t\t" . '  <td><a class="btn btn-success btn-icon" href="./launcher_update.php?update=' . $id . '"><i class="fa fa-pencil"></i></a></td>' . "\n";
	echo "\t\t\t" . ' <td><a class="btn btn-danger btn-icon" href="#" data-href="./launcher.php?delete=' . $id . '" data-toggle="modal" data-target="#confirm-delete"><i class="fa fa-trash"></i></a>';
	echo "\t\t\t\t\t" . '</td>';
	echo "\t\t\t\t\t" . '</tr>';
}
echo "\t\t\t\t\t\t\t" . '</tbody>';
echo "\t\t\t\t\t\t\t" . '</table>';

echo "\t\t\t\t\t\t" . '</div>';
echo "\t\t\t\t\t" . '</div>';
echo "\t\t\t\t" . '</div>';
echo "\t\t\t\t\t" . '</div>';
$db->close();
include ('includes/footer.php');
require ('includes/boxbr.php');
print "</body>\n";
?>