<template>
    <div class="table-box">
        <ProTable ref="proTable" :columns="columns" :request-api="getTableList">
            <!-- :data='testScriptData' 如果要显示静态数据，就切换该配置 -->
            <!-- 表格 header 按钮 -->
            <template #tableHeader="scope">
                <el-button v-auth.testScript="'add'" type="primary" :icon="CirclePlus" @click="openDialog('add')">
                    新增
                </el-button>

                <el-button
                    v-auth.testScript="'delete'"
                    type="danger"
                    :icon="Delete"
                    plain
                    :disabled="!scope.isSelected"
                    @click="batchDelete(scope.selectedListIds)"
                >
                    删除
                </el-button>
            </template>
            <!-- 表格操作 -->
            <template #operation="scope">
                <el-button
                    v-auth.testScript="'edit'"
                    type="primary"
                    link
                    :icon="EditPen"
                    :model-value="false"
                    @click="openDialog('edit', scope.row)"
                >
                    编辑
                </el-button>
                <el-button
                    v-auth.testScript="'delete'"
                    type="primary"
                    link
                    :icon="Delete"
                    @click="handleDelete(scope.row)"
                >
                    删除
                </el-button>
                <el-button
                    v-auth.testScript="'upgrade'"
                    type="primary"
                    v-if="scope.row.type !== 1"
                    link
                    :icon="Share"
                    @click="updateType(scope.row)"
                >
                    升级
                </el-button>
            </template>
        </ProTable>
    </div>
    <ComparisonPopup ref="comparisonPopup" />
    <ValueTypePopup ref="valueTypePopup" />
    <TestScriptPopup ref="testScriptPopup" />
</template>

<script setup lang="tsx" name="useRole">
import { type TestScript } from '@/api/device/interface/testScript'
import { useHandleData } from '@/hooks/useHandleData'
import ProTable from '@/components/ProTable/index.vue'
import type { ProTableInstance, ColumnProps } from '@/components/ProTable/interface'
import { CirclePlus, Delete, EditPen, Share } from '@element-plus/icons-vue'
import { useDictStore } from '@/stores/modules/dict'
import ComparisonPopup from './components/comparisonPopup.vue'
import TestScriptPopup from './components/testScriptPopup.vue'
import ValueTypePopup from './components/valueTypePopup.vue'
import { useRouter } from 'vue-router'
import { getPqScriptList, updatePqScript, deletePqScript } from '@/api/device/testScript/index'
import { computed, reactive, ref } from 'vue'
import { useModeStore } from '@/stores/modules/mode' // 引入模式 store
// defineOptions({
//     name: 'testScript'
// })
const router = useRouter()
const comparisonPopup = ref()
const testScriptPopup = ref()
const valueTypePopup = ref()
const modeStore = useModeStore()
const dictStore = useDictStore()

const getTableList = (params: any) => {
    let newParams = JSON.parse(JSON.stringify(params))
    const patternId = dictStore.getDictData('Pattern').find(item => item.name === modeStore.currentMode)?.id //获取数据字典中对应的id
    newParams.pattern = patternId
    return getPqScriptList(newParams)
}

// ProTable 实例
const proTable = ref<ProTableInstance>()

const showValueSearch = computed(() => {
    if (modeStore.currentMode == '比对式') {
        return false
    } else {
        return true
    }
})

// 表格配置项
const columns = reactive<ColumnProps<TestScript.ResTestScript>[]>([
    { type: 'selection', fixed: 'left', width: 70 },
    { type: 'index', fixed: 'left', width: 70, label: '序号' },
    {
        prop: 'name',
        label: '名称',
        minWidth: 350,
        search: { el: 'input' }
    },
    {
        prop: 'standardName',
        label: '参照标准名称',
        minWidth: 150
    },
    {
        prop: 'standardTime',
        label: '标准推行年份',
        minWidth: 150
    },
    {
        prop: 'valueType',
        label: '值类型',
        enum: dictStore.getDictData('Script_Value_Type'),
        fieldNames: { label: 'name', value: 'id' },
        search: showValueSearch.value ? { el: 'select' } : undefined,
        minWidth: 150
    },
    {
        prop: 'type',
        label: '模板类型',
        minWidth: 150,
        render: scope => {
            return <el-tag type={scope.row.type ? 'success' : 'primary'}> {scope.row.type ? '模版' : '脚本'} </el-tag>
        }
    },
    { prop: 'operation', label: '操作', fixed: 'right', width: 250 }
])

// 打开 drawer(新增、编辑)
const openDialog = (titleType: string, row: Partial<TestScript.ResTestScript> = {}) => {
    if (modeStore.currentMode == '比对式') {
        comparisonPopup.value?.open(titleType, row, modeStore.currentMode)
    } else {
        if (titleType == 'add') {
            router.push({
                path: '/machine/testScriptAdd',
                state: { title: '新增检测脚本', row: '', mode: modeStore.currentMode }
            })
        } else {
            router.push({
                path: '/machine/testScriptAdd',
                state: { title: '编辑检测脚本', row: JSON.stringify(row), mode: modeStore.currentMode }
            })
        }
    }
}

// 批量删除设备
const batchDelete = async (id: string[]) => {
    await useHandleData(deletePqScript, id, '删除所选脚本')
    proTable.value?.clearSelection()
    proTable.value?.getTableList()
}

// 删除设备
const handleDelete = async (params: TestScript.ResTestScript) => {
    await useHandleData(deletePqScript, [params.id], `删除【${params.name}】脚本`)
    proTable.value?.getTableList()
}

// 升级为模版
const updateType = async (params: TestScript.ResTestScript) => {
    await useHandleData(updatePqScript, params, `升级【${params.name}】为模版`)
    proTable.value?.getTableList()
}
</script>
