<?php
require 'config_db.php';
$data = json_decode(file_get_contents('php://input'), true);
$type = $data['type'];

if ($type == '') {
    $returnArr = array("ResponseCode" => "401", "Result" => "false", "ResponseMsg" => "Something Went wrong try again !");
} 
else if ($type >= 0) {

    $collection_report_details = array();
    $all_collection_report = array();

    $search_value = $data['search_value'];
    $current_date = $data['current_date'];

    if($search_value=="chit"){
        if($type == "3") {
            $u_id = $data['u_id'];
            $select_query = "SELECT customers_chit.cus_name as c_nick_name, customers_chit.id as c_id,customers_chit.cus_date as c_start_date,
                                customers_chit.cus_id as c_cus_id,cast(customers_chit.paid_time as date) as info,customers_chit.scheme as c_scheme,customers_chit.installment_number as c_ins_no,
                                (TIMESTAMPDIFF(MONTH, 
                                        STR_TO_DATE(customers_chit.cus_date, '%d-%m-%Y'), '$current_date') + 1) as info
                                FROM customers_chit
                                WHERE customers_chit.payment_status<>3 AND customers_chit.customer_status<>0
                                    AND
                                        customers_chit.installment_number < 
                                                TIMESTAMPDIFF(MONTH,STR_TO_DATE(customers_chit.cus_date, '%d-%m-%Y'), '$current_date') + 1
                            ORDER by customers_chit.id ASC;";
        } else {
            $select_query = "SELECT customers_chit.cus_name as c_nick_name, customers_chit.id as c_id,customers_chit.cus_date as c_start_date,
                                customers_chit.cus_id as c_cus_id,cast(customers_chit.paid_time as date) as info,customers_chit.scheme as c_scheme,customers_chit.installment_number as c_ins_no,
                                (TIMESTAMPDIFF(MONTH, 
                                        STR_TO_DATE(customers_chit.cus_date, '%d-%m-%Y'), '$current_date') + 1) as info
                                FROM customers_chit
                                WHERE customers_chit.payment_status<>3 AND customers_chit.customer_status<>0
                                    AND
                                        customers_chit.installment_number < 
                                                TIMESTAMPDIFF(MONTH,STR_TO_DATE(customers_chit.cus_date, '%d-%m-%Y'), '$current_date') + 1
                            ORDER by customers_chit.id ASC;";
        }
        $collection_report = $con->query($select_query);

    }
    else if($search_value == "finance"){
        $select_query = "SELECT customers_finance.id as c_id,customers_finance.cus_name as c_nick_name,customers_finance.cus_date as c_start_date,
                                customers_finance.cus_id as c_cus_id,cast(customers_finance.paid_time as date) as info
                            FROM customers_finance
                            WHERE customers_finance.payment_status<>3 AND customers_finance.customer_status<>0 
                                AND
                                    if(customers_finance.paid_time IS NOT NULL,
                                        DATE_ADD(cast(customers_finance.paid_time as date), INTERVAL 1 DAY) <= '$current_date',true)
                                AND
                                    if(customers_finance.paid_time IS  NULL,
                                        STR_TO_DATE(customers_finance.cus_date, '%d-%m-%Y') < '$current_date' ,true)
                                ORDER by customers_finance.id ASC;";

        $collection_report = $con->query($select_query);

    }
    else if($search_value == "interest"){
        $select_query = "SELECT customers_interest.id as c_id,customers_interest.cus_name as c_nick_name,customers_interest.cus_date as c_start_date,
                                customers_interest.cus_id as c_cus_id,cast(customers_interest.paid_time as date) as info
                            FROM customers_interest
                            WHERE customers_interest.payment_status<>3 AND customers_interest.customer_status<>0
                                AND
                                    if(customers_interest.paid_time IS NOT NULL,
                                        DATE_ADD(cast(customers_interest.paid_time as date), INTERVAL 30 DAY) <= '$current_date',true)
                                AND
                                    if(customers_interest.paid_time IS NULL,
                                        DATE_ADD(STR_TO_DATE(customers_interest.cus_date, '%d-%m-%Y'), INTERVAL 30 DAY) <= '$current_date',true)
                                ORDER by customers_interest.id ASC;";

        $collection_report = $con->query($select_query);
    }

    while ($collection_report_row = $collection_report->fetch_assoc()) {
        $collection_report_details['customer_id'] = $collection_report_row['c_id'];
        $collection_report_details['customer_name'] = $collection_report_row['c_nick_name'];
        $collection_report_details['customer_cus_id'] = $collection_report_row['c_cus_id'];
        // if($search_value=="chit")
        $collection_report_details['customer_scheme'] = $collection_report_row['c_scheme'];
        $collection_report_details['installment_number'] = $collection_report_row['c_ins_no'];
        $collection_report_details['customer_start_date'] = $collection_report_row['c_start_date'];
        $collection_report_details['customer_info'] = $collection_report_row['info'];

        // $collection_report_details['transaction_date'] = $collection_report_row['t_r_d'];
        // $collection_report_details['transaction_paid_amount'] = $collection_report_row['t_p_a'];
        // $collection_report_details['transaction_created_by'] = $collection_report_row['info'];
        
        // $total_paid_amount += $collection_report_row['t_p_a'];

        $all_collection_report[] = $collection_report_details;
    }

    $returnArr = array(
        "ResponseCode" => "200",
        "Result" => "true",
        "ResponseMsg" => "Success",
        "data" => $all_collection_report,
        // "total_paid_amount" => $total_paid_amount
    );
   


}
echo json_encode($returnArr);
mysqli_close($con);
?>