<?php
include '../connection.php';
if(!isset($_SESSION)){session_start();}
if(!isset($_SESSION["ID"])){header('Location: index.php');}

$direction = "../assets/img/gallery/";
$Data = "";

if(isset($_GET["Type"]) && isset($_GET["ID"]))
{
$id = $_GET["ID"];
$delete = "update gallery_images set status=3 WHERE ID={$id}";
$executeSql = mysqli_query($connection,$delete);
$_SESSION["deleteGallery"] = 1;
}
if(isset($_GET["Status"]) && isset($_GET["ID"]))
{
    if($_GET["Status"] == 'active')
        $status = 1;
    elseif($_GET["Status"] == 'deactive')
    {
        $status = 0;
    }
    $sqlUpdate = "update gallery_images set status={$status} where ID={$_GET["ID"]}";
    $executeSql = mysqli_query($connection,$sqlUpdate);
    $sqlUpdate = "update gallery_images set status={$status} where ID={$_GET["ID"]}";
    $executeSql = mysqli_query($connection,$sqlUpdate);
    if($_GET["Status"] == 'active')
    $_SESSION["updateActivate"] = 1;
elseif($_GET["Status"] == 'deactive')
{
    $_SESSION["updateActivate"] = 2;
}

}

$sqlRetrive = 'select ID,label_ar,label_en,images,updateDate,updateUsername,status from gallery_images where status in(0,1)';
$executeSql = mysqli_query($connection,$sqlRetrive);
$arrImages = array();
while($retriveData = mysqli_fetch_row($executeSql))
{
    $images = "";
    $label_ar = $retriveData[1];
    $label_en = $retriveData[2];
    $arrImages = explode(",",$retriveData[3]);
    $image = $retriveData[3];
    $UDate = $retriveData[4];
    $Uusername = $retriveData[5];
    $firstImage = $direction.$arrImages[0];
    foreach($arrImages as $img)
    {
        $images.= $direction.$img.",";
    }
    $lastSemicolon = strripos($images,",");
    $images = substr($images,0,$lastSemicolon);
    if($retriveData[6] == 1)
    {
        $action = "<a class='btn btn-danger' href='show-gallery.php?ID=$retriveData[0]&Status=deactive'>تعطيل</a>";
    }
    else
    {
        $action = "<a class='btn btn-success' href='show-gallery.php?ID=$retriveData[0]&Status=active'>تفعيل</a>";
    }
    $Data .= "<div class='col-md-4 text-center mt-2'>
    <section class='gallery'>
  <div>
    <a data-links='{$images}' class='magnific-gallery' >
      <img src='{$firstImage}' style='max-width:200px;max-height:200px;' />
      <h5>{$label_ar}</h5>
      <h5>{$label_en}</h5>
      <a href='show-gallery.php?ID=$retriveData[0]&Type=delete' class='btn btn-danger'>حذف</a>
      {$action}
    </a>   
  </div>
</section>
    </div>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>خذ فرصة</title>
    <?php include 'cssFile.php'; ?>
    <link rel="stylesheet" href="../assets/css/galleryStyle.css">

</head>
<body dir='rtl'>
    <?php include 'navbar.php';?>

    <div class="content-body">
        <div class='container' style='margin-top:30px;'>
        <div class="row justify-content-center">
            <div class="col-md-3 text-center col-gallery">
                <h3>معرض الصور</h3>
            </div>
        </div>
        <!-- End row -->
    <div class='row'>
                <div class='col-md text-right' style='margin:0 10px 10px 0px;'>
        <a class='btn btn-blue' href='add-gallery.php'>إضافة صور جديدة </a>
      </div>
      <!-- End col -->
              <div class='col-md text-left' style='margin:0 10px 10px 0px;'>
        <a class='btn btn-blue' href='delete-gallery.php'>عرض المحذوف</a>
      </div>
      <!-- End col -->
    </div>

      <div class='row'>

        <?php
             echo $Data;
        ?>

    </div>
    <!-- End row -->


        </div>
        <!-- End container -->
    </div>
    <!-- End content-body -->



    <?php include 'jsFile.php'; ?>
    <!-- gallery images js -->
<script src="../assets/js/jquery.magnific-popup.min.js"></script>
    <script>
$(document).ready(function(){
  $('.magnific-gallery').each(function(index , value){
  var gallery = $(this);
  var galleryImages = $(this).data('links').split(',');
    var items = [];
    for(var i=0;i<galleryImages.length; i++){
      items.push({
        src:galleryImages[i],
        title:''
      });
    }
    gallery.magnificPopup({
      mainClass: 'mfp-fade',
      items:items,
      gallery:{
        enabled:true,
        tPrev: $(this).data('prev-text'),
        tNext: $(this).data('next-text')
      },
      type: 'image'
    });
});
});
</script>
 <script>
  <?php
if(!isset($_SESSION))
  session_start();
if(isset($_SESSION["deleteGallery"]) && $_SESSION["deleteGallery"] == 1)
{ 
?>
Swal.fire(
  'تم حذف الصور بنجاح',
  '',
  'success'
)
<?php
$_SESSION["deleteGallery"] = 0;
}
?>
  </script>

  <script>
  <?php
if(!isset($_SESSION))
  session_start();
if(isset($_SESSION["updateActivate"]) && $_SESSION["updateActivate"] == 1)
{ 
?>
Swal.fire(
  'تم تفعيل الصور بنجاح',
  '',
  'success'
)
<?php
$_SESSION["updateActivate"] = 0;
}
?>

  </script>
    <script>
  <?php
if(!isset($_SESSION))
  session_start();
if(isset($_SESSION["updateActivate"]) && $_SESSION["updateActivate"] == 2)
{ 
?>
Swal.fire(
  'تم تعطيل الصور بنجاح',
  '',
  'success'
)
<?php
$_SESSION["updateActivate"] = 0;
}
?>

  </script>
      <script>
  <?php
if(!isset($_SESSION))
  session_start();
if(isset($_SESSION["recovery"]) && $_SESSION["recovery"] == 1)
{ 
?>
Swal.fire(
  'تم  الستعادة بنجاح',
  '',
  'success'
)
<?php
$_SESSION["recovery"] = 0;
}
?>

  </script>
</body>
</html>