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


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

	if ($mode == 0) {
		$group_details = array();
		$all_group = array();

		$all_type_cus_id = array(
			getChitCustomerId($con),
			getFinanceCustomerId($con),
			getInterestCustomerId($con)
		);

		$group_query = $con->query("select id,group_name,start_date,end_date from group_list where status = '1' Order by id DESC ");

		while ($row = $group_query->fetch_assoc()) {
			$group_details['id'] = $row['id'];
			$group_details['group_name'] = $row['group_name'];
			$group_details['start_date'] = $row['start_date'];
			$group_details['end_date'] = $row['end_date'];
			$group_details['start_end_date'] = $row['start_date']." to ".$row['end_date'];
			$all_group[] = $group_details;
		}

		$returnArr = array(
			"ResponseCode" => "200",
			"Result" => "true",
			"ResponseMsg" => "Success",
			"data" => $all_group,
			"id_result" => $all_type_cus_id
		);
	}
	//else new group
	else if ($mode == 1) {
		$group_name = strip_tags(mysqli_real_escape_string($con, $data['group_name']));
		$group_start_date = strip_tags(mysqli_real_escape_string($con, $data['group_start_date']));
		$group_end_date = strip_tags(mysqli_real_escape_string($con, $data['group_end_date']));
		$current_time = strip_tags(mysqli_real_escape_string($con, $data['current_time']));

		$insert_sql = "insert into group_list(
                `group_name`,`start_date`,`end_date`,`created_time`,`updated_time`)
            values(
            	'$group_name','$group_start_date','$group_end_date','$current_time','$current_time')";
		$result = $con->query($insert_sql);

		if ($result)
			$returnArr = array("ResponseCode" => "200", "Result" => "true", "ResponseMsg" => "New Group Added");
		else
			$returnArr = array("ResponseCode" => "201", "Result" => "false", "ResponseMsg" => "Failure");
	}
	else if ($mode == 2) {
	    $group_id = strip_tags(mysqli_real_escape_string($con, $data['group_id']));
		$group_name = strip_tags(mysqli_real_escape_string($con, $data['group_name']));
		$group_start_date = strip_tags(mysqli_real_escape_string($con, $data['group_start_date']));
		$group_end_date = strip_tags(mysqli_real_escape_string($con, $data['group_end_date']));
		$current_time = strip_tags(mysqli_real_escape_string($con, $data['current_time']));

        $update_group_sql = "update group_list set `group_name`='$group_name',
        `start_date`='$group_start_date',`end_date`='$group_end_date',`updated_time`='$current_time'
                                        where id = '$group_id'";

        $result = $con->query($update_group_sql);
        if ($result)
            $returnArr = array("ResponseCode" => "200", "Result" => "true", "ResponseMsg" => "Group Updated");
        else
            $returnArr = array("ResponseCode" => "201", "Result" => "false", "ResponseMsg" => "Failure");
	}
}

function getChitCustomerId($con)
{
	$last_cus_id = $con->query("SELECT cus_id FROM customers_chit ORDER BY id DESC LIMIT 1");
	$last_cus_id = $last_cus_id->fetch_assoc();
	$count = isset($last_cus_id['cus_id']);

	if ($count) {
		$receipt_number = $last_cus_id['cus_id'];
		$length = 7;
		$rec_text_number = explode("-", $receipt_number);
		$no = $rec_text_number[2] + 1;
		$no = substr(str_repeat(0, $length) . $no, -$length);
		$rec_no = "CUS" . "-C-" . $no;
	} else {
		$rec_no = "CUS" . "-C-" . "0000001";
	}
	return $rec_no;
}

function getFinanceCustomerId($con)
{
	$last_cus_id = $con->query("SELECT cus_id FROM customers_finance ORDER BY id DESC LIMIT 1");
	$last_cus_id = $last_cus_id->fetch_assoc();
	$count = isset($last_cus_id['cus_id']);

	if ($count) {
		$receipt_number = $last_cus_id['cus_id'];
		$length = 7;
		$rec_text_number = explode("-", $receipt_number);
		$no = $rec_text_number[2] + 1;
		$no = substr(str_repeat(0, $length) . $no, -$length);
		$rec_no = "CUS" . "-F-" . $no;
	} else {
		$rec_no = "CUS" . "-F-" . "0000001";
	}
	return $rec_no;
}



function getInterestCustomerId($con)
{
	$last_cus_id = $con->query("SELECT cus_id FROM customers_interest ORDER BY id DESC LIMIT 1");
	$last_cus_id = $last_cus_id->fetch_assoc();
	$count = isset($last_cus_id['cus_id']);

	if ($count) {
		$receipt_number = $last_cus_id['cus_id'];
		$length = 7;
		$rec_text_number = explode("-", $receipt_number);
		$no = $rec_text_number[2] + 1;
		$no = substr(str_repeat(0, $length) . $no, -$length);
		$rec_no = "CUS" . "-I-" . $no;
	} else {
		$rec_no = "CUS" . "-I-" . "0000001";
	}
	return $rec_no;
}

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