<template>
    <view class="voice-control">
        <!-- 模块信息卡片 -->
        <view class="card">
            <view class="status-titletop">模块信息</view>
            <view style="padding:20rpx;">
                <u--form labelPosition="left" labelWidth="100"
                    :labelStyle="{ marginRight: '16px', lineHeight: '32px', width: '50px', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis', color: '#000000' }">
                    <view class="version-wrap">
                        <u-form-item label="固件版本">
                            <u--text :text="'Version ' + firmwareVersion"></u--text>
                        </u-form-item>
                    </view>
                    <view class="version-wrap">
                        <u-form-item label="MAC地址">
                            <u--text :text="macAddress"></u--text>
                        </u-form-item>
                    </view>
                </u--form>
            </view>
        </view>

        <!-- 网络设置卡片 -->
        <view class="card basic-info">
            <view class="section-title">网络设置</view>
            <view class="info-content">
                <u-form labelPosition="left" labelWidth="120">
                    <u-form-item label="网络ID">
                        <view class="picker-input">
                            {{ networkIdOptions[networkIdIndex].label }}
                            <picker
                              :range="networkIdOptions"
                              range-key="label"
                              @change="onNetworkIdChange"
                              style="position:absolute;left:0;top:0;width:100%;height:100%;opacity:0;"
                            />
                        </view>
                    </u-form-item>
                    <u-form-item label="频段">
                        <view class="picker-input">
                            {{ frequencyBandOptions[frequencyBandIndex].label }}
                            <picker
                              :range="frequencyBandOptions"
                              range-key="label"
                              @change="onFrequencyBandChange"
                              style="position:absolute;left:0;top:0;width:100%;height:100%;opacity:0;"
                            />
                        </view>
                    </u-form-item>
                    <u-form-item label="信道">
                        <view class="picker-input">
                            {{ channelOptions[channelIndex].label }}
                            <picker
                              :range="channelOptions"
                              range-key="label"
                              @change="onChannelChange"
                              style="position:absolute;left:0;top:0;width:100%;height:100%;opacity:0;"
                            />
                        </view>
                    </u-form-item>
                    <u-form-item label="速率">
                        <view class="picker-input">
                            {{ rateOptions[rateIndex].label }}
                            <picker
                              :range="rateOptions"
                              range-key="label"
                              @change="onRateChange"
                              style="position:absolute;left:0;top:0;width:100%;height:100%;opacity:0;"
                            />
                        </view>
                    </u-form-item>
                    <u-form-item label="功率">
                        <view class="picker-input">
                            {{ powerOptions[powerIndex].label }}
                            <picker
                              :range="powerOptions"
                              range-key="label"
                              @change="onPowerChange"
                              style="position:absolute;left:0;top:0;width:100%;height:100%;opacity:0;"
                            />
                        </view>
                    </u-form-item>
                </u-form>
            </view>
        </view>

        <!-- 雷达触发设置卡片 -->
        <view class="card">
            <view class="section-title">雷达触发设置</view>
            <view class="info-content">
                <u-form labelPosition="left" labelWidth="120">
                    <u-form-item label="低速区间">
                        <view style="display: flex; align-items: center;">
                            <view class="picker-input" style="flex:2; margin-right: 10rpx;">
                                {{ lowSpeedMin }}
                                <picker
                                    :range="speedOptions"
                                    @change="onLowSpeedMinChange"
                                    style="position:absolute;left:0;top:0;width:100%;height:100%;opacity:0;"
                                />
                            </view>
                            <text style="margin: 0 10rpx;">~</text>
                            <view class="picker-input" style="flex:2;">
                                {{ lowSpeedMax }}
                                <picker
                                    :range="speedOptions"
                                    @change="onLowSpeedMaxChange"
                                    style="position:absolute;left:0;top:0;width:100%;height:100%;opacity:0;"
                                />
                            </view>
                            <text style="margin-left: 10rpx;">km/h</text>
                        </view>
                    </u-form-item>
                    <u-form-item label="触发速度区间">
                        <view style="display: flex; align-items: center;">
                            <view class="picker-input" style="flex:2; margin-right: 10rpx;">
                                {{ triggerSpeedMin }}
                                <picker
                                    :range="triggerSpeedOptions"
                                    @change="onTriggerSpeedMinChange"
                                    style="position:absolute;left:0;top:0;width:100%;height:100%;opacity:0;"
                                />
                            </view>
                            <text style="margin: 0 10rpx;">~</text>
                            <view class="picker-input" style="flex:2;">
                                {{ triggerSpeedMax }}
                                <picker
                                    :range="triggerSpeedOptions"
                                    @change="onTriggerSpeedMaxChange"
                                    style="position:absolute;left:0;top:0;width:100%;height:100%;opacity:0;"
                                />
                            </view>
                            <text style="margin-left: 10rpx;">km/h</text>
                        </view>
                    </u-form-item>
                    <u-form-item label="超速速度区间">
                        <view style="display: flex; align-items: center;">
                            <view class="picker-input" style="flex:2; margin-right: 10rpx;">
                                {{ overSpeedMin }}
                                <picker
                                    :range="overSpeedOptions"
                                    @change="onOverSpeedMinChange"
                                    style="position:absolute;left:0;top:0;width:100%;height:100%;opacity:0;"
                                />
                            </view>
                            <text style="margin: 0 10rpx;">~</text>
                            <view class="picker-input" style="flex:2;">
                                {{ overSpeedMax }}
                                <picker
                                    :range="overSpeedOptions"
                                    @change="onOverSpeedMaxChange"
                                    style="position:absolute;left:0;top:0;width:100%;height:100%;opacity:0;"
                                />
                            </view>
                            <text style="margin-left: 10rpx;">km/h</text>
                        </view>
                    </u-form-item>
                </u-form>
            </view>
        </view>

        <!-- 事件列表 -->
        <view class="card audio-list">
            <view class="section-title">
                <text>事件列表</text>
                <image src="https://xaznkj.cn/doc/photo/add.svg " mode="aspectFit" class="add-icon"
                    @click="showAddEventModal = true"></image>
            </view>
            <view class="list-container">
                <view class="empty-tip" v-if="eventList.length === 0">
                    <u-icon name="file" size="50" color="#c0c4cc"></u-icon>
                    <text>暂无事件</text>
                </view>
                <view class="audio-item" v-for="(item, index) in eventList" :key="index">
                    <view class="audio-info">
                        <u-icon name="arrow-right" size="18" color="#2979ff"></u-icon>
                        <text class="audio-name">{{ item.type }} - {{ item.source }}</text>
                    </view>
                    <view class="audio-actions">
                        <text class="audio-duration">{{ item.priority }}</text>
                        <u-icon name="trash" size="18" color="#ff4d4f" @click="deleteEvent(index)"></u-icon>
                    </view>
                </view>
            </view>
        </view>

        <!-- 添加事件弹窗 -->
        <u-popup :show="showAddEventModal" mode="center" @close="showAddEventModal = false" round="8">
            <view class="add-audio-modal">
                <view class="modal-title">添加事件</view>
                <view class="modal-content">
                    <u-form ref="eventForm">
                        <u-form-item label="事件类型">
                            <view class="picker-input">
                                {{ eventTypeOptions[newEvent.type] || '请选择' }}
                                <picker
                                  :range="eventTypeOptions"
                                  @change="onEventPickerChange('type', $event)"
                                  style="position:absolute;left:0;top:0;width:100%;height:100%;opacity:0;"
                                />
                            </view>
                        </u-form-item>
                        <u-form-item label="事件来源">
                            <view class="picker-input">
                                {{ eventSourceOptions[newEvent.source] || '请选择' }}
                                <picker
                                  :range="eventSourceOptions"
                                  @change="onEventPickerChange('source', $event)"
                                  style="position:absolute;left:0;top:0;width:100%;height:100%;opacity:0;"
                                />
                            </view>
                        </u-form-item>
                        <u-form-item label="优先级">
                            <view class="picker-input">
                                {{ priorityOptions[newEvent.priority] || '请选择' }}
                                <picker
                                  :range="priorityOptions"
                                  @change="onEventPickerChange('priority', $event)"
                                  style="position:absolute;left:0;top:0;width:100%;height:100%;opacity:0;"
                                />
                            </view>
                        </u-form-item>
                        <u-form-item label="输出引脚">
                            <view class="picker-input">
                                {{ pinOutputOptions[newEvent.pinOutput] || '请选择' }}
                                <picker
                                  :range="pinOutputOptions"
                                  @change="onEventPickerChange('pinOutput', $event)"
                                  style="position:absolute;left:0;top:0;width:100%;height:100%;opacity:0;"
                                />
                            </view>
                        </u-form-item>
                        <u-form-item label="高低电平">
                            <view class="picker-input">
                                {{ highLowLevelOptions[newEvent.highLowLevel] || '请选择' }}
                                <picker
                                  :range="highLowLevelOptions"
                                  @change="onEventPickerChange('highLowLevel', $event)"
                                  style="position:absolute;left:0;top:0;width:100%;height:100%;opacity:0;"
                                />
                            </view>
                        </u-form-item>
                    </u-form>
                </view>
                <view class="modal-footer">
                    <u-button @click="showAddEventModal = false" plain size="small">取消</u-button>
                    <u-button type="primary" @click="addEvent" size="small">确定</u-button>
                </view>
            </view>
        </u-popup>
    </view>
</template>

<script>
export default {
    data() {
        return {
            // 模块信息
            firmwareVersion: 'v1.0.0',
            macAddress: '00:1A:2B:3C:4D:5E',

            // 网络设置
            networkIdOptions: [
                { label: 'ID1', value: 'ID1' },
                { label: 'ID2', value: 'ID2' },
                { label: 'ID3', value: 'ID3' }
            ],
            networkIdIndex: 0,
            frequencyBandOptions: [
                { label: '2.4GHz', value: '2.4GHz' },
                { label: '5GHz', value: '5GHz' }
            ],
            frequencyBandIndex: 0,
            channelOptions: [
                { label: '1', value: '1' },
                { label: '6', value: '6' },
                { label: '11', value: '11' }
            ],
            channelIndex: 0,
            rateOptions: [
                { label: '54Mbps', value: '54Mbps' },
                { label: '150Mbps', value: '150Mbps' }
            ],
            rateIndex: 0,
            powerOptions: [
                { label: '20dBm', value: '20dBm' },
                { label: '30dBm', value: '30dBm' }
            ],
            powerIndex: 0,

            // 雷达触发设置
            lowSpeed: 0,
            triggerSpeed: 0,
            overSpeed: 0,
            speedOptions: Array.from({length: 101}, (_, i) => i),
            lowSpeedMin: 0,
            lowSpeedMax: 100,
            triggerSpeedOptions: Array.from({length: 151}, (_, i) => i),
            triggerSpeedMin: 0,
            triggerSpeedMax: 150,
            overSpeedOptions: Array.from({length: 201}, (_, i) => i),
            overSpeedMin: 0,
            overSpeedMax: 200,

            // 事件管理
            showAddEventModal: false,
            newEvent: {
                type: '',
                source: '',
                priority: '',
                pinOutput: '',
                highLowLevel: ''
            },
            eventList: [],

            // 下拉选项数据
            eventTypeOptions: ['运动', '振动', '温度', '湿度'],
            eventSourceOptions: ['传感器A', '传感器B', '手动', '自动'],
            priorityOptions: ['高', '中', '低'],
            pinOutputOptions: ['引脚1', '引脚2', '引脚3', '引脚4'],
            highLowLevelOptions: ['高电平', '低电平']
        };
    },
    methods: {
        onNetworkIdChange(e) {
            this.networkIdIndex = e.detail.value;
        },
        onFrequencyBandChange(e) {
            this.frequencyBandIndex = e.detail.value;
        },
        onChannelChange(e) {
            this.channelIndex = e.detail.value;
        },
        onRateChange(e) {
            this.rateIndex = e.detail.value;
        },
        onPowerChange(e) {
            this.powerIndex = e.detail.value;
        },
        onSliderChange(field, e) {
            this[field] = e.detail.value;
        },
        onLowSpeedMinChange(e) {
            const val = this.speedOptions[e.detail.value];
            this.lowSpeedMin = val;
            if (this.lowSpeedMax < val) this.lowSpeedMax = val;
        },
        onLowSpeedMaxChange(e) {
            const val = this.speedOptions[e.detail.value];
            this.lowSpeedMax = val;
            if (this.lowSpeedMin > val) this.lowSpeedMin = val;
        },
        onTriggerSpeedMinChange(e) {
            const val = this.triggerSpeedOptions[e.detail.value];
            this.triggerSpeedMin = val;
            if (this.triggerSpeedMax < val) this.triggerSpeedMax = val;
        },
        onTriggerSpeedMaxChange(e) {
            const val = this.triggerSpeedOptions[e.detail.value];
            this.triggerSpeedMax = val;
            if (this.triggerSpeedMin > val) this.triggerSpeedMin = val;
        },
        onOverSpeedMinChange(e) {
            const val = this.overSpeedOptions[e.detail.value];
            this.overSpeedMin = val;
            if (this.overSpeedMax < val) this.overSpeedMax = val;
        },
        onOverSpeedMaxChange(e) {
            const val = this.overSpeedOptions[e.detail.value];
            this.overSpeedMax = val;
            if (this.overSpeedMin > val) this.overSpeedMin = val;
        },
        onEventPickerChange(field, e) {
            this.newEvent[field] = e.detail.value;
        },
        addEvent() {
            if ([this.newEvent.type, this.newEvent.source, this.newEvent.priority, this.newEvent.pinOutput, this.newEvent.highLowLevel].every(v => v !== '')) {
                const event = {
                    type: this.eventTypeOptions[this.newEvent.type],
                    source: this.eventSourceOptions[this.newEvent.source],
                    priority: this.priorityOptions[this.newEvent.priority],
                    pinOutput: this.pinOutputOptions[this.newEvent.pinOutput],
                    highLowLevel: this.highLowLevelOptions[this.newEvent.highLowLevel]
                };
                this.eventList.push(event);
                this.newEvent = {
                    type: '',
                    source: '',
                    priority: '',
                    pinOutput: '',
                    highLowLevel: ''
                };
                this.showAddEventModal = false;
            } else {
                uni.showToast({ title: '请填写完整信息', icon: 'none' });
            }
        },
        deleteEvent(index) {
            this.eventList.splice(index, 1);
        }
    }
};
</script>

<style lang="scss" scoped>
// @import "@/uni_modules/uview-ui/scss/index.scss";
.voice-control {
    padding: 20rpx;
    background-color: #f7f8fa;
    min-height: 100vh;
    .card {
        background-color: #fff;
        border-radius: 16rpx;
        margin-bottom: 20rpx;
        box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
        overflow: hidden;
    }
    .status-titletop {
        font-weight: bold;
        font-size: 15px;
        color: #333333;
        line-height: 42rpx;
        text-align: left;
        padding: 15rpx 28rpx;
        background-color: #eef6ff;
        border-bottom: 1rpx solid #dceaff;
    }
    .version-wrap {
        background-color: #F7F7F7;
        border-radius: 10rpx;
        padding: 0 42rpx;
        font-size: 24rpx;
        color: #000000;
        line-height: 42rpx;
    }
    .section-title {
        font-size: 30rpx;
        font-weight: 600;
        color: #333;
        padding: 24rpx;
        border-bottom: 1rpx solid #f5f5f5;
        display: flex;
        align-items: center;
        .add-icon {
            width: 40rpx;
            height: 40rpx;
            margin-left: auto;
        }
    }
    .info-content {
        padding: 16rpx 24rpx 24rpx;
    }
    .picker-value {
        height: 80rpx;
        line-height: 80rpx;
        font-size: 28rpx;
        color: #333;
        padding: 0 20rpx;
        background-color: #f9f9f9;
        border-radius: 8rpx;
    }
    .audio-list {
        .list-container {
            padding: 0 24rpx;
        }
        .empty-tip {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            padding: 60rpx 0;
            color: #c0c4cc;
            font-size: 28rpx;
            gap: 20rpx;
            opacity: 0.8;
            transition: all 0.3s ease;
            &:active {
                transform: scale(0.98);
                opacity: 0.6;
            }
        }
        .audio-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 20rpx 0;
            border-bottom: 1rpx solid #f5f5f5;
            &:last-child {
                border-bottom: none;
            }
            .audio-info {
                display: flex;
                align-items: center;
                gap: 16rpx;
            }
            .audio-actions {
                display: flex;
                align-items: center;
                gap: 24rpx;
            }
        }
    }
    .add-audio-modal {
        width: 80vw;
        max-width: 600rpx;
        padding: 0;
        border-radius: 16rpx;
        background-color: #fff;
        overflow: hidden;
        .modal-title {
            font-size: 30rpx;
            font-weight: 600;
            color: #333;
            padding: 28rpx 28rpx 20rpx;
            text-align: center;
            border-bottom: 1rpx solid #f5f5f5;
        }
        .modal-content {
            padding: 0 28rpx;
            margin: 20rpx 0;
            max-height: 60vh;
            overflow-y: auto;
            .u-form-item {
                padding: 20rpx 0;
            }
        }
        .modal-footer {
            display: flex;
            justify-content: space-between;
            padding: 16rpx 28rpx 28rpx;
            border-top: 1rpx solid #f5f5f5;
            .u-button {
                width: 48%;
                height: 72rpx;
                font-size: 26rpx;
                border-radius: 40rpx;
            }
        }
    }
}
.picker-input {
  position: relative;
  width: 100%;
  height: 80rpx;
  line-height: 80rpx;
  background: #f7f7f7;
  border-radius: 10rpx;
  padding: 0 24rpx;
  font-size: 28rpx;
  color: #333;
  border: 1rpx solid #e5e5e5;
  margin-bottom: 10rpx;
  overflow: hidden;
}
.slider-value {
  margin-top: 10rpx;
  color: #2979ff;
  font-size: 26rpx;
}
.full-width-slider {
  width: 100%;
  min-width: 200rpx;
  max-width: 100%;
  display: block;
  margin: 0;
}
</style>