<template>
    <div>
        <div>
            <TableHeader ref="TableHeaderRef" area showExport>
                <template #select>
                    <!-- <el-form-item label="所在地市">
                        <el-select v-model="tableStore.table.params.deptId" clearable placeholder="请选择所在地市">
                            <el-option
                                v-for="item in areaOptionList"
                                :key="item.id"
                                :label="item.name"
                                :value="item.name"
                            ></el-option>
                        </el-select>
                    </el-form-item> -->
                    <el-form-item label="用户名称">
                        <el-input
                            placeholder="请输入用户名称"
                            v-model="tableStore.table.params.assessName"
                            clearable
                            show-word-limit
                            maxlength="32"
                        ></el-input>
                    </el-form-item>
                    <el-form-item label="接入变电站">
                        <el-input
                            placeholder="请输入接入变电站"
                            v-model="tableStore.table.params.powerstationName"
                            clearable
                            show-word-limit
                            maxlength="32"
                        ></el-input>
                    </el-form-item>
                    <el-form-item label="电站电压等级">
                        <el-select
                            v-model="tableStore.table.params.powerstationScaleList"
                            multiple
                            collapse-tags
                            collapse-tags-tooltip
                            filterable
                            clearable
                            style="width: 240px"
                            placeholder="请选择电站电压等级"
                        >
                            <el-option
                                v-for="item in voltageleveloption"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="母线电压等级">
                        <el-select
                            v-model="tableStore.table.params.busScaleList"
                            multiple
                            collapse-tags
                            collapse-tags-tooltip
                            filterable
                            clearable
                            style="width: 240px"
                            placeholder="请选择母线电压等级"
                        >
                            <el-option
                                v-for="item in voltageleveloption"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="用户负荷类型">
                        <el-select
                            v-model="tableStore.table.params.isWindfarm"
                            filterable
                            clearable
                            style="width: 240px"
                            placeholder="请选择用户负荷类型"
                        >
                            <el-option label="风电场" value="01"></el-option>
                            <el-option label="光伏" value="02"></el-option>
                            <el-option label="其他" value="03"></el-option>
                        </el-select>
                    </el-form-item>
                </template>
                <template #operation>
                    <el-button icon="el-icon-Plus" type="primary" @click="addForm">新增</el-button>
                    <el-button type="primary" icon="el-icon-Delete" @click="del">删除</el-button>
                </template>
            </TableHeader>
            <Table ref="tableRef" />
        </div>
    </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 { useDictData } from '@/stores/dictData'
import { ElMessage, ElMessageBox, ElDatePicker } from 'element-plus'
import { cableList, batchDel } from '@/api/advance-boot/assess'
import { useRouter } from 'vue-router'
import Form from './form.vue'
import {  bindAssessmentId } from '@/api/device-boot/Business'
import { forEach } from 'min-dash'
defineOptions({
    name: 'runManage/addUser'
})
const { push } = useRouter()
const dictData = useDictData()
const areaOptionList = dictData.getBasicData('jibei_area')
const linetypeList = ref([])
//字典获取电压等级
const voltageleveloption = dictData.getBasicData('Dev_Voltage_Stand')
const tableStore: any = new TableStore({
    url: '/advance-boot/assessUser/page',
    method: 'POST',
    publicHeight: 65,
    column: [
        {
            width: '60',
            type: 'checkbox'
        },
        {
            title: '序号',
            width: 80,
            field: 'name',
            formatter: (row: any) => {
                return (tableStore.table.params.pageNum - 1) * tableStore.table.params.pageSize + row.rowIndex + 1
            }
        },
        { field: 'deptName', title: '所属地市', minWidth: 100 },
        { field: 'assessName', title: '用户名称', minWidth: 180 },
        { field: 'powerstationName', title: '接入变电站', minWidth: 180 },
        {
            field: 'powerstationScale',
            title: '电站电压等级',
            minWidth: 180,
            formatter: (row: any) => {
                return voltageleveloption.find((item: any) => item.id === row.cellValue)?.name
            }
        },
        { field: 'busName', title: '接入母线', minWidth: 180 },
        {
            field: 'busScale',
            title: '母线电压等级',
            minWidth: 180,
            formatter: (row: any) => {
                return voltageleveloption.find((item: any) => item.id === row.cellValue)?.name
            }
        },
        { field: 'powersupplyCapacity', title: '供电设备容量(MVA)', minWidth: 180 },
        { field: 'minshortcircuitCapacity', title: '最小短路容量(MVA)', minWidth: 180 },
        { field: 'agreedCapacity', title: '用户协议容量(MVA)', minWidth: 180 },
        { field: 'lineName', title: 'PCC点名称', minWidth: 180 },
        {
            field: 'linetypeId',
            title: '导线类型',
            minWidth: 180,
            formatter: (row: any) => {
                return linetypeList.value.find((item: any) => item.linetypeId === row.cellValue)?.linetypeName
            }
        },
        { field: 'lineLen', title: '长度(km)', minWidth: 180 },
        { field: 'lineNum', title: '回路数', minWidth: 180 },
        {
            field: 'isWindfarm',
            title: '用户负荷类型',
            minWidth: 180,
            formatter: (row: any) => {
                return row.cellValue == '01' ? '风电场' : row.cellValue == '02' ? '光伏' : '其他'
            }
        },
        { field: 'windfarmCapacity', title: '风电场装机容量(MW)', minWidth: 180 },
        {
            field: 'singleLoad',
            title: '是否单相负荷',
            minWidth: 180,
            formatter: (row: any) => {
                return row.cellValue == '00' ? '是' : '否'
            }
        },
        // { field: 'name13', title: '单相负荷', minWidth: 180 },

        {
             title: '操作',fixed: 'right',
            width: '220',
            render: 'buttons',
           
            buttons: [
                {
                    name: 'edit',
                    title: '编辑',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton',
                    click: async row => {
                        push({
                            path: 'addUser',
                            query: {
                                id: row.assessId
                            }
                        })
                    }
                },
                {
                    name: 'edit',
                    title: '查看',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton',
                    click: async row => {
                        push({
                            path: 'addUser',
                            query: {
                                id: row.assessId,
                                mode: 'view'
                            }
                        })
                    }
                }
            ]
        }
    ],
    beforeSearchFun: () => {
        tableStore.table.params.deptId = tableStore.table.params.deptIndex
    },

    loadCallback: () => {}
})
tableStore.table.params.assessName = ''
tableStore.table.params.deptId = ''
tableStore.table.params.powerstationName = ''
tableStore.table.params.busScaleList = []
tableStore.table.params.isWindfarm = ''
tableStore.table.params.powerstationScaleList = []

provide('tableStore', tableStore)
const route = useRoute()

// 监听路由变化
watch(
    () => route.query,
    newQuery => {
        tableStore.index()
    }
    // { immediate: true }
)

// 新增评估用户页面
const addForm = () => {
    push({
        path: 'addUser'
    })
}
const Info = () => {
    cableList({}).then(res => {
        linetypeList.value = res.data
    })
}
//删除
const del = () => {
    if (tableStore.table.selection.length == 0) {
        ElMessage({
            type: 'warning',
            message: '请选择要删除的数据'
        })
    } else {
        ElMessageBox.confirm('此操作将永久删除, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            let ids = tableStore.table.selection.map(item => item.assessId)
            batchDel(ids).then(res => {
                ElMessage({
                    type: 'success',
                    message: '删除成功!'
                })
                ids.forEach(item => {
                    bindAssessmentId({
                        userReportId: item,
                        assessMentId: null
                    })
                })

                tableStore.index()
            })
        })
    }
}
onMounted(() => {
    tableStore.index()
    Info()
})
</script>
