<?php
session_start();

include("../includes/auth.php");

include("../config/db.php");

$success = "";

$conn->query("
CREATE TABLE IF NOT EXISTS campaigns (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL
)
");

if($_SERVER['REQUEST_METHOD'] == 'POST'){

    $name =
    trim($_POST['name']);

    if($name != ''){

        $stmt = $conn->prepare("
        INSERT INTO campaigns (name)
        VALUES (?)
        ");

        $stmt->bind_param(
            "s",
            $name
        );

        $stmt->execute();

        $success =
        "Campaña creada correctamente.";

    }

}

$campaigns = $conn->query("
SELECT *
FROM campaigns
ORDER BY id DESC
");

?>

<!DOCTYPE html>
<html lang="es">

<head>

<meta charset="UTF-8">

<title>
Campañas
</title>

<link rel="stylesheet" href="/assets/css/style.css">

<style>

.content-area{
    margin-left:260px;
    padding:40px;
}

.page-title{
    font-size:42px;
    font-weight:800;
    color:#02122c;
    margin-bottom:10px;
}

.page-subtitle{
    font-size:16px;
    color:#6b7280;
    margin-bottom:30px;
}

.white-card{
    background:#ffffff;
    border-radius:24px;
    padding:30px;
    margin-bottom:25px;
    box-shadow:0 10px 30px rgba(15,23,42,.06);
}

.form-group{
    margin-bottom:20px;
}

.form-group label{
    display:block;
    margin-bottom:10px;
    font-size:14px;
    font-weight:600;
    color:#5f6b7a;
}

.form-control{
    width:100%;
    height:52px;
    border:none;
    background:#f3f5fa;
    border-radius:14px;
    padding:0 16px;
    font-size:15px;
}

.btn-save{
    height:52px;
    border:none;
    border-radius:14px;
    padding:0 24px;
    background:linear-gradient(90deg,#5b4bff,#7b61ff);
    color:#ffffff;
    font-size:15px;
    font-weight:600;
    cursor:pointer;
}

.success-box{
    background:#dcfce7;
    color:#166534;
    padding:14px 18px;
    border-radius:12px;
    margin-bottom:20px;
    font-weight:600;
}

.crm-table{
    width:100%;
    border-collapse:collapse;
}

.crm-table th{
    text-align:left;
    padding:16px;
    font-size:13px;
    text-transform:uppercase;
    color:#7b8190;
    border-bottom:1px solid #edf0f5;
}

.crm-table td{
    padding:18px 16px;
    border-bottom:1px solid #edf0f5;
    color:#02122c;
    font-size:14px;
}

</style>

</head>

<body>

<?php include("../includes/sidebar.php"); ?>

<div class="content-area">

    <div class="page-title">
        Campañas
    </div>

    <div class="page-subtitle">
        Gestión de campañas comerciales
    </div>

    <div class="white-card">

        <?php if($success): ?>

            <div class="success-box">
                <?= $success ?>
            </div>

        <?php endif; ?>

        <form method="POST">

            <div class="form-group">

                <label>
                    Nombre de campaña
                </label>

                <input
                    type="text"
                    name="name"
                    class="form-control"
                    required
                >

            </div>

            <button
                type="submit"
                class="btn-save"
            >
                Crear campaña
            </button>

        </form>

    </div>

    <div class="white-card">

        <table class="crm-table">

            <thead>

                <tr>

                    <th>ID</th>
                    <th>Campaña</th>

                </tr>

            </thead>

            <tbody>

                <?php while($campaign = $campaigns->fetch_assoc()): ?>

                    <tr>

                        <td>
                            #<?= $campaign['id'] ?>
                        </td>

                        <td>
                            <?= htmlspecialchars($campaign['name']) ?>
                        </td>

                    </tr>

                <?php endwhile; ?>

            </tbody>

        </table>

    </div>

</div>

</body>
</html>