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

$errors = array();
$flag = true;
$flagImage = false;
$direction = "../assets/img/our-program/";
$image = "";
$errorList = "";
$labelAbout_ar = isset($_POST["labelAbout_ar"]) ? $_POST["labelAbout_ar"] : "";
$contentAbout_ar = isset($_POST["contentAbout_ar"]) ? $_POST["contentAbout_ar"] : "";
$labelNumber_ar = isset($_POST["labelNumber_ar"]) ? $_POST["labelNumber_ar"] : "";
$contentNumber_ar = isset($_POST["contentNumber_ar"]) ? $_POST["contentNumber_ar"] : "";
$labelach_ar = isset($_POST["labelach_ar"]) ? $_POST["labelach_ar"] : "";

$labelAbout_en = isset($_POST["labelAbout_en"]) ? $_POST["labelAbout_en"] : "";
$contentAbout_en = isset($_POST["contentAbout_en"]) ? $_POST["contentAbout_en"] : "";
$labelNumber_en = isset($_POST["labelNumber_en"]) ? $_POST["labelNumber_en"] : "";
$contentNumber_en = isset($_POST["contentNumber_en"]) ? $_POST["contentNumber_en"] : "";
$labelach_en = isset($_POST["labelach_en"]) ? $_POST["labelach_en"] : "";

if(isset($_POST["save"]))
{
    if(empty($labelAbout_ar) || empty($labelAbout_en)
    || empty($contentAbout_ar) || empty($contentAbout_en)
    || empty($labelNumber_ar) || empty($labelNumber_en)
    || empty($contentNumber_ar) || empty($contentNumber_en)
    || empty($labelach_ar) || empty($labelach_en))
    {
        $errors["empty"] = "الرجاء تعبئة جميع الحقول";
        $flag = false;
    }
    for($i = 0 ; $i <= 7 ; $i++)
    {
        if(!isset($labelNumber_ar[$i]) || empty($labelNumber_ar[$i]))
        {
            $errors["empty"] = "الرجاء تعبئة جميع الحقول";
            $flag = false;
        }
        if(!isset($labelNumber_en[$i]) || empty($labelNumber_en[$i]))
        {
            $errors["empty"] = "الرجاء تعبئة جميع الحقول";
            $flag = false;
        }
        if(!isset($contentNumber_ar[$i]) || empty($contentNumber_ar[$i]))
        {
            $errors["empty"] = "الرجاء تعبئة جميع الحقول";
            $flag = false;
        }
        if(!isset($contentNumber_en[$i]) || empty($contentNumber_en[$i]))
        {
            $errors["empty"] = "الرجاء تعبئة جميع الحقول";
            $flag = false;
        }
    }
    $image = basename($_FILES["image"]["name"]);
        $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 != "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)
    {
        $NumberStr =  implode("@",$labelNumber_ar);
        $counterStr =  implode("@",$contentNumber_ar);
        $NumberStrEN =  implode("@",$labelNumber_en);
        $counterStrEN =  implode("@",$contentNumber_en);
        if($flagImage === true)
        {
            if(!move_uploaded_file($_FILES["image"]["tmp_name"],$target_file))
            {
            $errors["imageUpload"] = "يرجى إعادة رفع الصورة";
            }
            else
            {
                $date = date("Y-m-d");
                $updateSql = "UPDATE programs_ar set labelAbout='{$labelAbout_ar}',contentAbout='{$contentAbout_ar}',labelNumber='{$NumberStr}',contentNumber='{$counterStr}',updateDate='{$date}',updateUsername='{$_SESSION["username"]}',image='{$image}',labelach='{$labelach_ar}' WHERE ID = 1";
                $executeSql = mysqli_query($connection,$updateSql);
                $updateSql = "UPDATE programs_en set labelAbout='{$labelAbout_en}',contentAbout='{$contentAbout_en}',labelNumber='{$NumberStrEN}',contentNumber='{$counterStrEN}',updateDate='{$date}',updateUsername='{$_SESSION["username"]}',image='{$image}',labelach='{$labelach_en}' WHERE ID = 1";
                $executeSql = mysqli_query($connection,$updateSql);
                $_SESSION["updateProgram"] = 1;
                header('Location: show-joodcom.php');
            } 
        }
        else
        {
            $date = date("Y-m-d");
            $updateSql = "UPDATE programs_ar set labelAbout='{$labelAbout_ar}',contentAbout='{$contentAbout_ar}',labelNumber='{$NumberStr}',contentNumber='{$counterStr}',updateDate='{$date}',updateUsername='{$_SESSION["username"]}',labelach='{$labelach_ar}' WHERE ID = 1";
            $executeSql = mysqli_query($connection,$updateSql);
            $updateSql = "UPDATE programs_en set labelAbout='{$labelAbout_en}',contentAbout='{$contentAbout_en}',labelNumber='{$NumberStrEN}',contentNumber='{$counterStrEN}',updateDate='{$date}',updateUsername='{$_SESSION["username"]}',labelach='{$labelach_en}' WHERE ID = 1";
            $executeSql = mysqli_query($connection,$updateSql);
            $_SESSION["updateProgram"] = 1;
            header('Location: show-joodcom.php');
        }
    }
    else
    {
        foreach($errors as $error)
        {
          $errorList .= $error."<br>";
        }
    }
}


$sqlretrive = "SELECT * FROM programs_ar WHERE ID = 1";
$executeSql = mysqli_query($connection,$sqlretrive);
$rowsJodcom = mysqli_fetch_row($executeSql);
$sqlretrive = "SELECT * FROM programs_en WHERE ID = 1";
$executeSqlEN = mysqli_query($connection,$sqlretrive);
$rowsJodcomEN = mysqli_fetch_row($executeSqlEN);
$arrLabelAr = explode("@" , $rowsJodcom[4]);
$arrCounterAr = explode("@" , $rowsJodcom[5]);
$arrLabelEn = explode("@" , $rowsJodcomEN[4]);
$arrCounterEn = explode("@" , $rowsJodcomEN[5]);
?>
<!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(isset($flag) && $flag === false)
            {
            echo "
            <div class='alert alert-danger'>
            $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-name">العنوان الأول</label>
                            <input type="text" name="labelAbout_ar" value="<?php echo $rowsJodcom[1];?>" class="form-control">                    
                        </div>
                        <!-- end form group -->
                    </div>
                    <!-- end form row -->
                    <div class='form-row'>
                        <div class='form-group col'>
                            <label for="ar-name">المحتوى</label>
                            <textarea name="contentAbout_ar" rows="4" class="form-control">
<?php echo $rowsJodcom[2];?>
                            </textarea>                     
                        </div>
                        <!-- end form group -->
                    </div>
                    <!-- end form row -->
                    <!-- end form row -->
                    <div class='form-row'>
                        <div class='form-group col'>
                            <label for="ar-name">العنوان الثاني</label>
                            <input type="text" name="labelach_ar" value="<?php echo $rowsJodcom[3];?>" class="form-control">                    
                        </div>
                        <!-- end form group -->
                    </div>
                    <div class='form-row'>
                        <div class='form-group col'>
                            <label for="ar-name">الإنجازات</label>
                            <input type="text" name="labelNumber_ar[]" value="<?php echo $arrLabelAr[0];?>" class="form-control">                     
                        </div>
                        <!-- end form group -->
                        <div class='form-group col'>
                            <label for="ar-name" style='color:#fff'>@</label>
                            <input type="text" name="labelNumber_ar[]" value="<?php echo $arrLabelAr[1];?>" class="form-control">                     
                        </div>
                        <!-- end form group -->
                        <div class='form-group col'>
                            <label for="ar-name" style='color:#fff'>@</label>
                            <input type="text" name="labelNumber_ar[]" value="<?php echo $arrLabelAr[2];?>" class="form-control">                     
                        </div>
                        <!-- end form group -->
                        <div class='form-group col'>
                            <label for="ar-name" style='color:#fff'>@</label>
                            <input type="text" name="labelNumber_ar[]" value="<?php echo $arrLabelAr[3];?>" class="form-control">                     
                        </div>
                        <!-- end form group -->
                    </div>
                    <!-- end form row -->
                    <div class='form-row'>
                        <div class='form-group col'>
                            <label for="ar-name">الأرقام</label>
                            <input type="text" name="contentNumber_ar[]" class="form-control" value='<?php echo $arrCounterAr[0];?>'>                     
                        </div>
                        <!-- end form group -->
                        <div class='form-group col'>
                            <label for="ar-name" style='color:#fff'>@</label>
                            <input type="text" name="contentNumber_ar[]" class="form-control" value='<?php echo $arrCounterAr[1];?>'>                     
                        </div>
                        <!-- end form group -->
                        <div class='form-group col'>
                            <label for="ar-name" style='color:#fff'>@</label>
                            <input type="text" name="contentNumber_ar[]" class="form-control" value='<?php echo $arrCounterAr[2];?>'>                     
                        </div>
                        <!-- end form group -->
                        <div class='form-group col'>
                            <label for="ar-name" style='color:#fff'>@</label>
                            <input type="text" name="contentNumber_ar[]" class="form-control" value='<?php echo $arrCounterAr[3];?>'>                     
                        </div>
                        <!-- end form group -->
                    </div>
                    <!-- end form row -->
                    <div class='form-row'>
                        <div class='form-group col'>
                            <label for="ar-name">الإنجازات</label>
                            <input type="text" name="labelNumber_ar[]" value="<?php echo $arrLabelAr[4];?>" class="form-control">                     
                        </div>
                        <!-- end form group -->
                        <div class='form-group col'>
                            <label for="ar-name" style='color:#fff'>@</label>
                            <input type="text" name="labelNumber_ar[]" value="<?php echo $arrLabelAr[5];?>" class="form-control">                     
                        </div>
                        <!-- end form group -->
                        <div class='form-group col'>
                            <label for="ar-name" style='color:#fff'>@</label>
                            <input type="text" name="labelNumber_ar[]" value="<?php echo $arrLabelAr[6];?>" class="form-control">                     
                        </div>
                        <!-- end form group -->
                        <div class='form-group col'>
                            <label for="ar-name" style='color:#fff'>@</label>
                            <input type="text" name="labelNumber_ar[]" value="<?php echo $arrLabelAr[7];?>" class="form-control">                     
                        </div>
                        <!-- end form group -->
                    </div>
                    <!-- end form row -->
                    <div class='form-row'>
                        <div class='form-group col'>
                            <label for="ar-name">الأرقام</label>
                            <input type="text" name="contentNumber_ar[]" class="form-control" value='<?php echo $arrCounterAr[4];?>'>                     
                        </div>
                        <!-- end form group -->
                        <div class='form-group col'>
                            <label for="ar-name" style='color:#fff'>@</label>
                            <input type="text" name="contentNumber_ar[]" class="form-control" value='<?php echo $arrCounterAr[5];?>'>                     
                        </div>
                        <!-- end form group -->
                        <div class='form-group col'>
                            <label for="ar-name" style='color:#fff'>@</label>
                            <input type="text" name="contentNumber_ar[]" class="form-control" value='<?php echo $arrCounterAr[6];?>'>                     
                        </div>
                        <!-- end form group -->
                        <div class='form-group col'>
                            <label for="ar-name" style='color:#fff'>@</label>
                            <input type="text" name="contentNumber_ar[]" class="form-control" value='<?php echo $arrCounterAr[7];?>'>                     
                        </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'style='direction:rtl;text-align:right;'>
                        <div class='form-group col' >
                            <label for="ar-name">العنوان الأول</label>
                            <input type="text" name="labelAbout_en" style='direction:ltr;text-align:left'  value="<?php echo $rowsJodcomEN[1];?>" class="form-control">                     
                        </div>
                        <!-- end form group -->
                    </div>
                    <!-- end form row -->
                    <div class='form-row'style='direction:rtl;text-align:right'>
                        <div class='form-group col'>
                            <label for="ar-name">المحتوى</label>
                            <textarea name="contentAbout_en" rows="4" style='direction:ltr;text-align:left' class="form-control">
<?php echo $rowsJodcomEN[2];?>
                            </textarea>                     
                        </div>
                        <!-- end form group -->
                    </div>
                    <!-- end form row -->
                    <div class='form-row'style='direction:rtl;text-align:right;'>
                        <div class='form-group col' >
                            <label for="ar-name">العنوان الثاني</label>
                            <input type="text" name="labelach_en" style='direction:ltr;text-align:left'  value="<?php echo $rowsJodcomEN[3];?>" class="form-control">                     
                        </div>
                        <!-- end form group -->
                    </div>
                    <!-- end form row -->
                    <div class='form-row'>
                        <div class='form-group col'>
                            <label for="ar-name">الإنجازات</label>
                            <input type="text" name="labelNumber_en[]" value="<?php echo $arrLabelEn[0];?>" class="form-control">                     
                        </div>
                        <!-- end form group -->
                        <div class='form-group col'>
                            <label for="ar-name" style='color:#fff'>@</label>
                            <input type="text" name="labelNumber_en[]" value="<?php echo $arrLabelEn[1];?>" class="form-control">                     
                        </div>
                        <!-- end form group -->
                        <div class='form-group col'>
                            <label for="ar-name" style='color:#fff'>@</label>
                            <input type="text" name="labelNumber_en[]" value="<?php echo $arrLabelEn[2];?>" class="form-control">                     
                        </div>
                        <!-- end form group -->
                        <div class='form-group col'>
                            <label for="ar-name" style='color:#fff'>@</label>
                            <input type="text" name="labelNumber_en[]" value="<?php echo $arrLabelEn[3];?>" class="form-control">                     
                        </div>
                        <!-- end form group -->
                    </div>
                    <!-- end form row -->
                    <div class='form-row'>
                        <div class='form-group col'>
                            <label for="ar-name">الأرقام</label>
                            <input type="text" name="contentNumber_en[]" class="form-control" value='<?php echo $arrCounterEn[0];?>'>                     
                        </div>
                        <!-- end form group -->
                        <div class='form-group col'>
                            <label for="ar-name" style='color:#fff'>@</label>
                            <input type="text" name="contentNumber_en[]" class="form-control" value='<?php echo $arrCounterEn[1];?>'>                     
                        </div>
                        <!-- end form group -->
                        <div class='form-group col'>
                            <label for="ar-name" style='color:#fff'>@</label>
                            <input type="text" name="contentNumber_en[]" class="form-control" value='<?php echo $arrCounterEn[2];?>'>                     
                        </div>
                        <!-- end form group -->
                        <div class='form-group col'>
                            <label for="ar-name" style='color:#fff'>@</label>
                            <input type="text" name="contentNumber_en[]" class="form-control" value='<?php echo $arrCounterEn[3];?>'>                     
                        </div>
                        <!-- end form group -->
                    </div>
                    <!-- end form row -->
                    <div class='form-row'>
                        <div class='form-group col'>
                            <label for="ar-name">الإنجازات</label>
                            <input type="text" name="labelNumber_en[]" value="<?php echo $arrLabelEn[4];?>" class="form-control">                     
                        </div>
                        <!-- end form group -->
                        <div class='form-group col'>
                            <label for="ar-name" style='color:#fff'>@</label>
                            <input type="text" name="labelNumber_en[]" value="<?php echo $arrLabelEn[5];?>" class="form-control">                     
                        </div>
                        <!-- end form group -->
                        <div class='form-group col'>
                            <label for="ar-name" style='color:#fff'>@</label>
                            <input type="text" name="labelNumber_en[]" value="<?php echo $arrLabelEn[6];?>" class="form-control">                     
                        </div>
                        <!-- end form group -->
                        <div class='form-group col'>
                            <label for="ar-name" style='color:#fff'>@</label>
                            <input type="text" name="labelNumber_en[]" value="<?php echo $arrLabelEn[7];?>" class="form-control">                     
                        </div>
                        <!-- end form group -->
                    </div>
                    <!-- end form row -->
                    <div class='form-row'>
                        <div class='form-group col'>
                            <label for="ar-name">الأرقام</label>
                            <input type="text" name="contentNumber_en[]" class="form-control" value='<?php echo $arrCounterEn[4];?>'>                     
                        </div>
                        <!-- end form group -->
                        <div class='form-group col'>
                            <label for="ar-name" style='color:#fff'>@</label>
                            <input type="text" name="contentNumber_en[]" class="form-control" value='<?php echo $arrCounterEn[5];?>'>                     
                        </div>
                        <!-- end form group -->
                        <div class='form-group col'>
                            <label for="ar-name" style='color:#fff'>@</label>
                            <input type="text" name="contentNumber_en[]" class="form-control" value='<?php echo $arrCounterEn[6];?>'>                     
                        </div>
                        <!-- end form group -->
                        <div class='form-group col'>
                            <label for="ar-name" style='color:#fff'>@</label>
                            <input type="text" name="contentNumber_en[]" class="form-control" value='<?php echo $arrCounterEn[7];?>'>                     
                        </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.$rowsJodcom[10]?>' 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 class='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()" -->
