<?php
include("../../include/db.php");

/**
 * ✅ Get screen_id from screen_form table
 */
function user_screen_form($form_name)
{
    $sql = "SELECT * FROM screen_form WHERE screen_name='$form_name'";
    $rs  = mysql_query($sql);
    $screen_id = null;
    while ($rsdata = mysql_fetch_array($rs)) {
        $screen_id = $rsdata['screen_id'];
    }
    return $screen_id;
}

/**
 * ✅ Centralized form_map (form_id => screen_name)
 */
$form_map = [
   '1'  => 'DashBoard',
'2'  => 'Master Entry',
'3'  => 'JobCard Module',
'4'  => 'Delivery Module',
'5'  => 'Consumable',
'6'  => 'Printing',
'7'  => 'Fusing',
'8'  => 'Invoice',
'21' => 'User Type Creation',
'22' => 'User Name Creation',
'23' => 'User Screen',
'24' => 'User Rights Creation',
'25' => 'Party Creation',
'26' => 'Unit Measurement',
'27' => 'Company Creation',
'28' => 'Operator Creation',
'29' => 'Shift Creation',
'30' => 'Machine Creation',
'31' => 'RollSize Creation',
'32' => 'Fabric Type Creation',
'33' => 'Job Type Creation',
'34' => 'Consumable Creation',
'35' => 'Payment Terms',
'36' => 'New Jobcard Creation',
'37' => 'Sample Jobcard Creation',
'38' => 'Media Entry',
'39' => 'Pending JobCard',
'40' => 'Inward Entry',
'41' => 'Fabric Delivery Challan',
'42' => 'Return Form',
'43' => 'Sticker Delivery Challan',
'44' => 'Empty can DC Form',
'45' => 'Delivery Report',
'46' => 'Ink Entry',
'47' => 'Media Purchase',
'48' => 'Ink Purchase',
'49' => 'Printer Production Entry',
'50' => 'Job No wise Report',
'51' => 'Operator wise Report',
'52' => 'Machine wise Report',
'53' => 'Printing Report',
'54' => 'Fusing Production Entry',
'55' => 'Job No wise Report',
'56' => 'Operator wise Report',
'57' => 'Machine wise Report',
'58' => 'Fusing Report',
'59' => 'Unbilling List',
'60' => 'Billing List',
'61' => 'Porforma Billing List',
'62' => 'Pay Slip',
'63' => 'Pay Slip Report',
'64' => 'Customer Wise Report',
'65' => 'Inhouse job Report Total',
'66' => 'Jobcard Wise Report',
'67' => 'Total Stock Wise Report',
'68' => 'Wages Report',
'69' => 'Customer wise Report',
'70' => 'Date wise Report',
'71' => 'Sample Customer Wise Report',
'72' => 'Date Wise Report',
'73' => 'Designer Wise Report',
'74' => 'Ink Report',
'75' => 'Media Report',
'76' => 'Stock Report',
'77' => 'Media Usage Analyze Report',
'78' => 'Ink Usage Consolidated Report',
'79' => 'Media Usage Console Report',
'80' => 'Fabric Report',
'81' => 'Job Process Report',
'82' => 'Consumable Return Form',
'83' => 'Jobcard Quotation',
'84' => 'Quotation Customer Report',
'85' => 'Deleted Jobcard',
'86' => 'Deleted Billing'
];

/********************************** ADD ***************************************/
if ($_POST['action'] == 'ADD') {
    $form_name  = $_POST['form_name'];   // numeric id like 21
    $scr_name   = isset($form_map[$form_name]) ? $form_map[$form_name] : '';
    $sub_screen = $_POST['sub_screen'];
    $form_id    = user_screen_form($scr_name);
    $create_date = date('Y-m-d');

    if ($scr_name == '') {
        die("❌ Invalid form_name mapping!");
    }

    // ✅ Insert into sub_screen if not exists
    $chk = mysql_query("SELECT * FROM sub_screen WHERE screen_name='$scr_name' AND sub_screen='$sub_screen' AND screen_ids='$form_name'");
    if (mysql_num_rows($chk) == 0) {
        mysql_query("INSERT INTO sub_screen (screen_name, sub_screen, screen_ids, cr_date) 
                     VALUES ('$scr_name','$sub_screen','$form_name','$create_date')");
    }

    // ✅ Insert into user_rights (store user_name as actual screen name, not numeric ID)
    $chk2 = mysql_query("SELECT * FROM user_rights WHERE user_name='$scr_name' AND user_screen='$sub_screen' AND form_id='$form_name'");
    if (mysql_num_rows($chk2) == 0) {
        mysql_query("INSERT INTO user_rights (user_name, user_screen, form_id, create_date) 
                     VALUES ('$scr_name','$sub_screen','$form_name','$create_date')");
    }

    echo "✅ Added Successfully!!";
}

/*********************************** EDIT **************************************/
if ($_POST['action'] == 'EDIT') {
    $edit_id    = intval($_POST['edit_id']);  // sub_screen.screen_id (PK)
     $edit = $_POST['edit'];
    //  echo $edit;
    //  exit();
    $form_name  = $_POST['form_name'];
    $sub_screen = $_POST['sub_screen'];

    $scr_name = isset($form_map[$form_name]) ? $form_map[$form_name] : '';
    $form_id  = user_screen_form($scr_name);

    if ($scr_name == '') {
        die("❌ Invalid form_name mapping!");
    }

    // ✅ Update sub_screen row
    mysql_query("UPDATE sub_screen 
                 SET screen_name='$scr_name', sub_screen='$sub_screen', screen_ids='$form_name' 
                 WHERE screen_id='$edit_id'");

    // ✅ Update user_rights row
    mysql_query("UPDATE user_rights 
                 SET user_screen='$sub_screen', form_id='$form_id', user_name='$scr_name' 
                 WHERE user_name='$scr_name' AND form_id='$form_id'");

    echo "✅ Updated Successfully!!";
}

/*********************************** DELETE ************************************/
if ($_POST['action'] == 'del_screen_sub') {
    // echo $_POST['screen_id']."karth";

    // include("../include/db_new.php");
    if (!$DB) {
        die("❌ Failed to select database: " . mysql_error());
    }

    $screen_id  = intval($_POST['screen_id']);

    $sub_screen = mysql_real_escape_string($_POST['sub_screen']);

    // delete from sub_screen
    mysql_query("DELETE FROM sub_screen WHERE screen_id='$screen_id'");
    // delete from user_rights
    mysql_query("DELETE FROM user_rights WHERE user_screen='$sub_screen'");

    echo "<script>alert('Deleted Successfully');</script>";
    include("user_screen_list.php");
}
?>
