<template>
    <div class="default-main">
        <TableHeader>
            <template #select>
                <el-form-item label="数据分类">
                    <el-select v-model.trim="tableStore.table.params.dataType" multiple filterable collapse-tags
                        clearable placeholder="请选择数据分类">
                        <el-option v-for="item in DataTypeSelect" :key="item.id" :label="item.name"
                            :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="数据存储">
                    <el-select v-model.trim="tableStore.table.params.classId" multiple filterable collapse-tags
                        clearable placeholder="请选择数据存储">
                        <el-option v-for="item in DataSelect" :key="item.id" :label="item.name"
                            :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="关键字筛选">
                    <el-input maxlength="32" show-word-limit v-model.trim="tableStore.table.params.searchValue"
                        placeholder="数据名称、别名、展示名称" clearable></el-input>
                </el-form-item>
            </template>
            <template #operation>
                <el-button :icon="Plus" type="primary" @click="addMenu" class="ml10">新增字典</el-button>
            </template>
        </TableHeader>
        <Table ref="tableRef" />
        <PopupDictionary ref="popupDictionary" v-if="show" @close="show = false"></PopupDictionary>
    </div>
</template>
<script setup lang="ts">
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 { useDictData } from '@/stores/dictData'
import PopupDictionary from './popupDictionary.vue'
import { ElMessage } from 'element-plus'
import { delCsDictData } from '@/api/system-boot/csDictData'
import { Plus } from '@element-plus/icons-vue'

defineOptions({
    name: 'govern/manage/basic/dictionary'
})
const show = ref(false)
const popupDictionary = ref()
const dictData = useDictData()
const DataSelect = dictData.getBasicData('Data')
const DataTypeSelect = dictData.getBasicData('Cs_Data_Type')
const ResourcesIdSelect = dictData.getBasicData('Data_Day')
const tableStore = new TableStore({
    url: '/system-boot/csDictData/list',
    method: 'POST',
    column: [
        {
            title: '序号',
            width: 80,
            formatter: (row: any) => {
                return (tableStore.table.params.pageNum - 1) * tableStore.table.params.pageSize + row.rowIndex + 1
            }
        },
        { title: '数据分类', field: 'dataTypeName', minWidth: 170 },
        { title: '数据名称', field: 'name', minWidth: 220 },
        { title: '别名', field: 'otherName', minWidth: 220 },
        { title: '展示名称', field: 'showName', minWidth: 170 },
        { title: '告警码', field: 'defaultValue', minWidth: 170 },
        {
            title: '相别',
            field: 'phase',
            minWidth: 80,
            formatter: (row: any) => {
                return row.cellValue
                    ? row.cellValue == '/'
                        ? '/'
                        : row.cellValue == 'M'
                            ? '/' : row.cellValue == 'T'
                                ? '/'
                                : row.cellValue + '相'
                    : '/'
            }
        },
        { title: '单位', field: 'unit', minWidth: 80 },
        { title: '基础数据类型', field: 'type', minWidth: 170 },
        { title: '数据谐波次数', field: 'harmStartEnd', minWidth: 170 },
        { title: '数据统计方法', field: 'statMethod', minWidth: 170 },
        { title: '数据存储', field: 'classIdName', minWidth: 120 },
        { title: '数据来源', field: 'resourcesIdName', minWidth: 120 },
        {
            title: '操作', fixed: 'right',
            align: 'center',
            width: '180',

            render: 'buttons',
            buttons: [
                {
                    name: 'edit',
                    title: '编辑',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton',
                    click: row => {
                        show.value = true
                        setTimeout(() => {
                            popupDictionary.value.open('编辑字典', row)
                        }, 100)
                    }
                },
                {
                    name: 'del',
                    title: '删除',
                    type: 'danger',
                    icon: 'el-icon-Delete',
                    render: 'confirmButton',
                    popconfirm: {
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        confirmButtonType: 'danger',
                        title: '确定删除吗？'
                    },
                    click: row => {
                        delCsDictData(row.id).then(res => {
                            ElMessage.success('删除成功')
                            tableStore.index()
                        })
                    }
                }
            ]
        }
    ],
    loadCallback: () => {
        tableStore.table.data.forEach((item: any) => {
            item.classIdName = DataSelect.find((child: any) => child.id == item.classId)?.name || '/'
            item.resourcesIdName = ResourcesIdSelect.find((child: any) => child.id == item.resourcesId)?.name || '/'
            // item.phaseName = item.phase === 'M' ? '无相别' : item.phase || '/'
            item.harmStartEnd = item.harmEnd ? item.harmStart + '-' + item.harmEnd : '/'
            for (let key in item) {
                if (typeof item[key] !== 'number') {
                    item[key] = item[key] || '/'
                }
            }
        })
    }
})

tableStore.table.params.searchValue = ''
tableStore.table.params.dataType = []
tableStore.table.params.classId = []
provide('tableStore', tableStore)
onMounted(() => {
    tableStore.index()
})
const addMenu = () => {
    show.value = true
    setTimeout(() => {
        popupDictionary.value.open('新增字典')
    }, 100)
}
</script>
