<template>
    <div class="default-main">
        <TableHeader ref="tableHeaderRef" showExport>
            <template #select>
                <el-form-item label="关键字筛选">
                    <el-input maxlength="32" show-word-limit  v-model="tableStore.table.params.searchValue" clearable placeholder="请输入设备名称"
                        style="width:200px" />
                </el-form-item>
                <el-form-item label="通讯状态">
                    <el-select v-model.trim="tableStore.table.params.connectStatus" filterable placeholder="请选择通讯状态"
                        clearable>
                        <el-option label="中断" :value="0"></el-option>
                        <el-option label="正常" :value="1"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="设备型号">
                    <el-select v-model.trim="tableStore.table.params.devModel" 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>
                <el-form-item label="icd模型">
                    <el-select v-model.trim="tableStore.table.params.icd" filterable placeholder="请选择设备系列" clearable>
                        <el-option v-for="item in icdList" :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.upgrade" filterable placeholder="请选择升级设备筛选"
                        clearable>
                        <el-option label="可升级" :value="1"></el-option>
                        <el-option label="不可升级" :value="0"></el-option>
                    </el-select>
                </el-form-item>
            </template>
            <template #operation>
                <el-button :icon="Document" type="primary" class="ml10" @click="maintenance">版本维护</el-button>
                <!-- <el-button :icon="Share" type="primary" class="ml10" @click="upgradeVersion">批量升级</el-button> -->
            </template>
        </TableHeader>
        <Table :checkbox-config="checkboxConfig" ref="tableRef" />
        <upgrade ref="upgradeRef" />
        <deviceLog ref="deviceLogRef" />
    </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 { useDictData } from '@/stores/dictData'
import { Share, Document } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import upgrade from './comp/upgrade.vue'
import deviceLog from './comp/deviceLog.vue'
defineOptions({
    name: 'govern/manage/programVersion'
})
const upgradeRef = ref()
const deviceLogRef = ref()
const dictData = useDictData()
const DevTypeOptions = ref()
const icdList = ref()
const tableHeaderRef = ref()
const { push, options, currentRoute } = useRouter()
const tableStore: any = new TableStore({
    url: '/cs-device-boot/EquipmentDelivery/version/page',
    method: 'POST',
    column: [
        // { type: 'checkbox', width: '60', },
        {
            field: 'index',
            title: '序号',
            width: '80',
            formatter: (row: any) => {
                return (tableStore.table.params.pageNum - 1) * tableStore.table.params.pageSize + row.rowIndex + 1
            }
        },
        { title: '设备名称', field: 'name', minWidth: '150' },
        { title: '版本号', field: 'version', minWidth: '100', formatter: (row: any) => { return row.cellValue || '/' } },
        { title: '协议版本', field: 'protocolVersion', minWidth: '100', formatter: (row: any) => { return row.cellValue || '/' } },
        { title: '版本日期', field: 'versionDate', minWidth: '150', formatter: (row: any) => { return row.cellValue || '/' } },
        { title: '设备型号', field: 'devModelName', minWidth: '120', formatter: (row: any) => { return row.cellValue || '/' } },
        { title: 'icd模型', field: 'icd', minWidth: '120', formatter: (row: any) => { return icdList.value.filter((item: any) => item.id == row.cellValue)[0]?.name || '/' } },
        { title: '所属工程', field: 'associatedEngineering', minWidth: '120', formatter: (row: any) => { return row.cellValue || '/' } },
        { title: '所属项目', field: 'associatedProject', minWidth: '120', formatter: (row: any) => { return row.cellValue || '/' } },
        {
            title: '状态',
            field: 'runStatus',
            render: 'tag',
            minWidth: '80',
            custom: {
                1: 'danger',
                2: 'success'
            },
            replaceValue: {
                1: '离线',
                2: '在线'
            }
        },
        { title: '更新时间', field: 'updateTime', minWidth: '150', formatter: (row: any) => { return row.cellValue || '/' }, sortable: true },
        { title: '修改人员', field: 'updateByName', minWidth: '100', formatter: (row: any) => { return row.cellValue || '/' } },
        {
            title: '操作',
            fixed: 'right',
            align: 'center',
            width: '130',
            render: 'buttons',
            buttons: [
                {
                    name: 'productSetting',
                    title: '升级',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    disabled: (row: any) => {
                        return row.upgrade == 0
                    },
                    render: 'basicButton',
                    click: (row: any) => {
                        upgradeRef.value.open({
                            id: [row.id],
                            devModel: row.devModel,
                            version: row.version

                        })
                    }
                },
                {
                    name: 'productSetting',
                    title: '日志',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton',
                    click: (row: any) => {
                        deviceLogRef.value.open(row)
                    }
                }
            ]
        }
    ],
    loadCallback: () => { }
})
const checkboxConfig = reactive({
    checkMethod: ({ row }) => {
        return row.upgrade == 1
    }
})
const getQuery = async () => {
    queryByCode('Device_Type').then(res => {
        const id = res.data.id
        queryByid(id).then(res => {
            res.data.map((item: any, index: any) => {
                if (item.pid == id) {
                    res.data.splice(index, 1)
                }
            })

            DevTypeOptions.value = res.data
        })
    })
    queryByCode('Icd_Model').then(res => {
        const id = res.data.id
        queryByid(id).then(res => {
            icdList.value = res.data
        })
    })

}

// 禁止点击

// 升级版本
const upgradeVersion = () => {
    if (!tableStore.table.selection.length) {
        ElMessage.warning('请选择需要升级的设备!')
        return
    }
    // 获取第一个设备的 devModel
    const firstDevModel = tableStore.table.selection[0].devModel

    // 检查是否所有设备的 devModel 都相同
    const allSame = tableStore.table.selection.every(device => device.devModel === firstDevModel)

    if (!allSame) {
        return ElMessage.warning('请选择相同设备型号的设备进行批量升级!')
    }

    upgradeRef.value.open(
        {
            id: tableStore.table.selection.map(item => item.id),
            devModel: firstDevModel

        }
    )
}
// 版本维护
const maintenance = () => {
    push({
        // path: '/versionMaintenance'
        path: '/version'
    })
}

tableStore.table.params.searchValue = ''
tableStore.table.params.connectStatus = ''
tableStore.table.params.devModel = ''
tableStore.table.params.icd = ''
tableStore.table.params.upgrade = ''
provide('tableStore', tableStore)
onMounted(() => {
    getQuery()
    tableStore.index()
})
</script>
