<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
//if (isset($_POST['first_name']) && isset($_POST['username'])) {
    include('db.php');
    include('EncryptDecrypt.php');

    $data = json_decode(file_get_contents("php://input"));

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

    $mcrypt = new MCrypt();
    $first_name = $data->first_name;
    $username = $data->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,login_id,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 . "','" . $username . "','" . $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 "Error2";
    }
//} else {
    //response(NULL, NULL, 400, "Invalid Request");
    //echo "Error1";
//}

function response($success)
{
    $response['success'] = $success;
	
	$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;
}
