<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" stripe>
            <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">
                <template #default="scope">
                    {{ scope.row.name === '其他出库' || scope.row.name === '其他入库' ? scope.row.name + '单' : scope.row.name }}
                </template>
            </el-table-column>
            <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" :currentPage="currentPage" :pageSize="pageSize">
    </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.updateCount = 0
    newFieldRef.value.drawerVisible = true
}

const handleEdit = (row: any) => {
    const data = Object.assign({}, row)
    newFieldRef.value.fieldTitle = '编辑字段'
    if (typeof data.kingdeeFiledDetail === 'string') data.kingdeeFiledDetail = JSON.parse(data.kingdeeFiledDetail)
    data.flag = '' + data.flag
    data.isAutoPerform = '' + data.isAutoPerform
    data.isInTransaction = '' + data.isInTransaction
    newFieldRef.value.fieidForm = data
    newFieldRef.value.kingdeeFiledDetailData = data.kingdeeFiledDetail
    newFieldRef.value.updateCount = -1
    newFieldRef.value.drawerVisible = true
}
const showDetails = (row: any) => {
    const data = Object.assign({}, row)
    newFieldRef.value.fieldTitle = '详情'
    if (typeof data.kingdeeFiledDetail === 'string') data.kingdeeFiledDetail = JSON.parse(data.kingdeeFiledDetail)
    data.flag = '' + data.flag
    data.isInTransaction = '' + data.isInTransaction
    newFieldRef.value.fieidForm = data
    newFieldRef.value.disabled = true
    newFieldRef.value.kingdeeFiledDetailData = data.kingdeeFiledDetail
    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(currentPage.value, pageSize.value)
                    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>