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

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

$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']);
}

if(empty($_FILES['ad_audio']['name'])){
die("Please upload ad audio");
}

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

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

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

if(!move_uploaded_file($tmp,$path)){
die("Upload failed");
}

$conn->query("
INSERT INTO radio_ads
(menu_id,ad_name,ad_audio,ad_time,repeat_hours,weekdays,expiry_date,status)
VALUES
('$menu_id','$name','ads/$newName','$time','$repeat','$weekdays','$expiry','$status')
");

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

}
?>

<!DOCTYPE html>
<html>
<head>

<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Add 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 GRID */

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

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

.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;
}

/* 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>Add 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()){
echo "<option value='{$m['id']}'>{$m['menu_name']}</option>";
}
?>

</select>
</div>


<div class="form-group">
<label>Ad Name</label>
<input type="text" name="ad_name" required>
</div>


<div class="form-group">
<label>Ad Audio File</label>
<input type="file" name="ad_audio" required>
</div>


<div class="form-group">
<label>Ad Play Time (Clock Time)</label>
<input type="time" name="ad_time" step="1" required>
</div>


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

<select name="repeat_hours">

<option value="0">Once per day</option>
<option value="1">Every 1 hour</option>
<option value="2">Every 2 hours</option>
<option value="3">Every 3 hours</option>
<option value="4">Every 4 hours</option>
<option value="6">Every 6 hours</option>
<option value="12">Every 12 hours</option>
<option value="24">Every 24 hours</option>

</select>

</div>


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

<div class="weekday-list">

<label class="weekday-item">
<input type="checkbox" name="weekdays[]" value="mon"> Monday
</label>

<label class="weekday-item">
<input type="checkbox" name="weekdays[]" value="tue"> Tuesday
</label>

<label class="weekday-item">
<input type="checkbox" name="weekdays[]" value="wed"> Wednesday
</label>

<label class="weekday-item">
<input type="checkbox" name="weekdays[]" value="thu"> Thursday
</label>

<label class="weekday-item">
<input type="checkbox" name="weekdays[]" value="fri"> Friday
</label>

<label class="weekday-item">
<input type="checkbox" name="weekdays[]" value="sat"> Saturday
</label>

<label class="weekday-item">
<input type="checkbox" name="weekdays[]" value="sun"> Sunday
</label>

</div>

</div>


<div class="form-group">
<label>Expiry Date</label>
<input type="date" name="expiry_date">
</div>


<div class="form-group">
<label>Status</label>

<select name="status">
<option value="1">Active</option>
<option value="0">Disabled</option>
</select>

</div>


<button class="btn" name="save">Save Advertisement</button>

</form>

</div>

</div>

</body>
</html>