// 等级设置页面 - H5版本

let selectedStalls = [];
let dropdownOpen = false;

// 切换摊位下拉框
function toggleStallDropdown() {
    const dropdown = document.getElementById('stall-dropdown-list');
    dropdownOpen = !dropdownOpen;
    
    if (dropdownOpen) {
        dropdown.classList.remove('hidden');
        // 点击外部关闭
        setTimeout(() => {
            document.addEventListener('click', closeDropdownOnClickOutside);
        }, 0);
    } else {
        dropdown.classList.add('hidden');
        document.removeEventListener('click', closeDropdownOnClickOutside);
    }
}

// 点击外部关闭下拉框
function closeDropdownOnClickOutside(event) {
    const dropdown = document.getElementById('stall-dropdown-list');
    const button = document.getElementById('stall-dropdown-btn');
    
    if (!dropdown.contains(event.target) && !button.contains(event.target)) {
        dropdown.classList.add('hidden');
        dropdownOpen = false;
        document.removeEventListener('click', closeDropdownOnClickOutside);
    }
}

// 选择摊位
function selectStall(stallName) {
    const checkbox = event.target.previousElementSibling || event.target.querySelector('input');
    
    if (checkbox) {
        checkbox.checked = !checkbox.checked;
        
        if (checkbox.checked && !selectedStalls.includes(stallName)) {
            selectedStalls.push(stallName);
        } else if (!checkbox.checked && selectedStalls.includes(stallName)) {
            selectedStalls = selectedStalls.filter(stall => stall !== stallName);
        }
        
        updateSelectedStallsDisplay();
        updateDropdownText();
    }
}

// 更新下拉框按钮文字
function updateDropdownText() {
    const button = document.getElementById('stall-dropdown-btn');
    if (selectedStalls.length === 0) {
        button.textContent = '请选择摊位名称...';
    } else if (selectedStalls.length === 1) {
        button.textContent = selectedStalls[0];
    } else {
        button.textContent = `已选择 ${selectedStalls.length} 个摊位`;
    }
}

// 更新已选摊位显示
function updateSelectedStallsDisplay() {
    const container = document.getElementById('selected-stalls');
    container.innerHTML = '';
    
    selectedStalls.forEach(stall => {
        const tag = document.createElement('span');
        tag.className = 'filter-tag';
        tag.innerHTML = `${stall} <span class="close" onclick="removeStall('${stall}')">×</span>`;
        container.appendChild(tag);
    });
}

// 移除选中的摊位
function removeStall(stallName) {
    selectedStalls = selectedStalls.filter(stall => stall !== stallName);
    
    // 更新checkbox状态
    const checkboxes = document.querySelectorAll('#stall-dropdown-list input[type="checkbox"]');
    checkboxes.forEach(checkbox => {
        if (checkbox.value === stallName) {
            checkbox.checked = false;
        }
    });
    
    updateSelectedStallsDisplay();
    updateDropdownText();
}

// 查询摊位
function queryStalls() {
    const tableRows = document.querySelectorAll('#stalls-table-body tr');
    
    if (selectedStalls.length === 0) {
        // 显示所有摊位
        tableRows.forEach(row => {
            row.style.display = '';
        });
        showNotification('显示所有摊位', 'info');
    } else {
        // 筛选显示
        let visibleCount = 0;
        tableRows.forEach(row => {
            const stallNameCell = row.querySelector('td:first-child');
            if (stallNameCell) {
                const stallName = stallNameCell.textContent.trim();
                if (selectedStalls.includes(stallName)) {
                    row.style.display = '';
                    visibleCount++;
                } else {
                    row.style.display = 'none';
                }
            }
        });
        showNotification(`筛选显示 ${visibleCount} 个摊位`, 'success');
    }
}

// 批量编辑
function batchEdit() {
    // 跳转到批量编辑第一步：店铺选择页面
    navigateTo('batch-edit-step1.html');
}

// 查看等级明细
function viewLevelDetail(stallName) {
    const encodedName = encodeURIComponent(stallName);
    navigateTo(`level-detail.html?stall=${encodedName}`);
}

// 导出函数到全局
window.toggleStallDropdown = toggleStallDropdown;
window.selectStall = selectStall;
window.removeStall = removeStall;
window.queryStalls = queryStalls;
window.batchEdit = batchEdit;
window.viewLevelDetail = viewLevelDetail;