<template>
    <div class="app-multiple app-multiple--checkbox" v-if="checkbox">
        <el-checkbox-group v-model="selectedValue" @change="onChecked">
            <el-checkbox :label="item.value" v-for="(item, index) in dataList" :key="index">
                {{ item.label }}
            </el-checkbox>
        </el-checkbox-group>
    </div>
    <div class="app-multiple app-multiple--select" v-else>
        <el-select v-model="selectedValue[0]" @change="onSelected" clearable>
            <el-option :label="item.label" :value="item.value" :disabled="item.disabled"
                v-for="(item, index) in selectList1" :key="index" />
        </el-select>
        <span>-</span>
        <el-select v-model="selectedValue[1]" @change="onSelected" clearable>
            <el-option :label="item.label" :value="item.value" :disabled="item.disabled"
                v-for="(item, index) in selectList2" :key="index" />
        </el-select>
    </div>
</template>

<script lang="ts" setup>
import { shallowRef, watch, PropType } from 'vue'
import { EnumType } from '@/stores/modules/enum'

const props = defineProps({
    modelValue: {
        type: Array as PropType<(number | undefined)[]>,
        default: () => ([])
    },
    dataList: {
        type: Array as PropType<EnumType[]>,
        default: () => ([])
    },
    checkbox: {
        type: Boolean,
        default: false
    },
    min: {
        type: Number,
        default: 0
    },
    max: {
        type: Number,
        default: 0
    },
})

const selectedValue = shallowRef(props.modelValue)
const selectList1 = shallowRef<EnumType[]>(JSON.parse(JSON.stringify(props.dataList)))
const selectList2 = shallowRef<EnumType[]>(JSON.parse(JSON.stringify(props.dataList)))

const emit = defineEmits(['update:modelValue', 'change'])

const onChange = (value: unknown) => {
    emit('update:modelValue', value)
    emit('change', value)
}

const onChecked = () => {
    onChange(selectedValue.value)
}

const onSelected = () => {
    const selection = [selectedValue.value[0] ?? '', selectedValue.value[1] ?? '']
    selectList1.value.forEach((e) => e.disabled = false)
    selectList2.value.forEach((e) => e.disabled = false)

    // 判断所有选项值是否为空
    if (selection.every((val) => val.toString() === '')) {
        onChange([])
    } else {
        if (selection[0].toString()) {
            selectList2.value.forEach((e) => {
                if (selection[0] > e.value) {
                    e.disabled = true
                }
            })
        }
        if (selection[1].toString()) {
            selectList1.value.forEach((e) => {
                if (selection[1] < e.value) {
                    e.disabled = true
                }
            })
        }
        onChange(selection)
    }
}

watch(() => props.modelValue, (val) => selectedValue.value = val)

watch(() => props.dataList, (val) => {
    selectList1.value = JSON.parse(JSON.stringify(val))
    selectList2.value = JSON.parse(JSON.stringify(val))
})
</script>

<style lang="less" scoped>
@import './index.less';
</style>