/********************************Staff NAME EXIST VALIDATION************************************/
$(document).ready(function(){
$('#staff_name').keyup(usernames_checkitems);
}); 
function usernames_checkitems(){ 
var staff_name = jQuery('#staff_name').val();
if(staff_name == "" || staff_name.length < 0){
jQuery('#staff_name');
}else{
jQuery.ajax({
   type: "POST",
   url: "chk_staff_name_db.php",
   data: 'staff_name='+ staff_name,
   cache: false,
   success: function(response){
if(response != 0){
document.getElementById("staff_name_err_design").innerHTML="Staff Name Already Exists.";
 }else
     {
	 document.getElementById("staff_name_err_design").innerHTML="";
     }
}
});
}
}
/********************************USER NAME EXIST VALIDATION************************************/

$(document).ready(function(){
$('#user_name').keyup(usernames12_checkitems);
}); 
function usernames12_checkitems(){ 
var user_name = jQuery('#user_name').val();
if(user_name == "" || user_name.length < 0){
jQuery('#user_name');
}else{
jQuery.ajax({
   type: "POST",
   url: "chk_user_name_db.php",
   data: 'user_name='+ user_name,
   cache: false,
   success: function(response){
	  
if(response != 0){
document.getElementById("user_name_err_design").innerHTML="User Name Already Exists.";
 }else
     { //salert(response);
	 document.getElementById("user_name_err_design").innerHTML="";
     }
}
});
}
}
/********************************Mob NAME EXIST VALIDATION************************************/
$(document).ready(function(){
$('#ph_no').keyup(userph_no12_checkitems);
}); 
function userph_no12_checkitems(){ 
var staff_name_ph = jQuery('#ph_no').val();
if(staff_name_ph == "" || staff_name_ph.length < 0){
jQuery('#ph_no');
}else{
	
jQuery.ajax({
   type: "POST",
   url: "chk_mob_no_db.php",
   data: 'staff_name_ph='+ staff_name_ph,
   cache: false,
   success: function(response){
if(response != 0){
document.getElementById("mob_no_name_err_design").innerHTML="Phone Number Already Exists.";
 }else
     {
	 document.getElementById("mob_no_name_err_design").innerHTML="";
     }
}
});
}
}

/******************************** Validation *********************************************************/
function user_creation_validate()
{
	var staff_name = document.getElementById("staff_name").value;
	if(staff_name=='')
	{
		document.getElementById("staff_name_err_design").innerHTML="* This field is Required.";
		document.getElementById("staff_name").focus();
		return false;
	}
	
	var designation = document.getElementById("designation").value;
	
 	if((designation==''))
	{
		document.getElementById("designation_err_design").innerHTML="* This field is Required.";
		document.getElementById("designation").focus();
		return false;
	}
    
    var ph_no = document.getElementById("ph_no").value;
 	if((ph_no==''))
	{
		document.getElementById("mob_no_name_err_design").innerHTML="* This field is Required.";
		document.getElementById("ph_no").focus();
		return false;
	}
	
	
	var user_name = document.getElementById("user_name").value;
	if(user_name=='')
	{
		document.getElementById("user_name_err_design").innerHTML="* This field is Required.";
		document.getElementById("user_name").focus();
		return false;
	}

    var password = document.getElementById("password").value;
 	if((password==''))
	{
		document.getElementById("password_name_err_design").innerHTML="* This field is Required.";
		document.getElementById("password").focus();
		return false;
	}
     var confirm_password = document.getElementById("confirm_password").value;
 	if((confirm_password==''))
	{
		document.getElementById("confirm_password_err_design").innerHTML="* This field is Required.";
		document.getElementById("confirm_password").focus();
		return false;
	}
	
	var user_type = document.getElementById("user_type").value;
	
 	if((user_type==''))
	{
		document.getElementById("user_type_err_design").innerHTML="* This field is Required.";
		document.getElementById("user_type").focus();
		return false;
	}  
	
    var active = document.getElementById("active").value;
 	if((active==''))
	{
		document.getElementById("active_err_designs").innerHTML="* This field is Required.";
		document.getElementById("active").focus();
		return false;
	}
}


function user_clear_validate(str)
{
	if(str!='')
	{
		document.getElementById("staff_name_err_design").innerHTML="";
	}

    if(str!='')
	{
		document.getElementById("designation_err_design").innerHTML="";
	}
	
	if(str!='')
	{
		document.getElementById("mob_no_name_err_design").innerHTML="";
	}
	
	if(str!='')
	{
		document.getElementById("user_name_err_design").innerHTML="";
	}
	
	if(str!='')
	{
		document.getElementById("password_name_err_design").innerHTML="";
	}
	if(str!='')
	{
		document.getElementById("confirm_password_err_design").innerHTML="";
	}
	
	if(str!='')
	{
		document.getElementById("user_type_err_design").innerHTML="";
	}
	if(str!='')
	{
		document.getElementById("active_err_designs").innerHTML="";
	}
    
}

/************************************ Main Add *******************************************************/
function user_add(staff_name,designation,ph_no,staff_adres,user_type,user_name,password,confirm_password,status,edit_id,action)
{ 
if((staff_name!='')&&(designation!='')&&(ph_no!='')&&(user_type!='')&&(user_name!='')&&(password!='')&& (confirm_password!='')&&(status!='') )
{
if(confirm_password==password)
{
	jQuery.ajax({
		type: "POST",
	    url:"user_entry_db.php",
		data:"staff_name="+staff_name+"&designation="+designation+"&ph_no="+ph_no+"&staff_adres="+staff_adres+"&user_type="+user_type+"&user_name="+user_name+"&password="+password+"&confirm_password="+confirm_password+"&status="+status+"&edit_id="+edit_id+"&action="+action,
		success: function(msg){jQuery("#user_creation_div").html(msg);
		alert(msg);
		window.close();
		window.opener.location.reload(true);
		
		}
		});
   }
   else
   {
	  alert("please enter correct password");
	  document.getElementById("confirm_password").focus();
   }
}
else
	{
    	user_creation_validate();
	}
}

/**************************** Main delete*******************************************************/
// function user_creation_delete_row(user_id)
// {
// 	if(confirm('Are You Sure! You Want To Delete?'))
// 	{
// 		jQuery.ajax({
// 		type: "POST",
// 	    url:"user_name_creation/user_entry_db.php",
// 		data: "user_id="+user_id+"&action=del_user",
// 		success: function(msg){jQuery("#user_creation_div").html(msg);
// 		alert("Deleted Successfully");
// 		window.location.reload(true);
// 	}
// 	});
//     }
// }

function user_creation_delete_row(user_id) {
    if (!confirm('Are you sure you want to delete?')) return;

    const dataToSend = $.param({ user_id: user_id, action: 'del_user' });
    console.log("AJAX URL: user_name_creation/user_entry_db.php");
    console.log("AJAX Data: ", dataToSend);

    $.ajax({
        type: "POST",
        url: "user_name_creation/user_entry_db.php",
        data: dataToSend,
        success: function(response) {
            console.log("Server Response:", response);
            $("#user_creation_div").html(response);
            //alert("Deleted Successfully");
            window.location.reload(true);
        },
        error: function(xhr, status, error) {
            console.error("Deletion failed:", error);
            alert("An error occurred while deleting the user.");
        }
    });
}


