<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);
include('includes/dbconnection.php');
if (strlen($_SESSION['imsaid']==0)) {
  header('location:logout.php');
  } else{
    if(isset($_POST['submit']))
  {
    $eid=$_GET['editid'];
    $slidertitle=trim($_POST['slidertitle']);
    $sliderdescription=trim($_POST['sliderdescription']);
    $sliderlink=trim($_POST['sliderlink']);
    $sliderorder=intval($_POST['sliderorder']);
    $status = isset($_POST['status']) ? '1' : '0';
    
    // Create upload directory if it doesn't exist
    $upload_dir = 'images/slider/';
    if (!file_exists($upload_dir)) {
        mkdir($upload_dir, 0777, true);
    }
    
    // Get current image from database
    $current_image_query = mysqli_query($con, "SELECT SliderImage FROM tblslider WHERE ID='$eid'");
    $current_image_row = mysqli_fetch_array($current_image_query);
    $current_image = $current_image_row['SliderImage'];
    
    // Handle file upload if provided
    $image_path = $current_image; // Keep current image by default
    if (!empty($_FILES['slider_image']['name'])) {
        $file_name = $_FILES['slider_image']['name'];
        $file_tmp = $_FILES['slider_image']['tmp_name'];
        $file_size = $_FILES['slider_image']['size'];
        $file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
        $allowed_extensions = array('png', 'jpg', 'jpeg', 'gif');
        
        if (in_array($file_ext, $allowed_extensions)) {
            if ($file_size < 5000000) { // 5MB limit
                $new_file_name = 'slider_' . time() . '.' . $file_ext;
                $upload_path = $upload_dir . $new_file_name;
                
                if (move_uploaded_file($file_tmp, $upload_path)) {
                    // Delete old image file if it exists
                    if (!empty($current_image)) {
                        $old_file_path = $upload_dir . $current_image;
                        if (file_exists($old_file_path)) {
                            unlink($old_file_path);
                        }
                    }
                    $image_path = $new_file_name;
                } else {
                    echo '<script>alert("Failed to upload file!")</script>';
                }
            } else {
                echo '<script>alert("File size too large! Maximum 5MB allowed.")</script>';
            }
        } else {
            echo '<script>alert("Invalid file type! Only PNG, JPG, JPEG, GIF allowed.")</script>';
        }
    }
     
    $query=mysqli_query($con, "UPDATE tblslider SET SliderTitle='$slidertitle', SliderImage='$image_path', SliderDescription='$sliderdescription', SliderLink='$sliderlink', SliderOrder='$sliderorder', Status='$status' WHERE ID=$eid");
    if ($query) {
   
    echo '<script>alert("Slider has been updated successfully.")</script>';
    echo '<script>window.location.href = "manage-slider.php"</script>';
  }
  else
    {
     echo '<script>alert("Something Went Wrong. Please try again")</script>';
    }

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

<!--Header-part-->
<?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="tip-bottom">Manage Slider</a> <a href="edit-slider.php" class="current">Edit Slider</a></div>
  <h1>Edit Slider Image</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-align-justify"></i> </span>
          <h5>Edit Slider</h5>
        </div>
        <div class="widget-content nopadding">
          <?php
$eid=$_GET['editid'];
$ret=mysqli_query($con,"select * from tblslider where ID=$eid");
$cnt=1;
while ($row=mysqli_fetch_array($ret)) {

?>
          <form name="editslider" method="post" class="form-horizontal" enctype="multipart/form-data">
            <div class="control-group">
              <label class="control-label">Slider Title :</label>
              <div class="controls">
                <input type="text" class="span11" name="slidertitle" id="slidertitle" value="<?php echo $row['SliderTitle'];?>" required />
              </div>
            </div>
            <div class="control-group">
              <label class="control-label">Current Image :</label>
              <div class="controls">
                <?php if (!empty($row['SliderImage'])): ?>
                  <img src="images/slider/<?php echo $row['SliderImage']; ?>" style="max-width: 200px; max-height: 120px; border: 1px solid #ddd; padding: 5px; border-radius: 5px;" />
                  <br><small>Current image: <?php echo $row['SliderImage']; ?></small>
                <?php else: ?>
                  <span style="color: #999;">No image uploaded</span>
                <?php endif; ?>
              </div>
            </div>
            <div class="control-group">
              <label class="control-label">New Slider Image :</label>
              <div class="controls">
                <input type="file" class="span11" name="slider_image" id="slider_image" accept="image/*" />
                <span class="help-block">Upload new image (PNG, JPG, JPEG, GIF - Max 5MB) - Leave empty to keep current image</span>
              </div>
            </div>
            <div class="control-group">
              <label class="control-label">Description :</label>
              <div class="controls">
                <textarea class="span11" name="sliderdescription" id="sliderdescription" rows="4"><?php echo $row['SliderDescription'];?></textarea>
              </div>
            </div>
            <div class="control-group">
              <label class="control-label">Link URL :</label>
              <div class="controls">
                <input type="text" class="span11" name="sliderlink" id="sliderlink" value="<?php echo $row['SliderLink'];?>" placeholder="e.g., shop-mobile.php" />
              </div>
            </div>
            <div class="control-group">
              <label class="control-label">Display Order :</label>
              <div class="controls">
                <input type="number" class="span11" name="sliderorder" id="sliderorder" value="<?php echo $row['SliderOrder'];?>" min="1" />
              </div>
            </div>
            <div class="control-group">
              <label class="control-label">Status :</label>
              <div class="controls">
                <label class="checkbox">
                  <input type="checkbox" name="status" id="status" <?php if($row['Status']=="1"){ echo "checked"; }?> />
                  Active
                </label>
              </div>
            </div>
            <div class="form-actions">
              <button type="submit" class="btn btn-success" name="submit">Update Slider</button>
            </div>
          </form>
          <?php } ?>
        </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 } ?>