<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="docType" 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>
        <el-pagination :current-page="currentPage" v-model:page-size="pageSize" :page-sizes="[10, 15, 20]"
            layout="->, total, sizes, prev, pager, next, jumper" :total="total" hide-on-single-page
            @size-change="handleSizeChange" @current-change="handleCurrentChange" style="margin-top: 20px;" />
    </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 currentPage = ref(1)
const pageSize = ref(10)
const total = ref()

const handleSizeChange = (value: number) => {
    pageSize.value = value
    initCommonConfig(currentPage.value, pageSize.value)
}

const handleCurrentChange = (value: number) => {
    currentPage.value = value
    initCommonConfig(value, pageSize.value)
}

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

const handleEdit = (row: any) => {
    newFieldRef.value.fieldTitle = '编辑字段'
    if (typeof row.kingdeeFiledDetail === 'string') row.kingdeeFiledDetail = JSON.parse(row.kingdeeFiledDetail)
    if (row.flag === '-1') row.flag = 'ERP->MES'
    else row.flag = 'ERP->MES'
    newFieldRef.value.fieidForm = row
    newFieldRef.value.kingdeeFiledDetailData = row.kingdeeFiledDetail
    // newFieldRef.value.oldObj()
    newFieldRef.value.drawerVisible = true
}
const showDetails = (row: any) => {
    newFieldRef.value.fieldTitle = '详情'
    if (typeof row.kingdeeFiledDetail === 'string') row.kingdeeFiledDetail = JSON.parse(row.kingdeeFiledDetail)
    row.flag = '' + row.flag
    newFieldRef.value.fieidForm = row
    newFieldRef.value.kingdeeFiledDetailData = row.kingdeeFiledDetail
    newFieldRef.value.disabled = true
    newFieldRef.value.drawerVisible = true
}
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 = (pageNo?: number, pageSize?: number, name?: string) => {
    getCommonConfig(pageNo, pageSize, name).then((res: any) => {
        if (res.code === 200) {
            fieidData.value = res.data.records
            total.value = res.data.total
            loading.value = false
            console.log(res, '通用配置列表数据');
        }
    })
}

initCommonConfig(currentPage.value, pageSize.value)
</script>

<style scoped>
</style>