<?php
session_start();
include "../db.php";

/* CHECK ID */

$id = isset($_GET['id']) ? intval($_GET['id']) : 0;

if($id==0){
echo "Invalid request";
exit;
}

/* FETCH STATION */

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

if($result->num_rows==0){
echo "Station not found";
exit;
}

$row=$result->fetch_assoc();

/* UPDATE */

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

$name=$_POST['menu_name'];
$slug=$_POST['menu_slug'];
$url=$_POST['stream_url'];
$type=$_POST['stream_type'];
$link=$_POST['menu_link'];
$status=$_POST['status'];

$conn->query("
UPDATE menus SET
menu_name='$name',
menu_slug='$slug',
stream_url='$url',
stream_type='$type',
menu_link='$link',
status='$status'
WHERE id=$id
");

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

}
?>

<!DOCTYPE html>
<html>
<head>

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

<style>

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 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:650px;
}

/* FORM */

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

label{
display:block;
font-weight:bold;
margin-bottom:5px;
}

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

button{
background:#009688;
color:white;
border:none;
padding:10px 18px;
border-radius:6px;
cursor:pointer;
}

</style>

</head>

<body>

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

<div class="main">

<div class="card">

<h2>Edit Radio Station</h2>

<form method="post">

<div class="form-group">
<label>Station Name</label>
<input type="text" name="menu_name"
value="<?php echo htmlspecialchars($row['menu_name']); ?>" required>
</div>

<div class="form-group">
<label>Station Slug</label>
<input type="text" name="menu_slug"
value="<?php echo htmlspecialchars($row['menu_slug']); ?>" required>
</div>

<div class="form-group">
<label>Stream URL</label>
<input type="text" name="stream_url"
value="<?php echo htmlspecialchars($row['stream_url']); ?>" required>
</div>

<div class="form-group">
<select name="stream_type">

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

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

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

</select>

</div>

<div class="form-group">
<label>Copyright Text</label>
<input type="text" name="menu_link"
value="<?php echo htmlspecialchars($row['menu_link']); ?>">
</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 type="submit" name="update">Update Station</button>

</form>

</div>

</div>

</body>
</html>