<?php
include "include/db.php";
include "include/adminauth.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_text = trim($search_text);
                
                $draw = $_REQUEST['draw'];
                $totalRecords = 0;                
                $total_array = array();          
           
                $length_str = '';

                $search_string = "";
                if(strlen($search_text) > 0){ 
                    $search_string = "WHERE invoice_no = $search_text";    
                }
                $query_vendor_pay_cnt = "SELECT * FROM client_email_pay_settle_fail $search_string ORDER BY id DESC";
                $run_vendor_pay_cnt= mysqli_query($conn, $query_vendor_pay_cnt); 
                $totalRecords = mysqli_num_rows($run_vendor_pay_cnt);  
                if($length == -1){
                    $length = $totalRecords;
                }

                $length_str = " LIMIT $start ,$length";

                $query_vendor_pay = "SELECT * FROM client_email_pay_settle_fail $search_string ORDER BY id DESC". $length_str;
                $run_vendor_pay= mysqli_query($conn, $query_vendor_pay);
                if(mysqli_num_rows($run_vendor_pay) >0){
                    while($row_vendor_pay = mysqli_fetch_array($run_vendor_pay)){
                        $invoice_no = $row_vendor_pay['invoice_no']; 
                        $message = $row_vendor_pay['message'];   
                        $client_email_id = $row_vendor_pay['client_email_id']; 
                        $int_client_id = $row_vendor_pay['client_id'];
                        $invoice_client_name = '';
                        $fetch_invoice_client_name_query = "SELECT * FROM client WHERE client_id = $int_client_id AND client_is_delete IS NULL";
                        $fetch_invoice_client_name_run = mysqli_query($conn, $fetch_invoice_client_name_query);
                        if(mysqli_num_rows($fetch_invoice_client_name_run)==1){
                            $fetch_invoice_client_name_row = mysqli_fetch_array($fetch_invoice_client_name_run);
                            $invoice_client_name = $fetch_invoice_client_name_row['client_name'];
                        }                        
                        $time = $row_vendor_pay['time']; 
                        $time = strtotime($time);
                        $time= date('m-d-Y', $time);
                        $data = array($invoice_no , $message , $invoice_client_name , $client_email_id , $time);
                        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);
            }
        }
    }
