<?php
session_start();
if(!isset($_SESSION['user_id'])){
    header("Location: /beauty_pos/login.php");
    exit();
}
include_once "../config/database.php";

$id = $_GET['id'] ?? 0;

if($id){

    // Check if exists
    $check = mysqli_query($conn, "SELECT id FROM expenses WHERE id='$id'");
    if(mysqli_num_rows($check) > 0){

        $delete = mysqli_query($conn, "DELETE FROM expenses WHERE id='$id'");

        if($delete){
            header("Location: /beauty_pos/expenses/index.php?success=deleted");
            exit();
        } else {
            header("Location: /beauty_pos/expenses/index.php?error=failed");
            exit();
        }

    } else {
        header("Location: /beauty_pos/expenses/index.php");
        exit();
    }

} else {
    header("Location: /beauty_pos/expenses/index.php");
    exit();
}