<?php
include 'includes/auth.php';
include 'db.php';
include 'includes/header.php';

// ================= HANDLE ADD =================
if(isset($_POST['add_service'])){
    $name = $conn->real_escape_string($_POST['name']);
    $price = floatval($_POST['price']);

    $stmt = $conn->prepare("INSERT INTO services (name, price, created_at) VALUES (?, ?, NOW())");
    $stmt->bind_param("sd", $name, $price);
    $stmt->execute();
    echo "<script>alert('Service added successfully');window.location='services.php';</script>";
    exit;
}

// ================= HANDLE EDIT =================
if(isset($_POST['edit_service'])){
    $id = intval($_POST['id']);
    $name = $conn->real_escape_string($_POST['name']);
    $price = floatval($_POST['price']);

    $stmt = $conn->prepare("UPDATE services SET name=?, price=? WHERE id=?");
    $stmt->bind_param("sdi", $name, $price, $id);
    $stmt->execute();
    echo "<script>alert('Service updated successfully');window.location='services.php';</script>";
    exit;
}

// ================= HANDLE DELETE =================
if(isset($_GET['delete_id'])){
    $id = intval($_GET['delete_id']);
    $conn->query("DELETE FROM services WHERE id=$id");
    echo "<script>alert('Service deleted successfully');window.location='services.php';</script>";
    exit;
}

// ================= FETCH SERVICES =================
$services = $conn->query("SELECT * FROM services ORDER BY created_at DESC");
?>

<div class="container mt-4">
     <div class="welcome-msg">
        <i class="fa fa-user-circle"></i> Welcome, <strong><?= htmlspecialchars($_SESSION['username'] ?? 'Guest') ?></strong>!
    </div>
    <h3>Manage Services</h3>
    <hr>

    <!-- ADD SERVICE FORM -->
    <div class="card mb-4">
        <div class="card-header bg-primary text-white">Add New Service</div>
        <div class="card-body">
            <form method="POST" class="row g-3">
                <div class="col-md-6">
                    <label class="form-label">Service Name</label>
                    <input type="text" name="name" class="form-control" required>
                </div>
                <div class="col-md-4">
                    <label class="form-label">Price (MK)</label>
                    <input type="number" name="price" class="form-control" step="0.01" required>
                </div>
                <div class="col-md-2 d-flex align-items-end">
                    <button type="submit" name="add_service" class="btn btn-success w-100">
                        <i class="fa fa-plus"></i> Add
                    </button>
                </div>
            </form>
        </div>
    </div>

    <!-- SERVICES TABLE -->
    <div class="card">
        <div class="card-header bg-dark text-white">Existing Services</div>
        <div class="card-body">
            <table class="table table-bordered table-striped">
                <thead class="bg-secondary text-white">
                    <tr>
                        <th>#</th>
                        <th>Name</th>
                        <th>Price (MK)</th>
                        <th>Created At</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                <?php $i=1; while($s = $services->fetch_assoc()): ?>
                    <tr>
                        <td><?= $i++ ?></td>
                        <td><?= htmlspecialchars($s['name']) ?></td>
                        <td><?= number_format($s['price'],2) ?></td>
                        <td><?= date('Y-m-d H:i', strtotime($s['created_at'])) ?></td>
                        <td>
                            <!-- EDIT BUTTON -->
                            <button class="btn btn-sm btn-warning" data-bs-toggle="modal" data-bs-target="#editModal<?= $s['id'] ?>">
                                <i class="fa fa-edit"></i>
                            </button>
                            <!-- DELETE BUTTON -->
                            <a href="services.php?delete_id=<?= $s['id'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('Delete this service?');">
                                <i class="fa fa-trash"></i>
                            </a>
                        </td>
                    </tr>

                    <!-- EDIT MODAL -->
                    <div class="modal fade" id="editModal<?= $s['id'] ?>" tabindex="-1" aria-hidden="true">
                      <div class="modal-dialog">
                        <div class="modal-content">
                          <form method="POST">
                              <div class="modal-header">
                                <h5 class="modal-title">Edit Service</h5>
                                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                              </div>
                              <div class="modal-body">
                                <input type="hidden" name="id" value="<?= $s['id'] ?>">
                                <div class="mb-3">
                                    <label class="form-label">Service Name</label>
                                    <input type="text" name="name" class="form-control" value="<?= htmlspecialchars($s['name']) ?>" required>
                                </div>
                                <div class="mb-3">
                                    <label class="form-label">Price (MK)</label>
                                    <input type="number" name="price" class="form-control" step="0.01" value="<?= $s['price'] ?>" required>
                                </div>
                              </div>
                              <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                                <button type="submit" name="edit_service" class="btn btn-primary">Save Changes</button>
                              </div>
                          </form>
                        </div>
                      </div>
                    </div>
                <?php endwhile; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php include 'includes/footer.php'; ?>
