<template>
    <div class="default-main">
        <TableHeader ref="tableHeaderRef" :showReset="false" showExport>
            <template #select>
                <el-form-item label="关键字筛选">
                    <el-input v-model="tableStore.table.params.keywords" clearable placeholder="请输入关键字" />
                </el-form-item>

                <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="add">新增</el-button>
                <el-button :icon="Back" @click="go(-1)">返回</el-button>
            </template>
        </TableHeader>
        <Table ref="tableRef" />
        <!-- 新增 -->
        <Form ref="formRef" />
    </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 { Plus, EditPen } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import { Back, Setting, Search } from '@element-plus/icons-vue'
import Form from './form.vue'
defineOptions({
    name: 'govern/manage/programVersion'
})
const dictData = useDictData()
const DevTypeOptions = ref()
const tableHeaderRef = ref()
const { push, go } = useRouter()
const formRef = ref()
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: 'versionAgreement', minWidth: '100' },
        { title: '版本日期', field: 'versionDate', minWidth: '100' },
        { title: '修改人员', field: 'versionDate', minWidth: '100' },
        { title: '修改日期', field: 'versionDate', minWidth: '100' },

        { title: '描述', field: 'versionDate', minWidth: '100' },
        { title: '版本类型', field: 'versionDate', minWidth: '100' },

        {
            title: '状态',
            render: 'switch',
            width: 100,
            field: 'usageStatus',
            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 => {
                //         editEquipmentDelivery({
                //             ...row,
                //             status: row.status == 5 ? 1 : row.status == 6 ? 2 : row.status,
                //             usageStatus: row.usageStatus == 1 ? 0 : 1
                //         }).then(res => {
                //             ElMessage.success(row.usageStatus == 1 ? '设备停用成功！' : '设备启用成功！')
                //             tableStore.index()
                //         })
                //     })
                // })
            }
        },

        {
            title: '操作',
            fixed: 'right',
            align: 'center',
            width: '120',
            render: 'buttons',
            buttons: [
                {
                    name: 'productSetting',
                    title: '修改',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton'
                },
                {
                    name: 'productSetting',
                    title: '删除',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton'
                }
            ]
        }
    ],
    loadCallback: () => {}
})
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
    })
})

tableStore.table.params.devType = ''
provide('tableStore', tableStore)

const add = () => {
    formRef.value.open('新增版本')
}
onMounted(() => {
    tableHeaderRef.value.onComSearch()
})
</script>
