<template>
    <div>
        <TableHeader datePicker area ref="TableHeaderRef" showExport showCustomColumn>
            <template #select>
                <el-form-item label="统计类型">
                    <el-select
                        v-model="tableStore.table.params.statisticalType"
                        value-key="id"
                        placeholder="请选择统计类型"
                        filterable
                    >
                        <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item" />
                    </el-select>
                </el-form-item>
               
                <el-form-item label="筛选数据">
                    <el-input
                        v-model="tableStore.table.params.searchValue"
                        placeholder="请输入项目名称"
                        clearable
                        :show-word-limit="true"
                        :maxlength="32"
                    />
                </el-form-item>
            </template>

            <template #operation>
                <!-- <el-button icon="el-icon-Download" type="primary">导出</el-button> -->
            </template>
        </TableHeader>
        <Table ref="tableRef" />
        <!-- 添加弹窗 -->
        <el-dialog draggable v-model="detailDialogVisible" :title="detailDialogTitle" width="1000px">
            <div v-loading="detailLoading">
                <div style="margin-bottom: 10px; text-align: right">
                    <el-button icon="el-icon-Download" type="primary" @click="exportDetailEvent">导出</el-button>
                </div>
                <vxe-table
                    ref="detailTableRef"
                    :data="detailData"
                    auto-resize
                    v-bind="defaultAttribute"
                    resizable
                    show-overflow
                    height="400px"
                >
                    <vxe-column title="序号" width="60" type="seq" align="center"></vxe-column>
                    <vxe-column field="subName" title="变电站名称" align="center" min-width="120"></vxe-column>
                    <vxe-column field="lineName" title="监测点名称" align="center" min-width="120"></vxe-column>
                    <vxe-column field="objName" title="监测对象名称" align="center" min-width="120"></vxe-column>
                    <vxe-column field="loadType" title="干扰源类型" align="center" min-width="120"></vxe-column>
                    <vxe-column field="powerFlag" title="监测位置" align="center" min-width="100"></vxe-column>
                    <vxe-column field="vharmonicValue" title="监测点污染值" align="center" min-width="120"></vxe-column>
                </vxe-table>
            </div>
        </el-dialog>
    </div>
</template>
<script setup lang="ts">
import { ref, onMounted, provide, nextTick, watch } from 'vue'
import TableStore from '@/utils/tableStore'
import Table from '@/components/table/index.vue'
import TableHeader from '@/components/table/header/index.vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { defaultAttribute } from '@/components/table/defaultAttribute'
import { mainHeight } from '@/utils/layout'
import { useDictData } from '@/stores/dictData'
import { addUse, updateUse, removeUse } from '@/api/advance-boot/bearingCapacity'
//
const detailDialogVisible = ref(false)
const detailLoading = ref(false)
const detailDialogTitle = ref('')
const detailData = ref([])
const dictData = useDictData()
const detailTableRef = ref()
const lineList = dictData.getBasicData('Line_Type')
const options = dictData.getBasicData('Pollution_Calc')
const tableStore = new TableStore({
    url: '/harmonic-boot/PollutionSubstation/downPollutionSensitiveUser',
    publicHeight: 65,
    isWebPaging: true,
    method: 'POST',
    filename: '敏感及重要用户评估',
    column: [
        {
            title: '序号',
            width: 80,
            formatter: (row: any) => {
                return (tableStore.table.params.pageNum - 1) * tableStore.table.params.pageSize + row.rowIndex + 1
            }
        },

        {
            field: 'gdName',
            title: '供电公司',
            width: 150
        },

        { field: 'subStationName', title: '变电站', minWidth: 150 },
        { field: 'projectName', title: '项目名称', minWidth: 150 },
        {
            field: 'subValue',
            title: '污染值',
            width: 150,
            formatter: (row: any) => {
                return row.cellValue == null ? '暂未安装监测点' : row.cellValue
            }
        },
        {
             title: '操作',fixed: 'right',
            width: '180',
            
            render: 'buttons',
            buttons: [
                {
                    name: 'edit',
                    title: '监测点污染详情',
                    type: 'primary',
                    icon: 'el-icon-Open',
                    render: 'basicButton',
                    disabled: row => {
                        return row.subValue == null
                    },
                    click: row => {
                        detailDialogTitle.value = '监测点污染详情'
                  
                        detailLoading.value = true
                        detailData.value = row.powerFlagPollutionList
                        detailDialogVisible.value = true
                        detailLoading.value = false
                    }
                }
            ]
        }
    ],

    beforeSearchFun: () => {
        tableStore.table.params.deptId = tableStore.table.params.deptIndex
        tableStore.table.params.ids = [tableStore.table.params.statisticalType?.id]
    },
    loadCallback: () => {
        tableStore.table.column[4].title = tableStore.table.params.statisticalType.name + '污染值'
    }
})

tableStore.table.params.searchValue = ''
tableStore.table.params.statisticalType = options[0]
tableStore.table.params.powerFlag = 0
tableStore.table.params.isUpToGrid = 0
// 导出详情数据的方法
const exportDetailEvent = () => {
    detailTableRef.value.exportData({
        filename: detailDialogTitle.value,
        sheetName: 'Sheet1',
        type: 'xlsx',
        useStyle: true,
        data: detailData.value,
        columnFilterMethod: function (column: any) {
            return !(column.$columnIndex === 0)
        }
    })
}
provide('tableStore', tableStore)
onMounted(() => {
    tableStore.index()
})
const setSearchValue = (val: string) => {
    tableStore.table.params.searchValue = val
    tableStore.index()
}

defineExpose({
    setSearchValue
})
</script>
