<?php
include "../db.php";

$limit = 10;
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$search = $_GET['search'] ?? '';

$start = ($page-1) * $limit;

/* SEARCH CONDITION */

$where = "";

if($search!=""){
$search = $conn->real_escape_string($search);
$where = "WHERE ads.ad_name LIKE '%$search%' 
OR menus.menu_name LIKE '%$search%'";
}

/* FETCH ADS */

$query = $conn->query("
SELECT ads.*, menus.menu_name
FROM radio_ads ads
LEFT JOIN menus ON menus.id=ads.menu_id
$where
ORDER BY ads.ad_time ASC
LIMIT $start,$limit
");

while($row=$query->fetch_assoc()){
?>

<tr>

<td><?php echo $row['id']; ?></td>

<td><?php echo $row['menu_name']; ?></td>

<td><?php echo $row['ad_name']; ?></td>

<td>
<audio controls>
<source src="../<?php echo $row['ad_audio']; ?>">
</audio>
</td>

<td><?php echo $row['ad_time']; ?></td>

<td>

<?php if($row['status']==1){ ?>
<span class="status-active">Active</span>
<?php }else{ ?>
<span class="status-disabled">Disabled</span>
<?php } ?>

</td>

<td>

<a href="edit_ad.php?id=<?php echo $row['id']; ?>" class="btn-edit">Edit</a>

<a href="delete_ad.php?id=<?php echo $row['id']; ?>"
class="btn-danger"
onclick="return confirm('Delete this advertisement?')">
Delete
</a>

<button class="btn-test"
onclick="playAd('../<?php echo $row['ad_audio']; ?>')">
Test
</button>

</td>

</tr>

<?php } ?>