<?php
session_name("admin_session");
session_start();
require "connection.php";

if (isset($_POST['submit'])) {
    $user_name = $_POST['user_name'];
    $password = $_POST['password'];
    $email = $_POST['user_email'];
    $user_type = 'reseller';
    $country = $_POST['country'];
    $city = $_POST['city'];
    $answer = $_POST['answer'];
    $added_by = $_SESSION['username'];

    if ($user_type == 'reseller') {
        $user_id = generateUniqueID();
    } else {
        $user_id = "0";
    }

    $insert_qry = "INSERT INTO user (user_id, password, answer, user_name, user_email, user_type, country, city, added_by) VALUES ('$user_id', '$password', '$answer', '$user_name', '$email', '$user_type', '$country', '$city', '$added_by')";
    $result = mysqli_query($conn, $insert_qry);

    if ($result) {
        header("Location: add_reseller.php?success=Reseller Created Successfully!!!");
    } else {
        header("Location: add_reseller.php?error=Failed insertion!!!");
    }
} else {
    header("Location: add_reseller.php?error=Something went wrong!!!");
}

function generateUniqueID()
{
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $user_id = 'R'; // Add the 'U' prefix

    $charactersLength = strlen($characters);

    for ($i = 0; $i < 5; $i++) { // Generate a 5-character random string
        $user_id .= $characters[rand(0, $charactersLength - 1)];
    }

    return $user_id;
}
?>


