<?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 {
    // get all members with status 0 also
	if ($mode == 0) {
		$members_details = array();
		$member_ids = array(
			getNewMemberId($con)
		);
		$member_query = $con->query("select * from app_users Order by id DESC ");
		while ($row1 = $member_query->fetch_assoc()) {
			$members_details['id'] = $row1['id'];
			$members_details['member_id'] = $row1['member_id'];
			$members_details['name'] = $row1['name'];
			$members_details['logo'] = $row1['logo'];
			$members_details['company'] = $row1['company'];
			$members_details['mobile'] = $row1['mobile'];
			$members_details['type'] = $row1['type'];
			$all_members [] = $members_details;
		}

		$returnArr = array(
			"ResponseCode" => "200",
			"Result" => "true",
			"ResponseMsg" => "Success",
			"data" => $all_members,
			"member_id" => $member_ids
		);
	}
	// get all members with active status only
	else if ($mode == 1) {
	$members_details = array();
		$member_ids = array(
			getNewMemberId($con)
		);
		$member_query = $con->query("select * from app_users where status = '1' Order by id DESC ");
		while ($row1 = $member_query->fetch_assoc()) {
			$members_details['id'] = $row1['id'];
			$members_details['member_id'] = $row1['member_id'];
			$members_details['name'] = $row1['name'];
			$members_details['logo'] = $row1['logo'];
			$members_details['company'] = $row1['company'];
			$members_details['mobile'] = $row1['mobile'];
			$members_details['type'] = $row1['type'];
			$all_members [] = $members_details;
		}

		$returnArr = array(
			"ResponseCode" => "200",
			"Result" => "true",
			"ResponseMsg" => "Success",
			"data" => $all_members,
			"member_id" => $member_ids
		);
	}
	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 getNewMemberId($con)
{
    $rand_id = generateRandomNumber(5);
	$member_id = $con->query("SELECT member_id FROM app_users ORDER BY id DESC");
	while ($row = $member_id->fetch_assoc()) {
	    if($rand_id == $row['member_id']) {
	        $rand_id = generateRandomNumber(5);
	    }
	}
	return $rand_id;
}

function generateRandomNumber($length) {
    $characters = '0123456789';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}

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