<?php

require_once __DIR__ . '/db/auth.php';
require_once __DIR__ . '/db/scstatsitem.php';
require_once __DIR__ . '/db/scitemcustom.php';

auth_start_session();
auth_bootstrap();
scstatsitem_bootstrap();
scitemcustom_bootstrap();

if (!auth_is_admin()) {
    header('Location: index.php');
    exit;
}

function scitemcustom_normalize_sign(?string $sign): string
{
    if ($sign === '-') {
        return '-';
    }

    if ($sign === '+') {
        return '+';
    }

    return '';
}

function scitemcustom_normalize_value(?string $value): ?string
{
    $raw = str_replace(',', '.', trim((string) $value));
    if ($raw === '' || !is_numeric($raw)) {
        return null;
    }

    return number_format(abs((float) $raw), 2, '.', '');
}

function scitemcustom_display_value($value): string
{
    if (!is_numeric((string) $value)) {
        return '0';
    }

    $formatted = number_format((float) $value, 2, '.', '');
    $formatted = rtrim(rtrim($formatted, '0'), '.');

    return $formatted === '' ? '0' : $formatted;
}

function scitemcustom_preview(string $sign, $value, string $unit): string
{
    $prefix = $sign === '-' ? '-' : ($sign === '+' ? '+' : '');

    return $prefix . scitemcustom_display_value($value) . ' ' . $unit;
}

function scitemcustom_current_owner_auth_id(PDO $db): int
{
    $session_user = isset($_SESSION['user']) ? trim((string) $_SESSION['user']) : '';
    if ($session_user === '') {
        return 0;
    }

    $stmt_owner = $db->prepare(
        'SELECT cl_auth_id
         FROM tbl_auth
         WHERE cl_auth_user = :user
         LIMIT 1'
    );
    $stmt_owner->execute(['user' => $session_user]);

    return (int) $stmt_owner->fetchColumn();
}

$flash = auth_flash_get();
$flash_type = $flash['type'] ?? '';
$flash_message = $flash['message'] ?? '';

$db = db();
$csrf_token = auth_csrf_token();
$allowed_signs = ['+', '', '-'];
$sign_labels = ['+' => '+', '' => 'Aucun', '-' => '-'];
$current_owner_auth_id = scitemcustom_current_owner_auth_id($db);

if ($current_owner_auth_id <= 0) {
    auth_flash_set('error', 'Utilisateur introuvable. Merci de vous reconnecter.');
    header('Location: logout.php');
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $submitted_csrf = $_POST['csrf_token'] ?? '';
    if (!auth_validate_csrf($submitted_csrf)) {
        auth_flash_set('error', 'Jeton CSRF invalide.');
        header('Location: scitemcustom.php');
        exit;
    }

    $action = $_POST['action'] ?? '';

    if ($action === 'add_custom_item') {
        $obj_id = (int) ($_POST['obj_id'] ?? 0);
        if ($obj_id <= 0) {
            auth_flash_set('error', 'Objet invalide.');
        } else {
            try {
                $stmt_check = $db->prepare('SELECT cl_scobjs_id FROM tbl_scobjs WHERE cl_scobjs_id = :id');
                $stmt_check->execute(['id' => $obj_id]);
                if (!$stmt_check->fetch()) {
                    auth_flash_set('error', 'Objet introuvable.');
                } else {
                    $stmt_insert = $db->prepare(
                        'INSERT INTO tbl_scitemcustom (cl_scitemcustom_owner_auth_id, cl_scitemcustom_obj_id)
                         VALUES (:owner_auth_id, :obj_id)'
                    );
                    $stmt_insert->execute([
                        'owner_auth_id' => $current_owner_auth_id,
                        'obj_id' => $obj_id,
                    ]);
                    auth_flash_set('success', 'Objet ajouté dans Item Custom.');
                }
            } catch (PDOException $e) {
                if ((string) $e->getCode() === '23000') {
                    auth_flash_set('error', 'Cet objet est déjà présent dans Item Custom.');
                } else {
                    auth_flash_set('error', 'Erreur lors de l\'ajout : ' . $e->getMessage());
                }
            }
        }

        header('Location: scitemcustom.php');
        exit;
    }

    if ($action === 'delete_custom_item') {
        $itemcustom_id = (int) ($_POST['itemcustom_id'] ?? 0);
        if ($itemcustom_id > 0) {
            try {
                $stmt_delete = $db->prepare(
                    'DELETE FROM tbl_scitemcustom
                     WHERE cl_scitemcustom_id = :id
                       AND cl_scitemcustom_owner_auth_id = :owner_auth_id'
                );
                $stmt_delete->execute([
                    'id' => $itemcustom_id,
                    'owner_auth_id' => $current_owner_auth_id,
                ]);

                if ($stmt_delete->rowCount() > 0) {
                    auth_flash_set('success', 'Objet Item Custom supprimé.');
                } else {
                    auth_flash_set('error', 'Objet introuvable ou non autorisé.');
                }
            } catch (PDOException $e) {
                auth_flash_set('error', 'Erreur lors de la suppression : ' . $e->getMessage());
            }
        }

        header('Location: scitemcustom.php');
        exit;
    }

    if ($action === 'add_custom_stat') {
        $itemcustom_id = (int) ($_POST['itemcustom_id'] ?? 0);
        $stat_id = (int) ($_POST['stat_id'] ?? 0);
        $sign = scitemcustom_normalize_sign($_POST['sign'] ?? '+');
        $value = scitemcustom_normalize_value($_POST['value'] ?? '');

        if ($itemcustom_id <= 0 || $stat_id <= 0 || $value === null) {
            auth_flash_set('error', 'Données de statistique invalides.');
        } else {
            try {
                $stmt_check = $db->prepare(
                    'SELECT c.cl_scitemcustom_id, s.cl_scstatsitem_id
                     FROM tbl_scitemcustom c
                     JOIN tbl_scstatsitem s ON s.cl_scstatsitem_id = :stat_id
                     WHERE c.cl_scitemcustom_id = :itemcustom_id
                       AND c.cl_scitemcustom_owner_auth_id = :owner_auth_id'
                );
                $stmt_check->execute([
                    'itemcustom_id' => $itemcustom_id,
                    'stat_id' => $stat_id,
                    'owner_auth_id' => $current_owner_auth_id,
                ]);

                if (!$stmt_check->fetch()) {
                    auth_flash_set('error', 'Objet ou statistique introuvable.');
                } else {
                    $stmt_insert = $db->prepare(
                        'INSERT INTO tbl_scitemcustomstat (
                            cl_scitemcustomstat_itemcustom_id,
                            cl_scitemcustomstat_stat_id,
                            cl_scitemcustomstat_sign,
                            cl_scitemcustomstat_value
                        ) VALUES (:itemcustom_id, :stat_id, :sign, :value)'
                    );
                    $stmt_insert->execute([
                        'itemcustom_id' => $itemcustom_id,
                        'stat_id' => $stat_id,
                        'sign' => $sign,
                        'value' => $value,
                    ]);
                    auth_flash_set('success', 'Statistique ajoutée à l\'objet.');
                }
            } catch (PDOException $e) {
                if ((string) $e->getCode() === '23000') {
                    auth_flash_set('error', 'Cette statistique est déjà configurée pour cet objet.');
                } else {
                    auth_flash_set('error', 'Erreur lors de l\'ajout : ' . $e->getMessage());
                }
            }
        }

        header('Location: scitemcustom.php');
        exit;
    }

    if ($action === 'update_custom_stat') {
        $custom_stat_id = (int) ($_POST['custom_stat_id'] ?? 0);
        $stat_id = (int) ($_POST['stat_id'] ?? 0);
        $sign = scitemcustom_normalize_sign($_POST['sign'] ?? '+');
        $value = scitemcustom_normalize_value($_POST['value'] ?? '');

        if ($custom_stat_id <= 0 || $stat_id <= 0 || $value === null) {
            auth_flash_set('error', 'Données de mise à jour invalides.');
        } else {
            try {
                $stmt_update = $db->prepare(
                    'UPDATE tbl_scitemcustomstat cs
                     JOIN tbl_scitemcustom c ON c.cl_scitemcustom_id = cs.cl_scitemcustomstat_itemcustom_id
                     JOIN tbl_scstatsitem s ON s.cl_scstatsitem_id = :stat_id
                     SET cs.cl_scitemcustomstat_stat_id = :stat_id,
                         cs.cl_scitemcustomstat_sign = :sign,
                         cs.cl_scitemcustomstat_value = :value
                     WHERE cs.cl_scitemcustomstat_id = :id
                       AND c.cl_scitemcustom_owner_auth_id = :owner_auth_id'
                );
                $stmt_update->execute([
                    'stat_id' => $stat_id,
                    'sign' => $sign,
                    'value' => $value,
                    'id' => $custom_stat_id,
                    'owner_auth_id' => $current_owner_auth_id,
                ]);

                if ($stmt_update->rowCount() > 0) {
                    auth_flash_set('success', 'Statistique mise à jour.');
                } else {
                    auth_flash_set('error', 'Statistique introuvable ou non autorisée.');
                }
            } catch (PDOException $e) {
                if ((string) $e->getCode() === '23000') {
                    auth_flash_set('error', 'Cette statistique est déjà configurée pour cet objet.');
                } else {
                    auth_flash_set('error', 'Erreur lors de la mise à jour : ' . $e->getMessage());
                }
            }
        }

        header('Location: scitemcustom.php');
        exit;
    }

    if ($action === 'delete_custom_stat') {
        $custom_stat_id = (int) ($_POST['custom_stat_id'] ?? 0);
        if ($custom_stat_id > 0) {
            try {
                $stmt_delete = $db->prepare(
                    'DELETE cs
                     FROM tbl_scitemcustomstat cs
                     JOIN tbl_scitemcustom c ON c.cl_scitemcustom_id = cs.cl_scitemcustomstat_itemcustom_id
                     WHERE cs.cl_scitemcustomstat_id = :id
                       AND c.cl_scitemcustom_owner_auth_id = :owner_auth_id'
                );
                $stmt_delete->execute([
                    'id' => $custom_stat_id,
                    'owner_auth_id' => $current_owner_auth_id,
                ]);

                if ($stmt_delete->rowCount() > 0) {
                    auth_flash_set('success', 'Statistique supprimée de l\'objet.');
                } else {
                    auth_flash_set('error', 'Statistique introuvable ou non autorisée.');
                }
            } catch (PDOException $e) {
                auth_flash_set('error', 'Erreur lors de la suppression : ' . $e->getMessage());
            }
        }

        header('Location: scitemcustom.php');
        exit;
    }
}

$search = trim($_GET['search'] ?? '');
$search_results = [];
if ($search !== '') {
    $stmt_search = $db->prepare(
        "SELECT *
         FROM tbl_scobjs
         WHERE (cl_scobjs_name LIKE :search OR cl_scobjs_type LIKE :search OR cl_scobjs_subtype LIKE :search OR cl_scobjs_uuid LIKE :search)
           AND cl_scobjs_id NOT IN (
               SELECT cl_scitemcustom_obj_id
               FROM tbl_scitemcustom
               WHERE cl_scitemcustom_owner_auth_id = :owner_auth_id
           )
         ORDER BY cl_scobjs_name ASC
         LIMIT 15"
    );
    $stmt_search->execute([
        'search' => '%' . $search . '%',
        'owner_auth_id' => $current_owner_auth_id,
    ]);
    $search_results = $stmt_search->fetchAll();
}

$stmt_stats_catalog = $db->query('SELECT * FROM tbl_scstatsitem ORDER BY cl_scstatsitem_name ASC, cl_scstatsitem_id ASC');
$stats_catalog = $stmt_stats_catalog->fetchAll();
$stats_by_id = [];
foreach ($stats_catalog as $stat_catalog_row) {
    $stats_by_id[(int) $stat_catalog_row['cl_scstatsitem_id']] = $stat_catalog_row;
}

$sql_custom_items = "SELECT c.*, o.cl_scobjs_name, o.cl_scobjs_uuid, o.cl_scobjs_type, o.cl_scobjs_subtype, o.cl_scobjs_rarity
                     FROM tbl_scitemcustom c
                     JOIN tbl_scobjs o ON o.cl_scobjs_id = c.cl_scitemcustom_obj_id
                     WHERE c.cl_scitemcustom_owner_auth_id = :owner_auth_id
                     ORDER BY o.cl_scobjs_name ASC, c.cl_scitemcustom_id ASC";
$stmt_custom_items = $db->prepare($sql_custom_items);
$stmt_custom_items->execute(['owner_auth_id' => $current_owner_auth_id]);
$custom_items = $stmt_custom_items->fetchAll();

$stmt_custom_stats = $db->prepare(
    "SELECT cs.*, st.cl_scstatsitem_name, st.cl_scstatsitem_unit
     FROM tbl_scitemcustomstat cs
     JOIN tbl_scitemcustom c ON c.cl_scitemcustom_id = cs.cl_scitemcustomstat_itemcustom_id
     JOIN tbl_scstatsitem st ON st.cl_scstatsitem_id = cs.cl_scitemcustomstat_stat_id
     WHERE c.cl_scitemcustom_owner_auth_id = :owner_auth_id
     ORDER BY cs.cl_scitemcustomstat_itemcustom_id ASC, st.cl_scstatsitem_name ASC, cs.cl_scitemcustomstat_id ASC"
);
$stmt_custom_stats->execute(['owner_auth_id' => $current_owner_auth_id]);
$custom_stats_rows = $stmt_custom_stats->fetchAll();
$custom_stats_by_item = [];
foreach ($custom_stats_rows as $custom_stat_row) {
    $item_key = (int) $custom_stat_row['cl_scitemcustomstat_itemcustom_id'];
    if (!isset($custom_stats_by_item[$item_key])) {
        $custom_stats_by_item[$item_key] = [];
    }
    $custom_stats_by_item[$item_key][] = $custom_stat_row;
}

$current_session_user = $_SESSION['user'] ?? '';
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Item Custom | R.E.A.C.T. Admin</title>
    <link rel="stylesheet" type="text/css" href="css/styles.css">
    <link rel="stylesheet" type="text/css" href="css/default.css">
    <style>
        :root {
            --primary: #a29b78;
            --primary-glow: rgba(162, 155, 120, 0.4);
            --bg-dark: #080a0f;
            --card-bg: rgba(20, 24, 33, 0.85);
            --border-glow: rgba(162, 155, 120, 0.25);
            --danger: #ff4d4d;
            --success: #00ff88;
        }

        @font-face {
            font-family: 'Electrolize';
            src: url('fonts/Electrolize-Regular.ttf') format('truetype');
        }

        body {
            background: radial-gradient(circle at top right, #1a1f2e, var(--bg-dark));
            background-attachment: fixed;
            color: #e0e0e0;
            font-family: 'Electrolize', sans-serif;
            margin: 0;
            overflow-x: hidden;
            min-height: 100vh;
        }

        .admin-layout {
            display: flex;
            flex-direction: column;
            max-width: 1400px;
            margin: 0 auto;
            padding: 2rem;
            animation: fadeIn 0.6s ease-out;
        }

        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(10px); }
            to { opacity: 1; transform: translateY(0); }
        }

        .admin-topbar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 1.5rem 2rem;
            background: var(--card-bg);
            backdrop-filter: blur(10px);
            border: 1px solid var(--border-glow);
            border-radius: 12px;
            margin-bottom: 2rem;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
            gap: 1rem;
        }

        .topbar-info h1 {
            margin: 0;
            font-size: 1.5rem;
            letter-spacing: 2px;
            text-transform: uppercase;
            background: linear-gradient(90deg, #fff, var(--primary));
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }

        .topbar-info p {
            margin: 0.25rem 0 0;
            font-size: 0.85rem;
            color: var(--primary);
            opacity: 0.8;
        }

        .btn-modern {
            padding: 0.6rem 1.2rem;
            border: 1px solid var(--primary);
            background: transparent;
            color: #fff;
            font-family: 'Electrolize', sans-serif;
            font-size: 0.9rem;
            text-transform: uppercase;
            cursor: pointer;
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            border-radius: 4px;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 5px;
            box-sizing: border-box;
        }

        .btn-modern:hover {
            background: var(--primary);
            color: var(--bg-dark);
            box-shadow: 0 0 15px var(--primary-glow);
        }

        .btn-modern.danger { border-color: var(--danger); color: var(--danger); }
        .btn-modern.danger:hover { background: var(--danger); color: #fff; }
        .btn-mini { padding: 0.45rem 0.75rem; font-size: 0.75rem; }

        .nav-tabs { display: flex; gap: 1rem; margin-bottom: 2rem; border-bottom: 1px solid var(--border-glow); padding-bottom: 1rem; }
        .nav-tabs a { text-decoration: none; color: #888; text-transform: uppercase; font-size: 0.9rem; transition: color 0.3s; }
        .nav-tabs a:hover, .nav-tabs a.active { color: var(--primary); }

        .flash {
            padding: 1rem 1.25rem;
            border-radius: 10px;
            margin-bottom: 1.5rem;
            border: 1px solid transparent;
        }

        .flash.success {
            background: rgba(0, 255, 136, 0.08);
            color: var(--success);
            border-color: rgba(0, 255, 136, 0.2);
        }

        .flash.error {
            background: rgba(255, 77, 77, 0.08);
            color: #ffb3b3;
            border-color: rgba(255, 77, 77, 0.2);
        }

        .admin-grid {
            display: grid;
            grid-template-columns: 370px 1fr;
            gap: 2rem;
            align-items: start;
        }

        .glass-card {
            background: var(--card-bg);
            backdrop-filter: blur(10px);
            border: 1px solid var(--border-glow);
            border-radius: 14px;
            box-shadow: 0 15px 40px rgba(0, 0, 0, 0.28);
            padding: 1.5rem;
        }

        .glass-card h2,
        .glass-card h3 {
            margin-top: 0;
            color: var(--primary);
            letter-spacing: 1px;
            text-transform: uppercase;
        }

        .stack {
            display: flex;
            flex-direction: column;
            gap: 1.5rem;
        }

        .form-group {
            margin-bottom: 1rem;
        }

        .form-group label {
            display: block;
            margin-bottom: 0.45rem;
            color: var(--primary);
            font-size: 0.85rem;
            text-transform: uppercase;
            letter-spacing: 1px;
        }

        .form-control {
            width: 100%;
            box-sizing: border-box;
            padding: 0.85rem 1rem;
            border-radius: 8px;
            background: rgba(255, 255, 255, 0.04);
            border: 1px solid rgba(162, 155, 120, 0.15);
            color: #fff;
            outline: none;
            font-family: 'Electrolize', sans-serif;
        }

        .form-control:focus {
            border-color: var(--primary);
            box-shadow: 0 0 0 3px rgba(162, 155, 120, 0.15);
        }

        .form-help {
            margin-top: 0.4rem;
            font-size: 0.8rem;
            color: #9fa7b8;
            line-height: 1.4;
        }

        .search-result-list,
        .item-card-list {
            display: flex;
            flex-direction: column;
            gap: 1rem;
        }

        .search-item,
        .custom-item-card {
            border: 1px solid var(--border-glow);
            border-radius: 12px;
            padding: 1rem;
            background: rgba(255, 255, 255, 0.03);
        }

        .search-item {
            display: flex;
            gap: 0.9rem;
            align-items: center;
            justify-content: space-between;
        }

        .item-meta {
            display: flex;
            align-items: center;
            gap: 0.9rem;
            min-width: 0;
        }

        .item-preview {
            width: 56px;
            height: 56px;
            object-fit: contain;
            border-radius: 10px;
            background: rgba(255, 255, 255, 0.04);
            border: 1px solid rgba(255, 255, 255, 0.08);
            padding: 0.3rem;
            flex-shrink: 0;
        }

        .item-name {
            color: #fff;
            font-size: 1rem;
            display: block;
        }

        .item-submeta {
            color: #96a0b5;
            font-size: 0.78rem;
            line-height: 1.45;
            word-break: break-word;
        }

        .custom-item-card {
            display: flex;
            flex-direction: column;
            gap: 1.25rem;
        }

        .custom-item-header {
            display: flex;
            justify-content: space-between;
            gap: 1rem;
            align-items: center;
            flex-wrap: wrap;
        }

        .inline-form {
            display: grid;
            grid-template-columns: minmax(0, 1.4fr) 110px 120px auto;
            gap: 0.75rem;
            align-items: end;
        }

        .modern-table {
            width: 100%;
            border-collapse: collapse;
            min-width: 780px;
        }

        .modern-table th,
        .modern-table td {
            padding: 0.9rem 0.8rem;
            border-bottom: 1px solid rgba(255, 255, 255, 0.08);
            vertical-align: middle;
        }

        .modern-table th {
            color: var(--primary);
            text-transform: uppercase;
            font-size: 0.78rem;
            letter-spacing: 1px;
            text-align: left;
        }

        .modern-table tbody tr:hover {
            background: rgba(255, 255, 255, 0.025);
        }

        .empty-state {
            padding: 2rem;
            text-align: center;
            color: #7f8897;
        }

        .badge {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            padding: 0.35rem 0.65rem;
            border-radius: 999px;
            border: 1px solid rgba(162, 155, 120, 0.25);
            background: rgba(162, 155, 120, 0.08);
            color: var(--primary);
            font-size: 0.78rem;
            white-space: nowrap;
        }

        .preview-pill {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            padding: 0.45rem 0.75rem;
            border-radius: 999px;
            background: rgba(255, 255, 255, 0.05);
            border: 1px solid rgba(255, 255, 255, 0.08);
            color: #fff;
            font-size: 0.85rem;
            min-width: 90px;
        }

        .actions-row {
            display: flex;
            gap: 0.4rem;
            justify-content: flex-end;
            flex-wrap: wrap;
        }

        .muted {
            color: #96a0b5;
        }

        .count-note {
            color: #96a0b5;
            font-size: 0.85rem;
            margin-top: -0.3rem;
            margin-bottom: 1rem;
        }

        .list-filter-bar {
            display: flex;
            gap: 0.75rem;
            align-items: center;
            flex-wrap: wrap;
            margin-bottom: 1rem;
        }

        .list-filter-input {
            flex: 1;
            min-width: 240px;
        }

        .list-filter-meta {
            color: #96a0b5;
            font-size: 0.8rem;
        }

        .hidden-by-filter {
            display: none !important;
        }

        @media (max-width: 1200px) {
            .admin-grid {
                grid-template-columns: 1fr;
            }
        }

        @media (max-width: 900px) {
            .admin-layout {
                padding: 1rem;
            }

            .admin-topbar {
                padding: 1rem 1.2rem;
                flex-direction: column;
                align-items: flex-start;
            }

            .inline-form {
                grid-template-columns: 1fr;
            }

            .search-item {
                flex-direction: column;
                align-items: stretch;
            }

            .actions-row {
                justify-content: stretch;
            }
        }
    </style>
</head>
<body>
    <div class="admin-layout">
        <header class="admin-topbar">
            <div class="topbar-info">
                <h1>Item Custom</h1>
                <p>Associer des objets à autant de bonus / malus de stats que nécessaire</p>
            </div>
            <div style="display:flex; gap:10px; flex-wrap:wrap; align-items:center;">
                <span style="opacity:0.8;">Session: <?php echo htmlspecialchars($current_session_user, ENT_QUOTES, 'UTF-8'); ?></span>
                <a href="index.php" class="btn-modern">Retour au site</a>
                <a href="logout.php" class="btn-modern danger">Session End</a>
            </div>
        </header>

        <nav class="nav-tabs">
            <a href="admin.php">Utilisateurs</a>
            <a href="scwebhook.php">WEBHOOK</a>
            <a href="scnotification.php">NOTIF DISCORD</a>
            <a href="scitems.php">Base d'Objets</a>
            <a href="scstatsitem.php">Stats Item</a>
            <a href="scitemcustom.php" class="active">Item Custom</a>
            <a href="scmining.php">Scanner Minage</a>
            <a href="scmanufactures.php">Manufactures</a>
            <a href="scvaisseaux.php">Vaisseaux</a>
            <a href="scpreset.php">Presets Vaisseau</a>
        </nav>

        <?php if ($flash_message !== ''): ?>
            <div class="flash <?php echo htmlspecialchars($flash_type, ENT_QUOTES, 'UTF-8'); ?>">
                <?php echo htmlspecialchars($flash_message, ENT_QUOTES, 'UTF-8'); ?>
            </div>
        <?php endif; ?>

        <div class="admin-grid">
            <aside class="stack">
                <section class="glass-card">
                    <h2>Ajouter un objet</h2>
                    <p class="count-note">Choisis un objet depuis la base, puis attribue-lui autant de stats que nécessaire.</p>
                    <form method="get" action="scitemcustom.php" style="display:flex; gap:0.75rem; flex-wrap:wrap;">
                        <input type="text" name="search" class="form-control" value="<?php echo htmlspecialchars($search, ENT_QUOTES, 'UTF-8'); ?>" placeholder="Rechercher un objet..." style="flex:1; min-width:220px;">
                        <button type="submit" class="btn-modern">Rechercher</button>
                        <a href="scitemcustom.php" class="btn-modern danger">Reset</a>
                    </form>

                    <?php if ($search !== ''): ?>
                        <div class="form-help" style="margin-top:1rem; margin-bottom:1rem;">Résultats disponibles pour l'ajout dans la liste Item Custom.</div>
                        <div class="search-result-list">
                            <?php if (empty($search_results)): ?>
                                <div class="search-item">
                                    <div class="item-submeta">Aucun objet disponible pour cette recherche.</div>
                                </div>
                            <?php else: ?>
                                <?php foreach ($search_results as $result): ?>
                                    <div class="search-item">
                                        <div class="item-meta">
                                            <img src="https://cstone.space/uifimages/<?php echo htmlspecialchars($result['cl_scobjs_uuid'], ENT_QUOTES, 'UTF-8'); ?>.png" class="item-preview" alt="">
                                            <div>
                                                <strong class="item-name"><?php echo htmlspecialchars($result['cl_scobjs_name'], ENT_QUOTES, 'UTF-8'); ?></strong>
                                                <div class="item-submeta">
                                                    <?php echo htmlspecialchars($result['cl_scobjs_type'], ENT_QUOTES, 'UTF-8'); ?>
                                                    <?php if (!empty($result['cl_scobjs_subtype'])): ?> / <?php echo htmlspecialchars($result['cl_scobjs_subtype'], ENT_QUOTES, 'UTF-8'); ?><?php endif; ?><br>
                                                    <?php echo htmlspecialchars($result['cl_scobjs_uuid'], ENT_QUOTES, 'UTF-8'); ?>
                                                </div>
                                            </div>
                                        </div>
                                        <form method="post">
                                            <input type="hidden" name="csrf_token" value="<?php echo htmlspecialchars($csrf_token, ENT_QUOTES, 'UTF-8'); ?>">
                                            <input type="hidden" name="action" value="add_custom_item">
                                            <input type="hidden" name="obj_id" value="<?php echo (int) $result['cl_scobjs_id']; ?>">
                                            <button type="submit" class="btn-modern">Ajouter</button>
                                        </form>
                                    </div>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </div>
                    <?php endif; ?>
                </section>

                <section class="glass-card">
                    <h2>Rappel</h2>
                    <div class="form-help">
                        <strong class="muted">Fonctionnement :</strong><br>
                        1. Ajoute un objet à la liste.<br>
                        2. Sélectionne une stat créée dans <em>Stats Item</em>.<br>
                        3. Choisis le signe <strong>+</strong> ou <strong>-</strong>.<br>
                        4. Saisis la valeur. L'unité s'applique automatiquement selon la stat choisie.
                    </div>
                    <div class="form-help" style="margin-top:1rem;">
                        Exemple : <strong>Puissance</strong> + <strong>10</strong> avec l'unité <strong>%</strong> donnera automatiquement <strong>+10%</strong>.
                    </div>
                </section>
            </aside>

            <main class="stack">
                <section class="glass-card">
                    <h2>Liste Item Custom</h2>
                    <p class="count-note"><?php echo count($custom_items); ?> objet(s) configuré(s) dans cette liste personnalisée.</p>

                    <?php if (empty($custom_items)): ?>
                        <div class="empty-state">Aucun objet Item Custom enregistré pour le moment.</div>
                    <?php else: ?>
                        <div class="list-filter-bar">
                            <input
                                type="text"
                                id="item-custom-filter"
                                class="form-control list-filter-input"
                                placeholder="Filtrer les objets déjà ajoutés par nom, type, sous-type ou UUID..."
                                autocomplete="off"
                            >
                            <button type="button" id="item-custom-filter-reset" class="btn-modern danger">Effacer</button>
                            <div class="list-filter-meta">
                                Affichage : <span id="item-custom-visible-count"><?php echo count($custom_items); ?></span> / <?php echo count($custom_items); ?>
                            </div>
                        </div>
                        <div class="form-help" style="margin-top:-0.25rem; margin-bottom:1rem;">Tape quelques lettres pour retrouver rapidement un objet déjà présent dans ta liste Item Custom.</div>
                        <div id="item-custom-no-results" class="empty-state hidden-by-filter">Aucun objet Item Custom ne correspond à cette recherche.</div>
                        <div class="item-card-list" id="item-custom-list">
                            <?php foreach ($custom_items as $item): ?>
                                <?php $item_id = (int) $item['cl_scitemcustom_id']; ?>
                                <?php $item_stats = $custom_stats_by_item[$item_id] ?? []; ?>
                                <?php $item_search_text = trim(implode(' ', array_filter([
                                    (string) ($item['cl_scobjs_name'] ?? ''),
                                    (string) ($item['cl_scobjs_type'] ?? ''),
                                    (string) ($item['cl_scobjs_subtype'] ?? ''),
                                    (string) ($item['cl_scobjs_uuid'] ?? ''),
                                ]))); ?>
                                <section class="custom-item-card" id="itemcustom-<?php echo $item_id; ?>" data-item-search="<?php echo htmlspecialchars($item_search_text, ENT_QUOTES, 'UTF-8'); ?>">
                                    <div class="custom-item-header">
                                        <div class="item-meta">
                                            <img src="https://cstone.space/uifimages/<?php echo htmlspecialchars($item['cl_scobjs_uuid'], ENT_QUOTES, 'UTF-8'); ?>.png" class="item-preview" alt="">
                                            <div>
                                                <strong class="item-name"><?php echo htmlspecialchars($item['cl_scobjs_name'], ENT_QUOTES, 'UTF-8'); ?></strong>
                                                <div class="item-submeta">
                                                    <?php echo htmlspecialchars($item['cl_scobjs_type'], ENT_QUOTES, 'UTF-8'); ?>
                                                    <?php if (!empty($item['cl_scobjs_subtype'])): ?> / <?php echo htmlspecialchars($item['cl_scobjs_subtype'], ENT_QUOTES, 'UTF-8'); ?><?php endif; ?><br>
                                                    UUID: <?php echo htmlspecialchars($item['cl_scobjs_uuid'], ENT_QUOTES, 'UTF-8'); ?>
                                                </div>
                                            </div>
                                        </div>
                                        <form method="post" onsubmit="return confirm('Supprimer cet objet Item Custom et toutes ses stats ?');">
                                            <input type="hidden" name="csrf_token" value="<?php echo htmlspecialchars($csrf_token, ENT_QUOTES, 'UTF-8'); ?>">
                                            <input type="hidden" name="action" value="delete_custom_item">
                                            <input type="hidden" name="itemcustom_id" value="<?php echo $item_id; ?>">
                                            <button type="submit" class="btn-modern danger">Supprimer l'objet</button>
                                        </form>
                                    </div>

                                    <div>
                                        <h3>Ajouter un bonus / malus</h3>
                                        <?php if (empty($stats_catalog)): ?>
                                            <div class="form-help">Aucune statistique disponible. Crée d'abord des entrées dans l'onglet <strong>Stats Item</strong>.</div>
                                        <?php else: ?>
                                            <form method="post" class="inline-form">
                                                <input type="hidden" name="csrf_token" value="<?php echo htmlspecialchars($csrf_token, ENT_QUOTES, 'UTF-8'); ?>">
                                                <input type="hidden" name="action" value="add_custom_stat">
                                                <input type="hidden" name="itemcustom_id" value="<?php echo $item_id; ?>">

                                                <div class="form-group" style="margin-bottom:0;">
                                                    <label>Statistique</label>
                                                    <select name="stat_id" class="form-control" required>
                                                        <?php foreach ($stats_catalog as $stat_option): ?>
                                                            <option value="<?php echo (int) $stat_option['cl_scstatsitem_id']; ?>">
                                                                <?php echo htmlspecialchars($stat_option['cl_scstatsitem_name'], ENT_QUOTES, 'UTF-8'); ?> (<?php echo htmlspecialchars($stat_option['cl_scstatsitem_unit'], ENT_QUOTES, 'UTF-8'); ?>)
                                                            </option>
                                                        <?php endforeach; ?>
                                                    </select>
                                                </div>

                                                <div class="form-group" style="margin-bottom:0;">
                                                    <label>Signe</label>
                                                    <select name="sign" class="form-control">
                                                        <?php foreach ($allowed_signs as $sign_option): ?>
                                                            <option value="<?php echo htmlspecialchars($sign_option, ENT_QUOTES, 'UTF-8'); ?>" <?php echo $sign_option === '+' ? 'selected' : ''; ?>>
                                                                <?php echo htmlspecialchars($sign_labels[$sign_option], ENT_QUOTES, 'UTF-8'); ?>
                                                            </option>
                                                        <?php endforeach; ?>
                                                    </select>
                                                </div>

                                                <div class="form-group" style="margin-bottom:0;">
                                                    <label>Valeur</label>
                                                    <input type="number" name="value" class="form-control" min="0" step="0.01" value="0" required>
                                                </div>

                                                <button type="submit" class="btn-modern">Ajouter la stat</button>
                                            </form>
                                        <?php endif; ?>
                                    </div>

                                    <div style="overflow-x:auto;">
                                        <table class="modern-table">
                                            <thead>
                                                <tr>
                                                    <th>Statistique</th>
                                                    <th style="width:110px;">Signe</th>
                                                    <th style="width:140px;">Valeur</th>
                                                    <th style="width:120px;">Unité</th>
                                                    <th style="width:130px;">Aperçu</th>
                                                    <th style="text-align:right; width:220px;">Actions</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php if (empty($item_stats)): ?>
                                                    <tr>
                                                        <td colspan="6" class="empty-state">Aucune statistique configurée pour cet objet.</td>
                                                    </tr>
                                                <?php else: ?>
                                                    <?php foreach ($item_stats as $item_stat): ?>
                                                        <tr>
                                                            <td>
                                                                <form method="post">
                                                                    <input type="hidden" name="csrf_token" value="<?php echo htmlspecialchars($csrf_token, ENT_QUOTES, 'UTF-8'); ?>">
                                                                    <input type="hidden" name="action" value="update_custom_stat">
                                                                    <input type="hidden" name="custom_stat_id" value="<?php echo (int) $item_stat['cl_scitemcustomstat_id']; ?>">
                                                                    <select name="stat_id" class="form-control" required>
                                                                        <?php foreach ($stats_catalog as $stat_option): ?>
                                                                            <option value="<?php echo (int) $stat_option['cl_scstatsitem_id']; ?>" <?php echo (int) $stat_option['cl_scstatsitem_id'] === (int) $item_stat['cl_scitemcustomstat_stat_id'] ? 'selected' : ''; ?>>
                                                                                <?php echo htmlspecialchars($stat_option['cl_scstatsitem_name'], ENT_QUOTES, 'UTF-8'); ?> (<?php echo htmlspecialchars($stat_option['cl_scstatsitem_unit'], ENT_QUOTES, 'UTF-8'); ?>)
                                                                            </option>
                                                                        <?php endforeach; ?>
                                                                    </select>
                                                            </td>
                                                            <td>
                                                                    <select name="sign" class="form-control">
                                                                        <?php foreach ($allowed_signs as $sign_option): ?>
                                                                            <option value="<?php echo htmlspecialchars($sign_option, ENT_QUOTES, 'UTF-8'); ?>" <?php echo $sign_option === $item_stat['cl_scitemcustomstat_sign'] ? 'selected' : ''; ?>>
                                                                                <?php echo htmlspecialchars($sign_labels[$sign_option], ENT_QUOTES, 'UTF-8'); ?>
                                                                            </option>
                                                                        <?php endforeach; ?>
                                                                    </select>
                                                            </td>
                                                            <td>
                                                                    <input type="number" name="value" class="form-control" min="0" step="0.01" value="<?php echo htmlspecialchars(scitemcustom_display_value($item_stat['cl_scitemcustomstat_value']), ENT_QUOTES, 'UTF-8'); ?>" required>
                                                            </td>
                                                            <td>
                                                                <span class="badge"><?php echo htmlspecialchars($item_stat['cl_scstatsitem_unit'], ENT_QUOTES, 'UTF-8'); ?></span>
                                                            </td>
                                                            <td>
                                                                <span class="preview-pill"><?php echo htmlspecialchars(scitemcustom_preview($item_stat['cl_scitemcustomstat_sign'], $item_stat['cl_scitemcustomstat_value'], $item_stat['cl_scstatsitem_unit']), ENT_QUOTES, 'UTF-8'); ?></span>
                                                            </td>
                                                            <td style="text-align:right;">
                                                                <div class="actions-row">
                                                                    <button type="submit" class="btn-modern btn-mini">Save</button>
                                                                </form>
                                                                <form method="post" onsubmit="return confirm('Supprimer cette statistique ?');">
                                                                    <input type="hidden" name="csrf_token" value="<?php echo htmlspecialchars($csrf_token, ENT_QUOTES, 'UTF-8'); ?>">
                                                                    <input type="hidden" name="action" value="delete_custom_stat">
                                                                    <input type="hidden" name="custom_stat_id" value="<?php echo (int) $item_stat['cl_scitemcustomstat_id']; ?>">
                                                                    <button type="submit" class="btn-modern btn-mini danger">X</button>
                                                                </form>
                                                                </div>
                                                            </td>
                                                        </tr>
                                                    <?php endforeach; ?>
                                                <?php endif; ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </section>
                            <?php endforeach; ?>
                        </div>
                    <?php endif; ?>
                </section>
            </main>
        </div>
    </div>
    <script>
        document.addEventListener('DOMContentLoaded', function () {
            var filterInput = document.getElementById('item-custom-filter');
            var resetButton = document.getElementById('item-custom-filter-reset');
            var visibleCount = document.getElementById('item-custom-visible-count');
            var emptyState = document.getElementById('item-custom-no-results');
            var itemCards = Array.prototype.slice.call(document.querySelectorAll('#item-custom-list .custom-item-card'));

            if (!filterInput || !resetButton || !visibleCount || !emptyState || itemCards.length === 0) {
                return;
            }

            function normalizeValue(value) {
                return (value || '')
                    .toString()
                    .toLowerCase()
                    .normalize('NFD')
                    .replace(/[̀-ͯ]/g, '')
                    .trim();
            }

            function applyFilter() {
                var query = normalizeValue(filterInput.value);
                var matches = 0;

                itemCards.forEach(function (card) {
                    var searchable = normalizeValue(card.getAttribute('data-item-search'));
                    var isMatch = query === '' || searchable.indexOf(query) !== -1;
                    card.classList.toggle('hidden-by-filter', !isMatch);
                    if (isMatch) {
                        matches += 1;
                    }
                });

                visibleCount.textContent = String(matches);
                emptyState.classList.toggle('hidden-by-filter', matches !== 0);
            }

            filterInput.addEventListener('input', applyFilter);
            resetButton.addEventListener('click', function () {
                filterInput.value = '';
                filterInput.focus();
                applyFilter();
            });
        });
    </script>
</body>
</html>
