<?php 
require 'config_db.php';
$data = json_decode(file_get_contents('php://input'), true);
$type = $data['type'];
$name = $data['name'];
if($type == '')
{
	$returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Something Went wrong try again !");
}
else
{
    if($type < 2) {
        $randomIntNumber = random_int(10000, 99999);
        $randomToken = generateRandomToken();
        $sql = "UPDATE `app_attendance_key` SET `name`='$name',`att_key`='$randomIntNumber',`att_token`='$randomToken' where id = '1'";
        $result = $con->query($sql);
        if($result) {
            $returnArr = array("ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Success","random_key"=>$randomIntNumber,"random_token"=>$randomToken);
        } else {
            $returnArr = array("ResponseCode"=>"201","Result"=>"false","ResponseMsg"=>"Failure","random_key"=>null,"random_token"=>null);
        }
    } else {
        $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Not Allowed","random_key"=>null,"random_token"=>null);
    }
}

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

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