<?php
    include("connection.php");
    if (isset($_POST['upload'])) {
        echo "Received ID: " . $_POST['id']; // Debugging line to check if ID is received

        $filtered_file = $_FILES['filtered_file']['name'];
        $tmp = $_FILES['filtered_file']['tmp_name'];
    
        $status = 'Filter';
        $id = $_POST['id'];
        
        // Update the existing record
        $update_sql = "UPDATE whatsapp_filter SET filtered_file = '$filtered_file', status = '$status' WHERE id='$id'";
        
        $update_result = mysqli_query($conn, $update_sql);

        if ($update_result) {
            move_uploaded_file($tmp, "../user/sheets/".$filtered_file);
            echo "Update successful!";
            header("Location: whatsapp_filter.php?msg=1");
            exit();
        } else {
            echo "Update failed: " . mysqli_error($conn);
            header("Location: whatsapp_filter.php?msg=2");
            exit();
        }
    }



    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        $itemId = $_POST['item_id'];
    
        if (isset($_POST['reject'])) {
            $status = 'Reject';
    
            $updateQuery = "UPDATE whatsapp_filter SET status = '$status' WHERE id = '$itemId'";
            $updateResult = mysqli_query($conn, $updateQuery);
    
            if ($updateResult) {
                // Redirect back to the view table page
                header('Location: whatsapp_filter.php');
                exit();
            } else {
                echo "Update failed: " . mysqli_error($conn); // Debugging purposes
            }
        }
    }

?>
