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


$direction = "../assets/img/infoTakeachance/";
$ar_label = isset($_POST["ar_label"]) ? $_POST["ar_label"] : "";
$ar_paragraph = isset($_POST["ar_paragraph"]) ? $_POST["ar_paragraph"] : "";
$en_label = isset($_POST["en_label"]) ? $_POST["en_label"] : "";
$en_paragraph = isset($_POST["en_paragraph"]) ? $_POST["en_paragraph"] : "";



$flag = true;
$errors = array();
$errorList = "";
$image = "";
$flagImage = false;
//check if click save
if(isset($_POST["save"]))
{
  //check method used
  if ($_SERVER['REQUEST_METHOD'] === 'POST')
  { 
    //nothing
  }
  else
  {
    // header( 'Location: ../ar/index.php');
  }
  
  $image = basename($_FILES["image"]["name"]);
  if(!empty($image))
  {
    $image = date('Y-M-D').$image;      
  }

  $target_file = $direction.$image;
  $image_Type = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

  if(empty($ar_label) || empty($ar_paragraph) || empty($en_label) || empty($en_paragraph))
  {
    $errors["empty"] = "يرجى تعبئة جميع الحقول";
    $flag = false; 
  }  //check if any textbox empty
  if(!file_exists($target_file))
  {
    if($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;
  }//check file exists
  

  if($flag === true)
  {
      if($flagImage === true)
        if(!move_uploaded_file($_FILES["image"]["tmp_name"],$target_file))
        {
        $errors["imageUpload"] = "يرجى إعادة رفع الصورة";
        }
        else
        {
        $date = date("Y-m-d");
        $updateSql = "UPDATE info_takeachance_ar SET label='{$ar_label}',paragraph='{$ar_paragraph}',updateDate='{$date}',updateUsername='{$_SESSION["username"]}',image='{$image}' WHERE ID=3";
        $executeSql = mysqli_query($connection,$updateSql);
        $updateSql = "UPDATE info_takeachance_en SET label='{$en_label}',paragraph='{$en_paragraph}',updateDate='{$date}',updateUsername='{$_SESSION["username"]}',image='{$image}' WHERE ID=3";
        $executeSql = mysqli_query($connection,$updateSql);
        $_SESSION["updateinfo"] = 1;
        header( 'Location: ../admin/show-info.php');  
        }//if check store image or not
        elseif($flagImage === false)
        {
            $date = date("Y-m-d");
            $updateSql = "UPDATE info_takeachance_ar SET label='{$ar_label}',paragraph='{$ar_paragraph}',updateDate='{$date}',updateUsername='{$_SESSION["username"]}' WHERE ID=3";
            $executeSql = mysqli_query($connection,$updateSql);
            $updateSql = "UPDATE info_takeachance_en SET label='{$en_label}',paragraph='{$en_paragraph}',updateDate='{$date}',updateUsername='{$_SESSION["username"]}' WHERE ID=3";
            $executeSql = mysqli_query($connection,$updateSql);
            $_SESSION["updateinfo"] = 1; 
            header( 'Location: ../admin/show-info.php');  
        }
  }//if for store image in file
  else
  {
    foreach($errors as $error)
    {
      $errorList .= $error."<br>";
    }
  }
}//end if click save


    $sqlretriveAR = "select ID ,label,paragraph,image,updateDate,updateUsername,status from info_takeachance_ar where ID=3";
    $executeSqlAR = mysqli_query($connection,$sqlretriveAR);
    $retriveDataAR = mysqli_fetch_row($executeSqlAR);
    $sqlretriveEN = "select ID ,label,paragraph,image,updateDate,updateUsername,status from info_takeachance_en where ID=3";
    $executeSqlEN = mysqli_query($connection,$sqlretriveEN);
    $retriveDataEN = mysqli_fetch_row($executeSqlEN);

?>
<!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-label">العنوان الرئيسي</label>
                            <input type="text" class='form-control' name="ar_label" value="<?php echo $retriveDataAR[1];?>">                      
                        </div>
                        <!-- end form group -->
                    </div>
                    <!-- end form row -->
                    <div class='form-row'>
                        <div class='form-group col'>
                            <label for="ar-paragraph">الفقرة</label>
                            <textarea class='form-control' rows="3" name="ar_paragraph" style='resize:none;'><?php echo $retriveDataAR[2];?></textarea>                 
                        </div>
                        <!-- end form group -->
                    </div>
                    <!-- end form row -->
                    <div class='form-row'>
                      <div class='form-group col'>
                            <label class='en-info' for="en-info">المعلومات باللغة الإنجليزية</label>                        
                        </div>
                        <!-- end form group -->
                    </div>
                    <!-- end form row -->
                    <div class='form-row'>
                      <div class='form-group col'>
                            <label for="en-label">العنوان الرئيسي</label>
                            <input type="text" class='form-control' name="en_label" style='text-align:left;direction:ltr;' value="<?php echo $retriveDataEN[1];?>">                      
                        </div>
                        <!-- end form group -->
                    </div>
                    <!-- end form row -->
                    <div class='form-row'>
                      <div class='form-group col'>
                            <label for="en-paragraph">الفقرة</label>
                            <textarea class='form-control' rows="3" name="en_paragraph" style='resize:none;text-align:left;direction:ltr;'><?php echo $retriveDataEN[2];?></textarea>                 
                        </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[3]?>' 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>
                    <!-- End form row -->
                    <div calss='form-row'>
                        <div class="form-group col text-center">
                            <button type="submit" name="save" class="save-image">حفظ</button>
                        </div>
                    </div>
                    <!-- end form row -->
                    </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'; ?>

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