<?php
include "include/db.php";
include "include/adminauth.php";

if($conn){                    
        if(isset($_REQUEST['length']) && isset($_REQUEST['start'])){
            $cl_id = $_REQUEST['client_id'];
            $url_client_id = trim($cl_id);
            if(strlen($_REQUEST['length']) > 0 && strlen($_REQUEST['start']) > 0 && strlen($url_client_id) > 0){
                $start = $_REQUEST['start'];
                $length = $_REQUEST['length'];
                $search_text = '';
                $search_text = $_REQUEST['search']['value'];
                $search_string = "";
                               
                if(strlen($search_text) > 0){
                    $search_string = "AND maps.maps_name LIKE '%$search_text%'";
                }
                $draw = $_REQUEST['draw'];  
                $query_bid_item_cnt = "SELECT * FROM maps LEFT JOIN category ON maps.cat_id = category.cat_id  WHERE maps.client_id = $url_client_id AND maps.maps_is_delete IS NULL AND maps.maps_approved = 1 $search_string ORDER BY category.cat_name DESC";
                
                $run_bid_item_cnt = mysqli_query($conn, $query_bid_item_cnt);
                $totalRecords = mysqli_num_rows($run_bid_item_cnt);   

                if($length == -1){
                    $length = $totalRecords;
                }
    
                $query = "SELECT * FROM maps LEFT JOIN category
                ON maps.cat_id = category.cat_id  WHERE maps.client_id = $url_client_id AND maps.maps_is_delete IS NULL AND maps.maps_approved = 1 $search_string ORDER BY category.cat_name DESC LIMIT $start ,$length"; 
                $run = mysqli_query($conn, $query);
                $total_array = array(); 
                if(mysqli_num_rows($run)>0){
                    while($row = mysqli_fetch_array($run)){ 
                        $maps_id = $row['maps_id'];
                        $ref_photo = $row['maps_ref_pic'];
                        $cat_name = $row['cat_name'];
                        $maps_name = $row['maps_name'];
                        $description = $row['maps_desc'];
                        $client_allow_price = $row['maps_client_allow_price'];
                        $min_bid_price = $row['maps_min_bid_price'];
                        $crew_pay = $row['maps_crew_pay'];
                        $photo_req = $row['maps_photo_req'];
                        $maps_uom =$row['maps_crew_unit_uom_id'];
                        if($maps_uom == NULL){
                            $query22 = "SELECT uom_name FROM uom WHERE uom_id = $maps_uom";
                                        
                            $run22 = mysqli_query($conn, $query22);
                            if(mysqli_num_rows($run22)==1){
                                $row22 = mysqli_fetch_array($run22);
                                $maps_uom = $row22['uom_name'];
                            }
                        }
                        $edit_row = '<a href="MAPS-sheet.php?edit='.$maps_id.'&cl_id='.$url_client_id.'">Edit</a><br><a href="MAPS-sheet.php?delete='.$maps_id.'&cl_id='.$url_client_id.'">Delete</a>'; 
                        $ref_img_row =  '<img src = "maps_ref_image.php?i='.$ref_photo.'" height = "80px" width = "120px">'; 
                        $data = array($edit_row, $ref_img_row, $cat_name, $maps_name, $description, $client_allow_price, $min_bid_price, $crew_pay, $maps_uom, $photo_req);
                        array_push($total_array,$data);  
                    }
                }
    
                $json_data = array(
                    "draw"            => intval($draw),   
                    "recordsTotal"    => intval($totalRecords),  
                    "recordsFiltered" => intval($totalRecords),
                    "data"            => $total_array  
                    );
                echo json_encode($json_data);
            }
        }
    }