<?php
require_once('config.php');
header('Content-Type: application/json');



if(isset($_POST['postTypeSow']) and $_POST['postTypeSow'] !=''){
    $target_posts = $_POST['postTypeSow'];

    if($target_posts == 'all' || $target_posts == ''){

      $tt_article_post = showAllttdata("mahamudfutureit_article", " ORDER BY  PostDatePublic  DESC ");

    }elseif($target_posts == 'published'){

      $tt_article_post = showAllttdata("mahamudfutureit_article", "WHERE PostStatus ='published' ORDER BY  PostDatePublic  DESC ");

    }elseif($target_posts == 'users'){

      $tt_article_post = showAllttdata("mahamudfutureit_article", "WHERE PostAdmin = '' ORDER BY  PostDatePublic  DESC ");

    }elseif($target_posts == 'pending'){

      $tt_article_post = showAllttdata("mahamudfutureit_article", "WHERE PostStatus ='pending' ORDER BY  PostDatePublic  DESC ");

    }elseif($target_posts == 'draft'){

      $tt_article_post = showAllttdata("mahamudfutureit_article", "WHERE PostStatus ='draft' ORDER BY  PostDatePublic  DESC ");

    }elseif($target_posts == 'rejected'){

      $tt_article_post = showAllttdata("mahamudfutureit_article", "WHERE PostStatus ='rejected' ORDER BY  PostDatePublic  DESC ");

    }
}



// Initialize an empty array
$data = [];

foreach ($tt_article_post as $the_article) {

  if ($the_article['PostStatus'] == 'pending') {
    $art_status_now = '<span class="bg-warning  text-white  rounded-pill  d-inline-block" style="font-size: 13px; padding: 3px 16px 0; ">Pending<span>';
  }
  elseif ($the_article['PostStatus'] == 'published') {
    $art_status_now = '<span class="bg-success text-white   rounded-pill  d-inline-block" style="font-size: 13px; padding: 3px 16px 0; ">Published<span> <a target="_blank" href="'.$site_domain.'mahamudfutureit-photocard/'.$the_article['PostId'].'/" class="bg-primary ms-2 text-white   rounded-pill  d-inline-block" style="font-size: 13px; padding: 3px 16px 0; "><i class="bi bi-download"></i></a>';
  }
  elseif ($the_article['PostStatus'] == 'draft') {
    $art_status_now = '<span class="bg-secondary text-white   rounded-pill  d-inline-block" style="font-size: 13px; padding: 3px 16px 0; ">Draft<span>';
  }
  elseif ($the_article['PostStatus'] == 'rejected') {
    $art_status_now = '<span class="bg-danger text-white   rounded-pill  d-inline-block" style="font-size: 13px; padding: 3px 16px 0; ">Rejected<span>';
  }

  if ($the_article['PostStatus'] == 'published') {
    $art_title = '<a target="_blank" href="'.articleLink($the_article['PostId']).'">'.$the_article['PostTitle'].' - '.$art_status_now.'</a>';
  }else {
    $art_title = $the_article['PostTitle']." - ".$art_status_now;
  }
  $data[] = [
      "ptitle" => $art_title ,
      "pdate" => ttdShow($the_article['PostDate']),
      "pcategory" => getSingleValue('mahamudfutureit_category', 'CategoryID', $the_article['PostCategory'], 'CategoryName') ,
      "pauthor" => getSingleValue('tt_accounts', 'UserID', $the_article['PostAuthor'], 'UserName'),
      "viewcount" => $the_article['ViewCount'],
      "paction" => '
                  <div class="text-end action-btn">
                  <span class="d-none">'.$the_article['PostId'].' '.$the_article['PostAuthor'].'</span>
                    <a class="bg-primary" href="article-edit.php?article_id='.$the_article['PostId'].'"><i class="bi bi-pencil"></i></a>
                    <a class="bg-danger actiion_to_delete" href="#" data-href="article.php?delid='.$the_article['PostId'].'"><i class="bi bi-trash3"></i></a>
                  </div>
                  '
  ];
}
// Output the data in JSON format
echo json_encode(["data" => $data]);
