<?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 = 'id';
$call_back   = 'services';
$table       = 'services';


## Search 
$searchQuery = " ";
if($searchValue != ''){
	$searchQuery = " and ( S.default_price like '%".$searchValue."%' or 
    S.offer_price like '%".$searchValue."%' or
    S.discount like '%".$searchValue."%' or
    S.discount_price like '%".$searchValue."%' or
    CC.sub_category_name like '%".$searchValue."%' ) ";
}

## Total number of records without filtering
$sel = mysqli_query( $con,"SELECT count(*) as allcount FROM services S JOIN child_categories CC ON S.child_id = CC.child_id" );
$records = mysqli_fetch_assoc($sel);
$totalRecords = $records['allcount'];

## Total number of records with filtering
$sel = mysqli_query($con,"SELECT count(*) as allcount FROM services S JOIN child_categories CC ON S.child_id = CC.child_id WHERE 1 ".$searchQuery);
$records = mysqli_fetch_assoc($sel);
$totalRecordwithFilter = $records['allcount'];

## Fetch records
$childQuery = "SELECT CC.sub_category_name, S.* FROM services S JOIN child_categories CC ON S.child_id = CC.child_id WHERE 1 ".$searchQuery." order by ".$columnName." ".$columnSortOrder." limit ".$row.",".$rowperpage;
$childRecords = mysqli_query($con, $childQuery);
$data = array();

while ( $row = mysqli_fetch_assoc( $childRecords ) ) {
    ( $row['is_best_seller'] == 1 ) ? $is_best_seller = 'Yes': $is_best_seller = 'No';
    ( $row['active'] == 1 ) ? $active = 'Yes': $active = 'No';
    $service_id  = $row['id'];
    $min_val = $row['min_visit_price'];
    $data[] = array(
      "service_name"     => $row['service_name'],            
   		"child_category"   => $row['sub_category_name'],            
   		"default_price"    => $row['default_price'],
      "min_visit_price"  => $min_val,
      "offer_price"      => $row['offer_price'],
      "is_best_seller"   => $is_best_seller,
      "discount"         => $row['discount'],
  		"active"           => $active,
      "edit"             => '<a href="javascript:void(0);" data-extension = "'.$extension.'" data-identifier = "'.$primary_key.'" data-id = '.$row['id'].' data-table = "'.$table.'" data-call_back = "'.$call_back.'" class = "edit" data-service_id = "'.$service_id.'" data-child_id = "'.$row['child_id'].'" data-default_price = "'.$row['default_price'].'" data-offer_price = "'.$row['offer_price'].'" data-service_name = "'.$row['service_name'].'" data-is_best_seller = "'.$row['is_best_seller'].'" data-discount = "'.$row['discount'].'" data-min-price = "'.$min_val.'" data-active = "'.$row['active'].'" data-service_image = "'.$row['service_image'].'"><i class="fa fa-pencil"></i></a>',
      "delete"           => '<a href="javascript:void(0);" data-extension = "'.$extension.'" data-identifier = "'.$primary_key.'" data-id = '.$row['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);
