<?php
include("../../include/db.php");
function user_screen_form($form_name)
{
 $sql="select * from screen_form where screen_name='$form_name'";
 $rs=mysql_query($sql);
 while($rsdata=mysql_fetch_array($rs))
 {
   $screen_id=$rsdata['screen_id'];
 }
 return $screen_id;
}
/********************************** ADD & UPDATE QUERY ***************************************/
if($_POST['action']=='ADD')
{
$form_name=$_POST['form_name'];



if($form_name=='1')
{
	$scr_name="DashBoard";
}
if($form_name=='2')
{
	$scr_name="Master Entry";
}
if($form_name=='3')
{
	$scr_name="JobCard Module";
}
if($form_name=='4')
{
	$scr_name="Delivery Module";
}
if($form_name=='5')
{
	$scr_name="Consumable";
}
if($form_name=='6')
{
	$scr_name="Printing";
}
if($form_name=='7')
{
	$scr_name="Fusing";
}

if($form_name=='8')
{
	$scr_name="Invoice";
}

$sub_screen=$_POST['sub_screen'];
$form_id=user_screen_form($form_name);
$create_date=date('Y-m-d');
$Insql=mysql_query("insert into sub_screen(screen_name,sub_screen,screen_ids,cr_date)values('$scr_name','$sub_screen','$form_name','$create_date')");
$Insql=mysql_query("insert into user_rights(user_name,user_screen,form_id,create_date)values('$form_name','$sub_screen','$form_id','$create_date')");
echo "Added Successfully!!";
}


/*********************************** UPDATE QUERY *********************************************/
if($_POST[action]=='EDIT')
{
	
$edit_id=$_POST['edit_id'];
$form_name=$_POST['form_name'];
$sub_screen=$_POST['sub_screen'];
$create_date=date('Y-m-d');
$acadamic_yr=explode("-",$create_date);
$acdmc_yr_res=$acadamic_yr[0];

if($form_name=='1')
{
	$scr_name="DashBoard";
}
if($form_name=='2')
{
	$scr_name="Master Entry";
}
if($form_name=='3')
{
	$scr_name="JobCard Module";
}
if($form_name=='4')
{
	$scr_name="Delivery Module";
}
if($form_name=='5')
{
	$scr_name="Consumable";
}
if($form_name=='6')
{
	$scr_name="Printing";
}
if($form_name=='7')
{
	$scr_name="Fusing";
}

if($form_name=='8')
{
	$scr_name="Invoice";
}
$Insql=mysql_query("update sub_screen SET screen_name='$scr_name',sub_screen='$sub_screen' where screen_id='$_POST[edit_id]'");
echo "Updated Successfully!!";
}

/**********************************************************************************************/
// if($_POST[action]== 'del_screen_sub')
// {
// $sub_screen=$_POST['sub_screen'];
//  $Insql=mysql_query("delete from sub_screen where screen_id='$_POST[screen_id]'");
//  $Insql=mysql_query("delete from user_rights where user_screen='$_POST[sub_screen]'");
// }
// include("user_screen_list.php");
if ($_POST['action'] == 'del_screen_sub') {
    include("../include/db_new.php");
    if (!$DB) {
        die("❌ Failed to select database: " . mysql_error());
    }

    // Get and sanitize input
    $screen_id = intval($_POST['screen_id']);
    $sub_screen = mysql_real_escape_string($_POST['sub_screen']);

    // Delete from sub_screen table
    $deleteSubScreenQuery = "DELETE FROM sub_screen WHERE screen_id = '$screen_id'";
    $subScreenResult = mysql_query($deleteSubScreenQuery, $ConDB);

    if (!$subScreenResult) {
        die("❌ Failed to delete from sub_screen: " . mysql_error());
    }

    // Delete from user_rights table
    $deleteUserRightsQuery = "DELETE FROM user_rights WHERE user_screen = '$sub_screen'";
    $userRightsResult = mysql_query($deleteUserRightsQuery, $ConDB);

    if (!$userRightsResult) {
        die("❌ Failed to delete from user_rights: " . mysql_error());
    }

    echo "<script>alert('Deleted Successfully');</script>";

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

?>
