<template>
    <div class="default-main" v-loading="loading">
        <TableHeader ref="tableHeaderRef">
            <template #select>
                <el-form-item label="模版名称">
                    <el-input maxlength="32" show-word-limit v-model.trim="tableStore.table.params.name" clearable
                        placeholder="请输入名称"></el-input>
                </el-form-item>
                <el-form-item label="装置型号">
                    <el-select v-model.trim="tableStore.table.params.devType" placeholder="请选择" clearable>
                        <el-option v-for="item in DevTypeOptions" :key="item.id" :label="item.name"
                            :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
            </template>
            <template #operation>
                <el-upload action="" class="upload-demo" :show-file-list="false" :auto-upload="false"
                    :on-change="chooseFile">
                    <el-button :icon="Plus" type="primary" class="ml10">新增模版</el-button>
                </el-upload>
            </template>
        </TableHeader>
        <Table ref="tableRef" />
    </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 { ElMessage } from 'element-plus'
import { queryByCode, queryByid } from '@/api/system-boot/dictTree'
import { Plus } from '@element-plus/icons-vue'
import { addDevModel } from '@/api/access-boot/analyzeModel'
import { AuditDevModel } from '@/api/cs-device-boot/devmodel'

defineOptions({
    name: 'govern/manage/basic/template'
})
const DevTypeOptions = ref()
const tableHeaderRef = ref()
queryByCode('Direct_Connected_Device').then(res => {
    queryByid(res.data.id).then(res => {
        DevTypeOptions.value = res.data
    })
})
const tableStore = new TableStore({
    url: '/cs-device-boot/devmodel/queryDevModelPage',
    method: 'POST',
    column: [
        { title: '装置型号', field: 'devTypeName' },
        { title: '模板名称', field: 'name' },
        { title: '版本号', field: 'versionNo' },
        { title: '版本时间', field: 'versionDate', sortable: true },
        {
            title: '操作',
            align: 'center',
            width: '180',
            render: 'buttons',
            buttons: [
                {
                    name: 'del',
                    title: '删除',
                    type: 'danger',
                    icon: 'el-icon-Delete',
                    render: 'confirmButton',
                    popconfirm: {
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        confirmButtonType: 'danger',
                        title: '确定删除吗？'
                    },
                    click: row => {
                        AuditDevModel({
                            id: row.id,
                            status: 0
                        }).then(() => {
                            ElMessage.success('删除成功')
                            tableStore.index()
                        })
                    }
                }
            ]
        }
    ]
})
tableStore.table.params.devType = ''
tableStore.table.params.name = ''
provide('tableStore', tableStore)
onMounted(() => {
    tableStore.index()
})
const loading = ref(false)
const chooseFile = (e: any) => {
    console.warn(e)
    loading.value = true
    addDevModel(e.raw).then((res: any) => {
        if (res.code == 'A0000') {
            loading.value = false
            tableStore.index()
        }

    }).catch((e) => {
        loading.value = false
    })
}
</script>
