<?php
function get_user_by_id($dbc,$id){
    $q = mysqli_query($dbc,"SELECT * FROM user_tbl WHERE id = '$id'");
    $data = mysqli_fetch_assoc($q);
   return $data;
}

function get_all_user($dbc){
    $query = "
        SELECT * FROM user_tbl    
    ";
    $q = mysqli_query($dbc,$query);
    $data = array();
    while ($row = mysqli_fetch_assoc($q)){
        $data[] = $row;
    }
    return $data;
}

function addNewUser($dbc,$fullName,$username,$password,$userType,$userPIN,$adminID,$userCountry,$status,$phoneNum,$emailadd){
    $query = "
        INSERT INTO user_tbl (fullName, username, password, userType, userPIN, adminID, userCountry, status,phoneNum,emailadd)
        VALUES ('$fullName','$username','$password','$userType',$userPIN,'$adminID','$userCountry','$status','$phoneNum','$emailadd')
    ";
    $q = mysqli_query($dbc,$query);
    if($q){
        return true;
    }
}

function updateUser($dbc,$id,$fullName,$userType,$userPIN,$userCountry,$status){
    $query = "
        UPDATE user_tbl SET 
          fullName = '$fullName',
          userType = '$userType',
          userPIN = '$userPIN',
          userCountry = '$userCountry',
          status = '$status'
        WHERE
        id = '$id'
    ";
    $q = mysqli_query($dbc,$query);
    if($q){
        return true;
    }
}

function check_if_username_exist($dbc,$username){
    $q = mysqli_query($dbc,"SELECT * FROM user_tbl WHERE username = '$username'");
    $rows = mysqli_num_rows($q);
    if($rows == 0){
        return true;
    }
}

function check_user_and_password($dbc,$userID,$password){
    $q = mysqli_query($dbc,"SELECT * FROM user_tbl WHERE id = '$userID'  AND password ='$password' AND status = 'ACTIVE' ");
    $rows = mysqli_num_rows($q);
    if ($rows == 1) {
        return true;
    }
}

function check_user_email_and_password_for_clients($dbc,$email,$password){
    $q = mysqli_query($dbc,"SELECT * FROM customer_tbl WHERE email = '$email'  AND password ='$password' AND status = 'ACTIVE' ");
    $rows = mysqli_num_rows($q);
    if ($rows == 1) {
        return true;
    }
}

function check_email_and_phoneNum($dbc,$emailadd,$phoneNum){
    $q = mysqli_query($dbc,"SELECT * FROM user_tbl WHERE emailadd = '$emailadd'  OR phoneNum ='$phoneNum' AND status = 'ACTIVE' ");
    $rows = mysqli_num_rows($q);
    if ($rows == 1) {
        return true;
    }
}

function change_user_password($dbc,$newPassword,$emailadd,$phoneNum){
    $q = mysqli_query($dbc,"UPDATE user_tbl SET password = '$newPassword' WHERE emailadd = '$emailadd'  OR phoneNum ='$phoneNum' AND status = 'ACTIVE'");
    if($q){
        return true;
    }
}

function change_user_password_client($dbc,$id,$newPassword){
    $q = mysqli_query($dbc,"UPDATE customer_tbl SET password = '$newPassword' WHERE id = '$id' AND status = 'ACTIVE'");
    if($q){
        return true;
    }
}

function change_user_status($dbc,$userID){
	$user = get_user_by_id($dbc,$userID)['status'];
	if($user=="ACTIVE"){
		$status = "INACTIVE";
	}else{
		$status = "ACTIVE";
	}
	
    $q = mysqli_query($dbc,"UPDATE user_tbl SET status = '$status' WHERE id = '$userID'");
    if($q){
        return true;
    }
}