{% extends "_base.html" %}

{% block title %}批次追蹤工具{% endblock %}

{% block head_extra %}
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft JhengHei', Arial, sans-serif;
            background: #f5f5f5;
            padding: 20px;
        }

        .container {
            max-width: 1800px;
            margin: 0 auto;
        }

        .header {
            background: linear-gradient(135deg, #4e54c8 0%, #8f94fb 100%);
            color: white;
            padding: 30px;
            border-radius: 8px;
            margin-bottom: 20px;
        }

        .header h1 {
            font-size: 28px;
            margin-bottom: 10px;
        }

        .header p {
            opacity: 0.9;
            font-size: 14px;
        }

        /* Single column layout with full width */
        .main-content {
            display: flex;
            flex-direction: column;
            gap: 16px;
        }

        /* Top filter bar */
        .filter-bar {
            display: flex;
            align-items: center;
            gap: 12px;
            padding: 16px 20px;
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            flex-wrap: wrap;
        }

        .filter-bar .filter-item {
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .filter-bar label {
            font-weight: 500;
            color: #666;
            font-size: 14px;
            white-space: nowrap;
        }

        .filter-bar select {
            padding: 8px 12px;
            border: 1px solid #ddd;
            border-radius: 6px;
            font-size: 14px;
            background: white;
            cursor: pointer;
            min-width: 120px;
        }

        .filter-bar select:focus {
            outline: none;
            border-color: #4e54c8;
        }

        .filter-bar .search-input {
            flex: 1;
            min-width: 200px;
            max-width: 400px;
            padding: 8px 12px;
            border: 1px solid #ddd;
            border-radius: 6px;
            font-size: 14px;
        }

        .filter-bar .search-input:focus {
            outline: none;
            border-color: #4e54c8;
        }

        /* Sticky selection bar for LOT and workcenter filter */
        .selection-bar {
            display: flex;
            align-items: center;
            gap: 16px;
            padding: 12px 20px;
            background: linear-gradient(135deg, #f8f9ff 0%, #eef1ff 100%);
            border: 1px solid #d4d9ff;
            border-radius: 8px;
            position: sticky;
            top: 0;
            z-index: 100;
            flex-wrap: wrap;
        }

        .selection-bar .wc-filter-container {
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .selection-bar .wc-filter-label {
            font-size: 13px;
            font-weight: 500;
            color: #555;
            white-space: nowrap;
        }

        .selection-bar .selection-hint {
            margin-left: auto;
            font-size: 12px;
            color: #888;
            display: flex;
            align-items: center;
            gap: 6px;
        }

        .selection-bar .selection-hint::before {
            content: "💡";
        }

        /* LOT selector dropdown */
        .lot-selector {
            position: relative;
        }

        .lot-selector-btn {
            display: flex;
            align-items: center;
            gap: 8px;
            padding: 8px 12px;
            border: 1px solid #ddd;
            border-radius: 6px;
            background: white;
            cursor: pointer;
            font-size: 14px;
            min-width: 180px;
        }

        .lot-selector-btn:hover {
            border-color: #4e54c8;
        }

        .lot-selector-btn .lot-count-badge {
            background: #4e54c8;
            color: white;
            padding: 2px 8px;
            border-radius: 10px;
            font-size: 12px;
            font-weight: 600;
        }

        .lot-selector-dropdown {
            position: absolute;
            top: 100%;
            left: 0;
            min-width: 350px;
            max-height: 450px;
            overflow-y: auto;
            background: white;
            border: 1px solid #ddd;
            border-radius: 6px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.15);
            z-index: 1000;
            display: none;
        }

        .lot-selector-dropdown.show {
            display: block;
        }

        .lot-selector-dropdown .lot-option {
            display: flex;
            align-items: center;
            padding: 8px 12px;
            border-bottom: 1px solid #f0f0f0;
            cursor: pointer;
            transition: background 0.15s;
        }

        .lot-selector-dropdown .lot-option:hover {
            background: #f5f7ff;
        }

        .lot-selector-dropdown .lot-option.selected {
            background: #e8f4e8;
        }

        .lot-selector-dropdown .lot-option-checkbox input[type="checkbox"] {
            width: 16px;
            height: 16px;
            cursor: pointer;
            accent-color: #4e54c8;
        }

        .lot-selector-dropdown .lot-option-info {
            flex: 1;
        }

        .lot-selector-dropdown .lot-option-id {
            font-weight: 600;
            color: #333;
            font-size: 13px;
        }

        .lot-selector-dropdown .lot-option-spec {
            font-size: 11px;
            color: #888;
        }

        .lot-selector-dropdown .lot-group-header {
            position: sticky;
            top: 0;
            z-index: 1;
        }

        /* Workcenter Group Selector */
        .wc-group-selector {
            position: relative;
        }

        .wc-group-btn {
            display: flex;
            align-items: center;
            gap: 8px;
            padding: 8px 12px;
            border: 1px solid #ddd;
            border-radius: 6px;
            background: white;
            cursor: pointer;
            font-size: 14px;
            min-width: 160px;
            transition: border-color 0.2s;
        }

        .wc-group-btn:hover {
            border-color: #4e54c8;
        }

        .wc-group-badge {
            background: #4e54c8;
            color: white;
            padding: 2px 8px;
            border-radius: 10px;
            font-size: 12px;
            font-weight: 600;
        }

        .wc-group-dropdown {
            position: absolute;
            top: 100%;
            left: 0;
            min-width: 280px;
            max-height: 400px;
            background: white;
            border: 1px solid #ddd;
            border-radius: 6px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.15);
            z-index: 1000;
            display: none;
            margin-top: 4px;
        }

        .wc-group-dropdown.show {
            display: block;
        }

        .wc-group-header {
            padding: 10px 12px;
            border-bottom: 1px solid #e0e0e0;
            display: flex;
            justify-content: space-between;
            align-items: center;
            font-size: 13px;
            background: #fafafa;
        }

        .wc-group-search {
            padding: 8px 12px;
            border-bottom: 1px solid #f0f0f0;
        }

        .wc-group-search input {
            width: 100%;
            padding: 6px 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 13px;
        }

        .wc-group-search input:focus {
            outline: none;
            border-color: #4e54c8;
        }

        .wc-group-list {
            max-height: 280px;
            overflow-y: auto;
        }

        .wc-group-item {
            display: flex;
            align-items: center;
            padding: 8px 12px;
            cursor: pointer;
            font-size: 13px;
            border-bottom: 1px solid #f5f5f5;
            transition: background 0.15s;
        }

        .wc-group-item:hover {
            background: #f5f7ff;
        }

        .wc-group-item.selected {
            background: #e8f4e8;
        }

        .wc-group-item label {
            display: flex;
            align-items: center;
            gap: 8px;
            cursor: pointer;
            width: 100%;
        }

        .wc-group-item input[type="checkbox"] {
            width: 16px;
            height: 16px;
            accent-color: #4e54c8;
        }

        .wc-group-footer {
            padding: 10px 12px;
            border-top: 1px solid #e0e0e0;
            display: flex;
            justify-content: flex-end;
            gap: 8px;
            background: #fafafa;
        }

        .wc-group-footer .btn-sm {
            padding: 6px 12px;
            font-size: 12px;
        }

        /* LOT info bar */
        .lot-info-bar {
            display: flex;
            align-items: center;
            gap: 24px;
            padding: 12px 20px;
            background: #f8f9fa;
            border-radius: 6px;
            flex-wrap: wrap;
        }

        .lot-info-bar .info-item {
            display: flex;
            align-items: center;
            gap: 6px;
        }

        .lot-info-bar .info-label {
            font-size: 12px;
            color: #888;
        }

        .lot-info-bar .info-value {
            font-size: 14px;
            font-weight: 600;
            color: #333;
        }

        /* Query mode tabs in filter bar */
        .query-mode-tabs {
            display: flex;
            gap: 4px;
            background: #f0f0f0;
            padding: 4px;
            border-radius: 6px;
        }

        .query-mode-tab {
            padding: 6px 12px;
            border: none;
            background: transparent;
            cursor: pointer;
            font-size: 13px;
            border-radius: 4px;
            transition: all 0.2s;
            white-space: nowrap;
        }

        .query-mode-tab:hover {
            background: #e0e0e0;
        }

        .query-mode-tab.active {
            background: white;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            font-weight: 600;
            color: #4e54c8;
        }

        /* Card style */
        .card {
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        }

        .card-header {
            background: #f8f9fa;
            padding: 15px 20px;
            border-bottom: 1px solid #e0e0e0;
            font-weight: 600;
            color: #333;
            border-radius: 8px 8px 0 0;
        }

        .card-body {
            padding: 20px;
            overflow-x: auto;
        }

        /* Input section */
        .input-section {
            display: flex;
            flex-direction: column;
            gap: 20px;
        }

        .section-title {
            font-size: 16px;
            font-weight: 600;
            color: #4e54c8;
            margin-bottom: 15px;
            padding-bottom: 10px;
            border-bottom: 2px solid #4e54c8;
        }

        /* Query type selector */
        .query-type-selector {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
        }

        .query-type-btn {
            padding: 10px 20px;
            border: 2px solid #e0e0e0;
            border-radius: 6px;
            background: white;
            cursor: pointer;
            font-size: 14px;
            transition: all 0.2s;
        }

        .query-type-btn:hover {
            border-color: #4e54c8;
        }

        .query-type-btn.active {
            border-color: #4e54c8;
            background: #f0f1ff;
            color: #4e54c8;
            font-weight: 600;
        }

        /* Text input */
        .filter-group {
            display: flex;
            flex-direction: column;
            gap: 8px;
        }

        .filter-group label {
            font-weight: 500;
            color: #333;
            font-size: 14px;
        }

        .filter-group .hint {
            font-size: 12px;
            color: #888;
        }

        textarea {
            width: 100%;
            min-height: 150px;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 6px;
            font-size: 14px;
            font-family: monospace;
            resize: vertical;
        }

        textarea:focus {
            outline: none;
            border-color: #4e54c8;
        }

        /* Equipment Selector */
        .equipment-selector {
            position: relative;
        }

        .equipment-input {
            width: 100%;
            padding: 10px 15px;
            border: 1px solid #ddd;
            border-radius: 6px;
            font-size: 14px;
            cursor: pointer;
            background: white;
        }

        .equipment-input:focus {
            outline: none;
            border-color: #4e54c8;
        }

        .equipment-dropdown {
            position: absolute;
            top: 100%;
            left: 0;
            right: 0;
            max-height: 350px;
            overflow-y: auto;
            background: white;
            border: 1px solid #ddd;
            border-radius: 6px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.15);
            z-index: 1000;
            display: none;
        }

        .equipment-dropdown.show {
            display: block;
        }

        .equipment-search {
            width: 100%;
            padding: 10px 15px;
            border: none;
            border-bottom: 1px solid #eee;
            font-size: 14px;
        }

        .equipment-search:focus {
            outline: none;
        }

        .equipment-list {
            max-height: 280px;
            overflow-y: auto;
        }

        .equipment-item {
            display: flex;
            align-items: center;
            padding: 10px 15px;
            cursor: pointer;
            border-bottom: 1px solid #f0f0f0;
        }

        .equipment-item:hover {
            background: #f5f7ff;
        }

        .equipment-item.selected {
            background: #e3e8ff;
        }

        .equipment-item input[type="checkbox"] {
            margin-right: 10px;
        }

        .equipment-info {
            flex: 1;
        }

        .equipment-name {
            font-weight: 500;
        }

        .equipment-workcenter {
            font-size: 12px;
            color: #666;
        }

        .selected-count {
            font-size: 12px;
            color: #4e54c8;
            margin-top: 5px;
        }

        /* Date Range */
        .date-range {
            display: flex;
            gap: 10px;
            align-items: center;
            flex-wrap: wrap;
        }

        input[type="date"] {
            padding: 10px 15px;
            border: 1px solid #ddd;
            border-radius: 6px;
            font-size: 14px;
        }

        input[type="date"]:focus {
            outline: none;
            border-color: #4e54c8;
        }

        /* Buttons */
        .btn {
            padding: 10px 20px;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 14px;
            font-weight: bold;
            transition: all 0.2s;
        }

        .btn-primary {
            background: #4e54c8;
            color: white;
        }

        .btn-primary:hover {
            background: #3d44a8;
        }

        .btn-success {
            background: #28a745;
            color: white;
        }

        .btn-success:hover {
            background: #218838;
        }

        .btn-secondary {
            background: #6c757d;
            color: white;
        }

        .btn-secondary:hover {
            background: #5a6268;
        }

        .btn-sm {
            padding: 6px 12px;
            font-size: 12px;
        }

        .btn:disabled {
            background: #ccc;
            cursor: not-allowed;
        }

        .btn-block {
            width: 100%;
        }

        .action-buttons {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
        }

        /* Tabs */
        .tabs {
            display: flex;
            border-bottom: 1px solid #e0e0e0;
            margin-bottom: 20px;
        }

        .tab {
            padding: 12px 20px;
            border: none;
            background: none;
            cursor: pointer;
            font-size: 14px;
            color: #666;
            border-bottom: 3px solid transparent;
            transition: all 0.2s;
        }

        .tab:hover {
            color: #4e54c8;
        }

        .tab.active {
            color: #4e54c8;
            border-bottom-color: #4e54c8;
            font-weight: 600;
        }

        .tab-content {
            display: none;
        }

        .tab-content.active {
            display: block;
        }

        /* Table */
        .table-container {
            overflow-x: auto;
            border: 1px solid #e0e0e0;
            border-radius: 8px;
        }

        table {
            width: 100%;
            min-width: 800px;
            border-collapse: collapse;
            font-size: 13px;
        }

        th, td {
            padding: 10px 12px;
            text-align: left;
            border-bottom: 1px solid #e0e0e0;
            white-space: nowrap;
        }

        th {
            background: #f8f9fa;
            font-weight: 600;
            color: #333;
            position: sticky;
            top: 0;
            z-index: 1;
        }

        /* Sticky action column on right */
        th.sticky-col, td.sticky-col {
            position: sticky;
            right: 0;
            background: #f8f9fa;
            box-shadow: -2px 0 4px rgba(0,0,0,0.05);
            z-index: 2;
        }

        td.sticky-col {
            background: white;
        }

        tr:hover td.sticky-col {
            background: #f5f7ff;
        }

        tr:hover {
            background: #f5f7ff;
        }

        /* Expandable Row */
        .expand-btn {
            background: none;
            border: none;
            cursor: pointer;
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 16px;
            color: #4e54c8;
        }

        .expand-btn:hover {
            background: #e3e8ff;
        }

        .lot-row.expanded {
            background: #f5f7ff;
        }

        .detail-row {
            display: none;
        }

        .detail-row.show {
            display: table-row;
        }

        .detail-cell {
            padding: 0;
            background: #fafafa;
        }

        .detail-content {
            padding: 15px 20px;
        }

        /* LOT List */
        .lot-list {
            display: flex;
            flex-direction: column;
            gap: 10px;
            margin-bottom: 20px;
        }

        .lot-item {
            display: flex;
            align-items: center;
            padding: 12px 15px;
            background: #f8f9fa;
            border-radius: 6px;
            cursor: pointer;
            transition: all 0.2s;
        }

        .lot-item:hover {
            background: #e3e8ff;
        }

        .lot-item.active {
            background: #4e54c8;
            color: white;
        }

        .lot-item .lot-id {
            font-weight: 600;
            flex: 1;
        }

        .lot-item .lot-info {
            font-size: 12px;
            opacity: 0.8;
        }

        /* LOT item with checkbox */
        .lot-item .lot-checkbox {
            flex-shrink: 0;
            margin-right: 10px;
            display: flex;
            align-items: center;
        }

        .lot-item .lot-checkbox input[type="checkbox"] {
            width: 16px;
            height: 16px;
            cursor: pointer;
            accent-color: #4e54c8;
        }

        .lot-item .lot-content {
            flex: 1;
            min-width: 0;
        }

        .lot-item.active .lot-checkbox input[type="checkbox"] {
            accent-color: white;
        }

        /* Timeline styles */
        #timelineContainer {
            border-top: 1px solid #e0e0e0;
            padding-top: 20px;
        }

        .timeline-bar {
            transition: transform 0.15s, box-shadow 0.15s;
        }

        .timeline-bar:hover {
            transform: scaleY(1.15);
            box-shadow: 0 2px 8px rgba(0,0,0,0.3);
            z-index: 10;
        }

        /* Scale button active state */
        #timelineContainer .btn.active {
            background: #4a90d9;
            color: white;
            border-color: #4a90d9;
        }

        /* Timeline bar labels */
        .timeline-bar-label,
        .timeline-bar-station {
            pointer-events: none;
        }

        /* Sticky LOT ID column when scrolling */
        #timelineScrollContainer {
            position: relative;
        }

        /* Timeline axis styling */
        .timeline-axis-container {
            position: sticky;
            top: 0;
            background: white;
            z-index: 3;
        }

        /* History timeline */
        .history-timeline {
            padding: 20px;
        }

        .history-step {
            display: flex;
            gap: 15px;
            padding: 15px 0;
            border-left: 3px solid #e0e0e0;
            padding-left: 20px;
            margin-left: 10px;
            position: relative;
        }

        .history-step::before {
            content: '';
            position: absolute;
            left: -8px;
            top: 20px;
            width: 12px;
            height: 12px;
            background: #4e54c8;
            border-radius: 50%;
        }

        .history-step:last-child {
            border-left-color: transparent;
        }

        .step-info {
            flex: 1;
        }

        .step-station {
            font-weight: 600;
            font-size: 15px;
            color: #333;
        }

        .step-equipment {
            font-size: 13px;
            color: #666;
            margin-top: 3px;
        }

        .step-time {
            font-size: 12px;
            color: #888;
            margin-top: 5px;
        }

        .step-qty {
            font-size: 13px;
            color: #28a745;
        }

        .step-actions {
            display: flex;
            gap: 8px;
        }

        /* Status badges */
        .badge {
            display: inline-block;
            padding: 3px 8px;
            border-radius: 4px;
            font-size: 11px;
            font-weight: 500;
        }

        .badge-info { background: #cfe2ff; color: #084298; }
        .badge-success { background: #d1e7dd; color: #0a3622; }
        .badge-warning { background: #fff3cd; color: #664d03; }
        .badge-danger { background: #f8d7da; color: #58151c; }

        /* Loading */
        .loading {
            text-align: center;
            padding: 40px;
            color: #666;
        }

        .loading-spinner {
            display: inline-block;
            width: 40px;
            height: 40px;
            border: 4px solid #f3f3f3;
            border-top: 4px solid #4e54c8;
            border-radius: 50%;
            animation: spin 1s linear infinite;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        .error {
            background: #f8d7da;
            color: #721c24;
            padding: 15px;
            border-radius: 6px;
            margin: 10px 0;
        }

        .info-box {
            background: #e3e8ff;
            color: #4e54c8;
            padding: 10px 15px;
            border-radius: 6px;
            font-size: 13px;
            margin-bottom: 15px;
        }

        /* Empty state */
        .empty-state {
            text-align: center;
            padding: 60px 20px;
            color: #666;
        }

        .empty-state p {
            margin-bottom: 10px;
        }

        /* Arrow animation */
        .arrow-icon {
            transition: transform 0.2s;
        }

        .arrow-icon.rotated {
            transform: rotate(90deg);
        }

        /* Section divider */
        .section-divider {
            border: none;
            border-top: 1px dashed #ddd;
            margin: 20px 0;
        }

        /* Association tabs in detail */
        .assoc-tabs {
            display: flex;
            gap: 5px;
            margin-bottom: 15px;
            padding: 5px;
            background: #f0f0f0;
            border-radius: 6px;
        }

        .assoc-tab {
            padding: 8px 15px;
            border: none;
            background: none;
            cursor: pointer;
            font-size: 13px;
            border-radius: 4px;
            transition: all 0.2s;
        }

        .assoc-tab:hover {
            background: #e0e0e0;
        }

        .assoc-tab.active {
            background: white;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            font-weight: 600;
        }

        /* Result header */
        .result-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
            padding: 10px 15px;
            background: #f8f9fa;
            border-radius: 6px;
        }

        .result-info {
            font-size: 14px;
            color: #666;
        }

        .result-actions {
            display: flex;
            gap: 10px;
        }
    </style>
{% endblock %}

{% block content %}
    <div class="container">
        <div class="header">
            <h1>批次追蹤工具</h1>
            <p>批次生產歷程追蹤、前後批比對、設備時段綜合查詢</p>
        </div>

        <div class="main-content">
            <!-- Top: Query Mode Tabs -->
            <div class="query-mode-tabs" style="width: fit-content;">
                <button class="query-mode-tab active" data-mode="batch" onclick="switchQueryMode('batch')">批次追蹤</button>
                <button class="query-mode-tab" data-mode="equipment" onclick="switchQueryMode('equipment')">設備時段</button>
            </div>

            <!-- Batch Query Filter Bar -->
            <div class="filter-bar" id="batchFilterBar">
                <div class="filter-item">
                    <label>查詢類型</label>
                    <select id="queryTypeSelect" onchange="setQueryType(this.value)">
                        <option value="lot_id">LOT ID</option>
                        <option value="serial_number">流水號</option>
                        <option value="work_order">GA工單</option>
                    </select>
                </div>

                <div class="filter-item" style="flex: 1;">
                    <input type="text" class="search-input" id="lotInputField" placeholder="輸入 LOT ID（多筆以逗號分隔）">
                </div>

                <button class="btn btn-primary" onclick="executeLotQuery()" id="lotQueryBtn">
                    查詢
                </button>
            </div>

            <!-- Sticky Sub-bar for LOT Selection and Workcenter Filter -->
            <div class="selection-bar" id="selectionBar" style="display: none;">
                <!-- LOT Selector -->
                <div class="lot-selector" id="lotSelectorContainer">
                    <div class="lot-selector-btn" onclick="toggleLotSelector()">
                        <span id="lotSelectorDisplay">選擇 LOT</span>
                        <span class="lot-count-badge" id="lotCountBadge">0</span>
                    </div>
                    <div class="lot-selector-dropdown" id="lotSelectorDropdown">
                        <!-- LOT options will be populated here -->
                    </div>
                </div>

                <!-- Workcenter Group Filter -->
                <div class="wc-filter-container" id="workcenterGroupSelectorContainer">
                    <span class="wc-filter-label">站點篩選:</span>
                    <div id="workcenterGroupSelector">
                        <!-- Rendered by JS -->
                    </div>
                </div>

                <!-- Quick action hint -->
                <div class="selection-hint" id="selectionHint">
                    <span>選擇批次和站點後點擊「套用篩選」</span>
                </div>
            </div>

            <!-- Equipment Query Filter Bar (hidden by default) -->
            <div class="filter-bar" id="equipmentFilterBar" style="display: none;">
                <div class="filter-item">
                    <label>選擇設備</label>
                    <div class="equipment-selector">
                        <div class="equipment-input" onclick="toggleEquipmentDropdown()" style="min-width: 200px;">
                            <span id="equipmentDisplay">點擊選擇設備...</span>
                        </div>
                        <div class="equipment-dropdown" id="equipmentDropdown">
                            <input type="text" class="equipment-search" placeholder="搜尋設備名稱或站點..." oninput="filterEquipments(this.value)">
                            <div class="equipment-list" id="equipmentList">
                                <div class="loading">
                                    <div class="loading-spinner"></div>
                                    <br>載入設備中...
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="filter-item">
                    <label>日期範圍</label>
                    <div class="date-range">
                        <input type="date" id="dateFrom">
                        <span>~</span>
                        <input type="date" id="dateTo">
                    </div>
                </div>

                <button class="btn btn-secondary btn-sm" onclick="setLast30Days()">近30天</button>

                <button class="btn btn-primary" onclick="executeEquipmentQuery()" id="eqQueryBtn">
                    查詢
                </button>
            </div>

            <!-- LOT Info Bar (shown when a LOT is selected) -->
            <div class="lot-info-bar" id="lotInfoBar" style="display: none;">
                <div class="info-item">
                    <span class="info-label">LOT ID</span>
                    <span class="info-value" id="infoLotId">-</span>
                </div>
                <div class="info-item">
                    <span class="info-label">產品規格</span>
                    <span class="info-value" id="infoSpec">-</span>
                </div>
                <div class="info-item">
                    <span class="info-label">工單</span>
                    <span class="info-value" id="infoWorkOrder">-</span>
                </div>
                <div class="info-item">
                    <span class="info-label">站點數</span>
                    <span class="info-value" id="infoStepCount">-</span>
                </div>
            </div>

            <!-- Results Panel (Full Width) -->
            <div class="card" style="flex: 1;">
                <div class="card-body">
                    <!-- Batch Results -->
                    <div id="batchResultsPanel">
                        <div class="empty-state" id="lotEmptyState">
                            <p>請輸入 LOT ID、流水號或工單後，點擊「查詢」</p>
                        </div>
                        <div id="lotResultsContent" style="display: none;"></div>
                    </div>

                    <!-- Equipment Results (hidden by default) -->
                    <div id="equipmentResultsPanel" style="display: none;">
                        <div class="empty-state" id="eqEmptyState">
                            <p>請選擇設備和日期範圍後，點擊「查詢」</p>
                        </div>
                        <div id="eqResultsContent" style="display: none;"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block scripts %}
    {% set query_tool_js = frontend_asset('query-tool.js') %}
    <script type="module" src="{{ query_tool_js }}"></script>
{% endblock %}

