<template>
    <hc-dialog v-model="isShow" ui="hc-exctab-element-add-col-tab" title="添加新元素字段" widths="800px" :padding="false" :is-close="false" @close="dialogClose">
        <template #extra>
            <el-button type="primary" size="small" @click="addTableData">合成文本</el-button>
        </template>
        <hc-table ref="tabRef" :column="tableColumn" :datas="tableData" :is-index="false">
            <template #textInfo="{ row }">
                <hc-table-input v-model="row.textInfo" />
            </template>
            <template #textElementType="{ row }">
                <el-select v-model="row.textElementType" filterable block>
                    <el-option v-for="item in dataType" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
            </template>
            <template #textDeviation="{ row }">
                <hc-table-input v-model="row.textDeviation" />
            </template>
            <template #action="{ index }">
                <el-link type="danger" @click="delRowClick(index)">删除</el-link>
            </template>
        </hc-table>
        <template #footer>
            <el-button hc-btn @click="dialogClose">取消</el-button>
            <el-button hc-btn type="primary" :loading="submitLoading" @click="dialogSubmit">提交</el-button>
        </template>
    </hc-dialog>
</template>

<script setup>
import { ref, watch } from 'vue'
import { getDictionaryData } from '~uti/tools'
import { getObjValue } from 'js-fast-way'
import mainApi from '~api/exctab/exceltab'

const props = defineProps({
    info: {
        type: Object,
        default: () => ({}),
    },
})

//事件
const emit = defineEmits(['finish', 'close'])

//双向绑定
const isShow = defineModel('modelValue', {
    default: false,
})

//监听可否编辑
const dataInfo = ref(props.info)
watch(() => props.info, (data) => {
    dataInfo.value = getObjValue(data)
}, { immediate: true, deep: true })

//监听显示
watch(isShow, (val) => {
    if (val) getInfoData()
})

//获取数据详情
const getInfoData = () => {
    getDataType()
}

//元素表格
const tabRef = ref(null)
const tableColumn = [
    { key: 'textInfo', name: '元素名称' },
    { key: 'textElementType', name: '数据类型' },
    { key: 'textDeviation', name: '允许偏差值' },
    { key: 'action', name: '操作', width: 80, align: 'center' },
]
const tableData = ref([])

//获取元素类型
const dataType = ref([])
const getDataType = async () => {
    dataType.value = await getDictionaryData('data_type')
}

//新增元素
const addTableData = () => {
    tableData.value.push({
        exctabId: dataInfo.value.id,
        textInfo: '',
    })
}

//删除
const delRowClick = (index) => {
    tableData.value.splice(index, 1)
}

//提交
const submitLoading = ref(false)
const dialogSubmit = async () => {
    submitLoading.value = true
    const { isRes } = await mainApi.addColByTabId(tableData.value)
    submitLoading.value = false
    if (!isRes) return
    window.$message.success('操作成功')
    dialogClose()
    emit('finish')
}

//关闭弹窗
const dialogClose = () => {
    isShow.value = false
    submitLoading.value = false
    tableData.value = []
    emit('close')
}
</script>

<style lang="scss">
.el-overlay-dialog .el-dialog.hc-new-dialog.hc-exctab-element-add-col-tab .el-dialog__body{
    height: 660px;
    padding: 12px 0;
}
</style>
