<template>
    <div >
        <TableHeader >
            <template v-slot:operation>
                <el-upload
                    action=""
                    class="upload-demo"
                    accept=".svg,.png,.jpg,.jpeg"
                    :show-file-list="false"
                    :auto-upload="false"
                    :on-change="chooseImage"
                >
                    <el-button :icon="Plus" type="primary" class="ml10">新增拓扑图</el-button>
                </el-upload>
            </template>
        </TableHeader>
        <Table ref="tableRef" />
        <popupEdit ref="popupRef"></popupEdit>
    </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 popupEdit from './popupEdit.vue'
import { deleteTopoTemplate, uploadTopo } from '@/api/cs-device-boot/topologyTemplate'
import { ElMessage } from 'element-plus'

defineOptions({
    name: 'govern/manage/gplot'
})
const tableRef = ref()
const popupRef = ref()
const tableStore = new TableStore({
    showPage: false,
    url: '/cs-device-boot/topologyTemplate/queryImage',
    method: 'POST',
    publicHeight: 60,
    column: [
        {
            title: '序号',
            width: 80,
            formatter: (row: any) => {
                return (tableStore.table.params.pageNum - 1) * tableStore.table.params.pageSize + row.rowIndex + 1
            }
        },
        { title: '拓扑图模版名称', field: 'name', align: 'center' },
        { title: '监测点数量', field: 'pointNum', align: 'center' },
        { title: '拓扑图', field: 'filePath', align: 'center', render: 'image' },
        {
            title: '操作', fixed: 'right',
            align: 'center',
            width: '180',
            render: 'buttons',
            buttons: [
                {
                    name: 'edit',
                    title: '编辑',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton',
                    click: row => {
                        popupRef.value.open(row)
                    }
                },
                {
                    name: 'del',
                    title: '删除',
                    type: 'danger',
                    icon: 'el-icon-Delete',
                    render: 'confirmButton',
                    popconfirm: {
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        confirmButtonType: 'danger',
                        title: '确定删除该菜单吗？'
                    },
                    click: row => {
                        deleteTopoTemplate(row.id).then(res => {
                            ElMessage.success('删除成功')
                            tableStore.index()
                        })
                    }
                }
            ]
        }
    ],
    loadCallback: () => {
        tableStore.table.data.forEach((item: any) => {
            item.pointNum = item.csLineTopologyTemplateVOList.length
        })
    }
})
provide('tableStore', tableStore)

onMounted(() => {
    tableStore.table.ref = tableRef.value
    tableStore.index()
    tableStore.table.loading = false
})
const addMenu = () => {
    // console.log(popupRef)
    popupRef.value.open('新增菜单')
}
const chooseImage = e => {
    const isWord =
        e.name.endsWith('.svg') || e.name.endsWith('.png') || e.name.endsWith('.jpg') || e.name.endsWith('.jpeg')

    if (!isWord) {
        return ElMessage.error('请上传图片或svg文件!')
    }

    uploadTopo(e.raw).then(res => {
        ElMessage.success('新增成功')
        tableStore.index()
    })
}
</script>
