<?php 
#######################CHECK ERROR ###################################

ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
error_reporting(E_ALL); 
include 'chk.php';
######################################################################

if (!file_exists('apks')) {
	mkdir('apks', 511, true);
}

$dbboxbra = new SQLite3('./api/.boxbr_store.db');
$dbboxbra->exec('CREATE TABLE IF NOT EXISTS boxbrstore(id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL, title VARCHAR(100), imageurl VARCHAR(100), url VARCHAR(100), linktype VARCHAR(100), description VARCHAR(100),createdon VARCHAR(100))');

$rows = $dbboxbra->query('SELECT COUNT(*) as count FROM boxbrstore');
$row = $rows->fetchArray();
$numRows = $row['count'];
$HOSTa = $lurl = (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] !== 'off') ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/img/boxbr_demo.png';
$date = date('Y-m-d H:i:s');
/*
if ($numRows == 0) {
	$dbboxbra->exec('INSERT INTO boxbrstore(title,imageurl, url, linktype, description, createdon) VALUES(\'Sample\',\'' . $HOSTa. '\',\'http://apkboxbr.k6.com.br\',\'0\',\'BoxBR Apks Rebrand\',\'' . $date . '\')');
}
*/	

if(isset($_GET['delete'])){

$resd = $dbboxbra->query('SELECT * FROM  boxbrstore WHERE id=' . $_GET['delete']);
$rowd = $resd->fetchArray();

$url= $rowd['imageurl'];
$file_to_delete = basename(parse_url($url, PHP_URL_PATH));
	$file_to_delete = 'apksimage/' . $file_to_delete;
	
//echo $file_to_delete;
	if (file_exists($file_to_delete)) {
		
	unlink($file_to_delete);
	}
	$dbboxbra->exec('DELETE FROM boxbrstore WHERE id=' . $_GET['delete']);
		header("Location: store.php");
	}

include 'includes/header.php';

echo ' <!-- Begin Page Content -->' . "\n";
echo '        <div class="container-fluid">' . "\n";

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 '          <!-- Content Row -->' . "\n";
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-primary">' . "\n";
//echo '                 <div title="Click Me - Show/Hide Settings" onclick="myFunction(\'store\')" style="display: grid; grid-template-columns:1% auto;" class="card-header bg-primary py-3">
echo '                 <div class="card-header bg-primary py-3">
                   
  
  <div class="item2"><h4 align="center" class="m-0 font-weight-bold text-white"><i class="fas fa-server" style="font-size:20px">&nbsp;&nbsp;</i>
  Gerenciador de Loja</h4>
                			</div>
                </div>

                         <div id="store" style = "display:nonxe"  class="card-body">' . "\n";


echo '
  <div class="input-group "><div class="input-group-prepend ">
<a class="btn btn-success btn-icon-split" href="./store_create.php">
    <span class="icon text-white-50"><i class="fas fa-plus" ></i></span><span class="text"> Add App</span></a>' . "\n";
echo '                        </button></a>' . "\n";
echo '  </div>
    <div class="input-group-prepend ">
    <span class="input-group-text " style="font-size:24px;color:#1cc88a"><i class="fas fa-search"></i></span></div>
    <input class="form-control" type="text" id="search" placeholder="Buscar Titulo / Data / Tipo ..."  name="search_value"/>
    <div class="input-group-prepend ">
	<!--		Show Numbers Of Rows 		-->

			 		<select class  ="input-group-text" name="state" id="maxRows"  style="width:150px;">
						 <option value="5000">Mostrar Todos</option>
						 <option value="5">Mostrar 5 </option>
						 <option value="10">Mostrar 10</option>
						 <option value="15">Mostrar 15<s/option>
						 <option value="20">Mostrar 20</option>
						 <option value="50">Mostrar 50</option>
						 <option value="70">Mostrar 70</option>
						 <option value="100">Mostrar 100</option>
						</select>
                    </div></div>' . "\n";
 echo '                                       
<hr>

<div class="table-responsive">
<table id="myTable" cellpadding="0" cellspacing="0" border="0" class="table stable-striped table-sm datatable stable-bordered">
							<thead align="center" class="text-primary">
								<tr>
								<th><strong>APK Imagem</strong></th>
								<th><strong>APK Titulo</strong></th>
								<th><strong>APK Link</strong></th>
								<th><strong>Data Add</strong></th>
								<th><strong>Editar</strong></th>
								<th><strong>Deletar</strong></th>
								</tr>
							</thead>' . "\n";

$dbboxbra = new SQLite3('./api/.boxbr_store.db');	
$res = $dbboxbra->query('SELECT * FROM boxbrstore');
 while ($row = $res->fetchArray()) {

	 if($row['linktype'] == '0'){
	 $rtype = 'Local File';
	 }else{
 $rtype = 'Link URL'; }
	  echo '  
							<tbody align="center">
								<tr class="text-primary">
								
								<td><img src="'.$row['imageurl'].'" alt="" border=3 height=80 width=150></img></td>
								<td>'.$row['title'].'</td>
								<td title="'.$row['url'].'">Mouse over to show</td>
								<td>'.$row['createdon'].'</td>
								
								<td><a class="btn btn-success btn-icon" href="./store_update.php?update='.$row['id'].'"><i class="fas fa-edit"></i></a></td>

								<td><a class="btn btn-danger btn-icon" href="./store.php?delete='.$row['id'].'"><i class="fas fa-trash"></i></a></td>


								</tr>
							</tbody>
							' . "\n";  }
$dbboxbra->close();
	 echo ' 	</table>' . "\n";
echo "\t\t" . '</div>
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

	<div class="pagination-container">
  <nav>
    <ul class="pagination pagination-lgn">
      <li class="page-item" data-page="prev">
      <a class="page-link" href="#"> < &nbsp;&nbsp;&nbsp; <span class="sr-only ">(current)</span></a>
      </li>
      <li class="page-item" data-page="next" id="prev">
      <a class="page-link" href="#"> &nbsp;&nbsp;&nbsp; > <span class="sr-only">(current)</span></a>
      </li>
    </ul>
  </nav>
  
</div>
' . "\n";
echo '</main>' . "\n";
echo "\n";
echo '           ' . "\n";
echo '            ' . "\n";
echo '    <br>' . "\n";
	?>

				<script type="text/javascript">
				//	getPagination('#table-id');
					//getPagination('.table-class');
					getPagination('table');

				</script>

<?php
echo '

<script>
      function myFunction(DIV) {
  var x = document.getElementById(DIV);
  var y = document.getElementById(DIV+"show");
  if (x.style.display === "none") {
    x.style.display = "block";
     y.className = "fa fa-minus";
  } else if (x.style.display === "block") {
 
    x.style.display = "none";
     y.className = "fa fa-plus";
  }else {
    x.style.display = "none";
     y.className = "fa fa-plus";
  }
}
  </script>

'."\n";
echo '
<script>
$("#search").keyup(function () {
    var value = this.value.toLowerCase().trim();

    $("table tr").each(function (index) {
        if (!index) return;
        $(this).find("td").each(function () {
            var id = $(this).text().toLowerCase().trim();
            var not_found = (id.indexOf(value) == -1);
            $(this).closest(\'tr\').toggle(!not_found);
            return not_found;
        });
    });
});
</script>' . "\n";
echo '    <script>' . "\n";
echo '$(\'#confirm-delete\').on(\'show.bs.modal\', function(e) {' . "\n";
echo '    $(this).find(\'.btn-ok\').attr(\'href\', $(e.relatedTarget).data(\'href\'));' . "\n";
echo '});' . "\n";
echo '    </script>' . "\n";

############# end ##############
include 'includes/footer.php';
require ('includes/boxbr.php');
print "</body>\n";
?>