<?php
require_once 'config/db.php';

$edit_id = isset($_GET['edit']) ? (int)$_GET['edit'] : null;
$name = '';
$description = '';
$form_title = 'Add New Service';

if ($edit_id) {
    $stmt = $pdo->prepare("SELECT * FROM lead_services WHERE id = ?");
    $stmt->execute([$edit_id]);
    $srv = $stmt->fetch();
    if ($srv) {
        $name = $srv['name'];
        $description = $srv['description'];
        $form_title = 'Edit Service';
    } else {
        header("Location: lead_services.php");
        exit;
    }
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = trim($_POST['name'] ?? '');
    $description = trim($_POST['description'] ?? '');

    if (empty($name)) {
        $error = "Service Name is required.";
    } else {
        try {
            if ($edit_id) {
                $stmt = $pdo->prepare("UPDATE lead_services SET name = ?, description = ? WHERE id = ?");
                $stmt->execute([$name, $description, $edit_id]);
                header("Location: lead_services.php?msg=updated");
            } else {
                $stmt = $pdo->prepare("INSERT INTO lead_services (name, description) VALUES (?, ?)");
                $stmt->execute([$name, $description]);
                header("Location: lead_services.php?msg=added");
            }
            exit;
        } catch (PDOException $e) {
            $error = "Database Error: " . $e->getMessage();
        }
    }
}

require_once 'includes/header.php';
?>

<div class="dashboard-content" style="max-width: 600px; margin: 0 auto;">
    <div style="display: flex; align-items: center; justify-content: space-between; margin-bottom: 25px;">
        <h2><i class="fa-solid fa-briefcase"></i> <?= $form_title ?></h2>
        <a href="lead_services.php" style="color:var(--text-main); text-decoration: none; font-weight: 500;"><i class="fa-solid fa-arrow-left"></i> Back to Services</a>
    </div>

    <?php if(isset($error)): ?>
        <div class="badge badge-pending" style="display:block; padding:15px; margin-bottom: 20px; background-color: #fee2e2; color: #ef4444; font-size:1rem;"><?= htmlspecialchars($error) ?></div>
    <?php endif; ?>

    <div class="data-card" style="background:var(--white); padding:30px; border-radius:12px; box-shadow:0 4px 15px rgba(0,0,0,0.03);">
        <form method="POST">
            <div style="margin-bottom: 20px;">
                <label style="display: block; margin-bottom: 8px; font-weight: 600; color: #475569;">Service Name *</label>
                <input type="text" name="name" value="<?= htmlspecialchars($name) ?>" placeholder="e.g. Swimming Pool Construction" required style="width: 100%; padding: 12px; border: 1px solid var(--border); border-radius: 8px; font-size: 0.95rem;">
            </div>

            <div style="margin-bottom: 25px;">
                <label style="display: block; margin-bottom: 8px; font-weight: 600; color: #475569;">Description (Optional)</label>
                <textarea name="description" rows="5" placeholder="Short summary of the service..." style="width: 100%; padding: 12px; border: 1px solid var(--border); border-radius: 8px; font-size: 0.95rem; resize: vertical;"><?= htmlspecialchars($description) ?></textarea>
            </div>

            <div style="display: flex; gap: 15px;">
                <button type="submit" style="flex: 1; padding: 14px; background: var(--primary); color: #fff; border: none; border-radius: 8px; font-weight: 700; cursor: pointer; font-size: 1rem;">
                    <?= $edit_id ? 'Save Changes' : 'Create Service' ?>
                </button>
                <a href="lead_services.php" style="padding: 14px 25px; background: #f1f5f9; color: #475569; text-decoration: none; border-radius: 8px; font-weight: 600; text-align: center;">Cancel</a>
            </div>
        </form>
    </div>
</div>

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