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

$id=$_GET['id'];

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

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

$name=$_POST['name'];
$link=$_POST['link'];
$stream=$_POST['stream'];
$type=$_POST['stream_type'];

$imageName=$_FILES['image']['name'];
$tmpName=$_FILES['image']['tmp_name'];

$imgPath=$row['menu_image'];

if(!empty($imageName)){

$newImageName=time()."_".$imageName;
$uploadPath="../uploads/".$newImageName;

if(move_uploaded_file($tmpName,$uploadPath)){
$imgPath="uploads/".$newImageName;
}

}

$conn->query("
UPDATE menus SET
menu_name='$name',
menu_link='$link',
stream_url='$stream',
stream_type='$type',
menu_image='$imgPath'
WHERE id=$id
");

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

}
?>

<!DOCTYPE html>
<html>
<head>

<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Edit Radio Station</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);
}

/* HEADER */

.page-header{
display:flex;
justify-content:space-between;
align-items:center;
margin-bottom:25px;
}

.page-header h2{
margin:0;
}

/* BUTTON */

.btn{
background:#009688;
color:white;
padding:8px 16px;
border:none;
border-radius:6px;
text-decoration:none;
cursor:pointer;
font-size:14px;
}

/* FORM GRID */

.form-grid{
display:grid;
grid-template-columns:1fr 1fr;
gap:20px;
}

/* FULL WIDTH */

.full{
grid-column:1/3;
}

/* LABEL */

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

/* INPUT */

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

/* IMAGE */

.preview-img{
width:140px;
height:90px;
object-fit:cover;
border-radius:6px;
border:1px solid #ddd;
}

/* MOBILE */

@media(max-width:768px){

.form-grid{
grid-template-columns:1fr;
}

.full{
grid-column:1;
}

}

</style>

</head>

<body>

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

<div class="main">

<div class="card">

<div class="page-header">

<h2>Edit Radio Station</h2>

<a href="menus.php" class="btn">← Back</a>

</div>

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

<div>

<label>Radio Name</label>

<input
type="text"
name="name"
value="<?php echo $row['menu_name']; ?>"
required>

</div>

<div>

<label>Stream Type</label>

<select name="stream_type">

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

<option value="HLS"
<?php if($row['stream_type']=="HLS") echo "selected"; ?>>
HLS (.m3u8)
</option>

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

</select>

</div>

<div class="full">

<label>Streaming URL</label>

<input
type="text"
name="stream"
value="<?php echo $row['stream_url']; ?>"
required>

</div>

<div>

<label>Menu Link</label>

<input
type="text"
name="link"
value="<?php echo $row['menu_link']; ?>">

</div>

<div>

<label>Current Image</label>
<br>

<img
src="../<?php echo $row['menu_image']; ?>"
class="preview-img">

</div>

<div>

<label>Upload New Image</label>

<input type="file" name="image">

</div>

<div class="full">

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

</div>

</form>

</div>

</div>

</body>
</html>