<?php

session_start();

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

if(
    !isset($_SESSION['can_manage_users'])
    ||
    $_SESSION['can_manage_users'] != 1
){

    die("No tienes permisos.");

}

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

/*
|--------------------------------------------------------------------------
| GUARDAR LOGO
|--------------------------------------------------------------------------
*/

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

    if(
        isset($_FILES['crm_logo'])
        &&
        $_FILES['crm_logo']['error'] == 0
    ){

        $extension =
        strtolower(
            pathinfo(
                $_FILES['crm_logo']['name'],
                PATHINFO_EXTENSION
            )
        );

        $allowed = [

            'png',
            'jpg',
            'jpeg',
            'webp',
            'svg'

        ];

        if(!in_array($extension, $allowed)){

            $error =
            "Formato no permitido.";

        } else {

            $new_name =
            'logo_'.time().'.'.$extension;

            $upload_path =
            '../uploads/branding/'.$new_name;

            if(
                move_uploaded_file(
                    $_FILES['crm_logo']['tmp_name'],
                    $upload_path
                )
            ){

                $logo_path =
                '/uploads/branding/'.$new_name;

                /*
                |--------------------------------------------------------------------------
                | EXISTE
                |--------------------------------------------------------------------------
                */

                $exists = $conn->query("
                SELECT id
                FROM settings
                WHERE setting_key = 'crm_logo'
                LIMIT 1
                ");

                if($exists->num_rows > 0){

                    $conn->query("
                    UPDATE settings
                    SET setting_value = '".$conn->real_escape_string($logo_path)."'
                    WHERE setting_key = 'crm_logo'
                    ");

                } else {

                    $stmt =
                    $conn->prepare("
                    INSERT INTO settings
                    (
                        setting_key,
                        setting_value
                    )
                    VALUES
                    (
                        'crm_logo',
                        ?
                    )
                    ");

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

                    $stmt->execute();

                }

                $success =
                "Logo actualizado correctamente.";

            }

        }

    }

}

/*
|--------------------------------------------------------------------------
| LOGO ACTUAL
|--------------------------------------------------------------------------
*/

$current_logo = '';

$getLogo = $conn->query("
SELECT setting_value
FROM settings
WHERE setting_key = 'crm_logo'
LIMIT 1
");

if($getLogo->num_rows > 0){

    $logoData =
    $getLogo->fetch_assoc();

    $current_logo =
    $logoData['setting_value'];

}

?>

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

<head>

<meta charset="UTF-8">

<title>
Branding CRM
</title>

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

<style>

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

.white-card{
    background:#fff;
    border-radius:24px;
    padding:30px;
    box-shadow:0 10px 30px rgba(15,23,42,.06);
    max-width:700px;
}

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

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

.logo-preview{
    width:220px;
    height:220px;
    border-radius:24px;
    background:#f8fafc;
    display:flex;
    align-items:center;
    justify-content:center;
    overflow:hidden;
    margin-bottom:30px;
}

.logo-preview img{
    max-width:90%;
    max-height:90%;
    object-fit:contain;
}

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

.btn-save{
    margin-top:25px;
    height:52px;
    border:none;
    border-radius:14px;
    padding:0 24px;
    background:#5b4bff;
    color:#fff;
    font-weight:700;
    cursor:pointer;
}

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

.error-box{
    background:#fee2e2;
    color:#991b1b;
    padding:16px;
    border-radius:14px;
    margin-bottom:20px;
}

</style>

</head>

<body>

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

<div class="content-area">

<div class="white-card">

<div class="page-title">
Branding CRM
</div>

<div class="page-subtitle">
Personaliza el logo del sistema
</div>

<?php if($success): ?>

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

<?php endif; ?>

<?php if($error): ?>

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

<?php endif; ?>

<div class="logo-preview">

<?php if($current_logo != ''): ?>

<img
src="<?= htmlspecialchars($current_logo) ?>"
>

<?php else: ?>

Sin logo

<?php endif; ?>

</div>

<form
method="POST"
enctype="multipart/form-data"
>

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

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

</form>

</div>

</div>

</body>
</html>