<?php

require('../lib/dbclass.php');

$id = $_POST['id'];

if (!$id) {
    $id = 0;
}


if ($_POST['clubid'] == '') {
    $_POST['clubid'] = 0;
}

if ($_POST['ward_id'] == '') {
    $_POST['ward_id'] = 1;
}

if ($id > 0) {
    $usertype = 2;
    unset($_POST['user_type1']);
    unset($_POST['user_type']);
} else {
    $usertype = 2;
    unset($_POST['user_type1']);
    $_POST['user_type'] = 2;
}



$name = $_POST['name'];

$ward_name = $_POST['ward_id'];

$totalward = findQuery("select COUNT(*) nos from tbl_user where ward_id='$ward_name' and name='$name' and  user_type=$usertype and status=0 and id!=$id");
$count     = $totalward[0]['nos'];




if ($count > 0) {
    $message = 'Already Taken.';


    $encodedMessage = urlencode($message);



    $url = "add_user.php?message={$encodedMessage}";

    // Redirect to the new URL
    echo "<script>window.location.href='{$url}';</script>";
} else {
    unset($_POST['id']);


    $idnew = saveArray('tbl_user', $_POST, $id);

    $password = $name . '-' . $idnew;
    $password = encrypt($password);

    if ($idnew != $id) {
        updateQuery("update tbl_user set username=concat(10000,$idnew),password='$password' where id=$idnew");
    }
}

if ($idnew) {
    $message = 'Registered successfully!';



    $encodedMessage = urlencode($message);



    $url = "add_user.php?message={$encodedMessage}";

    // Redirect to the new URL
    echo "<script>window.location.href='{$url}';</script>";
} else {
    $message = 'Something went wrong, please try again.';


    $encodedMessage = urlencode($message);



    $url = "add_user.php?message={$encodedMessage}";

    // Redirect to the new URL
    echo "<script>window.location.href='{$url}';</script>";
}
