<?php
include("../../include/db.php");
/********************** Chk Item For ADD*******************************************/
function check_item($user_name)
{
 $Isql=mysql_query("select * from user_creation where user_name='$user_name' and delete_status!='1'");
$rscount=mysql_num_rows($Isql);
return $rscount;
}
function check_item12($staff_name,$ph_no)
{
$Isql=mysql_query("select * from user_creation where staff_name='$staff_name' and staff_ph_no='$ph_no' and delete_status!='1'");
$rscount1=mysql_num_rows($Isql);
return $rscount1;
}
/********************** Chk Item For Edit*******************************************/
function check_user_item($user_name,$edit_id)
{
$sql_query="select * from user_creation where user_name='$user_name'  and delete_status!='1' and user_id!='$edit_id' ";
$rs=mysql_query($sql_query);
$rscount1=mysql_num_rows($rs);
return $rscount1;

 }

 function check_user_item12($staff_name,$ph_no)
{  
  $sql_query="select * from user_creation where staff_name='$staff_name' and staff_ph_no='$ph_no' and delete_status!='1' ";
 $rs=mysql_query($sql_query);
 $rscount1=mysql_num_rows($rs);
 if($rscount1!=0)
 {
 while($rsdata=mysql_fetch_object($rs))
 {
 $user_name_edit1=$rsdata->user_id;
 } 
	return $user_name_edit1;
 }
 }

/********************************** ADD & UPDATE QUERY ***************************************/
if($_POST['action']=='ADD')
{
$staff_name=ucfirst($_POST['staff_name']);
$designation=$_POST['designation'];
$ph_no=$_POST['ph_no'];
$staff_adres=$_POST['staff_adres'];
$user_type=$_POST['user_type'];
$user_name=$_POST['user_name'];
$password=$_POST['password'];
$branchname=$_POST['branch'];
$delete_status=$_POST['delete_status'];
$confirm_password=$_POST['confirm_password'];
$status=$_POST['status'];
$create_date=date('Y-m-d');
$acadamic_yr=explode("-",$create_date);
$acdmc_yr_res=$acadamic_yr[0];
$check_item=check_item($user_name);
$check_item12=check_item12($staff_name,$ph_no);
if($check_item12==0)
{
if(($check_item==0))
{ 
$Insql=mysql_query("insert into user_creation(	staff_name,staff_designation,staff_ph_no,staff_address,user_type,user_name,password,status,confirm_password,branch,acc_year,create_date)values('$staff_name','$designation','$ph_no','$staff_adres','$user_type','$user_name','$password','$status','$confirm_password','$branchname','$acdmc_yr_res','$create_date')");

echo "Added Successfully";
}else
{
	echo "Already Exists";
}
}
else
{
echo "Already Exists";

}
}
/*********************************** UPDATE QUERY *********************************************/
if($_POST['action']=='EDIT')
{
 $edit_id=$_POST['edit_id'];
$staff_name=ucfirst($_POST['staff_name']);
$designation=$_POST['designation'];
$ph_no=$_POST['ph_no'];
$staff_adres=$_POST['staff_adres'];
$user_type=$_POST['user_type'];
$user_name=$_POST['user_name'];
$acc_year=$_POST['acc_year'];
$branchname=$_POST['branch'];
$password=$_POST['password'];
$confirm_password=$_POST['confirm_password'];
$status=$_POST['status'];
$create_date=date('Y-m-d');
$acadamic_yr=explode("-",$create_date);
$acdmc_yr_res=$acadamic_yr[0];
//echo $check_user_name=check_user_item($user_name,$password);
$check_item121=check_user_item($user_name,$edit_id);

$check_user12=check_user_item12($staff_name,$ph_no);
if(($check_user12==$_POST['edit_id'])||($check_user12==''))	
{
if($check_item121==0)
{
$Insql=mysql_query("update user_creation SET  staff_name='$staff_name', staff_designation='$designation',staff_ph_no='$ph_no',staff_address='$staff_adres',user_type='$user_type', user_name='$user_name',password='$password',status='$status',confirm_password='$confirm_password',acc_year='$acdmc_yr_res',branch='$branchname', create_date='$create_date',acc_year='$acdmc_yr_res' where user_id='$_POST[edit_id]'");
echo "Updated Succesfully!!";
}else
{
	echo "Already Exists";
}
}
else
{
	echo "Already Exists";
}
}
/**********************************************************************************************/
// if($_POST['action']== 'del_user')
// {
// $Insql=mysql_query("update user_creation SET delete_status='1' where user_id='$_POST[user_id]'");
// include("user_entry_list.php");

// }
if ($_POST['action'] == 'del_user') {
    include("../include/db_new.php");
    if (!$DB) {
        die("❌ Failed to select database: " . mysql_error());
    }

    // Get and sanitize ID
    $user_id = intval($_POST['user_id']);

    // Perform soft delete
    $deleteQuery = "UPDATE user_creation SET delete_status='1' WHERE user_id='$user_id'";
    $result = mysql_query($deleteQuery, $ConDB);

    if (!$result) {
        die("❌ Query execution error: " . mysql_error());
    }

    if (mysql_affected_rows() > 0) {
        echo "<script>alert('Deleted Successfully');</script>";
    } else {
        echo "<script>alert('No rows updated. ID may not exist.');</script>";
    }

    // Include the updated list
    include("user_entry_list.php");
}

?>
