<?php
    require('includes/config.php');

## Read value
$draw = $_POST['draw'];
$row = $_POST['start'];
$rowperpage = $_POST['length']; // Rows display per page
$columnIndex = $_POST['order'][0]['column']; // Column index
$columnName = $_POST['columns'][$columnIndex]['data']; // Column name
$columnSortOrder = $_POST['order'][0]['dir']; // asc or desc
$searchValue = $_POST['search']['value']; // Search value 
$primary_key = 'child_id';
$call_back   = 'secondary-category';
$table       = 'child_categories';

## Search 
$searchQuery = " ";
if($searchValue != ''){
	$searchQuery = " and ( PC.category_name like '%".$searchValue."%' or 
        SC.sub_category_name like '%".$searchValue."%' ) ";
}

## Total number of records without filtering
$sel = mysqli_query($con,"SELECT count(*) as allcount FROM child_categories SC JOIN parent_categories PC ON PC.category_id = SC.parent_id");
$records = mysqli_fetch_assoc($sel);
$totalRecords = $records['allcount'];

## Total number of records with filtering
$sel = mysqli_query($con,"SELECT count(*) as allcount FROM child_categories SC JOIN parent_categories PC ON PC.category_id = SC.parent_id WHERE 1 ".$searchQuery);
$records = mysqli_fetch_assoc($sel);
$totalRecordwithFilter = $records['allcount'];

## Fetch records
$childQuery = "SELECT SC.*, PC.category_name AS primary_category, PC.category_id AS primary_category_id FROM child_categories SC JOIN parent_categories PC ON PC.category_id = SC.parent_id WHERE 1 ".$searchQuery." order by ".$columnName." ".$columnSortOrder." limit ".$row.",".$rowperpage;
$childRecords = mysqli_query($con, $childQuery);
$data = array();

while ($row = mysqli_fetch_assoc($childRecords)) {
            $primaryCategoryId      = $row['primary_category_id'];
            $id                     = $row['child_id'];
            $primaryCategory        = $row['primary_category'];
            $subCategoryName        = $row['sub_category_name'];
            $childCategoryImgPath   = $row['child_category_img_path'];
            $status                 = $row['active']; 
            ( $row['active'] == 1 ) ? $active = 'Yes': $active = 'No';
    $data[] = array(
    		"category_name"       => $primaryCategory,
            "sub_category_name"   => $subCategoryName,    		
    		"category_img_path"   => $childCategoryImgPath,
    		"active"              => $active,
    		"created_at"          => date( 'd-m-Y', $row['created_at'] ),
            "edit"                => '<a href="javascript:void(0);" data-extension = "'.$extension.'" data-identifier = "'.$primary_key.'" data-id = '.$row['child_id'].' data-table = "'.$table.'" data-call_back = "'.$call_back.'" data-child_id = "'.$id.'"  data-primaryCategory = "'.$primaryCategoryId.'" data-subCategoryName = "'.$subCategoryName.'" data-childCategoryImgPath = "'.$childCategoryImgPath.'" data-status = "'.$status.'" class = "edit"><i class="fa fa-pencil"></i></a>',

            "delete"              => '<a href="javascript:void(0);" data-extension = "'.$extension.'" data-identifier = "'.$primary_key.'" data-id = '.$row['child_id'].' data-table = "'.$table.'" data-call_back = "'.$call_back.'" class = "delete"><i class="fa fa-trash"></i></a>',
    	);
}

## Response
$response = array(
    "draw" => intval($draw),
    "iTotalRecords" => $totalRecords,
    "iTotalDisplayRecords" => $totalRecordwithFilter,
    "aaData" => $data
);

echo json_encode($response);
