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

$direction = "../assets/img/news/";
$flag = true;
$flagImage = false;

$errors = array();
$errorList = "";
$news_ar = isset($_POST["news_ar"]) ? $_POST["news_ar"] : "";
$news_en = isset($_POST["news_en"]) ? $_POST["news_en"] : "";
$ar_paragraph = isset($_POST["ar_paragraph"]) ? $_POST["ar_paragraph"] : "";
$en_paragraph = isset($_POST["en_paragraph"]) ? $_POST["en_paragraph"] : "";
$ar_summary = isset($_POST["ar_summary"]) ? $_POST["ar_summary"] : "";
$en_summary = isset($_POST["en_summary"]) ? $_POST["en_summary"] : "";
$active = isset($_POST["switch"]) ? $_POST["switch"] : 0;
$news_ar = addslashes($news_ar);
$news_en = addslashes($news_en);
$ar_paragraph = addslashes($ar_paragraph);
$en_paragraph = addslashes($en_paragraph);
$ar_summary = addslashes($ar_summary);
$en_summary = addslashes($en_summary);
if(isset($_POST["save"]))
{
  //check method used
  if ($_SERVER['REQUEST_METHOD'] === 'POST')
  { 
    //nothing
  }
  else
  {
    header( 'Location: show-news.php');
  }
  if(!empty($_FILES["image"]["name"])){
      $image = basename($_FILES["image"]["name"]);
      $image = date('h:i:sa').$image;
      $flagImage = true;
      $target_file = $direction.$image;
      $image_Type = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
      if($image_Type != "jpg" && $image_Type != "png" && $image_Type != "jpeg" && $image_Type != "gif" && $image_Type != "jfif")
      {
          $errors["imageType"] = "يجب أن تكون الصورة بأمتداد (jpg,jpeg,png,gif)";
          $flag = false;
      }
      elseif($_FILES["image"]["size"] > 5000000)
      {
          $errors["imageSize"] = "يجب أن يكون حجم الصورة أقل من 5 ميجا بايت";
          $flag = false;
      }
  }
  else{
      $image = $_POST["imageName"];
  }

  if(empty($news_ar) || empty($news_en) || 
     empty($ar_paragraph) || empty($en_paragraph) ||
     empty($ar_summary) || empty($en_summary))
  {
    $errors["empty"] = "يرجى تعبئة جميع الحقول";
    $flag = false; 
  }  //check if any textbox empty
  

    if($flag === true)
  {
      if($flagImage === true)
      {

        if(!move_uploaded_file($_FILES["image"]["tmp_name"],$target_file))
        {
          $errors["imageUpload"] = "يرجى إعادة رفع الصورة";
        }
        else
        {
          $date = date("Y-m-d");
          $UpdatetSql = "UPDATE news SET newsName_ar='$news_ar',content_ar='$ar_paragraph',summary_ar='$ar_summary',newsName_en='$news_en',content_en='$en_paragraph',summary_en='$en_summary',image='$image',updateUsername='{$_SESSION["username"]}',status='$active' WHERE ID = {$_GET["ID"]}";
          $executeSql = mysqli_query($connection,$UpdatetSql);
          $_SESSION["UpdateNews"] = 1;
          header( 'Location: show-news.php');  
        }//if check store image or not
      }
      else
      {
        $date = date("Y-m-d");
        $UpdatetSql = "UPDATE news SET newsName_ar='$news_ar',content_ar='$ar_paragraph',summary_ar='$ar_summary',newsName_en='$news_en',content_en='$en_paragraph',summary_en='$en_summary',image='$image',updateUsername='{$_SESSION["username"]}',status='$active' WHERE ID = {$_GET["ID"]}";
        $executeSql = mysqli_query($connection,$UpdatetSql);
        $_SESSION["UpdateNews"] = 1;
        header( 'Location: show-news.php');  
    }

  }//if for store image in file
  else
  {
    foreach($errors as $error)
    {
      $errorList .= $error."<br>";
    }
  }
}//end if click save

unset($_SESSION["IDNews"]);
if(isset($_GET["ID"]))
{
    
    $sqlDataAr = "select * from news where ID={$_GET["ID"]}";
    $executeSql = mysqli_query($connection,$sqlDataAr);
    $data = mysqli_fetch_assoc($executeSql);
    $rertiveData = $data; 
//     $sqlDataEN = "select ID,newsName,contentNews,image,summary from news_en where ID={$_GET["ID"]}";
//     $executeSql = mysqli_query($connection,$sqlDataEN);
//     $rertiveDataEN = mysqli_fetch_row($executeSql);
}
else
{
    header('Location: show-news.php');
}
?>
<!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-md-10'>
        <?php
            if($flag === false)
            {
            echo "
            <div class='alert alert-danger text-right'>
            $errorList
            </div>";
            }
        ?>
    <form  action="edit-news.php?ID=<?php echo $_GET["ID"]; ?>" method='post' enctype="multipart/form-data">
            <div class="card text-right" id="add-info">
                <div class="card-header"><h3>تعديل الخبر</h3></div>
                <div class="card-body">
                    <div class='form-row'>
                        <div class='form-group col-md'>
                            <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-md'>
                            <label for="news_ar">إسم الخبر</label>       
                            <input type='text' class='form-control' name='news_ar' value='<?php echo $rertiveData["newsName_ar"];?>'>                 
                        </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 $rertiveData["content_ar"];?></textarea>                 
                        </div>
                        <!-- end form group -->
                    </div>
                    <!-- end form row -->
                    <div class='form-row'>
                        <div class='form-group col'>
                            <label for="ar-summary">ملخص الخبر</label>
                            <textarea class='form-control' rows="2" name="ar_summary" style='resize:none;'><?php echo $rertiveData["summary_ar"];?></textarea>                 
                        </div>
                        <!-- end form group -->
                    </div>
                    <!-- end form row -->
                    <div class="form-row">
                    <div class='form-group col-md'>
                            <label class='en-info' for="en-info">المعلومات باللغة الإنجليزية</label>                        
                        </div>
                        <!-- End form-group -->
                    </div>
                    <div class="form-row">
                      <div class='form-group col-md'>
                            <label for="news_en">إسم الخبر</label>       
                            <input type='text' class='form-control' name='news_en' style='text-align:left;direction:ltr;' value='<?php echo $rertiveData["newsName_en"];?>'>                                        
                        </div>
                        <!-- End form-group -->
                    </div>
                    <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 $rertiveData["content_en"];?></textarea>                 
                        </div>
                        <!-- end form group -->
                    </div>
                    <div class="form-row">
                      <div class='form-group col'>
                            <label for="en-summary">ملخص الخبر</label>
                            <textarea class='form-control' rows="2" name="en_summary" style='resize:none;text-align:left;direction:ltr;'><?php echo $rertiveData["summary_en"];?></textarea>                 
                        </div>
                        <!-- end form group -->
                    </div>
                    <div class='form-row'>
                        <div class='form-group col'>
                        	<input type="hidden" name="imageName" value="<?= $rertiveData["image"]?>">
                        <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.$rertiveData["image"]?>' 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'>
                            <div dir='ltr'>
                                <span class="margin-r">تفعيل</span>
                                <label class="el-switch el-switch-green">
                                    <input type="checkbox" id='checkbox' value='1' checked name="switch">
                                    <span class="el-switch-style"></span>
                                </label>
                            </div>
                        </div>
                        <!-- end form group -->
                    </div>
                    <!-- end form row -->
                    <div class="form-row">
                        <div class="form-group col text-center">
                        <button type="submit" name="save" class="save-image">حفظ</button>

                        </div>
                    </div>
                </div>
                <!-- End card-body -->
            </div>
            <!-- End card -->
    </form>
        </div>
        <!-- End col-md -->
    </div>
    <!-- End row -->
</div>
<!-- End container -->
</div>
<!-- End content-body -->
<!-- assets / js -->
    <?php include 'jsFile.php'; ?>
  </body>
</html>
<!-- onload="reloader()" -->
