<?php
include("../../include/db.php");
if(($_POST['action']=="ADD")) 
{
 $from_time=$_POST['from_time'];
 $to_time=$_POST['to_time'];
 $shift_name=$_POST['shift_name'];
 $description=$_POST['description'];
$Insql=mysql_query("insert into shift_creation(from_time,to_time,shift_name,description)values('$from_time','$to_time','$shift_name','$description')");
echo "Added Successfully!!";
}

if(($_POST['action']=="EDIT"))
{
$edit_id = $_POST['edit_id']; 
$from_time=$_POST['from_time'];
$to_time= $_POST['to_time'];
$shift_name=$_POST['shift_name'];
$description=$_POST['description'];
$Insql=mysql_query("update shift_creation set from_time='$from_time',to_time='$to_time',shift_name='$shift_name',description='$description'  where  id='$_POST[edit_id]'");
echo "Updated Successfully!!";
}

if (isset($_POST['action']) && $_POST['action'] == "delete") {
include("../../include/db_new.php");


if (!$ConDB) {
    die("❌ Database connection error: " . mysql_error());
}

$DB = mysql_select_db(ACSQL_DB_NAME, $ConDB);
if (!$DB) {
    die("❌ Failed to select database: " . mysql_error());
}


    $id = intval($_POST['id']); // Ensures it's an integer to prevent injection

    $deleteQuery = "DELETE FROM shift_creation WHERE id = '$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("shift_entry_list.php");
}
?>