<?php
function createUser($username,$fullnames, $email, $tell, $userGroup, $possition, $status, $user){
     $errors = array();
     $response = array();

     if($username==null){ array_push($errors,"username must not be null");}
     if($email==null){ array_push($errors,"email must not be null");}
     if(userExists($username, $fullnames, $email)==true){ array_push($errors,"username already exists");}else{
         if(count($errors) == 0){
        $password= psdGen();
        $psd = $password[1];
        $bsid = bsid();
        $query="INSERT INTO `users`(`fullName`, `password`, `email`, `tell`, `userGroup`, `position`, `status`, `bsID`)
         VALUES ('$fullnames','$psd','$email','$tell','$userGroup','$possition','$status',$bsid)";
         exe($query);
         coms($email, "your password is $password[0]");
         logger("userCreation",$user, $query);

         $response = array("status" => "1",
         "msg" => "user created successfull");



         }else{
         $response = array("status" => "0",
         "msg" => json_encode( $errors));
         }
     }

     return $response;
}

function userExists($username,$fullnames,$email){
    $query = "SELECT * FROM users WHERE email = '$email'";
    $result = exe($query);
    $result = mysqli_fetch_array($result);
    if($result['email'] == null or $result['email']==""){return false;}
    else{return true;}
}

// function exe($query){
//    return mysqli_query($db, $query);
// }
function psdGen(){
    $party1 = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
    $party2 = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","u","v","w","x","y","Z");
    $party3 = array("@","#","!","*","&","!","$","?","%",">");
    $party4 = rand(10,99);

    $password = $party2[rand(0,25)].$party1[rand(0,25)].$party3[rand(0,9)].$party4.$party2[rand(0,25)].$party2[rand(0,25)].$party3[rand(0,9)].$party1[rand(0,25)].$party3[rand(0,9)];
    $secPsd = md5($password);

    $psd = array($password,$secPsd);

    return $psd;
}

function bsid() {

    return 1;
}

function coms($email,$msg){


    return 1;
}

function login($username,$password){
    $psd = md5($password);
    $query = "SELECT * FROM users WHERE username = '$username' AND password = '$psd'";
    $result = exe($query);
    if(mysqli_num_rows($result)==1){
        $msg = "login Successfully";
        logger("login", $username, $query);
    }else{
        $msg = "login Failure";
        log("login Error", $username, $query);
    }

    return $msg; // login response
}
?>