<?php
require_once('config.php');

// Delete Category
if (isset($_GET['delid'])) {

  if ($c_user_ud == 'active') {

      $id_for_delete = $_GET['delid'] ;

      // Get Post Image Folder
      $date_offpost = new DateTime(getSingleValue('mahamudfutureit_article', 'PostId', $id_for_delete, 'PostDate'));
      $year_offpost = $date_offpost->format('Y');
      $month_offpost = $date_offpost->format('m');

      //  Get File Location
      $post_images_fd = array(
                          "../uploads/".$year_offpost."/".$month_offpost."/".getSingleValue('mahamudfutureit_article', 'PostId', $id_for_delete, 'PostImage'),
                          "../uploads/".$year_offpost."/".$month_offpost."/".getSingleValue('mahamudfutureit_article', 'PostId', $id_for_delete, 'PostImageOG')
                        );


      if (deleteData('mahamudfutureit_article', 'PostId', $id_for_delete)){

        foreach ($post_images_fd as $file_oftpost) {
            if (file_exists($file_oftpost)) {
                unlink($file_oftpost);
            }
        }

        header("Location:article.php");
        exit;
      }else {
        $tt_alert = ttAlert('danger', 'Something went wrong to delete Article !');
      }
  }else {
    $tt_alert = ttAlert('danger', 'You Cannot Delete This Post !');
  }


}


// Show Post By Type
if (isset($_GET['from'])) {
  $target_posts = ($_GET['from'] == '') ? 'all' : $_GET['from'];
}else{
  $target_posts =  'all'  ;
}

if($target_posts == 'all'){
  $my_page_title = 'All Article';
}
elseif($target_posts == 'published'){
  $my_page_title = 'Published Article';
}
elseif($target_posts == 'pending'){
    $my_page_title = 'Pending Article';
}
elseif($target_posts == 'draft'){
  $my_page_title = 'Draft Article';
}
elseif($target_posts == 'rejected'){
  $my_page_title = 'Rejected Article';
}
elseif($target_posts == 'users'){
  $my_page_title = 'Users Article';
}
else{
    header("Location:404.php");
    exit;
}

// Include Heaser File
require_once('inc/header.php');
?>
<main id="main" class="main ">
  <div class="row">
    <div class="col-6 align-items-center">
      <div class="pagetitle mb-0">
        <h1><?php echo $my_page_title ?></h1>
      </div>
    </div>
    <div class="col-6 text-end">
        <a href="article-new.php" class="btn btn-primary btn-sm"><i class="bi bi-node-plus"></i> New Article </a>
    </div>
  </div>

  <?php     echo $tt_alert ?? ''; ?>

  <section class="section mt-4">
    <div class="bg-white rounded-2">
      <table id="MahamudFutureITDTable" class="display" style="width:100%">
        <thead>
          <tr>
            <th>Title</th>
            <th>Date</th>
            <th>Category</th>
            <th>Author</th>
            <th>View</th>
            <th>Actions</th>
          </tr>
        </thead>
      </table>
      <script>
          $(document).ready(function () {
              $('#MahamudFutureITDTable').DataTable({
                  ajax: {
                      url: 'article-server.php',
                      type: 'POST',
                      data: function (d) {
                      d.postTypeSow = '<?php echo $target_posts ?>';
                  }
                  },
                  columns: [
                      { data: 'ptitle' },
                      { data: 'pdate' },
                      { data: 'pcategory' },
                      { data: 'pauthor' },
                      { data: 'viewcount' },
                      { data: 'paction' },
                  ],
                  order: [],
                  responsive: true,
              });
          });
      </script>
    </div>
  </section>
</main>
<script>
  document.addEventListener("click", function(event) {
    // Check if the clicked element or its parent has the class 'actiion_to_delete'
    const target = event.target.closest(".actiion_to_delete");

    if (target) {
      event.preventDefault(); // Prevent default link behavior
      const url = target.getAttribute("data-href");

      if (confirm("Are you sure you want to delete this item?")) {
        window.location.href = url;
      }
    }
  });
</script>

<?php require_once('inc/footer.php'); ?>
