<template>
    <div class="dictiontary-list-detail child-router">
        <TableHeader>
            <template #select>
                <el-form-item label="">
                    <el-page-header @back="$emit('close')">
                        <template #content>
                            <span class="text-large font-600 mr-3">{{ props.detail.name }}详情信息</span>
                        </template>
                    </el-page-header>
                </el-form-item>
                <el-form-item label="过滤筛选">
                    <el-input maxlength="32" show-word-limit style="width: 240px"
                        v-model.trim="tableStore.table.params.searchValue" clearable placeholder="请输入名称或编码筛选" />
                </el-form-item>
            </template>
            <template #operation>
                <el-button :icon="Plus" type="primary" @click="add">新增</el-button>
            </template>
        </TableHeader>
        <Table ref="tableRef" />
        <PopupDetailEdit ref="popupEditRef"></PopupDetailEdit>
    </div>
</template>
<script setup lang="ts">
import { Plus } from '@element-plus/icons-vue'
import { ref, onMounted, provide } from 'vue'
import TableStore from '@/utils/tableStore'
import Table from '@/components/table/index.vue'
import TableHeader from '@/components/table/header/index.vue'
import { ElMessage } from 'element-plus'
import PopupDetailEdit from './popupDetailEdit.vue'
import { dictDataDelete } from '@/api/system-boot/dicData'

defineOptions({
    name: 'setting/dictionary/list/detail'
})

interface Props {
    detail: anyObj
}

const props = withDefaults(defineProps<Props>(), {
    detail: () => {
        return {}
    }
})
const popupEditRef = ref()
const tableStore = new TableStore({
    url: '/system-boot/dictData/getTypeIdData',
    method: 'POST',
    column: [
        { title: '名称', field: 'name' },
        { title: '编码', field: 'code' },
        { title: '排序', field: 'sort' },
        { title: '计算值', field: 'value' },
        { title: '事件等级', field: 'levelName' },
        { title: '算法描述', field: 'algoDescribe' },
        { title: '状态', field: 'stateName' },
        {
            title: '操作',
            width: '180',
            render: 'buttons',
            fixed: 'right',
            buttons: [
                {
                    title: '编辑',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton',
                    click: row => {
                        popupEditRef.value.open('编辑', {
                            ...row,
                            openDescribe: props.detail.openDescribe,
                            openLevel: props.detail.openLevel,
                            typeName: props.detail.name + row.name,
                            typeId: props.detail.id
                        })
                    }
                },
                {
                    title: '删除',
                    type: 'danger',
                    icon: 'el-icon-Delete',
                    render: 'confirmButton',
                    popconfirm: {
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        confirmButtonType: 'danger',
                        title: '确定删除该字典类型吗？'
                    },
                    click: row => {
                        dictDataDelete([row.id]).then(() => {
                            ElMessage.success('删除成功')
                            tableStore.index()
                        })
                    }
                }
            ]
        }
    ],
    loadCallback: () => {
        tableStore.table.data.forEach((item: any) => {
            item.stateName = item.state === 1 ? '正常' : '删除'
            switch (item.level) {
                case 0:
                    item.levelName = '普通'
                    break
                case 1:
                    item.levelName = '中等'
                    break
                case 2:
                    item.levelName = '严重'
                    break
                default:
                    item.levelName = '未知'
                    break
            }
            for (let key in item) {
                if (typeof item[key] !== 'number') {
                    item[key] = item[key] || '/'
                }
            }
        })
    }
})

provide('tableStore', tableStore)
tableStore.table.params.searchValue = ''
tableStore.table.params.searchState = 0
tableStore.table.params.typeId = props.detail.id
if (props.detail.openLevel !== 1) {
    tableStore.table.column = tableStore.table.column.filter((item: any) => item.field !== 'levelName')
}
if (props.detail.openDescribe !== 1) {
    tableStore.table.column = tableStore.table.column.filter((item: any) => item.field !== 'algoDescribe')
}
onMounted(() => {
    tableStore.index()
})
const add = () => {
    popupEditRef.value.open('新增', {
        openLevel: props.detail.openLevel,
        openDescribe: props.detail.openDescribe,
        typeName: props.detail.name,
        typeId: props.detail.id
    })
}
</script>
