<template>
    <div style="padding: 20px">
        <el-button type="primary" style="margin-bottom: 20px;" @click="showFieid">新增字段</el-button>
        <el-table :data="fieidData" border v-loading="loading">
            <el-table-column label="第三方标识" prop="docType" align="center" />
            <el-table-column label="金蝶标识" prop="sFormId" align="center" />
            <el-table-column label="日志名称" prop="name" align="center" />
            <el-table-column label="配置方向" prop="flag" align="center">
                <template #default="scope">
                    {{ scope.row.flag === -1 ? 'ERP->MES' : 'MES->ERP' }}
                </template>
            </el-table-column>>
            <el-table-column label="创建时间" prop="createTime" align="center" />
            <el-table-column label="操作" width="200" align="center">
                <template #default="scope">
                    <el-link :underline="false" type="primary" @click="showDetails(scope.row)">详情</el-link>
                    <el-divider direction="vertical" />
                    <el-link :underline="false" type="primary" @click="handleEdit(scope.row)">编辑</el-link>
                    <el-divider direction="vertical" />
                    <el-link :underline="false" type="primary" @click="handleDelete(scope.row.id)">删除
                    </el-link>
                </template>
            </el-table-column>
        </el-table>
    </div>
    <new-field ref="newFieldRef" @initCommonConfig="initCommonConfig"></new-field>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus';
import { getCommonConfig, deleteCommonConfig } from '@/services/api/systemApi/fieldConfiguration/commonConfig'
import newField from './components/fieldConfiguration/newField.vue';

const fieidData = ref()
const loading = ref(true)
const newFieldRef = ref()


const showFieid = () => {
    newFieldRef.value.fieldTitle = '新增字段'
    newFieldRef.value.drawerVisible = true
}

const handleEdit = (row: any) => {
    newFieldRef.value.fieldTitle = '编辑字段'
    row.kingdeeFiledDetail = JSON.parse(row.kingdeeFiledDetail)
    if (row.flag === '-1') row.flag = 'ERP->MES'
    else row.flag = 'ERP->MES'
    newFieldRef.value.fieidForm = row
    // console.log(row.kingdeeFiledDetail);
    // console.log(typeof row.kingdeeFiledDetail);
    // if (JSON.stringify(row.kingdeeFiledDetail) !== '{}') 
    // newFieldRef.value.kingdeeFiledDetailData = row.kingdeeFiledDetail
    newFieldRef.value.drawerVisible = true
}
const showDetails = (row: any) => { }
const handleDelete = (id: number) => {
    ElMessageBox.confirm(
        '确定删除该字段?',
        '配置',
        { confirmButtonText: '确认', cancelButtonText: '取消', type: 'warning' })
        .then(() => {
            deleteCommonConfig(id).then((res: any) => {
                if (res.code === 200) {
                    initCommonConfig()
                    ElMessage({ type: 'success', message: '删除成功' })
                }
            })
        })
}

// 通用配置列表数据 
const initCommonConfig = () => {
    getCommonConfig().then((res: any) => {
        if (res.code === 200) {
            fieidData.value = res.data.records
            loading.value = false
            console.log(res, '通用配置列表数据');
        }
    })
}
initCommonConfig()
</script>

<style scoped>
</style>