<template>
    <view class="company-search-page">
        <!-- 头部导航 -->
        <view class="header">
            <view class="header-left" @click="goBack">
                <view class="back-arrow"><uni-icons type="search" size="32" color="#FFFFFF"></uni-icons>‹</view>
            </view>
            <view class="header-title">选择企业</view>
            <view class="header-right"></view>
        </view>

        <!-- 搜索框 -->
        <view class="search-container">
        <view class="search-box">
            <view class="search-icon">🔍</view>
            <input
                class="search-input" 
                placeholder="请输入企业名称进行搜索" 
                v-model="searchKeyword"
                @input="onSearchInput"
            />
            <view class="clear-btn" v-if="searchKeyword" @click="clearSearch">
                <view class="clear-icon">✕</view>
            </view>
        </view>
        </view>

        <!-- 搜索结果 -->
        <scroll-view class="content" scroll-y="true" :style="{ height: scrollViewHeight }">
            <!-- 搜索提示 -->
            <view class="search-tip" v-if="!searchKeyword">
                <text class="tip-text">请输入企业名称进行搜索</text>
            </view>

            <!-- 加载中 -->
            <view class="loading-container" v-if="loading">
                <view class="loading-text">搜索中...</view>
            </view>

            <!-- 搜索结果列表 -->
            <view class="result-list" v-if="searchResults.length > 0">
                <view 
                    class="result-item" 
                    v-for="(company, index) in searchResults" 
                    :key="company.companyId"
                    @click="selectCompany(company)"
                >
                    <view class="company-info">
                        <view class="company-name">{{ company.name }}</view>
                        <view class="company-location" v-if="company.location">
                            <text class="location-icon">📍</text>
                            <text class="location-text">{{ company.location }}</text>
                        </view>
                        <view class="company-scale" v-if="company.scale">
                            <text class="scale-label">规模：</text>
                            <text class="scale-text">{{ getScaleText(company.scale) }}</text>
                        </view>
                        <view class="company-nature" v-if="company.nature">
                            <text class="nature-label">性质：</text>
                            <text class="nature-text">{{ getNatureText(company.nature) }}</text>
                        </view>
                        <view class="company-description" v-if="company.description">
                            <text class="desc-text">{{ company.description }}</text>
                        </view>
                    </view>
                    <view class="select-icon">
                        <view class="arrow-icon">›</view>
                    </view>
                </view>
            </view>

            <!-- 无结果提示 -->
            <view class="no-result" v-if="searchKeyword && !loading && searchResults.length === 0">
                <view class="empty-icon">📭</view>
                <view class="no-result-text">未找到相关企业</view>
                <view class="no-result-tip">请尝试其他关键词</view>
            </view>

            <!-- 底部安全区域 -->
            <view class="bottom-safe-area"></view>
        </scroll-view>
    </view>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { createRequest } from '@/utils/request';

// 搜索关键词
const searchKeyword = ref('');
// 搜索结果
const searchResults = ref([]);
// 加载状态
const loading = ref(false);
// 防抖定时器
let debounceTimer = null;

// 滚动视图高度
const scrollViewHeight = ref('calc(100vh - 200rpx)');

// 计算滚动视图高度
const calculateScrollViewHeight = () => {
    const systemInfo = uni.getSystemInfoSync();
    const windowHeight = systemInfo.windowHeight;
    const headerHeight = 100; // 头部高度
    const searchHeight = 120; // 搜索框高度
    const scrollHeight = windowHeight - headerHeight - searchHeight;
    scrollViewHeight.value = `${scrollHeight}px`;
};

// 页面加载时计算高度
onMounted(() => {
    calculateScrollViewHeight();
});

// 搜索输入处理（防抖）
const onSearchInput = () => {
    // 清除之前的定时器
    if (debounceTimer) {
        clearTimeout(debounceTimer);
    }
    
    // 设置新的定时器
    debounceTimer = setTimeout(() => {
        if (searchKeyword.value.trim()) {
            searchCompanies();
        } else {
            searchResults.value = [];
        }
    }, 500); // 500ms 防抖延迟
};

// 搜索企业
const searchCompanies = async () => {
    if (!searchKeyword.value.trim()) {
        searchResults.value = [];
        return;
    }

    try {
        loading.value = true;
        
        const response = await createRequest('/app/company/likeList', {
            name: searchKeyword.value.trim()
        }, 'get', false);
        
        if (response.code === 200) {
            searchResults.value = response.rows || [];
        } else {
            uni.showToast({
                title: response.msg || '搜索失败',
                icon: 'none'
            });
            searchResults.value = [];
        }
    } catch (error) {
        console.error('搜索企业失败:', error);
        uni.showToast({
            title: '搜索失败，请重试',
            icon: 'none'
        });
        searchResults.value = [];
    } finally {
        loading.value = false;
    }
};

// 清除搜索
const clearSearch = () => {
    searchKeyword.value = '';
    searchResults.value = [];
    if (debounceTimer) {
        clearTimeout(debounceTimer);
    }
};

// 获取企业规模文本
const getScaleText = (scale) => {
    const scaleMap = {
        '1': '1-20人',
        '2': '21-50人',
        '3': '51-100人',
        '4': '101-200人',
        '5': '201-500人',
        '6': '500人以上'
    };
    return scaleMap[scale] || '未知';
};

// 获取企业性质文本
const getNatureText = (nature) => {
    const natureMap = {
        '1': '有限责任公司',
        '2': '股份有限公司',
        '3': '个人独资企业',
        '4': '合伙企业',
        '5': '外商投资企业',
        '6': '其他'
    };
    return natureMap[nature] || '未知';
};

// 选择企业
const selectCompany = (company) => {
    // 返回上一页并传递选中的企业信息
    uni.navigateBack({
        delta: 1,
        success: () => {
            // 通过事件总线或全局状态传递数据
            uni.$emit('companySelected', {
                id: company.companyId,
                name: company.name,
                address: company.location
            });
        }
    });
};

// 返回上一页
const goBack = () => {
    uni.navigateBack();
};
</script>

<style lang="scss" scoped>
.company-search-page {
    height: 100vh;
    background-color: #f5f5f5;
    display: flex;
    flex-direction: column;
    position: relative;
    overflow: hidden;
}

.header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20rpx 30rpx;
    background: #fff;
    border-bottom: 1rpx solid #eee;
    
    .header-left {
        width: 60rpx;
        height: 60rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        
        .back-arrow {
            font-size: 40rpx;
            color: #333;
            font-weight: bold;
        }
    }
    
    .header-title {
        font-size: 36rpx;
        font-weight: 600;
        color: #333;
    }
    
    .header-right {
        width: 60rpx;
    }
}

.search-container {
    padding: 20rpx 30rpx;
    background: #fff;
    border-bottom: 1rpx solid #eee;
    
    .search-box {
        display: flex;
        align-items: center;
        background: #f5f5f5;
        border-radius: 12rpx;
        padding: 0 20rpx;
        height: 80rpx;
        
        .search-icon {
            font-size: 32rpx;
            margin-right: 20rpx;
            color: #999;
        }
        
        .search-input {
            flex: 1;
            height: 100%;
            background: transparent;
            border: none;
            font-size: 28rpx;
            color: #333;
            line-height: 1.4;
            display: flex;
            align-items: center;
            
            &::placeholder {
                color: #999;
                font-size: 28rpx;
                line-height: 1.4;
            }
        }
        
        .clear-btn {
            width: 32rpx;
            height: 32rpx;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-left: 20rpx;
            
            .clear-icon {
                font-size: 24rpx;
                color: #999;
            }
        }
    }
}

.content {
    flex: 1;
    padding: 0;
    overflow: hidden;
}

.search-tip {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 400rpx;
    
    .tip-text {
        font-size: 28rpx;
        color: #999;
    }
}

.loading-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 200rpx;
    
    .loading-text {
        font-size: 28rpx;
        color: #666;
    }
}

.result-list {
    background: #fff;
    
    .result-item {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 30rpx;
        border-bottom: 1rpx solid #f0f0f0;
        
        &:last-child {
            border-bottom: none;
        }
        
        &:active {
            background: #f8f8f8;
        }
        
        .company-info {
            flex: 1;
            
            .company-name {
                font-size: 30rpx;
                color: #333;
                font-weight: 500;
                margin-bottom: 15rpx;
                line-height: 1.4;
            }
            
            .company-location {
                display: flex;
                align-items: center;
                margin-bottom: 10rpx;
                
                .location-icon {
                    font-size: 20rpx;
                    margin-right: 8rpx;
                }
                
                .location-text {
                    font-size: 24rpx;
                    color: #666;
                    flex: 1;
                }
            }
            
            .company-scale, .company-nature {
                display: flex;
                align-items: center;
                margin-bottom: 8rpx;
                
                .scale-label, .nature-label {
                    font-size: 22rpx;
                    color: #999;
                    margin-right: 8rpx;
                }
                
                .scale-text, .nature-text {
                    font-size: 22rpx;
                    color: #666;
                }
            }
            
            .company-description {
                margin-top: 10rpx;
                
                .desc-text {
                    font-size: 22rpx;
                    color: #888;
                    line-height: 1.4;
                    display: -webkit-box;
                    -webkit-box-orient: vertical;
                    -webkit-line-clamp: 2;
                    line-clamp: 2;
                    overflow: hidden;
                    text-overflow: ellipsis;
                }
            }
        }
        
        .select-icon {
            width: 40rpx;
            height: 40rpx;
            display: flex;
            align-items: center;
            justify-content: center;
            
            .arrow-icon {
                font-size: 24rpx;
                color: #999;
            }
        }
    }
}

.no-result {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 400rpx;
    
    .empty-icon {
        font-size: 120rpx;
        margin-bottom: 30rpx;
    }
    
    .no-result-text {
        font-size: 28rpx;
        color: #666;
        margin-bottom: 10rpx;
    }
    
    .no-result-tip {
        font-size: 24rpx;
        color: #999;
    }
}

.bottom-safe-area {
    height: 120rpx;
    background: transparent;
}
</style>
