<?php
include "ominterior_db.php";

// Get service ID from URL
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$service = null;

// Fetch existing data
if ($id > 0) {
    $result = $conn->query("SELECT * FROM services WHERE id = $id");
    if ($result->num_rows > 0) {
        $service = $result->fetch_assoc();
    } else {
        die("Service not found.");
    }
}

// Handle update
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $icon = $conn->real_escape_string($_POST['icon']);
    $title = $conn->real_escape_string($_POST['title']);
    $description = $conn->real_escape_string($_POST['description']);

    $updateSql = "UPDATE services SET icon='$icon', title='$title', description='$description' WHERE id=$id";

    if ($conn->query($updateSql) === TRUE) {
        echo "<div class='alert alert-success text-center'>Service updated successfully.</div>";
        header('location:services.php');
    } else {
        echo "<div class='alert alert-danger'>Error: " . $conn->error . "</div>";
    }
}

?>

<!doctype html>
<html lang="en" dir="ltr">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">

<link rel="icon" href="assets/images/black-logo.png" type="image/x-icon"/>

<title>OM INTERIOR - Dashborad</title>

<!-- Bootstrap Core and vandor -->
<link rel="stylesheet" href="assets/plugins/bootstrap/css/bootstrap.min.css" />

<!-- Plugins css -->
<link rel="stylesheet" href="assets/plugins/charts-c3/c3.min.css"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<!-- Core css -->
<link rel="stylesheet" href="assets/css/main2.css"/>
<link rel="stylesheet" href="assets/css/theme1.css"/>
</head>

<body class="font-montserrat">
<!-- Page Loader -->
<div class="page-loader-wrapper">
    <div class="loader">
    </div>
</div>

<div id="main_content">

<?php include 'haderTop_logo.php'?>
    
<?php include 'rightside_menu.php'?>

<?php include 'leftside_menu.php' ?>


    <div class="page">
        <div id="page_top" class="section-body top_dark">
            <div class="container-fluid">
                <div class="page-header">
                    <div class="left">
                        <a href="javascript:void(0)" class="icon menu_toggle mr-3"><i class="fa  fa-align-left"></i></a>
                        <h1 class="page-title">Services</h1>                        
                    </div>
                    <div class="right">
                        <?php include 'dropdown_flex_menu.php' ?>
                    </div>
                </div>
            </div>
        </div>

    
        <div class="container py-5">
            <h2 class="mb-4">Edit Service</h2>

            <?php if ($service): ?>
            <form method="POST" class="bg-white p-4 rounded shadow-sm">
                <div class="mb-3">
                    <label class="form-label">Icon</label>
                    <input type="text" class="form-control" name="icon" value="<?= htmlspecialchars($service['icon']) ?>" required>
                </div>
                <div class="mb-3">
                    <label class="form-label">Title</label>
                    <input type="text" class="form-control" name="title" value="<?= htmlspecialchars($service['title']) ?>" required>
                </div>
                <div class="mb-3">
                    <label class="form-label">Description</label>
                    <input type="text" class="form-control" name="description" value="<?= htmlspecialchars($service['description']) ?>" required>
                </div>
                <button type="submit" class="btn btn-success">Update Service</button>
                <a href="display_services.php" class="btn btn-secondary">Back</a>
            </form>
            <?php endif; ?>
        </div>
                                
                
        <div class="section-body">
            <footer class="footer">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-md-12 col-sm-12 text-md-right">
                            <ul class="list-inline mb-0">
                                <li class="list-inline-item"><a href="doc/index.html">OM INTERIOR</a></li>
                            </ul>
                        </div>
                    </div>
                </div>
            </footer>
        </div>
    </div>    
</div>




<script src="assets/bundles/lib.vendor.bundle.js"></script>

<script src="assets/bundles/apexcharts.bundle.js"></script>
<script src="assets/bundles/counterup.bundle.js"></script>
<script src="assets/bundles/knobjs.bundle.js"></script>
<script src="assets/bundles/c3.bundle.js"></script>

<script src="assets/js/core.js"></script>
<script src="assets/js/page/project-index.js"></script>
</body>
</html>
