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

$id=$_GET['id'];

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

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

$menu_id=$_POST['menu_id'];
$name=$_POST['ad_name'];
$time=$_POST['ad_time'];
$repeat=$_POST['repeat_hours'];
$expiry=$_POST['expiry_date'];
$status=$_POST['status'];

$weekdays="all";
if(isset($_POST['weekdays'])){
$weekdays=implode(",",$_POST['weekdays']);
}

$audioPath=$row['ad_audio'];

$file=preg_replace('/[^A-Za-z0-9.\-_]/','_',$_FILES['ad_audio']['name']);
$tmp=$_FILES['ad_audio']['tmp_name'];

if(!empty($file)){

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

$uploadPath="../ads/".$newName;

if(move_uploaded_file($tmp,$uploadPath)){
$audioPath="ads/".$newName;
}

}

$conn->query("
UPDATE radio_ads SET
menu_id='$menu_id',
ad_name='$name',
ad_audio='$audioPath',
ad_time='$time',
repeat_hours='$repeat',
weekdays='$weekdays',
expiry_date='$expiry',
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:600px;
}

/* 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;
}

/* WEEKDAY CHECKBOX FIX */

.weekday-list{
display:grid;
grid-template-columns:1fr 1fr;
gap:8px;
margin-top:8px;
}

.weekday-item{
display:flex;
align-items:center;
font-weight:normal;
gap:6px;
}

.weekday-item input{
width:auto;
margin:0;
}

/* BUTTON */

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

/* AUDIO PREVIEW */

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

/* MOBILE */

@media(max-width:768px){

.sidebar{
display:none;
}

.main{
padding:20px;
}

.weekday-list{
grid-template-columns:1fr;
}

}

</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 ORDER BY menu_name ASC");

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

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

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

}
?>

</select>

</div>


<div class="form-group">
<label>Ad Name</label>
<input type="text" name="ad_name" value="<?php echo $row['ad_name']; ?>" required>
</div>


<div class="form-group">
<label>Current Ad Audio</label>

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

</div>


<div class="form-group">
<label>Replace Audio (optional)</label>
<input type="file" name="ad_audio">
</div>


<div class="form-group">
<label>Ad Play Time (Clock Time)</label>
<input type="time" step="1" name="ad_time" value="<?php echo $row['ad_time']; ?>" required>
</div>


<div class="form-group">
<label>Repeat Interval</label>

<select name="repeat_hours">

<option value="0" <?php if($row['repeat_hours']==0) echo "selected"; ?>>Once per day</option>
<option value="1" <?php if($row['repeat_hours']==1) echo "selected"; ?>>Every 1 hour</option>
<option value="2" <?php if($row['repeat_hours']==2) echo "selected"; ?>>Every 2 hours</option>
<option value="3" <?php if($row['repeat_hours']==3) echo "selected"; ?>>Every 3 hours</option>
<option value="4" <?php if($row['repeat_hours']==4) echo "selected"; ?>>Every 4 hours</option>
<option value="6" <?php if($row['repeat_hours']==6) echo "selected"; ?>>Every 6 hours</option>
<option value="12" <?php if($row['repeat_hours']==12) echo "selected"; ?>>Every 12 hours</option>
<option value="24" <?php if($row['repeat_hours']==24) echo "selected"; ?>>Every 24 hours</option>

</select>

</div>


<?php $days=explode(",",$row['weekdays']); ?>

<div class="form-group">
<label>Play On Weekdays</label>

<div class="weekday-list">

<label class="weekday-item"><input type="checkbox" name="weekdays[]" value="mon" <?php if(in_array("mon",$days)) echo "checked"; ?>> Monday</label>

<label class="weekday-item"><input type="checkbox" name="weekdays[]" value="tue" <?php if(in_array("tue",$days)) echo "checked"; ?>> Tuesday</label>

<label class="weekday-item"><input type="checkbox" name="weekdays[]" value="wed" <?php if(in_array("wed",$days)) echo "checked"; ?>> Wednesday</label>

<label class="weekday-item"><input type="checkbox" name="weekdays[]" value="thu" <?php if(in_array("thu",$days)) echo "checked"; ?>> Thursday</label>

<label class="weekday-item"><input type="checkbox" name="weekdays[]" value="fri" <?php if(in_array("fri",$days)) echo "checked"; ?>> Friday</label>

<label class="weekday-item"><input type="checkbox" name="weekdays[]" value="sat" <?php if(in_array("sat",$days)) echo "checked"; ?>> Saturday</label>

<label class="weekday-item"><input type="checkbox" name="weekdays[]" value="sun" <?php if(in_array("sun",$days)) echo "checked"; ?>> Sunday</label>

</div>

</div>


<div class="form-group">
<label>Expiry Date</label>
<input type="date" name="expiry_date" value="<?php echo $row['expiry_date']; ?>">
</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>


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

</form>

</div>

</div>

</body>
</html>