<template>
    <div class="default-main">
        <TableHeader ref="tableHeaderRef" :showReset="false" showExport showCustomColumn>
            <template #select>
                <el-form-item label="设备型号:">
                    <el-select
                        v-model.trim="tableStore.table.params.devType"
                        filterable
                        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-button :icon="Plus" type="primary" @click="addMenu" class="ml10">新增版本</el-button>
                <el-button :icon="Back" @click="go(-1)">返回</el-button>
            </template>
        </TableHeader>
        <Table ref="tableRef" />
        <PopupVersion ref="popupVersionRef" v-if="showPopup" @closePopup="closePopup"></PopupVersion>
    </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 { delCsDictData } from '@/api/system-boot/csDictData'
import { ElMessage, ElMessageBox } from 'element-plus'
import { queryByCode, queryByid } from '@/api/system-boot/dictTree'
import { useDictData } from '@/stores/dictData'
import PopupVersion from '@/views/govern/manage/basic/popupVersion.vue'
import { Plus, Back } from '@element-plus/icons-vue'
import { auditEdData } from '@/api/cs-device-boot/edData'
import { activateUser, deluser, passwordConfirm } from '@/api/user-boot/user'
import { getFileUrl, downLoadFile } from '@/api/cs-system-boot/manage'
import { useRouter } from 'vue-router'
defineOptions({
    name: 'govern/manage/basic/version'
})
const popupVersionRef = ref()
const dictData = useDictData()
const showPopup = ref(false)
const DevTypeOptions = ref()
const tableHeaderRef = ref()
const { push, go } = useRouter()
const tableStore = new TableStore({
    url: '/cs-device-boot/edData/queryEdDataPage',
    method: 'POST',
    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', minWidth: '100' },
        { title: '版本号', field: 'versionNo', minWidth: '100' },
        {
            title: '校验码',
            field: 'crc',
            minWidth: '120',
            formatter: (row: any) => {
                return row.cellValue || '/'
            }
        },
        { title: '协议版本', field: 'versionAgreement', minWidth: '100' },
        { title: '版本日期', field: 'versionDate', minWidth: '100' },

        { title: '归档日期', field: 'updateTime', minWidth: '150' },

        { title: '描述', field: 'description', minWidth: '200' },
        // {
        //     title: '状态',
        //     field: 'status',
        //     render: 'tag',
        //     minWidth: '80',
        //     custom: {
        //         0: 'danger',
        //         1: 'success'
        //     },
        //     replaceValue: {
        //         0: '禁用',
        //         1: '启用'
        //     }
        // },
        {
            title: '使用状态',
            render: 'switch',
            width: 100,
            field: 'status',
            activeText: '启用',
            inactiveText: '禁用',
            inactiveValue: '0',
            activeValue: '1',
            onChangeField: (row: any, value: any) => {
                // console.log("🚀 ~ row:", row)
                ElMessageBox.prompt('二次校验密码确认', '', {
                    confirmButtonText: '确认',
                    cancelButtonText: '取消',
                    customClass: 'customInput',
                    inputType: 'text',
                    beforeClose: (action, instance, done) => {
                        if (action === 'confirm') {
                            if (instance.inputValue == null) {
                                return ElMessage.warning('请输入密码')
                            } else if (instance.inputValue?.length > 32) {
                                return ElMessage.warning(
                                    '密码长度不能超过32位,当前密码长度为' + instance.inputValue.length
                                )
                            } else {
                                done()
                            }
                        } else {
                            done()
                        }
                    }
                }).then(({ value }) => {
                    passwordConfirm(value).then(res => {
                        auditEdData({
                            id: row.id,
                            status: row.status == 0 ? 1 : 0
                        }).then((res: any) => {
                            ElMessage.success(row.status == 0 ? '启用成功!' : '禁用成功!')
                            tableStore.index()
                        })
                    })
                })
            }
        },
        {
            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)
                    }
                },
                {
                    name: 'edit',
                    title: '编辑',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton',
                    click: row => {
                        showPopup.value = true
                        setTimeout(() => {
                            popupVersionRef.value.open('编辑版本', row)
                        }, 100)
                    }
                }
            ]
        }
    ],
    loadCallback: () => {
        tableStore.table.data.forEach((item: any) => {
            item.statusName = item.status == 1 ? '禁用' : '启用'
            for (let key in item) {
                if (typeof item[key] !== 'number') {
                    item[key] = item[key] || '/'
                }
            }
        })
    }
})
queryByCode('Device_Type').then(res => {
    const id = res.data.id
    queryByid(id).then(res1 => {
        res1.data.map((item: any, index: any) => {
            if (item.pid == id) {
                res1.data.splice(index, 1)
            }
        })

        DevTypeOptions.value = res1.data
    })
})
// 下载报告
const downloadTheReport = (name: string) => {
    ElMessage.info('下载中......')
    getFileUrl(name).then((res: any) => {
        downLoadFile(res.data).then(res => {
            let blob = new Blob([res], {
                type: 'application/octet-stream'
            })
            const url = window.URL.createObjectURL(blob)
            const link = document.createElement('a') // 创建a标签
            link.href = url
            link.download = name.split('/').pop() || '升级文件' // 设置下载的文件名
            document.body.appendChild(link)
            link.click() //执行下载
            document.body.removeChild(link)
            ElMessage.success('下载成功')
        })
    })
}

tableStore.table.params.devType = ''
provide('tableStore', tableStore)
onMounted(() => {
    tableHeaderRef.value.onComSearch()
})
const addMenu = () => {
    showPopup.value = true
    setTimeout(() => {
        popupVersionRef.value.open('新增版本')
    }, 100)
}
const closePopup = () => {
    showPopup.value = false
}
</script>
<style lang="scss">
.customInput {
    .el-input__inner {
        -webkit-text-security: disc !important;
    }
}
</style>
