/**
 * 搜索和过滤功能模块
 * 负责设备搜索、过滤和查询功能
 */
var SearchFilter = (function() {
    'use strict';
    
    var currentSearchedDevice = null;
    var markerState = 3; // 1:all; 2:orange; 3:red
    
    function searchDevice(deviceId) {
        if (!deviceId || !deviceId.trim()) {
            clearSearch();
            return false;
        }
        
        deviceId = deviceId.trim();
        currentSearchedDevice = deviceId;
        
        var success = DeviceMarkers.locateDevice(deviceId);
        
        if (success) {
            if (window.layer && typeof window.layer.msg === 'function') {
                window.layer.msg('已定位到设备: ' + deviceId);
            }
            
            // 获取设备信息并显示天气预测（如果启用）
            var targetFeature = DeviceMarkers.findDeviceById(deviceId);
            if (targetFeature && window.WeatherForecast && window.WeatherForecast.isEnabled()) {
                var deviceInfo = targetFeature.get('deviceInfo');
                if (deviceInfo) {
                    window.WeatherForecast.showWeatherForecast(deviceInfo);
                }
            }
        } else {
            if (window.layer && typeof window.layer.msg === 'function') {
                window.layer.msg('未找到设备: ' + deviceId);
            }
        }
        
        return success;
    }
    
    function clearSearch() {
        currentSearchedDevice = null;
        var searchInput = document.getElementById('deviceSearchNew');
        if (searchInput) {
            searchInput.value = '';
        }
        
        // 恢复到当前的过滤状态
        applyCurrentFilter();
    }
    
    function applyCurrentFilter() {
        switch(markerState) {
            case 1:
                showAllDevices();
                break;
            case 2:
                showWarning1Devices();
                break;
            case 3:
                showWarning2Devices();
                break;
        }
    }
    
    function showAllDevices() {
        currentSearchedDevice = null;
        clearSearchInput();
        DeviceMarkers.showAllDevices();
        markerState = 1;
        updateFilterSelect('all');
    }
    
    function showWarning1Devices() {
        currentSearchedDevice = null;
        clearSearchInput();
        DeviceMarkers.showWarning1Devices();
        markerState = 2;
        updateFilterSelect('warning1');
    }
    function showWarning2Devices() {
        currentSearchedDevice = null;
        clearSearchInput();
        DeviceMarkers.showWarning2Devices();
        markerState = 3;
        updateFilterSelect('warning2');
    }
    function clearSearchInput() {
        var searchInput = document.getElementById('deviceSearchNew');
        if (searchInput) {
            searchInput.value = '';
        }
    }
    
    function updateFilterSelect(filterValue) {
        var filterSelect = document.getElementById('warningFilter');
        if (filterSelect) {
            filterSelect.value = filterValue;
        }
    }
    
    function filterDevicesByStatus(statusType) {
        switch(statusType) {
            case 'warning1':
                showWarning1Devices();
                break;
            case 'warning2':
                showWarning2Devices();
                break;
            case 'offline':
            case 'no_fwd':
            case 'nofixed':
            case 'nogga':
                // 对于这些状态，显示所有设备，让用户在弹窗中选择
                showAllDevices();
                break;
            default:
                showAllDevices();
                break;
        }
    }
    
    function queryDevices(statusType) {
        filterDevicesByStatus(statusType);
        
        // 打开设备列表弹窗
        if (window.layer && typeof window.layer.open === 'function') {
            var index = window.layer.open({
                title: '设备列表',
                type: 2,
                shade: 0.2,
                maxmin: true,
                shadeClose: true,
                anim: 2,
                offset: 'rb',
                area: ['100%', '50%'],
                content: '../page/gnss_q_status?query=' + statusType,
            });
        }
    }
    
    function locateDeviceOnMap(deviceId, latitude, longitude) {
        currentSearchedDevice = deviceId;
        var success = DeviceMarkers.locateDevice(deviceId);
        
        if (success && window.layer && typeof window.layer.closeAll === 'function') {
            window.layer.closeAll();
        }
        
        return success;
    }
    
    function locateDeviceDirectly(deviceId) {
        currentSearchedDevice = deviceId;
        return DeviceMarkers.locateDevice(deviceId);
    }
    
    function getCurrentSearchedDevice() {
        return currentSearchedDevice;
    }
    
    function getMarkerState() {
        return markerState;
    }
    
    function setMarkerState(state) {
        markerState = state;
    }
    
    return {
        searchDevice: searchDevice,
        clearSearch: clearSearch,
        showAllDevices: showAllDevices,
        showWarning1Devices: showWarning1Devices,
        showWarning2Devices: showWarning2Devices,
        filterDevicesByStatus: filterDevicesByStatus,
        queryDevices: queryDevices,
        locateDeviceOnMap: locateDeviceOnMap,
        locateDeviceDirectly: locateDeviceDirectly,
        getCurrentSearchedDevice: getCurrentSearchedDevice,
        getMarkerState: getMarkerState,
        setMarkerState: setMarkerState
    };
})(); 