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


foreach($_POST as $key => &$value){
    $value = addslashes($value);
}
$aboutus_ar = isset($_POST["aboutus_ar"]) ? $_POST["aboutus_ar"] : "";
$aboutus_en = isset($_POST["aboutus_en"]) ? $_POST["aboutus_en"] : "";
$ourvision_ar = isset($_POST["ourvision_ar"]) ? $_POST["ourvision_ar"] : "";
$ourvision_en = isset($_POST["ourvision_en"]) ? $_POST["ourvision_en"] : "";
$ourmessage_ar = isset($_POST["ourmessage_ar"]) ? $_POST["ourmessage_ar"] : "";
$ourmessage_en = isset($_POST["ourmessage_en"]) ? $_POST["ourmessage_en"] : "";
$ourgoals_ar = isset($_POST["ourgoals_ar"]) ? $_POST["ourgoals_ar"] : "";
$ourgoals_en = isset($_POST["ourgoals_en"]) ? $_POST["ourgoals_en"] : "";
$ourvalue_ar = isset($_POST["ourvalue_ar"]) ? $_POST["ourvalue_ar"] : "";
$ourvalue_en = isset($_POST["ourvalue_en"]) ? $_POST["ourvalue_en"] : "";
$active = isset($_POST["switch"]) ? $_POST["switch"] : 1;

$flag = true; 
$errors = array();
$errorList = "";


if(isset($_POST["save"]))
{
  //check method used
  if ($_SERVER['REQUEST_METHOD'] === 'POST')
  { 
    //nothing
  }
  else
  {
    // header( 'Location: ../ar/index.php');
  }
  if(empty($aboutus_ar) || empty($aboutus_en) || empty($ourvision_ar) || empty($ourvision_en) ||
     empty($ourmessage_ar) || empty($ourmessage_en) || empty($ourgoals_ar) || empty($ourgoals_en) ||
     empty($ourvalue_ar) || empty($ourvalue_en))
  {
    $errors["empty"] = "يرجى تعبئة جميع الحقول";
    $flag = false; 
  }  //check if any textbox empty
  if($flag === true)
  {
      $date = date("Y-m-d");
      $sqlInsert = "UPDATE aboutus_ar SET aboutus='{$aboutus_ar}',ourvision='{$ourvision_ar}',ourmessage='{$ourmessage_ar}',ourgoals='{$ourgoals_ar}',
      ourvalue='{$ourvalue_ar}',updateDate='$date',updateUsername='fatooh',status=$active WHERE ID = {$_SESSION["updateID"]}";
      $executeSql = mysqli_query($connection,$sqlInsert);
      $sqlInsert = "UPDATE aboutus_en SET aboutus='{$aboutus_en}',ourvision='{$ourvision_en}',ourmessage='{$ourmessage_en}',ourgoals='{$ourgoals_en}',
      ourvalue='{$ourvalue_en}',updateDate='$date',updateUsername='fatooh',status=$active WHERE ID = {$_SESSION["updateID"]}";
      $executeSql = mysqli_query($connection,$sqlInsert);
      $_SESSION["updatesuccess"] = 1;
      header( 'Location: show-aboutus.php');  
  }
  else
  {
    foreach($errors as $error)
    {
      $errorList .= $error."<br>";
    }
  }
}
if(!isset($_GET["ID"]) && !isset($_SESSION["updateID"]))
{
    header( 'Location: show-aboutus.php');  

}
elseif(isset($_GET["ID"]) && !isset($_SESSION["updateID"]))
{
    $ID = $_SESSION["updateID"] = $_GET["ID"];
}
elseif(!isset($_GET["ID"]) && isset($_SESSION["updateID"]))
{
    $ID = $_SESSION["updateID"];

}

    $ID = $_SESSION["updateID"] = 1;
    $sqlretriveAR = "SELECT ID, aboutus, ourvision, ourmessage, ourgoals, ourvalue, insertDate, insertUsername, status FROM aboutus_ar where ID={$ID}";
    $executeSqlAR = mysqli_query($connection,$sqlretriveAR);
    $retriveDataAR = mysqli_fetch_row($executeSqlAR);
    $sqlretriveEN = "SELECT ID, aboutus, ourvision, ourmessage, ourgoals, ourvalue, insertDate, insertUsername, status FROM aboutus_en where ID={$ID}";
    $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-aboutus">من نحن</label>
                            <textarea name="aboutus_ar" rows="3" style='resize:none' class="form-control"><?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-ourvision">رؤيتنا</label>
                            <textarea class='form-control' rows="3" name="ourvision_ar" 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 for="ar-message">رسالتنا</label>
                            <textarea class='form-control' rows="3" name="ourmessage_ar" style='resize:none;'><?php echo $retriveDataAR[3];?></textarea>                 
                        </div>
                        <!-- end form group -->

                    </div>
                    <!-- end form row -->
                    <div class='form-row'>
                        <div class='form-group col'>
                            <label for="ar-ourgoals">أهدافنا</label>
                            <textarea class='form-control' rows="3" name="ourgoals_ar" style='resize:none;'><?php echo $retriveDataAR[4];?></textarea>                 
                        </div>
                        <!-- end form group -->

                    </div>
                    <!-- end form row -->
                    <div class='form-row'>
                        <div class='form-group col'>
                            <label for="ar-ourvalue">قيمنا</label>
                            <textarea class='form-control' rows="3" name="ourvalue_ar" style='resize:none;'><?php echo $retriveDataAR[5];?></textarea>                 
                        </div>
                        <!-- end form group -->

                    </div>
                    <!-- end form row -->
                    <div calss='form-row'>
                    <div class='form-group col'>
                            <label class='en-info' for="en-info">المعلومات باللغة الإنجليزية</label>                        
                        </div>
                        <!-- end form group -->
                    </div>
                    <!-- end form row -->
                    <div calss='form-row'>
                    <div class='form-group col'>
                            <label for="en-aboutus">من نحن</label>
                            <textarea name="aboutus_en" rows="3" style='resize:none;direction:ltr;text-align:left' class="form-control"><?php echo $retriveDataEN[1];?></textarea>
                        </div>
                        <!-- end form group -->
                    </div>
                    <!-- end form row -->
                    <div calss='form-row'>
                    <div class='form-group col'>
                            <label for="en-ourvision">رؤيتنا</label>
                            <textarea class='form-control' rows="3" name="ourvision_en" style='resize:none;direction:ltr;text-align:left'><?php echo $retriveDataEN[2];?></textarea>                 
                        </div>
                        <!-- end form group -->
                    </div>
                    <!-- end form row -->
                    <div calss='form-row'>
                        <div class='form-group col'>
                            <label for="en-message">رسالتنا</label>
                            <textarea class='form-control' rows="3" name="ourmessage_en" style='resize:none;direction:ltr;text-align:left'><?php echo $retriveDataEN[3];?></textarea>                 
                        </div>
                        <!-- end form group -->
                    </div>
                    <!-- end form row -->
                    <div calss='form-row'>
                        <div class='form-group col'>
                            <label for="en-ourgoals">أهدافنا</label>
                            <textarea class='form-control' rows="3" name="ourgoals_en" style='resize:none;direction:ltr;text-align:left'><?php echo $retriveDataEN[4];?></textarea>                 
                        </div>
                        <!-- end form group -->
                    </div>
                    <!-- end form row -->
                    <div calss='form-row'>
                        <div class='form-group col'>
                            <label for="en-ourvalue">قيمنا</label>
                            <textarea class='form-control' rows="3" name="ourvalue_en" style='resize:none;direction:ltr;text-align:left'><?php echo $retriveDataEN[5];?></textarea>                 
                        </div>
                        <!-- end form group -->
                    </div>
                    <!-- end form row -->
                    <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()" -->
