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

$direction = "../assets/img/news/";
$Data = "";


if(isset($_GET["type"]) && isset($_GET["ID"]))
{
$id = $_GET["ID"];
$delete = "update news set status = 1 WHERE ID={$id}";
$executeSql = mysqli_query($connection,$delete);
$_SESSION["rcovery"] = 1;
header('Location: show-news.php');
}
if(isset($_GET["Status"]) && isset($_GET["ID"]))
{
    if($_GET["Status"] == 'active')
        $status = 1;
    elseif($_GET["Status"] == 'deactive')
    {
        $status = 0;
    }
    $sqlUpdate = "update news set status={$status} where ID={$_GET["ID"]}";
    $executeSql = mysqli_query($connection,$sqlUpdate);
    if($_GET["Status"] == 'active')
    $_SESSION["updateActivate"] = 1;
elseif($_GET["Status"] == 'deactive')
{
    $_SESSION["updateActivate"] = 2;
}

}
// retrive all image from table
$newsSql = "select * from news where status = 3";
$executeSql = mysqli_query($connection,$newsSql);
while($retriveData = mysqli_fetch_assoc($executeSql))
{
    $Data .= 
    "
    <div class='col-12'>
        <div class='card mb-3 text-right'>
            <div class='row no-gutters'>
                <div class='col-md-3'>
                    <img src='$direction{$retriveData["image"]}' class='card-img' alt='...' style='max-width:300px;height:200px;'>
                </div>
                <div class='col-md-6'>
                    <div class='card-body'>
                        <h5 class='card-title'>{$retriveData["newsName_ar"]}</h5>
                        <p class='card-text'>{$retriveData["summary_ar"]}</p>
                    </div>
                </div>
                <div class='col-md-3' style='padding: 10px;'>
                    <button type='button' class='btn btn-info btn-block view-news' id='{$retriveData["ID"]}' data-toggle='modal' data-target='#showNews'>عرض التفاصيل</button>
                    <a href='show-deleteNews.php?ID={$retriveData["ID"]}&type=recovery' class='btn btn-info btn-block'>إستعادة</a>
                </div>
            </div>
            <!-- End row -->
            <div class='row no-gutters'>
                <div class='col-md-12'>
                    <div class='card-footer text-muted text-center'>
                        بواسطة : {$retriveData["insertUsername"]}
                         | 
                        بتاريخ : {$retriveData["insertDate"]}
                    </div>
                </div>
            </div>
            <!-- End row -->
        </div>
        <!-- End card -->
    </div>
    <!-- End col -->
    ";
}
?>
<!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-3 text-center col-news">
                <h3>الأخبار</h3>
            </div>
        </div>
        <!-- End row -->

        <div class='row'>

            <?php
                echo $Data;
            ?>

        </div>
        <!-- End row -->
    </div>
    <!-- End container -->
</div>
<!-- End content body -->
<!-- assets / js -->
    <?php include 'jsFile.php'; ?>
    <script>
    $(document).ready(function(){
    $('.view-news').click(function(){
        var newsID = $(this).attr("id");
        $.ajax({
            url: "showsinglenews.php",
            method:"post",
            data:{newsID,newsID},
            success:function(data)
            {
                $('#show-news').html(data);
                $('#showNews').model("show");
            }
        });
    });
});

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

<!-- Modal -->
<div class="modal fade " id="showNews" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" dir='rtl'>
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">عرض تفاصيل الخبر</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body" id='show-news'>
        
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" data-dismiss="modal" style='margin-right: .25rem;'>الغاء</button>
      </div>
    </div>
  </div>
</div>