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

$labelAbout_ar = isset($_POST["labelAbout_ar"]) ? $_POST["labelAbout_ar"] : "";
$contentAbout = isset($_POST["contentAbout"]) ? $_POST["contentAbout"] : "";
$LabelGuarantee = isset($_POST["LabelGuarantee"]) ? $_POST["LabelGuarantee"] : "";
$Guarantee1 = isset($_POST["Guarantee1"]) ? $_POST["Guarantee1"] : "";
$Guarantee2 = isset($_POST["Guarantee2"]) ? $_POST["Guarantee2"] : "";


$flag = true; 
$errors = array();
$errorList = "";
$flagImage = false;
$flagImage2 = false;

if(isset($_POST["save"]))
{
  //check method used
  if ($_SERVER['REQUEST_METHOD'] === 'POST')
  { 
    //nothing
  }
  else
  {
    // header( 'Location: ../ar/index.php');
  }
  if(empty($labelAbout_ar) || empty($contentAbout) || empty($LabelGuarantee) || empty($Guarantee1) ||
     empty($Guarantee2))
  {
    $errors["empty"] = "يرجى تعبئة جميع الحقول";
    $flag = false; 
  }  //check if any textbox empty
    $image = basename($_FILES["image"]["name"]);
    $image2 = basename($_FILES["image2"]["name"]);
  if(!empty($image))
  {
    $image = date('Y-M-D').$image;
    $target_file = $direction.$image;
    $image_Type = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
    if(!file_exists($target_file))
    {
      if($image_Type != "jfif" && $image_Type != "jpg" && $image_Type != "png" && $image_Type != "jpeg" && $image_Type != "gif")
    {
      $errors["imageType"] = "يجب أن تكون الصورة بأمتداد (jpg,jpeg,png,gif)";
      $flag = false;
    }
    elseif($_FILES["image"]["size"] > 5000000)
    {
      $errors["imageSize"] = "يجب أن يكون حجم الصورة أقل من 5 ميجا بايت";
      $flag = false;
    }
    $flagImage = true;
    }
  }
  if(!empty($image2))
  {
  $image2 = date('Y-M-D').$image2;
  $target_file2 = $direction.$image2;
  $image_Type2 = strtolower(pathinfo($target_file2,PATHINFO_EXTENSION));
    if(!file_exists($target_file2))
    {
      if($image_Type != "jfif" && $image_Type2 != "jpg" && $image_Type2 != "png" && $image_Type2 != "jpeg" && $image_Type2 != "gif")
    {
      $errors["imageType"] = "يجب أن تكون الصورة بأمتداد (jpg,jpeg,png,gif)";
      $flag = false;
    }

    elseif($_FILES["image2"]["size"] > 5000000)
    {
           $errors["imageSize"] = "يجب أن يكون حجم الصورة أقل من 5 ميجا بايت";
      $flag = false;   
    }
    $flagImage2 = true;
    }
  }
  $image2 = basename($_FILES["image2"]["name"]);
  $target_file2 = $direction.$image2;
  $image_Type2 = strtolower(pathinfo($target_file2,PATHINFO_EXTENSION));
  if(!file_exists($target_file2))
  {
    if($image_Type2 != "jpg" && $image_Type2 != "png" && $image_Type2 != "jpeg" && $image_Type2 != "gif")
    {
      $errors["imageType"] = "يجب أن تكون الصورة بأمتداد (jpg,jpeg,png,gif)";
      $flag = false;
    }
    elseif($_FILES["image2"]["size"] > 5000000)
    {
      $errors["imageSize"] = "يجب أن يكون حجم الصورة أقل من 5 ميجا بايت";
      $flag = false;
    }
    $flagImage2 = true;
  }//check file exists
  if($flag === true)
  {
      if($flagImage === true && $flagImage2 === true)
      {
        if(!move_uploaded_file($_FILES["image"]["tmp_name"],$target_file) || !move_uploaded_file($_FILES["image2"]["tmp_name"],$target_file2))
        {
        $errors["imageUpload"] = "يرجى إعادة رفع الصورة";
        }
        else
        {
            $sqlInsert = "UPDATE goshan_en SET labelAbout='{$labelAbout_ar}',contentAbout='{$contentAbout}',LabelGuarantee='{$LabelGuarantee}',Guarantee1='{$Guarantee1}',Guarantee2='{$Guarantee2}',image1='{$image}',image2='{$image2}' WHERE ID = 1";
            $executeSql = mysqli_query($connection,$sqlInsert);
            $_SESSION["updatesuccess"] = 1;
            header( 'Location: show-goshan.php');  
        }
      }
      elseif($flagImage === true)
      {
          if(!move_uploaded_file($_FILES["image"]["tmp_name"],$target_file))
          {
            $errors["imageUpload"] = "يرجى إعادة رفع الصورة";
          }
          else
          $sqlInsert = "UPDATE goshan_en SET labelAbout='{$labelAbout_ar}',contentAbout='{$contentAbout}',LabelGuarantee='{$LabelGuarantee}',Guarantee1='{$Guarantee1}',Guarantee2='{$Guarantee2}',image1='{$image}' WHERE ID = 1";
          $executeSql = mysqli_query($connection,$sqlInsert);
          $_SESSION["updatesuccess"] = 1;
          header( 'Location: show-goshan.php');  
      }
      elseif($flagImage2 ===  true)
      {
        if(!move_uploaded_file($_FILES["image2"]["tmp_name"],$target_file2))
        {
          $errors["imageUpload"] = "يرجى إعادة رفع الصورة";
        }
        else
        $sqlInsert = "UPDATE goshan_en SET labelAbout='{$labelAbout_ar}',contentAbout='{$contentAbout}',LabelGuarantee='{$LabelGuarantee}',Guarantee1='{$Guarantee1}',Guarantee2='{$Guarantee2}',image2='{$image2}' WHERE ID = 1";
        $executeSql = mysqli_query($connection,$sqlInsert);
        $_SESSION["updatesuccess"] = 1;
        header( 'Location: show-goshan.php');  
    }
    else
    {
        $sqlInsert = "UPDATE goshan_en SET labelAbout='{$labelAbout_ar}',contentAbout='{$contentAbout}',LabelGuarantee='{$LabelGuarantee}',Guarantee1='{$Guarantee1}',Guarantee2='{$Guarantee2}' WHERE ID = 1";
        $executeSql = mysqli_query($connection,$sqlInsert);
        $_SESSION["updatesuccess"] = 1;
        header( 'Location: show-goshan.php');  
    }

  }
  else
  {
    foreach($errors as $error)
    {
      $errorList .= $error."<br>";
    }
  }
}

    $sqlretriveAR = "SELECT labelAbout, contentAbout, LabelGuarantee, Guarantee1, Guarantee2, image1, image2 FROM goshan_en WHERE ID = 1";
    $executeSqlAR = mysqli_query($connection,$sqlretriveAR);
    $retriveDataAR = mysqli_fetch_row($executeSqlAR);

?>
<!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'>
<div class="container" style='margin-top:30px;'>
    <div class='row justify-content-center'>
        <div class='col-10'>
            <?php
            if($flag === false)
            {
            echo "
            <div class='alert alert-danger text-right'>
            $errorList
            </div>";
            }
        ?>
            <div class="card text-right" id="add-info">
                <div class="card-header"><h3>تعديل قوشان المستقبل</h3></div>
                <div class="card-body">
                    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post' enctype="multipart/form-data">
                    <div class='form-row'>
                        <div class='form-group col'>
                            <label class='ar-info' for="ar-info">المعلومات باللغة الإنجليزية</label>                        
                        </div>
                        <!-- end form group -->

                    </div>
                    <!-- end form row -->
                    <div class='form-row'>
                        <div class='form-group col'>
                            <label for="ar-aboutus">العنوان الأول</label>
                            <input type="text" name="labelAbout_ar" style='direction:ltr;text-align:left;' id="" value="<?php echo $retriveDataAR[0];?>" class="form-control">
                        </div>
                        <!-- end form group -->

                    </div>
                    <!-- end form row -->
                    <div class='form-row'>
                        <div class='form-group col'>
                            <label for="ar-ourvision">المحتوى</label>
                            <textarea class='form-control' rows="3" name="contentAbout" style='resize:none;direction:ltr;text-align:left;'><?php echo $retriveDataAR[1];?></textarea>                 
                        </div>
                        <!-- end form group -->

                    </div>
                    <!-- end form row -->
                    <div class='form-row'>
                        <div class='form-group col'>
                            <label for="ar-message">العنوان الثاني</label>
                            <input type="text" name="LabelGuarantee" id="" style='direction:ltr;text-align:left;' value="<?php echo $retriveDataAR[2];?>" class="form-control">
                        </div>
                        <!-- end form group -->

                    </div>
                    <!-- end form row -->
                    <div class='form-row'>
                        <div class='form-group col'>
                            <label for="ar-ourgoals">الكفالة السنوية</label>
                            <input type="text" name="Guarantee1" style='direction:ltr;text-align:left;' value="<?php echo $retriveDataAR[3];?>" id="" class="form-control">
                        </div>
                        <!-- end form group -->
                    </div>
                    <!-- end form row -->
                    <div class='form-row'>
                        <div class='form-group col'>
                        <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" type='file' onchange="readURL(this);" />
                            <div class="drag-text">
                            <h1><img src='<?php echo $direction.$retriveDataAR[5]?>' style='max-width:200px;'></h1>
                            </div>
                        </div>
                        <div class="file-upload-content">
                            <img class="file-upload-image" src="#" alt="your image" />
                            <div class="image-title-wrap">
                                <button type="button" onclick="removeUpload()" class="remove-image">حذف</button>
                            </div>
                        </div>
                        </div>
                        <!-- End form group -->
                        </div>
                    <div class='form-row'>
                        <div class='form-group col'>
                            <label for="ar-ourvalue">الكفالة الشهرية</label>
                            <input type="text" name="Guarantee2" style='direction:ltr;text-align:left;' id="" value="<?php echo $retriveDataAR[4];?>" class="form-control">
                        </div>
                        <!-- end form group -->
                    </div>
                    <!-- end form row -->
                    <div class='form-row'>
                    <div class='form-group col'>
                        <button class="file-upload-btn" type="button" onclick="$('.file-upload-input2').trigger( 'click' )">إضافة صورة جديدة</button>
                        <div class="image-upload-wrap2">
                            <input class="file-upload-input2" name="image2" type='file' onchange="readURL2(this);" />
                            <div class="drag-text">
                            <h1><img src='<?php echo $direction.$retriveDataAR[6]?>' style='max-width:200px;'></h1>
                            </div>
                        </div>
                        <div class="file-upload-content2">
                            <img class="file-upload-image2" src="#" alt="your image" />
                            <div class="image-title-wrap">
                                <button type="button" onclick="removeUpload2()" class="remove-image">حذف</button>
                            </div>
                        </div>
                        </div>
                        <!-- End form group -->
                        </div>
                    <div class="form-row">
                        <div class="form-group col text-center">
                            <label class='text-white'>@</label>
                            <button type="submit" name="save" class="save-image">حفظ</button>
                        </div>
                    </div>
                    </form>
                    <!-- End form -->
                </div>
                <!-- End card-body -->
            </div>
            <!-- End Card -->
        </div>
        <!-- end col -->
    </div>
    <!-- end row -->
</div>
<!-- end container -->
</div>
<!-- End content-body -->
<!-- assets / js -->
    <?php include 'jsFile.php'; ?>
<script>

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

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