<?php
require_once('header.php');

if(!isset($_REQUEST['id']) || !is_numeric($_REQUEST['id'])){
    header('location: service.php');
    exit;
}

$id=(int)$_REQUEST['id'];

$statement=$pdo->prepare("
    SELECT *
    FROM tbl_service
    WHERE id=?
    LIMIT 1
");

$statement->execute([$id]);

$row=$statement->fetch(PDO::FETCH_ASSOC);

if(!$row){
    header('location: service.php');
    exit;
}

$photo=$row['main_image']??'';

if(!empty($photo)){

    $image_path=
        dirname(__DIR__).
        DIRECTORY_SEPARATOR.'assets'.
        DIRECTORY_SEPARATOR.'images'.
        DIRECTORY_SEPARATOR.'products'.
        DIRECTORY_SEPARATOR.$photo;

    if(file_exists($image_path)){
        unlink($image_path);
    }
}

$brochure=$row['brochure']??'';

if(!empty($brochure)){

    $brochure_path=
        dirname(__DIR__).
        DIRECTORY_SEPARATOR.$brochure;

    if(file_exists($brochure_path)){
        unlink($brochure_path);
    }
}

$statement=$pdo->prepare("
    DELETE FROM tbl_service_location
    WHERE service_id=?
");

$statement->execute([$id]);

$statement=$pdo->prepare("
    DELETE FROM tbl_service
    WHERE id=?
");

$statement->execute([$id]);

header('location: service.php');
exit;
?>