<?php
session_start();
error_reporting(0);
include('includes/dbconnection.php');
if (strlen($_SESSION['imsaid']==0)) {
  header('location:logout.php');
  } else{

// Code for deleting slider
if(isset($_GET['delid']))
{
$rid=intval($_GET['delid']);
// Get image name before deleting
$query = mysqli_query($con, "SELECT SliderImage FROM tblslider WHERE ID=$rid");
$row = mysqli_fetch_array($query);
if($row && !empty($row['SliderImage'])) {
    $image_path = 'images/slider/' . $row['SliderImage'];
    if(file_exists($image_path)) {
        unlink($image_path); // Delete the image file
    }
}

$sql=mysqli_query($con,"delete from tblslider where ID=$rid");

 echo "<script>alert('Slider deleted successfully');</script>"; 
  echo "<script>window.location.href = 'manage-slider.php'</script>";     
}

// Code for updating status
if(isset($_GET['statusid']) && isset($_GET['status']))
{
    $statusid = intval($_GET['statusid']);
    $status = intval($_GET['status']);
    $new_status = ($status == 1) ? 0 : 1;
    
    $sql = mysqli_query($con, "UPDATE tblslider SET Status='$new_status' WHERE ID=$statusid");
    
    if($sql) {
        $status_text = ($new_status == 1) ? 'activated' : 'deactivated';
        echo "<script>alert('Slider $status_text successfully');</script>"; 
        echo "<script>window.location.href = 'manage-slider.php'</script>";
    }
}

  ?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Mobile Store Management System|| Manage Slider</title>
<?php include_once('includes/cs.php');?>
</head>
<body>

<?php include_once('includes/header.php');?>
<?php include_once('includes/sidebar.php');?>

<div id="content">
  <div id="content-header">
    <div id="breadcrumb"> <a href="dashboard.php" title="Go to Home" class="tip-bottom"><i class="icon-home"></i> Home</a> <a href="manage-slider.php" class="current">Manage Slider</a> </div>
    <h1>Manage Image Slider</h1>
  </div>
  <div class="container-fluid">
    <hr>
    <div class="row-fluid">
      <div class="span12">
        <div class="widget-box">
          <div class="widget-title"> <span class="icon"><i class="icon-th"></i></span>
            <h5>Manage Slider Images</h5>
            <div class="buttons">
              <a href="add-slider.php" class="btn btn-primary btn-mini">Add New Slider</a>
            </div>
          </div>
          <div class="widget-content nopadding">
            <table class="table table-bordered data-table">
              <thead>
                <tr>
                  <th>S.NO</th>
                  <th>Image</th>
                  <th>Title</th>
                  <th>Description</th>
                  <th>Link</th>
                  <th>Order</th>
                  <th>Status</th>
                  <th>Creation Date</th>
                  <th>Action</th>
                </tr>
              </thead>
              <tbody>
                <?php
$ret=mysqli_query($con,"select * from tblslider ORDER BY SliderOrder ASC, CreationDate DESC");
$cnt=1;
while ($row=mysqli_fetch_array($ret)) {
?>
                <tr class="gradeX">
                  <td><?php echo $cnt;?></td>
                  <td>
                    <?php if (!empty($row['SliderImage'])): ?>
                      <img src="images/slider/<?php echo $row['SliderImage']; ?>" style="max-width: 80px; max-height: 50px; border: 1px solid #ddd; padding: 2px; border-radius: 3px;" />
                    <?php else: ?>
                      <span style="color: #999; font-size: 12px;">No image</span>
                    <?php endif; ?>
                  </td>
                  <td><?php echo $row['SliderTitle'];?></td>
                  <td>
                    <?php 
                    $description = $row['SliderDescription'];
                    echo strlen($description) > 50 ? substr($description, 0, 50) . '...' : $description;
                    ?>
                  </td>
                  <td>
                    <?php if (!empty($row['SliderLink'])): ?>
                      <a href="<?php echo $row['SliderLink']; ?>" target="_blank" style="font-size: 12px;">
                        <?php echo strlen($row['SliderLink']) > 20 ? substr($row['SliderLink'], 0, 20) . '...' : $row['SliderLink']; ?>
                      </a>
                    <?php else: ?>
                      <span style="color: #999; font-size: 12px;">No link</span>
                    <?php endif; ?>
                  </td>
                  <td><?php echo $row['SliderOrder'];?></td>
                  <td>
                    <?php if($row['Status']=="1"){ ?>
                      <span class="label label-success">Active</span>
                      <br><a href="manage-slider.php?statusid=<?php echo $row['ID'];?>&status=<?php echo $row['Status'];?>" onclick="return confirm('Do you want to deactivate this slider?');" style="font-size: 11px;">Deactivate</a>
                    <?php } else { ?>
                      <span class="label label-important">Inactive</span>
                      <br><a href="manage-slider.php?statusid=<?php echo $row['ID'];?>&status=<?php echo $row['Status'];?>" onclick="return confirm('Do you want to activate this slider?');" style="font-size: 11px;">Activate</a>
                    <?php } ?>
                  </td>
                  <td><?php echo date('M d, Y', strtotime($row['CreationDate']));?></td>
                  <td class="center">
                    <a href="edit-slider.php?editid=<?php echo $row['ID'];?>" title="Edit Slider"><i class="icon-edit"></i></a> 
                    || 
                    <a href="manage-slider.php?delid=<?php echo ($row['ID']);?>" onclick="return confirm('Do you really want to Delete this slider? This action cannot be undone.');" title="Delete Slider"><i class="icon-remove-sign" aria-hidden="true"></i></a>
                  </td>
                </tr>
                <?php 
$cnt=$cnt+1;
}?> 
              </tbody>
            </table>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<!--Footer-part-->
<div class="row-fluid">
  <div id="footer" class="span12"> 2024 &copy; Mobile Store Management System. Brought to you by <a href="#">Admin</a> </div>
</div>

<!--end-Footer-part--> 
<?php include_once('includes/js.php');?>
</body>
</html>
<?php } ?>