 <?php
 error_reporting(0);
include("../../include/db.php");
function get_checks($user_name,$val)
  {
	 $sql_user="select * from user_permissions where box_name='$val' and user_type_id='$user_name'";
	$rs_user=mysql_query($sql_user);
	$rs_count=mysql_num_rows($rs_user);
	return $rs_count;
 } 
 function get_check_data($user_name)
  {
    $sql_user="select * from user_permission_main where user_name='$user_name' ";
	$rs_user=mysql_query($sql_user);
	$rs_count=mysql_num_rows($rs_user);
	return $rs_count;
 } 


/********************************** ADD & UPDATE QUERY ***************************************/
if($_POST[action] == 'ADD')
{
$user_name=$_POST['user_name'];
$user_value=$_POST['user_value'];
$user_checks=$_POST['user_checks'];
$overall_main=$_POST['user_checks1'];
$pur_main_form=$_POST['jobcards'];
$overall_process=$_POST['deliverys'];
$account_main=$_POST['printers'];
$setting_form=$_POST['fusing'];
$stock_rep_main=$_POST['invoice'];
$consumable=$_POST['consumable'];

$create_date=date('Y-m-d');

$Insql=mysql_query("update user_permissions set permission='0' where user_type_id='$user_name' ");

if(($user_checks!=''))
{
$user_exp=explode(",",$user_checks);
foreach($user_exp as $val)
{
	$check=get_checks($user_name,$val);
	if($check=='0')
	{
      $Insql=mysql_query("insert into user_permissions(user_type_id,box_name,form_id,permission,cr_date)values('$user_name','$val','1','1','$create_date')");
	}
	else
	{
		$Insql=mysql_query("update user_permissions set permission='1' where user_type_id='$user_name' and box_name='$val' and form_id='1'");
		}
}
}
if(($overall_main!=''))
{
$user_main=explode(",",$overall_main);
foreach($user_main as $val)
{
	$check=get_checks($user_name,$val);
	if($check=='0')
	{
      $Insql=mysql_query("insert into user_permissions(user_type_id,box_name,form_id,permission,cr_date)values('$user_name','$val','2','1','$create_date')");
	}
	else{
		$Insql=mysql_query("update user_permissions set permission='1' where user_type_id='$user_name' and box_name='$val' and form_id='2'");
		}
}
}
if(($pur_main_form!=''))
{
$pur_main_form1=explode(",",$pur_main_form);
foreach($pur_main_form1 as $pur_main_form)
{
	 $check=get_checks($user_name,$pur_main_form);
	if($check=='0')
	{
$Insql=mysql_query("insert into user_permissions(user_type_id,box_name,form_id,permission,cr_date)values('$user_name','$pur_main_form','3','1','$create_date')");

	}else
	   {
		$Insql=mysql_query("update user_permissions set permission='1' where user_type_id='$user_name' and box_name='$pur_main_form' and form_id='3'");
		}
}
}
if(($overall_process!=''))
{
$overall_process1=explode(",",$overall_process);
foreach($overall_process1 as $overall_process)
{
	 $check=get_checks($user_name,$overall_process);
	if($check=='0')
	{
$Insql=mysql_query("insert into user_permissions(user_type_id,box_name,form_id,permission,cr_date)values('$user_name','$overall_process','4','1','$create_date')");
	}
	else 
	{
	$Insql=mysql_query("update user_permissions set permission='1' where user_type_id='$user_name' and box_name='$overall_process' and form_id='4' ");
	}
}
}

if(($account_main!=''))
{
$acco_exp=explode(",",$account_main);
foreach($acco_exp as $accounts)
{
	 $check=get_checks($user_name,$accounts);
	if($check=='0')
	{
$Insql=mysql_query("insert into user_permissions(user_type_id,box_name,form_id,permission,cr_date)values('$user_name','$accounts','6','1','$create_date')");
	}
	else
	{
	$Insql=mysql_query("update user_permissions set permission='1' where user_type_id='$user_name' and box_name='$accounts' and form_id='6' ");
}
}
}
if(($setting_form!=''))
{
$sett_exp=explode(",",$setting_form);
foreach($sett_exp as $settings)
{
	 $check=get_checks($user_name,$settings);
	if($check=='0')
	{
$Insql=mysql_query("insert into user_permissions(user_type_id,box_name,form_id,permission,cr_date)values('$user_name','$settings','7','1','$create_date')");
	}
	else{
	$Insql=mysql_query("update user_permissions set permission='1' where user_type_id='$user_name' and box_name='$settings' and form_id='7' ");
		}
}
}
if(($stock_rep_main!=''))
{
$sett_exp1=explode(",",$stock_rep_main);
foreach($sett_exp1 as $settings1)
{
	 $check=get_checks($user_name,$settings1);
	if($check=='0')
	{
$Insql=mysql_query("insert into user_permissions(user_type_id,box_name,form_id,permission,cr_date)values('$user_name','$settings1','8','1','$create_date')");
	}
	else{
	$Insql=mysql_query("update user_permissions set permission='1' where user_type_id='$user_name' and box_name='$settings1' and form_id='8' ");
		}
}
}
if(($consumable!=''))
{
$consumable2=explode(",",$consumable);
foreach($consumable2 as $consumable3)
{
	 $check=get_checks($user_name,$consumable3);
	if($check=='0')
	{
$Insql=mysql_query("insert into user_permissions(user_type_id,box_name,form_id,permission,cr_date)values('$user_name','$consumable3','5','1','$create_date')");
	}
	else{
	$Insql=mysql_query("update user_permissions set permission='1' where user_type_id='$user_name' and box_name='$consumable3' and form_id='5' ");
		}
}
}

$check_data=get_check_data($user_name);
if($check_data=='0')
{
$Insql=mysql_query("insert into user_permission_main(user_name,createdate)values('$user_name','$create_date')");
}
echo "<script>alert('Successfully Added')</script>";
}
// if($_POST[action]== 'del_user_type')
// {
//  $Insql=mysql_query("delete from user_permission_main where main_id='$_POST[user_id]'");
//  include("user_rights_list.php");
// }
if ($_POST['action'] == 'del_user_type') {
    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 deletion from user_permission_main
    $deleteQuery = "DELETE FROM user_permission_main WHERE main_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 deleted. ID may not exist.');</script>";
    }

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

?>
