<template>
    <div  v-loading="loading">
        <TableHeader ref="tableHeaderRef" showCustomColumn>
            <template #select>
                <el-form-item label="模版名称">
                    <el-input maxlength="32" show-word-limit 
                        
                        style="width: 200px"
                        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"
                    :accept="'.json'"
                    :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'
import {  downLoadFile } from '@/api/cs-system-boot/manage'
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',
    publicHeight: 60,
    column: [
         {
            field: 'index',
            title: '序号',
            width: '80',
            formatter: (row: any) => {
                return (tableStore.table.params.pageNum - 1) * tableStore.table.params.pageSize + row.rowIndex + 1
            }
        },
        { title: '设备型号', field: 'devTypeName' },
        { title: '模板名称', field: 'name' },
        { title: '版本号', field: 'versionNo' },
        { title: '版本时间', field: 'versionDate', sortable: true },
        {
            title: '操作', fixed: 'right',
            align: 'center',
            width: '180',
            render: 'buttons',
            buttons: [
                {
                    name: 'productSetting',
                    title: '下载模版',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton',
                    click: row => {
                        downloadTheReport(row.filePath,row.devTypeName)
                    }
                },
                {
                    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
        })
}
// 下载报告
const downloadTheReport = (filePath: string,name:string) => {
    ElMessage.info('下载中......')
    downLoadFile(filePath).then(res => {
        let blob = new Blob([res], {
            type: '	application/json'
        })
        const url = window.URL.createObjectURL(blob)
        const link = document.createElement('a') // 创建a标签
        link.href = url
        link.download = name+'.json' // 设置下载的文件名
        document.body.appendChild(link)
        link.click() //执行下载
        document.body.removeChild(link)
        ElMessage.success('下载成功')
    })
}
</script>
