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

$query = '';
$column = array("curicular_extraacts.nwtitle","curicular_extraacts.nwdate");
$output = array();

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

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


if(isset($_POST["order"]))
{
	$query .= 'ORDER BY '.$column[$_POST['order']['0']['column']].' '.$_POST['order']['0']['dir'].' ';
}
else
{
	$query .= 'ORDER BY curicular_extraacts.nwdate 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['cr_id'];	
	$prd_img = (!empty($row['nwimg']) && file_exists('../../img/shield-extras/'.$row['nwimg'])) ? $row['nwimg'] : 'logo-badge.png';
	
	$sub_array = array();

	$sub_array[] = '
	
	<div class="smlinf">
		<div class="prdinfo">
			<div class="image d-flex align-item-center">
				<a href="../news?preg='.$row['nwurl'].'" target="t_blank">
					<img src="../img/shield-extras/'.$prd_img.'" alt="'.utf8_encode($row['nwtitle']).'" class="img-fluid" />
				</a>
			</div>
			
			<div class="content py-0">
				<h5 class="name">
					<a href="../news?preg='.$row['nwurl'].'" class="text-primary font-weight-bold" target="t_blank">'.utf8_encode($row['nwtitle']).'</a>
				</h5>
				
				<span class="email text-dark font-weight-bold">'.utf8_encode($row['newsbrief']).'</span>
			</div>
		</div>
	</div>';
	$sub_array[] = '<div class="text-center font-weight-bold">'.utf8_encode($row['nwdept']).'</div>';
	$sub_array[] = '<div class="text-center">'.ucwords($row['nwby']).'</div>';
	$sub_array[] = '<div class="text-center">'.date('jS-M-y', strtotime($row['nwdate'])).'</div>';
	$sub_array[] = '<div class="text-center">'.date('jS-M-y', strtotime($row['dated'])).'</div>';
	
	$sub_array[] = '
		<div class="dropdown custom-dropdown vertical-align text-center">
		<a class="dropdown-toggle bg-danger text-white border-0 btn rounded-circle" href="#" role="button" id="dropdownMenuLink7" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
				<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-more-horizontal"><circle cx="12" cy="12" r="1"></circle><circle cx="19" cy="12" r="1"></circle><circle cx="5" cy="12" r="1"></circle></svg>
			</a>
			
			<div class="dropdown-menu" aria-labelledby="dropdownMenuLink7">
				
				<a class="dropdown-item" href="../extracuricular?preg='.$row['nwurl'].'">
				<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-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> View </a>
				
				<a class="dropdown-item" href="?preg=edit-record&prd='.$prd_id.'">
				<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> Edit </a>
				
				<a class="dropdown-item" href="javascript:void(0);" id="del_'.$prd_id.'" onClick="takeoff('.$prd_id.',\'lynfhe\')" >
				<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> Delete</a>
			
			</div>
		</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 curicular_extraacts WHERE delete_status = 0");
	$statement->execute();
	return $statement->rowCount();
}

?>