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

$id=$_GET['id'];

$r=$conn->query("SELECT * FROM radio_ads WHERE id='$id'");
$row=$r->fetch_assoc();

if(isset($_POST['update'])){

$menu_id=$_POST['menu_id'];
$name=$_POST['ad_name'];
$interval=$_POST['ad_interval'];
$status=$_POST['status'];

$audio=$row['ad_audio'];

if(!empty($_FILES['ad_audio']['name'])){

$file=$_FILES['ad_audio']['name'];
$tmp=$_FILES['ad_audio']['tmp_name'];

$newname=time()."_".$file;

$path="../ads/".$newname;

move_uploaded_file($tmp,$path);

$audio="ads/".$newname;

}

$conn->query("
UPDATE radio_ads SET
menu_id='$menu_id',
ad_name='$name',
ad_audio='$audio',
ad_interval='$interval',
status='$status'
WHERE id='$id'
");

header("Location: manage_ads.php");
exit;

}
?>

<!DOCTYPE html>
<html>
<head>

<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Edit Advertisement</title>

<style>

/* GLOBAL */

body{
margin:0;
font-family:Arial;
background:#f2f2f2;
display:flex;
}

/* SIDEBAR */

.sidebar{
width:220px;
background:#009688;
color:white;
min-height:100vh;
padding:20px 10px;
box-sizing:border-box;
}

.sidebar h2{
text-align:center;
margin-bottom:20px;
}

.sidebar a{
display:block;
color:white;
text-decoration:none;
padding:10px;
border-radius:4px;
margin-bottom:6px;
}

.sidebar a:hover{
background:rgba(255,255,255,0.2);
}

/* MAIN */

.main{
flex:1;
padding:30px;
}

/* CARD */

.card{
background:white;
border-radius:10px;
padding:25px;
box-shadow:0 2px 10px rgba(0,0,0,0.08);
max-width:700px;
}

/* HEADER */

.page-header{
margin-bottom:20px;
}

.page-header h2{
margin:0;
}

/* FORM */

.form-group{
margin-bottom:18px;
}

label{
display:block;
margin-bottom:6px;
font-weight:bold;
font-size:14px;
}

input, select{
width:100%;
padding:10px;
border:1px solid #ddd;
border-radius:6px;
box-sizing:border-box;
}

/* BUTTON */

.btn{
background:#009688;
color:white;
padding:10px 18px;
border:none;
border-radius:6px;
cursor:pointer;
font-size:14px;
}

.btn:hover{
background:#00796b;
}

/* AUDIO PREVIEW */

audio{
width:100%;
margin-top:8px;
}

/* MOBILE */

@media(max-width:768px){

.sidebar{
display:none;
}

.main{
padding:20px;
}

}

</style>

</head>

<body>

<?php include "sidebar.php"; ?>

<div class="main">

<div class="card">

<div class="page-header">
<h2>Edit Advertisement</h2>
</div>

<form method="post" enctype="multipart/form-data">

<div class="form-group">

<label>Select Radio Station</label>

<select name="menu_id">

<?php
$r=$conn->query("SELECT * FROM menus");

while($m=$r->fetch_assoc()){

$sel=$m['id']==$row['menu_id']?"selected":"";

echo "<option value='{$m['id']}' $sel>{$m['menu_name']}</option>";

}
?>

</select>

</div>


<div class="form-group">

<label>Ad Name</label>
<input name="ad_name" value="<?php echo $row['ad_name']; ?>">

</div>


<div class="form-group">

<label>Ad Interval (seconds)</label>
<input name="ad_interval" value="<?php echo $row['ad_interval']; ?>">

</div>


<div class="form-group">

<label>Status</label>

<select name="status">

<option value="1" <?php if($row['status']==1) echo "selected"; ?>>Active</option>

<option value="0" <?php if($row['status']==0) echo "selected"; ?>>Disabled</option>

</select>

</div>


<div class="form-group">

<label>Current Audio</label>

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

</div>


<div class="form-group">

<label>Replace Audio</label>
<input type="file" name="ad_audio">

</div>


<button class="btn" name="update">Update Advertisement</button>

</form>

</div>

</div>

</body>
</html>