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

$query = '';
$querygt = '';
$column = array("newspost.nwtitle","newspost.dated");
$output = array();

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

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

if(isset($_POST["order"]))
{
	$query .= 'ORDER BY '.$column[$_POST['order']['0']['column']].' '.$_POST['order']['0']['dir'].' ';
}
else
{
	$query .= 'ORDER BY newspost.dated DESC ';
}

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['nw_id'];	
	$gt_imgs = $row['nwimg'];
	$prd_img = (!empty($gt_imgs) && file_exists('../../post_files/'.$gt_imgs)) ? "../post_files/".$gt_imgs : '../img/kmart-icon.png';
	
	$sub_array = array();

	$sub_array[] = '
	
	<div class="smlinf">
		<div class="prdinfo">
			<div class="image d-flex align-item-center">
				<a href="../#?prd='.$row['nwurl'].'" target="t_blank">
					<img src="'.$prd_img.'" alt="'.utf8_encode($row['nwtitle']).'" class="img-fluid" />
				</a>
			</div>
			
			<div class="content py-0">
				<h5 class="name">
					<a href="../#?prd='.$row['nwurl'].'" class="text-primary font-weight-bold" target="t_blank">'.utf8_encode($row['nwtitle']).'</a>
				</h5>
			</div>
		</div>
	</div>';
	
	
	$sub_array[] = '<p class="m-0">'.utf8_encode($row['newsbrief']).'</p>';
	
	$sub_array[] = date('jS-M-Y', strtotime($row['dated']));

	$sub_array[] = '
		<div class="table-data-feature text-center">
												
		<a href="../?prd='.$row['nwurl'].'" target="t_blank" class="bg-info text-white bs-tooltip border-0 btn rounded-circle" title="Read">
			<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-external-link"><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"></path>
			<polyline points="15 3 21 3 21 9"></polyline><line x1="10" y1="14" x2="21" y2="3"></line></svg>
		</a>
		
		<a href="?setup=edit-post&prd='.$prd_id.'" class="bg-secondary text-white bs-tooltip border-0 btn rounded-circle" title="Edit">
			<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" 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>
		
		<button class="bg-danger text-white bs-tooltip border-0 btn rounded-circle" id="del_'.$prd_id.'" onClick="takeoff('.$prd_id.',\'vctytl\')"  title="Delete">
			<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" 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>
		</button>
		
	</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 newspost WHERE delete_status = 0 ");
	$statement->execute();
	return $statement->rowCount();
}

?>