<%# ========== Single extra VLESS inbound (server-rendered) ========== %>
<%
    // The same partial is used both for server-side rendering of existing
    // inbounds and as a structural reference for the client-side template
    // (see addExtraInbound() in scripts.ejs). Field names use parallel arrays
    // (xray_extra_*[]) so Express's body parser produces aligned indices.
    const e = extra || {};
    const transport = e.transport || 'tcp';
    const security = e.security || 'reality';
    const fingerprint = e.fingerprint || 'chrome';
    const fingerprintPool = e.fingerprintPool || [];
    const fpMode = fingerprintPool.length > 0 ? 'pool' : 'single';
    const fingerprintList = ['chrome','firefox','safari','ios','android','edge','360','qq','random','randomized'];
    const fpLabel = (f) => f === 'ios' ? 'iOS' : (f === 'qq' ? 'QQ' : (f === '360' ? '360' : f.charAt(0).toUpperCase() + f.slice(1)));
    const summary = `${transport.toUpperCase()} / ${security} : ${e.port || ''}`;
%>
<details class="node-advanced-details extra-inbound-card" data-extra-idx="<%= idx %>">
    <summary class="node-advanced-summary">
        <span>
            <i class="ti ti-plug"></i>
            <span class="extra-inbound-title"><%= e.label || `${t('nodes.xrayExtraInbound')} #${idx + 1}` %></span>
            <span class="extra-inbound-summary"><%= summary %></span>
        </span>
        <span style="display:flex;align-items:center;gap:8px;">
            <button type="button" class="btn btn-sm btn-danger" onclick="removeExtraInbound(this)" title="<%= t('nodes.xrayRemoveInbound') %>">
                <i class="ti ti-trash"></i>
            </button>
            <i class="ti ti-chevron-down node-advanced-chevron"></i>
        </span>
    </summary>
    <div class="form-section" style="border-radius:0;margin:0;border-top:1px solid var(--border);">
        <input type="hidden" name="xray_extra_id" value="<%= e.id || '' %>">

        <div class="form-row">
            <div class="form-group">
                <label><%= t('nodes.xrayInboundLabel') %></label>
                <input type="text" name="xray_extra_label" value="<%= e.label || '' %>"
                       maxlength="64" placeholder="<%= t('nodes.xrayInboundLabelPlaceholder') %>"
                       oninput="updateExtraInboundTitle(this)">
                <label class="checkbox-label" style="margin-top:6px; font-size:12px; color:var(--text-muted);">
                    <input type="checkbox" name="xray_extra_uniqueName" value="<%= e.id || '' %>"
                           <%= e.uniqueName ? 'checked' : '' %>>
                    <span><%= t('nodes.xrayInboundUniqueName') %></span>
                </label>
                <small class="hint" style="font-size:11px;"><%= t('nodes.xrayInboundUniqueNameHint') %></small>
            </div>
            <div class="form-group">
                <label><%= t('nodes.xrayInboundTag') %></label>
                <input type="text" name="xray_extra_inboundTag" value="<%= e.inboundTag || '' %>"
                       pattern="[A-Za-z0-9_\-]{1,64}" maxlength="64" required
                       placeholder="vless-extra-<%= idx + 1 %>">
            </div>
            <div class="form-group">
                <label><%= t('nodes.port') %></label>
                <input type="number" name="xray_extra_port" value="<%= e.port || '' %>"
                       min="1" max="65535" required
                       oninput="updateExtraInboundSummary(this)">
            </div>
        </div>

        <div class="form-row">
            <div class="form-group">
                <label><%= t('nodes.xrayTransport') %></label>
                <select name="xray_extra_transport" onchange="updateExtraXrayTransportUI(this); updateExtraInboundSummary(this);">
                    <option value="tcp" <%= transport === 'tcp' ? 'selected' : '' %>>TCP</option>
                    <option value="ws" <%= transport === 'ws' ? 'selected' : '' %>>WebSocket</option>
                    <option value="grpc" <%= transport === 'grpc' ? 'selected' : '' %>>gRPC</option>
                    <option value="xhttp" <%= transport === 'xhttp' ? 'selected' : '' %>>XHTTP (SplitHTTP)</option>
                </select>
            </div>
            <div class="form-group">
                <label><%= t('nodes.xraySecurity') %></label>
                <select name="xray_extra_security" onchange="updateExtraXraySecurityUI(this); updateExtraInboundSummary(this);">
                    <option value="reality" <%= security === 'reality' ? 'selected' : '' %>><%= t('nodes.xraySecurityReality') %></option>
                    <option value="tls" <%= security === 'tls' ? 'selected' : '' %>><%= t('nodes.xraySecurityTls') %></option>
                    <option value="none" <%= security === 'none' ? 'selected' : '' %>><%= t('nodes.xraySecurityNone') %></option>
                </select>
            </div>
        </div>

        <div class="form-row">
            <div class="form-group">
                <label><%= t('nodes.xrayFingerprint') %></label>
                <select class="extra-fp-mode" onchange="updateExtraFingerprintModeUI(this)">
                    <option value="single" <%= fpMode === 'single' ? 'selected' : '' %>><%= t('nodes.xrayFingerprintModeSingle') %></option>
                    <option value="pool" <%= fpMode === 'pool' ? 'selected' : '' %>><%= t('nodes.xrayFingerprintModePool') %></option>
                </select>
            </div>
            <div class="form-group extra-alpn-group" style="<%= security === 'tls' ? '' : 'display:none' %>">
                <label><%= t('nodes.xrayAlpn') %></label>
                <input type="text" name="xray_extra_alpn" value="<%= (e.alpn || []).join(', ') %>" placeholder="h2, http/1.1">
            </div>
        </div>

        <%# Single fingerprint — always submitted (one per row) so the parallel
            xray_extra_*[] arrays stay index-aligned; ignored when pool is used. %>
        <div class="form-group extra-fp-single" style="<%= fpMode === 'single' ? '' : 'display:none' %>">
            <select name="xray_extra_fingerprint">
                <% fingerprintList.forEach(function(f) { %>
                    <option value="<%= f %>" <%= fingerprint === f ? 'selected' : '' %>><%= fpLabel(f) %></option>
                <% }); %>
            </select>
        </div>

        <%# Pool — UI-only checkboxes (no name); the per-row hidden CSV input is the
            source of truth and is kept in sync by JS (cleared in single mode). %>
        <div class="form-group extra-fp-pool-block" style="<%= fpMode === 'pool' ? '' : 'display:none' %>">
            <div class="fp-checkbox-grid">
                <% fingerprintList.forEach(function(f) { %>
                    <label class="fp-checkbox">
                        <input type="checkbox" class="extra-fp-pool-cb" value="<%= f %>"
                               <%= fingerprintPool.indexOf(f) !== -1 ? 'checked' : '' %> onchange="syncExtraFingerprintPool(this)">
                        <span><%= fpLabel(f) %></span>
                    </label>
                <% }); %>
            </div>
            <input type="hidden" name="xray_extra_fingerprintPool" class="extra-fp-pool-csv" value="<%= fingerprintPool.join(',') %>">
            <small style="display:block;margin-top:8px;"><%= t('nodes.xrayFingerprintPoolHint') %></small>
        </div>

        <div class="form-group extra-flow-group" style="<%= transport === 'tcp' ? '' : 'display:none' %>">
            <label><%= t('nodes.xrayFlow') %></label>
            <input type="text" name="xray_extra_flow" value="<%= e.flow || 'xtls-rprx-vision' %>" placeholder="xtls-rprx-vision">
        </div>

        <%# VLESS fallbacks[].dest — TCP+TLS only %>
        <div class="form-group extra-fallback-group" style="<%= (transport === 'tcp' && security === 'tls') ? '' : 'display:none' %>">
            <label><%= t('nodes.xrayFallbackDestLabel') %></label>
            <input type="text" name="xray_extra_fallbackDest" value="<%= e.fallbackDest || '' %>"
                   placeholder="<%= t('nodes.xrayFallbackDestPlaceholder') %>"
                   maxlength="253" autocomplete="off" spellcheck="false">
            <small><%= t('nodes.xrayFallbackDestHint') %></small>
        </div>

        <%# Reality-specific (extra) %>
        <div class="extra-reality-fields" style="<%= security === 'reality' ? '' : 'display:none' %>">
            <div class="form-row">
                <div class="form-group">
                    <label><%= t('nodes.xrayRealityDest') %></label>
                    <input type="text" name="xray_extra_realityDest" value="<%= e.realityDest || 'www.google.com:443' %>" placeholder="www.google.com:443">
                </div>
                <div class="form-group">
                    <label><%= t('nodes.xrayRealitySni') %></label>
                    <input type="text" name="xray_extra_realitySni" value="<%= (e.realitySni || ['www.google.com']).join(', ') %>" placeholder="www.google.com">
                </div>
            </div>
            <div class="form-row">
                <div class="form-group">
                    <label><%= t('nodes.xrayPrivateKey') %></label>
                    <div class="input-with-button">
                        <input type="text" class="extra-reality-priv" name="xray_extra_realityPrivateKey" value="<%= e.realityPrivateKey || '' %>" placeholder="<%= t('nodes.xrayAutoGenerated') %>">
                        <button type="button" class="btn btn-sm" onclick="generateExtraXrayKeys(this)" title="<%= t('nodes.xrayGenerateKeys') %>"><i class="ti ti-refresh"></i></button>
                    </div>
                </div>
                <div class="form-group">
                    <label><%= t('nodes.xrayPublicKey') %></label>
                    <input type="text" class="extra-reality-pub" name="xray_extra_realityPublicKey" value="<%= e.realityPublicKey || '' %>" placeholder="<%= t('nodes.xrayAutoGenerated') %>" readonly>
                </div>
            </div>
            <div class="form-group">
                <label><%= t('nodes.xrayShortIds') %></label>
                <input type="text" name="xray_extra_realityShortIds" value="<%= (e.realityShortIds || ['']).join(', ') %>" placeholder=", 0123456789abcdef">
            </div>
            <div class="form-group">
                <label><%= t('nodes.xraySpiderX') %></label>
                <input type="text" name="xray_extra_realitySpiderX" value="<%= e.realitySpiderX || '' %>" placeholder="/r/programming/">
                <small><%= t('nodes.xraySpiderXHint') %></small>
            </div>
        </div>

        <%# WebSocket fields %>
        <div class="extra-ws-fields" style="<%= transport === 'ws' ? '' : 'display:none' %>">
            <div class="form-row">
                <div class="form-group">
                    <label><%= t('nodes.xrayWsPath') %></label>
                    <input type="text" name="xray_extra_wsPath" value="<%= e.wsPath || '/' %>" placeholder="/">
                </div>
                <div class="form-group">
                    <label><%= t('nodes.xrayWsHost') %></label>
                    <input type="text" name="xray_extra_wsHost" value="<%= e.wsHost || '' %>" placeholder="cdn.example.com">
                </div>
            </div>
        </div>

        <%# gRPC fields %>
        <div class="extra-grpc-fields" style="<%= transport === 'grpc' ? '' : 'display:none' %>">
            <div class="form-group">
                <label><%= t('nodes.xrayGrpcService') %></label>
                <input type="text" name="xray_extra_grpcServiceName" value="<%= e.grpcServiceName || 'grpc' %>" placeholder="grpc">
            </div>
        </div>

        <%# XHTTP (SplitHTTP) fields %>
        <div class="extra-xhttp-fields" style="<%= transport === 'xhttp' ? '' : 'display:none' %>">
            <div class="form-row">
                <div class="form-group">
                    <label><%= t('nodes.xrayXhttpPath') %></label>
                    <input type="text" name="xray_extra_xhttpPath" value="<%= e.xhttpPath || '/' %>" placeholder="/">
                </div>
                <div class="form-group">
                    <label><%= t('nodes.xrayXhttpHost') %></label>
                    <input type="text" name="xray_extra_xhttpHost" value="<%= e.xhttpHost || '' %>" placeholder="cdn.example.com">
                </div>
            </div>
            <div class="form-group">
                <label><%= t('nodes.xrayXhttpMode') %></label>
                <select name="xray_extra_xhttpMode">
                    <option value="auto" <%= (e.xhttpMode || 'auto') === 'auto' ? 'selected' : '' %>>Auto</option>
                    <option value="packet-up" <%= e.xhttpMode === 'packet-up' ? 'selected' : '' %>>Packet Up</option>
                    <option value="stream-up" <%= e.xhttpMode === 'stream-up' ? 'selected' : '' %>>Stream Up</option>
                    <option value="stream-one" <%= e.xhttpMode === 'stream-one' ? 'selected' : '' %>>Stream One</option>
                </select>
            </div>
        </div>
    </div>
</details>
