<?php
include "include/db.php";
include "include/auth.php";
    if($conn){
        if(isset($_REQUEST['length']) && isset($_REQUEST['start'])){
            if(strlen($_REQUEST['length']) > 0 && strlen($_REQUEST['start']) > 0){
                $start = $_REQUEST['start'];
                $length = $_REQUEST['length'];
                $search_text = $_REQUEST['search']['value'];
                $search_string = "is_delete IS NULL";
                if(strlen($search_text) > 0){
                    $search_string = "(prop_case_num LIKE '%$search_text%' AND is_delete IS NULL) OR (prop_loan_num LIKE '%$search_text%' AND is_delete IS NULL) OR (prop_full_add LIKE '%$search_text%' AND is_delete IS NULL)";
                }
                $draw = $_REQUEST['draw'];
                $property_query = "SELECT * FROM property WHERE $search_string";
                $property_run = mysqli_query($conn, $property_query);
                $totalRecords = mysqli_num_rows($property_run);                
                if($length == -1){
                    $length = $totalRecords;
                }
                $query = "SELECT * FROM property WHERE $search_string ORDER BY prop_id LIMIT $start ,$length";
                $run = mysqli_query($conn, $query);
                $recordsFiltered = mysqli_num_rows($run);
                $total_array = array();
                if($run){
                    while($row = mysqli_fetch_array($run)){
                        $prop_id = $row['prop_id'];
                        $prop_full_add = $row['prop_full_add']; 
                        $prop_city = $row['prop_city']; 
                        $prop_state = $row['prop_state']; 
                        $prop_zip = $row['prop_zip']; 
                        $prop_case_num = $row['prop_case_num'];       
                        $prop_county = $row['prop_county'];
                        $is_delete = $row['is_delete'];  
                        $view = "<a href='property.php?view=$prop_id'>View</a>";
                        $edit = "<a href='property.php?edit=$prop_id'>Edit</a>";                             
                        $delete = "<a href='property.php?delete=$prop_id&prop_case_num=$prop_case_num' class=\"delete\" data-confirm=\"Are you sure to delete $prop_case_num?\">Delete</a>";
                    
                        $check = "<a href='property.php?assign=$prop_id'>Check</a>";             
                        $data = array($prop_id,$prop_case_num,$prop_full_add,$prop_city,$prop_state,$prop_zip, $prop_county,$edit, $delete, $check);
                        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);
            }
        }
    }   
?>
           