<?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_customer_chit_details = array();
 
    $mode = $data['mode'];
    $groupname = $data['group_name'];
    
    if($groupname == "All Groups") {
            $cy = $con->query("select * from customers_chit 
										where customer_status=1");
    } else {
            $cy = $con->query("select * from customers_chit 
										where group_name='$groupname' and customer_status=1");
    }
										
	while ($row = $cy->fetch_assoc()) {
		$cus_id = $row['id'];
		$customer_chit_details['id'] = $row['id'];
		$customer_chit_details['cus_date'] = $row['cus_date'];
		$customer_chit_details['cus_nick_name'] = $row['cus_name'];
		$customer_chit_details['cus_name'] = $row['cus_nick_name'];
// 		$customer_chit_details['cus_nick_name'] = $row['cus_nick_name'];
// 		$customer_chit_details['cus_name'] = $row['cus_name'];
		$customer_chit_details['cus_id'] = $row['cus_id'];
		$customer_chit_details['cus_type'] = $row['cus_type'];
		$customer_chit_details['cus_mobile'] = $row['cus_mobile'];
		$customer_chit_details['address'] = $row['address'];
		$customer_chit_details['nominee_name'] = $row['nominee_name'];
		$customer_chit_details['nominee_number'] = $row['nominee_number'];

		$customer_chit_details['created_time'] =date('d-m-Y', strtotime($row['created_time'])); 

		$customer_chit_details['group_name'] = $row['group_name'];
		$customer_chit_details['commitment_month'] = $row['commitment_month'];
		$customer_chit_details['commitment_amount'] = $row['commitment_amount'];
		$customer_chit_details['previous_amount'] = $row['previous_amount'];
		$customer_chit_details['scheme'] = $row['scheme'];
		$customer_chit_details['installment_number'] = $row['installment_number'];
		$customer_chit_details['installment_balance_amount'] = $row['installment_balance_amount'];

		$sum_prev_paid_amount = "select sum(current_installment_amount) as prev_amount from transaction_chit where customer_id = '$cus_id'";
		$sum_prev_paid_amount = $con->query($sum_prev_paid_amount)->fetch_assoc()['prev_amount'];
		$sum_prev_paid_amount = $sum_prev_paid_amount == null ? 0 : $sum_prev_paid_amount;

		$customer_chit_details['previous_paid_amount'] = $sum_prev_paid_amount + $row['previous_amount'];

		$customer_chit_details['multiply_number'] = $row['multiply_number'];
		$customer_chit_details['current_chit_amount'] = $row['current_chit_amount'];
		$customer_chit_details['auction_amount'] = $row['auction_amount'];
		$customer_chit_details['final_chit_amount'] = $row['final_chit_amount'];

		$customer_chit_details['conversion_status'] = $row['conversion_status'];
		$customer_chit_details['payment_status'] = $row['payment_status'];
		$customer_chit_details['customer_status'] = $row['customer_status'];
		$customer_chit_details['bid_amount'] = $row['bid_amount'];
		$customer_chit_details['bid_date'] = $row['bid_date'];


		$all_customer_chit_details[] = $customer_chit_details;
	}

    $returnArr = array(
        "ResponseCode" => "200",
        "Result" => "true",
        "ResponseMsg" => $select_query,
        "customer_chit_list" => $all_customer_chit_details
    );

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