<?php
include_once "../../includes/connectConfig.php";

$query = '';
$column = array("faqspost.fq_title","faqspost.fq_info");
$output = array();

$query .= "SELECT * FROM faqspost WHERE delete_status = '0' AND ";

if(isset($_POST["search"]["value"]))
{	
	$query .= '(faqspost.fq_title LIKE "%'.$_POST["search"]["value"].'%") ';
}


if(isset($_POST["order"]))
{
	$query .= 'ORDER BY '.$column[$_POST['order']['0']['column']].' '.$_POST['order']['0']['dir'].' ';
}
else
{
	$query .= 'ORDER BY faqspost.fq_id ASC ';
}

if($_POST["length"] != -1)
{
	$query .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
}

$statement = $db_con->prepare($query);

$statement->execute();

$result = $statement->fetchAll();

$data = array();

$filtered_rows = $statement->rowCount();

foreach($result as $row)
{
	$prd_id = $row['fq_id'];	
	$prd_img = 'logo-badge.png';
	
	$sub_array = array();

	$sub_array[] = '
	
	<div class="smlinf">
		<div class="prdinfo">
			<div class="image d-flex align-item-center">
				<a href="javascript:void(0);">
					<img src="../img/'.$prd_img.'" alt="shield logo" class="img-fluid" />
				</a>
			</div>
			
			<div class="content py-0">
				<h5 class="name">
					<a href="javascript:void(0);" class="text-primary font-weight-bold">'.utf8_encode($row['fq_title']).'</a>
				</h5>
				<span class="email text-dark font-weight-bold">'.utf8_encode($row['fq_info']).'</span>
			</div>
		</div>
	</div>';

	$sub_array[] = '
	    <div class="dropdown custom-dropdown vertical-align text-center">
			<a href="?preg=edit-faq&prd='.$prd_id.'" class="bg-info text-white bs-tooltip border-0 btn rounded-circle"  title="Edit Post">
			<svg xmlns="http://www.w3.org/2000/svg" width="18" height="24" viewBox="0 0 24 25" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" class="feather feather-edit-3">
			<path d="M12 20h9"></path><path d="M16.5 3.5a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4L16.5 3.5z"></path></svg></a>
		
		    <a  href="javascript:void(0);" id="del_'.$prd_id.'" onClick="takeoff('.$prd_id.',\'plwxs\')" class="bg-danger text-white bs-tooltip border-0 btn rounded-circle" title="Delete">
			<svg xmlns="http://www.w3.org/2000/svg" width="18" height="24" viewBox="0 0 24 25" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" class="feather feather-trash-2 table-cancel">
			<polyline points="3 6 5 6 21 6"></polyline><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path><line x1="10" y1="11" x2="10" y2="17"></line><line x1="14" y1="11" x2="14" y2="17"></line>
			</svg></a>
		</div>
	';
	
	$data[] = $sub_array;
}

$output = array(
	"draw"				=>	intval($_POST["draw"]),
	"recordsTotal"  	=>  $filtered_rows,
	"recordsFiltered" 	=> 	get_total_all_records($db_con),
	"data"    			=> 	$data
);
echo json_encode($output);

function get_total_all_records($db_con)
{
	$statement = $db_con->prepare("SELECT * FROM faqspost WHERE delete_status = 0");
	$statement->execute();
	return $statement->rowCount();
}

?>