<?php
session_start();
require_once '../config/db.php';

if (isset($_GET['id'])) {
    $id = $conn->real_escape_string($_GET['id']);

    // 1. Optional: Fetch logo paths if you want to delete the actual image files from the server
    $res = $conn->query("SELECT left_logo, right_logo FROM company WHERE id = '$id'");
    if ($res->num_rows > 0) {
        $data = $res->fetch_assoc();
        
        // Delete files from the folder to save space (Optional but recommended)
        if (file_exists($data['left_logo'])) unlink($data['left_logo']);
        if (file_exists($data['right_logo'])) unlink($data['right_logo']);
    }

    // 2. Delete the record from the database
    $sql = "DELETE FROM company WHERE id = '$id'";

    if ($conn->query($sql) === TRUE) {
        $_SESSION['msg'] = "Company deleted successfully.";
        $_SESSION['msg_type'] = "success";
    } else {
        $_SESSION['msg'] = "Error deleting record: " . $conn->error;
        $_SESSION['msg_type'] = "error";
    }
}

// Redirect back to the management list
header("Location: ../manage-companies.php");
exit();
?>