<?php
header("Content-Type:application/json");
if (isset($_GET['first_name']) && isset($_GET['username'])) {
    include('db.php');
    include('EncryptDecrypt.php');

    date_default_timezone_set('Asia/Kolkata');
    $datetime = date("Y-m-d H:i:s");
    $date = date("Y-m-d");

    $mcrypt = new MCrypt();
    $first_name = $_GET['first_name'];
    $username = $_GET['username'];
    $company_id = 0;
    $status = 'Active';
    $login_flag = 0;
    $user_id = 0;
    $p_out= generateRandomString();
    $password = $mcrypt->encrypt($p_out);

    $result = mysqli_query(
        $con,
        "INSERT INTO `user_account`(company_id,first_name,last_name,email_id,designation_id,grade,project,location,state,
        region,manager,biography,status,password,user_image,login_flag,last_gen_pw_ts,
        created_user,created_ts,last_updated_user,last_updated_ts)
        VALUES (" . $company_id . ",'" . $first_name . "','','','','','','','','','','','" . $status . "','" . $password . "',
		''," . $login_flag . ",'" . $datetime . "'," . $user_id . ",'" . $datetime . "'," . $user_id . ",'" . $datetime . "')"
    );
    if ($result) {
        response("success");
        mysqli_close($con);
    } else {
        //response(NULL, NULL, 200, "No Record Found");
        echo "Error";
    }
} else {
    //response(NULL, NULL, 400, "Invalid Request");
    echo "Error";
}

function response($success)
{
    $response = [];
    $response[] = [ 
        'id' => "success",
        'course' => "list"
    ];
    $json_response = json_encode($response);
    echo $json_response;
}

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