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

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

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

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

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

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

move_uploaded_file($tmp,$path);

$conn->query("
INSERT INTO radio_ads
(menu_id,ad_name,ad_audio,ad_interval,status)
VALUES
('$menu_id','$name','ads/$newname','$interval',1)
");

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 AREA */

.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 GROUP */

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

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

/* INPUTS */

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

/* 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>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");

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 name="ad_name" required>

</div>


<div class="form-group">

<label>Upload Ad Audio</label>
<input type="file" name="ad_audio" required>

</div>


<div class="form-group">

<label>Ad Interval (seconds)</label>
<input name="ad_interval" value="120">

</div>


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

</form>

</div>

</div>

</body>
</html>