<template>
    <div>
        <!--治理效果报表 -->
        <TableHeader :showReset="false">
            <template v-slot:select>
                <el-form-item label="治理对象">
                    <el-select
                        v-model="tableStore.table.params.power"
                        placeholder="请选择治理对象"
                        clearable
                        style="width: 130px"
                    >
                        <el-option
                            v-for="item in powerList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </el-form-item>
            </template>
            <template v-slot:operation>
                <el-button @click="downloadExcel" class="" type="primary" icon="el-icon-Download">导出excel</el-button>
            </template>
        </TableHeader>
        <div style="display: flex">
            <div
                id="luckysheet"
                :style="{ width: `calc(${prop.width} )`, height: `calc(${prop.height} - 57px )` }"
            ></div>
        </div>
    </div>
</template>
<script setup lang="ts">
import { ref, onMounted, provide, reactive, watch, h } from 'vue'
import TableStore from '@/utils/tableStore'
import { exportExcel } from '@/views/govern/reportForms/export.js'
import TableHeader from '@/components/table/header/index.vue'
import { useDictData } from '@/stores/dictData'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getTimeOfTheMonth } from '@/utils/formatTime'
import MyEchart from '@/components/echarts/MyEchart.vue'
import { useConfig } from '@/stores/config'
import Json from './index.json'

const prop = defineProps({
    width: { type: String },
    height: { type: String },
    timeKey: { type: String },
    timeValue: { type: Object }
})
const config = useConfig()
const powerList: any = ref([
    {
        label: '1#变压器',
        value: '1'
    },
    {
        label: '2#变压器',
        value: '2'
    }
])

const tableStore: any = new TableStore({
    url: '/user-boot/role/selectRoleDetail?id=0',
    method: 'POST',

    showPage: false,
    exportName: '主要监测点列表',
    column: [],
    beforeSearchFun: () => {
        tableStore.table.params.searchBeginTime = prop.timeValue?.[0] || getTimeOfTheMonth(prop.timeKey)[0]
        tableStore.table.params.searchEndTime = prop.timeValue?.[1] || getTimeOfTheMonth(prop.timeKey)[1]
    },
    loadCallback: () => {}
})

const tableRef = ref()
provide('tableRef', tableRef)
tableStore.table.params.power = '1'

provide('tableStore', tableStore)
// 下载表格
const downloadExcel = () => {
    exportExcel(luckysheet.getAllSheets(), '治理效果报表')
}
onMounted(() => {
    luckysheet.create({
        container: 'luckysheet',
        title: '', // 表 头名
        lang: 'zh', // 中文
        showtoolbar: false, // 是否显示工具栏
        showinfobar: false, // 是否显示顶部信息栏
        showsheetbar: true, // 是否显示底部sheet按钮
        allowEdit: false, // 禁止所有编辑操作（必填）
        data: Json
    })

    tableStore.index()
})
watch(
    () => prop.timeKey,
    val => {
        tableStore.index()
    }
)
watch(
    () => prop.timeValue, // 监听的目标（函数形式避免直接传递 props 导致的警告）
    (newVal, oldVal) => {
        tableStore.index()
    },
    {
        deep: true // 若 timeValue 是对象/数组，需开启深度监听
    }
)

const addMenu = () => {}
</script>
<style lang="scss" scoped>
:deep(.el-select) {
    min-width: 80px;
}

</style>
