<template>
    <div class="tabs-container">
        <el-tabs type="border-card" class="fixed-width-tabs" style="height: 100%">
            <el-tab-pane label="短闪">
                <!-- 全局设置菜单内容 Modulation-->

                <div style="display: flex">
                    <el-form-item label="标准值">
                        <el-select
                            v-model="form[0].flickerData.flickerValue"
                            style="width: 180px"
                            placeholder="请选择标准值"
                        >
                            <el-option
                                v-for="item in standard"
                                :key="item.label"
                                :label="item.label"
                                :value="item.value"
                            />
                        </el-select>
                    </el-form-item>
                    <el-form-item abel-width="180px">
                        <template #label>
                            <el-select
                                v-model="form[0].flickerData.waveType"
                                @change="changeLable"
                                style="width: 170px"
                            >
                                <el-option
                                    v-for="item in waveList"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </template>

                        <el-select
                            placeholder="请选择波动类型"
                            style="width: 180px"
                            filterable
                            allow-create
                            v-model="form[0].flickerData.fchagFre"
                            :disabled="!form[0].flickerFlag || props.childForm[0].flickerData.flickerValue == null"
                            @change="changeWaveType"
                        >
                            <el-option
                                v-for="(item, i) in waveChildrenList"
                                :key="item.fchagFre"
                                :value="item.fchagFre"
                                :label="item.fchagFre"
                            />
                        </el-select>
                    </el-form-item>
                </div>
                <div style="display: flex; justify-content: end">
                    <el-form-item label="变动量(%)" label-width="180px">
                        <el-input
                            placeholder="请输入变动量"
                            style="width: 180px"
                            v-model="form[0].flickerData.fchagValue"
                            :disabled="!form[0].flickerFlag || props.childForm[0].flickerData.flickerValue == null"
                        />
                    </el-form-item>
                </div>
            </el-tab-pane>
        </el-tabs>
        <el-tabs type="border-card" class="fixed-width-tabs" style="height: 100%">
            <el-tab-pane label="波形">
                <!-- 全局设置菜单内容 WaveForm -->
                <div>
                    <el-form-item label="波类型" label-width="100px">
                        <el-select
                            placeholder="请选择波类型"
                            style="width: 150px"
                            v-model="form[0].flickerData.waveFluType"
                            :disabled="!form[0].flickerFlag"
                        >
                            <el-option label="RECT" value="RECT"></el-option>
                            <el-option label="SIN" value="SIN"></el-option>
                            <el-option label="SQU" value="SQU"></el-option>
                        </el-select>
                    </el-form-item>
                </div>
                <div>
                    <el-form-item label="占空比(%)" label-width="100px">
                        <el-input
                            style="width: 150px"
                            v-model="form[0].flickerData.fdutyCycle"
                            :disabled="!form[0].flickerFlag"
                        />
                    </el-form-item>
                </div>
            </el-tab-pane>
        </el-tabs>
    </div>
</template>

<script setup lang="ts">
import { onMounted, ref, computed } from 'vue'
const props = defineProps({
    childForm: {
        type: [Array, Object] as any,
        required: true
    }
})

const standard = [
    {
        label: '1',
        value: '1'
    },
    {
        label: '3',
        value: '3'
    }
]
const waveList = [
    {
        label: '变动频度(次/min)',
        value: 'CPM',
        children1: [
            {
                fchagFre: '1',
                fchagValue: '2.724'
            },
            {
                fchagFre: '2',
                fchagValue: '2.211'
            },
            {
                fchagFre: '7',
                fchagValue: '1.459'
            },
            {
                fchagFre: '39',
                fchagValue: '0.906'
            },
            {
                fchagFre: '110',
                fchagValue: '0.725'
            },
            {
                fchagFre: '1620',
                fchagValue: '0.402'
            }
        ],
        children3: [
            {
                fchagFre: '7',
                fchagValue: '4.377'
            },
            {
                fchagFre: '110',
                fchagValue: '2.175'
            },
            {
                fchagFre: '1620',
                fchagValue: '1.206'
            }
        ]
    },
    {
        label: '变动频率(HZ)',
        value: 'HZ',
        children1: [
            {
                fchagFre: '0.008333',
                fchagValue: '2.724'
            },
            {
                fchagFre: '0.016667',
                fchagValue: '2.211'
            },
            {
                fchagFre: '0.058333',
                fchagValue: '1.459'
            },
            {
                fchagFre: '0.325',
                fchagValue: '0.906'
            },
            {
                fchagFre: '0.916',
                fchagValue: '0.725'
            },
            {
                fchagFre: '13.5',
                fchagValue: '0.402'
            }
        ],
        children3: [
            {
                fchagFre: '0.058333',
                fchagValue: '4.377'
            },
            {
                fchagFre: '0.916',
                fchagValue: '2.175'
            },
            {
                fchagFre: '13.5',
                fchagValue: '1.206'
            }
        ]
    }
]
const waveChildrenList = computed(() => {
    return waveList.filter((item: any) => item.value === form.value[0].flickerData.waveType)[0][
        props.childForm[0].flickerData.flickerValue == '1' ? 'children1' : 'children3'
    ]
})
const changeLable = e => {
    setTimeout(() => {
        if (props.childForm[0].flickerData.fchagFre != '') {
            let data = waveChildrenList.value.filter((item: any) =>
                props.childForm[0].flickerData.waveType == 'CPM'
                    ? item.fchagFre == Math.round(props.childForm[0].flickerData.fchagFre * 120)
                    : (item.fchagFre - 0).toFixed(3) == (props.childForm[0].flickerData.fchagFre / 120).toFixed(3)
            )
            if (data.length > 0) {
                props.childForm[0].flickerData.fchagValue = data[0].fchagValue
                props.childForm[0].flickerData.fchagFre = data[0].fchagFre
            } else {
                props.childForm[0].flickerData.fchagFre =
                    props.childForm[0].flickerData.waveType == 'CPM'
                        ? Math.round(props.childForm[0].flickerData.fchagFre * 120)
                        : (props.childForm[0].flickerData.fchagFre / 120).toFixed(3)
            }
        }
    }, 100)
}
// [频率变化]
const changeWaveType = e => {
    props.childForm[0].flickerData.fchagValue = waveChildrenList.value?.filter(
        (item: any) => item.fchagFre == e
    )[0].fchagValue
}
const form: any = computed({
    get() {
        if (props.childForm[0].flickerData.flickerValue == null) {
            props.childForm[0].flickerData.fchagValue = ''
            props.childForm[0].flickerData.fchagFre = ''
        }
        return props.childForm
    },
    set(value) {}
})
</script>

<style scoped>
.form-container {
    display: flex;
    justify-content: space-between; /* 使两个 el-form-item 之间有间距 */
    align-items: center; /* 使两个 el-form-item 垂直居中对齐 */
}

.tabs-container {
    display: flex;
    justify-content: center; /* 使两个 el-tabs 居中对齐 */
    align-items: stretch; /* 使两个 el-tabs 高度拉伸 */
    height: 100%;
    padding: 0 20px; /* 可选：添加左右间距 */
}

.fixed-width-tabs {
    margin-right: 100px; /* 可选：添加右侧间距 */
}

.fixed-width-tabs:last-child {
    margin-right: 0; /* 最后一个 el-tabs 不需要右侧间距 */
}

.el-tab-pane {
    display: flex;
    flex-direction: column; /* 确保内容上下排列 */
}

.el-form-item {
    margin-bottom: 20px; /* 可选：添加表单项之间的间距 */
}
</style>
