<?php 
include '../connection.php'; 
if(!isset($_SESSION)) session_start();
if(!isset($_SESSION["ID"])){header('Location: index.php');}
$direction = "../assets/img/infoTakeachance/";
$Data = "";
$untype=0;
if(isset($_GET["id"]) && isset($_GET["type"]))
{
    $id = $_GET["id"];
    $type= $_GET["type"];
    switch($type)
    {
        case 'delete':
        $type = 0;
        break;
        case 'active' :
        $type = 1;
        default:
        header( 'Location: ../admin/show-info.php');  
    }
    if($type == 1)
    {
        $updateSql = "update info_takeachance_ar set status=0";
        $executeSql = mysqli_query($connection,$updateSql);
        $updateSql = "update info_takeachance_en set status=0";
        $executeSql = mysqli_query($connection,$updateSql);
        $updateSql = "update info_takeachance_ar set status={$type} where ID={$id}";
        $executeSql = mysqli_query($connection,$updateSql);
        $updateSql = "update info_takeachance_en set status={$type} where ID={$id}";
        $executeSql = mysqli_query($connection,$updateSql);
        $_SESSION["active"] = 1;
    }
    elseif($type ==0)
    {
        $sqlDelete = "DELETE FROM info_takeachance_ar WHERE ID={$id}";
        $executeSql = mysqli_query($connection,$sqlDelete);
        $sqlDelete = "DELETE FROM info_takeachance_en WHERE ID={$id}";
        $executeSql = mysqli_query($connection,$sqlDelete);
        $_SESSION["delete"] = 1;
    }

}
// retrive all image from table
$newsSql = "select ID ,label,paragraph,image,updateDate,updateUsername,status from info_takeachance_ar order by ID DESC";
$executeSql = mysqli_query($connection,$newsSql);
while($retriveData = mysqli_fetch_row($executeSql))
{
    if($retriveData[6] == 1)
    {
        $action = "";
    }
    elseif($retriveData[6] == 0)
    {
        $action = "<a href='show-info.php?id={$retriveData[0]}&type=active' class='btn btn-success btn-block'>تفعيل</a>
        <a href='show-info.php?id={$retriveData[0]}&type=delete' class='btn btn-danger btn-block'>حذف</a>";
    }
    $Data .= 
    "
    <div class='col-12 text-right'>
        <div class='card mb-3'>
            <div class='row no-gutters'>
                <div class='col-md-3'>
                    <img src='$direction{$retriveData[3]}' 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[1]}</h5>
                        <p class='card-text' style='text-align:justify'>{$retriveData[2]}</p>
                    </div>
                </div>
                <div class='col-md-3' style='padding: 10px;'>
                    <button type='button' class='btn btn-info btn-block view-info' id='{$retriveData[0]}' data-toggle='modal' data-target='#showInfo'>عرض التفاصيل</button>
                    {$action}
                    <a href='edit-info.php' class='btn btn-warning 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[5]}
                         | 
                        بتاريخ : {$retriveData[4]}
                    </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-about-takeachance">
                <h3>عن خذ فرصة</h3>
            </div>
            
        </div>
        <!-- End row -->

        <div class='row' style='margin-top:50px;'>

            <?php
                echo $Data;
            ?>

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

    </script>
    <script>

<?php
if(!isset($_SESSION))
  session_start();
if(isset($_SESSION["updateinfo"]) && $_SESSION["updateinfo"] == 1)
{ 
?>
Swal.fire(
  'تم تعديل المعلومات بنجاح',
  '',
  'success'
)
<?php
$_SESSION["updateinfo"] = 0;
}
?>

  </script>

      <script>

<?php
if(!isset($_SESSION))
  session_start();
if(isset($_SESSION["delete"]) && $_SESSION["delete"] == 1)
{ 
?>
Swal.fire(
  'تم الحذف بنجاح',
  '',
  'error'
)
<?php
$_SESSION["delete"] = 0;
}
?>

  </script>
      <script>

<?php
if(!isset($_SESSION))
  session_start();
if(isset($_SESSION["active"]) && $_SESSION["active"] == 1)
{ 
?>
Swal.fire(
  'تم التفعيل بنجاح',
  '',
  'success'
)
<?php
$_SESSION["active"] = 0;
}
?>

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


<!-- Modal -->
<div class="modal fade " id="showInfo" 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-info'>
        
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" data-dismiss="modal" style='margin-right: .25rem;'>الغاء</button>
      </div>
    </div>
  </div>
</div>