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

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

/* Upload slider */

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

$name=$_FILES['image']['name'];
$tmp=$_FILES['image']['tmp_name'];
$link=$_POST['link'];

$path="../uploads/sliders/".$name;

move_uploaded_file($tmp,$path);

mysqli_query($conn,"INSERT INTO sliders(image,link) VALUES('$name','$link')");

}

/* Delete slider */

if(isset($_GET['delete'])){

$id=$_GET['delete'];

mysqli_query($conn,"DELETE FROM sliders WHERE id='$id'");

}

/* Get edit slider */

$edit_slider=null;

if(isset($_GET['edit'])){
$id=$_GET['edit'];
$result=mysqli_query($conn,"SELECT * FROM sliders WHERE id='$id'");
$edit_slider=mysqli_fetch_assoc($result);
}

/* Update slider */

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

$id=$_POST['id'];
$link=$_POST['link'];

if(!empty($_FILES['image']['name'])){

$name=$_FILES['image']['name'];
$tmp=$_FILES['image']['tmp_name'];

$path="../uploads/sliders/".$name;

move_uploaded_file($tmp,$path);

mysqli_query($conn,"UPDATE sliders SET image='$name', link='$link' WHERE id='$id'");

}else{

mysqli_query($conn,"UPDATE sliders SET link='$link' WHERE id='$id'");

}

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

}

$slides=mysqli_query($conn,"SELECT * FROM sliders");

?>

<!DOCTYPE html>
<html>

<head>

<meta name="viewport" content="width=device-width, initial-scale=1">

<title>Manage Sliders</title>

<style>

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

/* Cards */

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

button{
background:#0f6c74;
color:white;
border:none;
padding:10px 16px;
border-radius:6px;
cursor:pointer;
}

input{
width:100%;
padding:10px;
margin-top:10px;
box-sizing:border-box;
border:1px solid #ccc;
border-radius:5px;
}

/* Slider Grid */

.slider-grid{
display:grid;
grid-template-columns:repeat(auto-fit,minmax(200px,1fr));
gap:20px;
}

.slider-card{
background:white;
padding:10px;
border-radius:10px;
box-shadow:0 3px 8px rgba(0,0,0,0.1);
text-align:center;
}

.slider-card img{
width:100%;
border-radius:6px;
}

.action{
display:inline-block;
margin-top:8px;
padding:6px 10px;
border-radius:4px;
font-size:12px;
text-decoration:none;
color:white;
}

.edit{
background:#007bff;
}

.delete{
background:red;
}

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

input{
width:100%;
padding:10px;
border:1px solid #ccc;
border-radius:5px;
margin-bottom:15px;
box-sizing:border-box;
}

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

</style>

</head>

<body>

<!-- Sidebar -->

<?php include("layout/sidebar.php"); ?>

<div class="main">

<div class="topbar">

<h3>Manage Sliders</h3>

<div>Super Admin</div>

</div>

<div class="container">

<!-- Upload / Edit Card -->

<div class="card">

<?php if($edit_slider){ ?>

<h3>Edit Slider</h3>

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

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

<label>Slider Link</label>
<input type="text" name="link" value="<?php echo $edit_slider['link']; ?>" required>

<label>Change Image (optional)</label>
<input type="file" name="image">

<br><br>

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

</form>

<?php } else { ?>

<h3>Upload New Slider</h3>

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

<label>Slider Image</label>
<input type="file" name="image" required>

<label>Slider Link</label>
<input type="text" name="link" placeholder="https://example.com" required>

<br>

<button name="upload">Upload Slider</button>

</form>

<?php } ?>

</div>

<!-- Slider List -->

<div class="card">

<h3>Slider Images</h3>

<div class="slider-grid">

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

<div class="slider-card">

<a href="<?php echo $s['link']; ?>" target="_blank">
<img src="../uploads/sliders/<?php echo $s['image']; ?>">
</a>

<br>

<a class="action edit" href="?edit=<?php echo $s['id']; ?>">Edit</a>

<a class="action delete" href="?delete=<?php echo $s['id']; ?>" onclick="return confirm('Delete this slider?')">Delete</a>

</div>

<?php } ?>

</div>

</div>

</div>

</div>

</body>

</html>