<?php
session_start();

if(!isset($_SESSION['user_id'])){

    header("Location: /login.php");
    exit;

}

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

$success = "";
$error = "";

/*
|--------------------------------------------------------------------------
| TABLA OFFICES
|--------------------------------------------------------------------------
*/

$conn->query("
CREATE TABLE IF NOT EXISTS offices (

    id INT AUTO_INCREMENT PRIMARY KEY,

    office_name VARCHAR(255) NOT NULL,

    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP

)
");

/*
|--------------------------------------------------------------------------
| AGREGAR office_name SI NO EXISTE
|--------------------------------------------------------------------------
*/

$check_column =
$conn->query("
SHOW COLUMNS
FROM offices
LIKE 'office_name'
");

if($check_column->num_rows == 0){

    $conn->query("
    ALTER TABLE offices
    ADD COLUMN office_name VARCHAR(255) NULL
    ");

}

/*
|--------------------------------------------------------------------------
| MIGRAR name -> office_name
|--------------------------------------------------------------------------
*/

$check_name_column =
$conn->query("
SHOW COLUMNS
FROM offices
LIKE 'name'
");

if($check_name_column->num_rows > 0){

    $conn->query("
    UPDATE offices
    SET office_name = name
    WHERE office_name IS NULL
    OR office_name = ''
    ");

}

/*
|--------------------------------------------------------------------------
| CREAR OFICINA
|--------------------------------------------------------------------------
*/

if(
    $_SERVER['REQUEST_METHOD'] == 'POST'
    &&
    isset($_POST['create_office'])
){

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

    if($office_name != ''){

        $safe_name =
        $conn->real_escape_string($office_name);

        $conn->query("
        INSERT INTO offices
        (
            office_name
        )
        VALUES
        (
            '$safe_name'
        )
        ");

        $success =
        "Oficina creada correctamente.";

    }

}

/*
|--------------------------------------------------------------------------
| EDITAR OFICINA
|--------------------------------------------------------------------------
*/

if(
    $_SERVER['REQUEST_METHOD'] == 'POST'
    &&
    isset($_POST['update_office'])
){

    $office_id =
    intval($_POST['office_id']);

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

    $safe_name =
    $conn->real_escape_string($office_name);

    $conn->query("
    UPDATE offices
    SET office_name = '$safe_name'
    WHERE id = '$office_id'
    ");

    $success =
    "Oficina actualizada correctamente.";

}

/*
|--------------------------------------------------------------------------
| ELIMINAR OFICINA
|--------------------------------------------------------------------------
*/

if(isset($_GET['delete'])){

    $delete_id =
    intval($_GET['delete']);

    $conn->query("
    DELETE FROM offices
    WHERE id = '$delete_id'
    ");

    header("Location: offices.php");
    exit;

}

/*
|--------------------------------------------------------------------------
| LISTADO
|--------------------------------------------------------------------------
*/

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

?>

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

<head>

<meta charset="UTF-8">

<title>
Oficinas
</title>

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

<style>

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

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

.page-subtitle{
    color:#64748b;
    margin-bottom:30px;
}

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

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

.form-group label{
    display:block;
    margin-bottom:10px;
    font-weight:700;
    color:#334155;
}

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

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

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

.office-table th{
    text-align:left;
    padding:16px;
    border-bottom:1px solid #e2e8f0;
    color:#64748b;
    font-size:13px;
    text-transform:uppercase;
}

.office-table td{
    padding:18px 16px;
    border-bottom:1px solid #e2e8f0;
    color:#02122c;
}

.actions{
    display:flex;
    gap:10px;
}

.btn-edit{
    height:40px;
    border:none;
    border-radius:12px;
    padding:0 16px;
    background:#eef2ff;
    color:#4338ca;
    font-weight:700;
    cursor:pointer;
}

.btn-delete{
    display:flex;
    align-items:center;
    justify-content:center;
    height:40px;
    padding:0 16px;
    border-radius:12px;
    text-decoration:none;
    background:#fee2e2;
    color:#dc2626;
    font-weight:700;
}

.success-box{
    background:#dcfce7;
    color:#166534;
    padding:15px;
    border-radius:14px;
    margin-bottom:20px;
}

</style>

</head>

<body>

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

<div class="content-area">

    <div class="page-title">
        Oficinas
    </div>

    <div class="page-subtitle">
        Gesti¨Žn de oficinas
    </div>

    <?php if($success): ?>

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

    <?php endif; ?>

    <div class="white-card">

        <form method="POST">

            <input
                type="hidden"
                name="create_office"
                value="1"
            >

            <div class="form-group">

                <label>
                    Nombre de oficina
                </label>

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

            </div>

            <button
                type="submit"
                class="btn-save"
            >
                Crear oficina
            </button>

        </form>

    </div>

    <div class="white-card">

        <table class="office-table">

            <thead>

                <tr>

                    <th>ID</th>
                    <th>Oficina</th>
                    <th>Acciones</th>

                </tr>

            </thead>

            <tbody>

                <?php while($office = $offices->fetch_assoc()): ?>

                    <tr>

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

                        <td>

                            <form
                                method="POST"
                                style="
                                display:flex;
                                gap:10px;
                                align-items:center;
                                "
                            >

                                <input
                                    type="hidden"
                                    name="update_office"
                                    value="1"
                                >

                                <input
                                    type="hidden"
                                    name="office_id"
                                    value="<?= $office['id'] ?>"
                                >

                                <input
                                    type="text"
                                    name="office_name"
                                    class="form-control"
                                    value="<?= htmlspecialchars($office['office_name'] ?? '') ?>"
                                    style="
                                    max-width:300px;
                                    "
                                >

                        </td>

                        <td>

                                <div class="actions">

                                    <button
                                        type="submit"
                                        class="btn-edit"
                                    >
                                        Guardar
                                    </button>

                                    <a
                                        href="?delete=<?= $office['id'] ?>"
                                        class="btn-delete"
                                    >
                                        Eliminar
                                    </a>

                                </div>

                            </form>

                        </td>

                    </tr>

                <?php endwhile; ?>

            </tbody>

        </table>

    </div>

</div>

</body>
</html>