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

if(!isset($_SESSION['admin'])){
header("Location:index.php");
exit();
}

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

$id=$_POST['id'];
$name=$_POST['name'];
$type=$_POST['type'];
$url=$_POST['url'];
$enabled = isset($_POST['enabled']) ? 1 : 0;

mysqli_query($conn,"UPDATE streams SET
name='$name',
type='$type',
url='$url',
enabled='$enabled'
WHERE id='$id'");
}

$streams=mysqli_query($conn,"SELECT * FROM streams ORDER BY id ASC");
?>

<!DOCTYPE html>
<html>

<head>

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

<style>

*{
box-sizing:border-box;
}

body{
margin:0;
font-family:Arial;
background:#eef1f5;
display:flex;
}

/* Sidebar */

.sidebar{
width:230px;
background:#0f6c74;
color:white;
height:100vh;
position:fixed;
padding-top:20px;
}

.sidebar h2{
text-align:center;
margin-bottom:30px;
}

.sidebar a{
display:block;
padding:12px 20px;
color:white;
text-decoration:none;
}

.sidebar a:hover{
background:#0a5258;
}

/* Main */

.main{
margin-left:230px;
width:100%;
}

/* Topbar */

.topbar{
background:white;
padding:15px;
display:flex;
justify-content:space-between;
align-items:center;
box-shadow:0 2px 6px rgba(0,0,0,0.1);
}

/* Container */

.container{
padding:20px;
}

/* Card */

.card{
background:white;
padding:25px;
border-radius:10px;
box-shadow:0 3px 8px rgba(0,0,0,0.1);
margin-bottom:20px;
}

/* Form Layout */

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

label{
font-size:14px;
color:#555;
display:block;
margin-bottom:4px;
}

/* Inputs */

input[type="text"],
select{
width:100%;
padding:10px;
border:1px solid #ccc;
border-radius:6px;
font-size:14px;
}

/* Checkbox Row */

.checkbox-row{
display:flex;
align-items:center;
gap:8px;
margin-top:10px;
}

/* Button */

button{
background:#0f6c74;
color:white;
padding:10px 18px;
border:none;
border-radius:6px;
cursor:pointer;
font-size:14px;
margin-top:10px;
}

button:hover{
background:#0a5258;
}

</style>

</head>

<body>

<!-- Sidebar -->

<div class="sidebar">

<h2>Booking Services</h2>

<a href="dashboard.php">Dashboard</a>
<a href="sliders.php">Manage Sliders</a>
<a href="services.php">Manage Menu</a>
<a href="ticker.php">Manage Ticker</a>
<a href="settings.php">Site Settings</a>
<a href="stream.php">Streaming Manager</a>
<a href="logout.php">Logout</a>

</div>

<!-- Main -->

<div class="main">

<div class="topbar">

<h3>Streaming Manager</h3>
<div>Super Admin</div>

</div>

<div class="container">

<div class="card">
<h3>Manage Radio Streams</h3>
</div>

<?php while($s=mysqli_fetch_assoc($streams)){ ?>

<div class="card">

<form method="post">

<input type="hidden" name="id" value="<?php echo $s['id']; ?>">

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

<div class="form-group">
<label>Stream Type</label>
<select name="type">
<option value="MP3" <?php if($s['type']=="MP3") echo "selected"; ?>>MP3</option>
<option value="HLS" <?php if($s['type']=="HLS") echo "selected"; ?>>HLS</option>
<option value="Icecast" <?php if($s['type']=="Icecast") echo "selected"; ?>>Icecast</option>
</select>
</div>

<div class="form-group">
<label>Stream URL</label>
<input type="text" name="url" value="<?php echo $s['url']; ?>">
</div>

<div class="checkbox-row">
<input type="checkbox" name="enabled" <?php if($s['enabled']) echo "checked"; ?>>
<label>Enable Stream</label>
</div>

<button name="update">Update Stream</button>

</form>

</div>

<?php } ?>

</div>

</div>

</body>
</html>