<?php 
include '../connection.php'; 
if(!isset($_SESSION)) session_start();
if(!isset($_SESSION["ID"])){header('Location: index.php');}
$direction = "../assets/img/gallery/";
$flag_image = true;
$errors = array();
$errorList = "";
$image = "";
$label_ar = isset($_POST["label_ar"]) ? $_POST["label_ar"] : "";
$label_en = isset($_POST["label_en"]) ? $_POST["label_en"] : "";

//check if click save
if(isset($_POST["save-image"]))
{
  //check method used
  if ($_SERVER['REQUEST_METHOD'] === 'POST')
  { 
    //nothing
  }
  else
  {
    // header( 'Location: ../ar/index.php');
  }
  $count = 0;
  $nameImages = array();
  $targetImages = array();
  $image_Type = array();
  $c= 1;
  while($count < count($_FILES["image"]["name"]))
  {
      $nameImages[$count] = basename($_FILES["image"]["name"][$count]);
      $nameImages[$count] = $c.date("h:i:sa").$nameImages[$count];
      $targetImages[$count] = $direction.$nameImages[$count];
      $image_Type[$count] = strtolower(pathinfo($targetImages[$count],PATHINFO_EXTENSION));
      $count++;
      $c++;
  }
  $count = 0;
  while($count < count($_FILES["image"]["name"]))
  {
    if(file_exists($targetImages[$count]))
    {
      $errors["fileExists"] = "يرجى إختيار الصورة";
      $flag_image = false;
    }
    elseif($image_Type[$count] != "jfif" && $image_Type[$count] != "jpg" && $image_Type[$count] != "png" && $image_Type[$count] != "jpeg" && $image_Type[$count] != "gif")
    {
      $errors["imageType"] = "يجب أن تكون الصور بأمتداد (jpg,jpeg,png,gif)";
      $flag_image = false;
    }
    elseif($_FILES["image"]["size"][$count] > 5000000)
    {
      $errors["imageSize"] = "يجب أن يكون حجم الصورة أقل من 5 ميجا بايت";
      $flag_image = false;
    }
    $count++;
  }
  if($flag_image === true)
  {
        $count = 0;
        while($count < count($_FILES["image"]["name"]))
        {
            if(!move_uploaded_file($_FILES["image"]["tmp_name"][$count],$targetImages[$count]))
            {
              $errors["imageUpload"] = "يرجى إعادة رفع الصورة";
              $flag_image = false;
            }
        $count++;
        }
        if($flag_image == true)
        {
            $date = date("Y-m-d");
            $stringImage = implode( ",", $nameImages);
            $sqlinsert = "INSERT INTO gallery_images (label_ar,label_en,images,insertUsername,updateUsername,updateDate,status) VALUES ('{$label_ar}','{$label_en}','{$stringImage}','{$_SESSION["username"]}','{$_SESSION["username"]}','{$date}',1)";
            $executeSql = mysqli_query($connection,$sqlinsert);
            $_SESSION["insertImages"] = 1;        
        }
  }
  else
  {
    foreach($errors as $error)
    {
      $errorList .= $error."<br>";
    }
  }
}//end if click save


?>
<!doctype html>
<html lang="ar">
  <head>
    <title>خذ فرصة</title>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- assets / css -->
    <?php include 'cssFile.php'; ?>
  </head>
  <body  dir="rtl">

<?php include 'navbar.php';?>
<div class='content-body'>
   <form  action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post' enctype="multipart/form-data" style='margin-top:50px;'>
    <div class="file-upload">
      <?php
        if($flag_image === false)
        {
          echo "
          <div class='alert alert-danger'>
          $errorList
          </div>";
        }
      ?>

    <div class="form-row text-right">
        <div class="form-group col">
            <label for="">العنوان (باللغة العربية)</label>
            <input type="text" class='form-control' name='label_ar'>
        </div>
        <div class="form-group col">
            <label for="">العنوان (باللغة الإنجليزية)</label>
            <input type="text" class='form-control' name='label_en'>
        </div>
    </div>
  <button class="file-upload-btn" type="button" onclick="$('.file-upload-input').trigger( 'click' )">إضافة صورة جديدة</button>

  <div class="image-upload-wrap">
    <input class="file-upload-input" name="image[]" multiple="" type='file' onchange="readURL(this);" />
    <div class="drag-text">
    <h1><i class="fas fa-image"></i></h1>
    </div>
  </div>
  <div class="file-upload-content">
    <img class="file-upload-image" src="#" alt="your image" />
    <div class="image-title-wrap">
      <button type="submit" name="save-image" class="save-image">حفظ</button>
      <button type="button" onclick="removeUpload()" class="remove-image">حذف</button>
    </div>
  </div>
  <div>


	</div>
</div>
</div>
<!-- End content body -->
<!-- assets / js -->
    <?php include 'jsFile.php'; ?>
<script>


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

  </script>
  </body>
</html>
<!-- onload="reloader()" -->
