<?php include 'dbconnect.php'; ?>

<?php
$success = "";
$error = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $client_name = $_POST['client_name'];
    $agent_name = $_POST['agent_name'];
    $topic = $_POST['topic'];
    $conversation_date = $_POST['conversation_date'];
    $client_message = $_POST['client_message'];
    $agent_reply = $_POST['agent_reply'];
    $next_day_plan = $_POST['next_day_plan'];
    $next_day_plan_date = $_POST['next_day_plan_date'];

    $sql = "INSERT INTO client_agent_conversations 
        (client_name, agent_name, topic, conversation_date, client_message, agent_reply, next_day_plan, next_day_plan_date) 
        VALUES (?, ?, ?, ?, ?, ?, ?, ?)";

    $stmt = $con->prepare($sql);
    $stmt->bind_param("ssssssss", $client_name, $agent_name, $topic, $conversation_date, $client_message, $agent_reply, $next_day_plan, $next_day_plan_date);

    if ($stmt->execute()) {
        $success = "✅ Conversation saved successfully!";
    } else {
        $error = "❌ Error: " . $stmt->error;
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Add Conversation</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        /* 👇 your full CSS */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(to right, #eef2f3, #8e9eab);
            min-height: 100vh;
            padding: 20px;
        }

        .top-bar {
            background-color: #2c3e50;
            color: #ecf0f1;
            padding: 15px 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            border-radius: 8px;
            margin-bottom: 30px;
        }

        .top-bar .logout-btn a {
            background-color: #e74c3c;
            color: #fff;
            padding: 8px 14px;
            text-decoration: none;
            border-radius: 5px;
            font-weight: bold;
        }

        .top-bar .logout-btn a:hover {
            background-color: #c0392b;
        }

        .container {
            background-color: #fff;
            padding: 25px 30px;
            border-radius: 12px;
            box-shadow: 0 4px 15px rgba(0,0,0,0.1);
            width: 100%;
            max-width: 550px;
            margin: auto;
        }

        h2 {
            margin-bottom: 20px;
            color: #333;
            text-align: center;
        }

        .form-box label {
            display: block;
            margin-top: 15px;
            margin-bottom: 5px;
            font-weight: bold;
            color: #444;
        }

        .form-box input[type="text"],
        .form-box input[type="date"],
        .form-box textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #bbb;
            border-radius: 6px;
            background-color: #f9f9f9;
            font-size: 16px;
        }

        .form-box textarea {
            resize: vertical;
            min-height: 80px;
        }

        .form-box input[type="submit"] {
            margin-top: 20px;
            width: 100%;
            padding: 12px;
            background-color: #4CAF50;
            border: none;
            color: white;
            font-weight: bold;
            border-radius: 6px;
            cursor: pointer;
            font-size: 16px;
        }

        .form-box input[type="submit"]:hover {
            background-color: #45a049;
        }

        .error, .success {
            padding: 12px;
            margin-bottom: 15px;
            border-left: 6px solid;
            border-radius: 4px;
        }

        .error {
            background-color: #ffdddd;
            color: red;
            border-color: #f44336;
        }

        .success {
            background-color: #ddffdd;
            color: green;
            border-color: #4CAF50;
        }

        @media (max-width: 600px) {
            .top-bar {
                flex-direction: column;
                align-items: flex-start;
            }

            .top-bar .logout-btn {
                margin-top: 10px;
            }
        }
    </style>
</head>
<body>

<div class="top-bar">
    <div class="logout-btn">
        <a href="logout.php">🔓 Logout</a>
    </div>
</div>

<div class="container">
    <h2>Add New Conversation</h2>

    <?php if (!empty($error)): ?>
        <div class="error"><?php echo $error; ?></div>
    <?php elseif (!empty($success)): ?>
        <div class="success"><?php echo $success; ?></div>
    <?php endif; ?>

    <form method="post" class="form-box">
        <label>Client/Employee Name:</label>
        <input type="text" name="client_name" required>

        <label>Agent Name:</label>
        <input type="text" name="agent_name" required>

        <label>Topic:</label>
        <input type="text" name="topic" required>

        <label>Date:</label>
        <input type="date" name="conversation_date" required>

        <label>Client/Employee Message:</label>
        <textarea name="client_message" required></textarea>

        <label>Agent Reply:</label>
        <textarea name="agent_reply" required></textarea>

        <label>Next Day Plan:</label>
        <textarea name="next_day_plan"></textarea>

        <label>Next Day Plan Date:</label>
        <input type="date" name="next_day_plan_date">

        <input type="submit" value="Save Conversation">
    </form>
</div>

</body>
</html>
