<?php
// 1. INCLUDE DATABASE CONNECTION
// This file must define your connection variable (e.g., $conn using PDO)
require_once '../new/con.php'; 

$message = ""; // Variable to hold success/error alerts

// 2. PROCESS FORM SUBMISSION
if (isset($_POST['submit'])) {
    // Collect and sanitize input data
    $service_name = trim($_POST['service_name']);
    $price = trim($_POST['price']);
    $status = 1; // Fixed status logic as requested

    // Basic validation
    if (empty($service_name) || empty($price)) {
        $message = "<div class='message error'>All fields are required.</div>";
    } else {
        try {
            // Prepared statement to prevent SQL Injection
            $sql = "INSERT INTO pricing (service_name, price, status) VALUES (:service_name, :price, :status)";
            $stmt = $conn->prepare($sql);
            
            // Bind parameters
            $stmt->bindParam(':service_name', $service_name);
            $stmt->bindParam(':price', $price);
            $stmt->bindParam(':status', $status, PDO::PARAM_INT);
            
            // Execute the query
            if ($stmt->execute()) {
                $message = "<div class='message success'>Pricing added successfully!</div>";
            } else {
                $message = "<div class='message error'>Something went wrong. Please try again.</div>";
            }
            
        } catch (PDOException $e) {
            // Display error details (useful for development)
            $message = "<div class='message error'>Database Error: " . htmlspecialchars($e->getMessage()) . "</div>";
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Add New Pricing</title>
    <style>
        * {
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin: 0;
            padding: 0;
        }
        body {
            background-color: #f4f6f9;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }
        .form-container {
            background: #ffffff;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.05);
            width: 100%;
            max-width: 400px;
        }
        h2 {
            margin-bottom: 20px;
            color: #333;
            font-size: 24px;
            text-align: center;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 8px;
            color: #666;
            font-size: 14px;
        }
        input[type="text"], input[type="number"] {
            width: 100%;
            padding: 12px;
            border: 1px solid #ccc;
            border-radius: 4px;
            font-size: 16px;
            transition: border-color 0.3s;
        }
        input:focus {
            border-color: #007bff;
            outline: none;
        }
        button {
            width: 100%;
            padding: 12px;
            background-color: #007bff;
            border: none;
            border-radius: 4px;
            color: white;
            font-size: 16px;
            cursor: pointer;
            transition: background 0.3s;
        }
        button:hover {
            background-color: #0056b3;
        }
        .message {
            padding: 12px;
            margin-bottom: 20px;
            border-radius: 4px;
            text-align: center;
            font-size: 14px;
            font-weight: 500;
        }
        .success { background-color: #d4edda; color: #155724; border: 1px solid #c3e6cb; }
        .error { background-color: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; }
    </style>
</head>
<body>

<div class="form-container">
    <h2>Add Service Pricing</h2>
    
    <?php echo $message; ?>

    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="POST">
        <div class="form-group">
            <label for="service_name">Service Name</label>
            <input type="text" id="service_name" name="service_name" placeholder="e.g., Premium Consultation" required>
        </div>
        
        <div class="form-group">
            <label for="price">Price ($)</label>
            <input type="number" id="price" name="price" step="0.01" placeholder="0.00" required>
        </div>
        
        <button type="submit" name="submit">Save Pricing</button>
    </form>
</div>

</body>
</html>