<template>
    <el-input
        v-model="calculationFormulaName"
        style="width: 100%"
        class="mb20"
        placeholder="请输入计算公式"
        :autosize="{ minRows: 2, maxRows: 2 }"
        @change="handleInput"
        type="textarea"
    >
    </el-input>
    <div class="flex">
        <el-select
            v-if="!loading"
            class="mr20"
            @change="handleSelectConfirm"
            filterable
            placeholder="请选择字典列表"
        >
            <el-option v-for="item in dictList" :key="item.label" :label="item.label" :value="item.label">
            </el-option>
        </el-select>
        <el-select v-if="!loading" @change="handleSelectConfirm" filterable placeholder="请选择全局列表">
            <el-option v-for="item in globalList" :key="item.label" :label="item.label" :value="item.label">
            </el-option>
        </el-select>
    </div>
</template>
<script setup lang="ts">
// import { computedTypeList } from '@/libs/commonMeth'
import { getByDictType } from '@/api/main';
// 使用 defineEmits 注册一个自定义事件
const emit = defineEmits(['getValue']);
interface PropTypes {
    // 字典数据
    dictType: string;
}
const { dictType } = defineProps<PropTypes>();
// 需要传递出去的数据
/*
  1、dictList
  2、globalList
  3、calculationFormulaName
 */
// -----------------------费用计算公式开始-----------------------
let loading = ref(true);
const formulaValue = inject('calculationFormulaName') as any;
watch(formulaValue, (newVal: number) => {
    if (newVal > 1722437870770) {
        calculationFormulaName.value = '';
    } else {
        calculationFormulaName.value = newVal;
    }
});
// 获取字段
const dictList = ref([] as any);
const globalList = ref([] as any);
function computedTypeList(type: string) {
    getByDictType(type)
        .then(res => {
            res.data.dictList.forEach((item: any) => {
                dictList.value.push({
                    label: item.dictLabel,
                    value: item.dictValue,
                });
            });
            res.data.globalList.forEach((item: any) => {
                globalList.value.push({
                    label: item.name,
                    value: item.key,
                });
            });
            emit('getValue', calculationFormulaName.value, dictList, globalList);
        })
        .then(() => {
            loading.value = false;
        });
}
computedTypeList(dictType);

const calculationFormulaName = ref();
const handleSelectConfirm = e => {
    if (e) {
        if (!calculationFormulaName.value) {
            calculationFormulaName.value = '';
        }
        // 赋值中文字段
        calculationFormulaName.value += e;
        // 子传父
        emit('getValue', calculationFormulaName.value, dictList, globalList);
    }
};
const handleInput = () => {
    // 子传父
    emit('getValue', calculationFormulaName.value, dictList, globalList);
};
// -----------------------费用计算公式结束-----------------------
defineExpose({
    calculationFormulaName,
});
</script>
<style scoped></style>
