<template>
    <div class="default-main" style="position: relative">
        <TableHeader :showReset="false" showExport showCustomColumn>
            <template #select>
                <el-form-item label="关键字筛选">
                    <el-input maxlength="32" show-word-limit style="width: 240px"
                        v-model.trim="tableStore.table.params.searchValue" clearable placeholder="请输入用户名称" />
                </el-form-item>
            </template>
            <template #operation>
                <el-button :icon="Plus" type="primary" @click="addMenu" class="ml10">新增</el-button>
            </template>
        </TableHeader>
        <Table ref="tableRef" />
        <detail ref="detail"></detail>
        <Bind ref="BindRef" :bingRow="bingRow" @close="bingRow = null" v-if="bingRow"></Bind>
    </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 Detail from './detail.vue'
import { ElMessage } from 'element-plus'
import { deleteUser } from '@/api/cs-device-boot/sensitiveLoadMange'
import { Plus } from '@element-plus/icons-vue'
import { useDictData } from '@/stores/dictData'
import Bind from './component/bind.vue'

defineOptions({
    name: 'govern/sensitiveLoadMange/index'
})
const detail = ref()
const BindRef = ref()
const bingRow = ref<anyObj | null>(null)
const dictData = useDictData()
const interferenceType = dictData.getBasicData('Interference_Source')

const tableStore: any = new TableStore({
    url: '/cs-harmonic-boot/pqSensitiveUser/getList',
    method: 'POST',
    column: [
        {
            title: '序号',
            width: 80,
            formatter: (row: any) => {
                return (tableStore.table.params.pageNum - 1) * tableStore.table.params.pageSize + row.rowIndex + 1
            }
        },
        { title: '用户名称', field: 'name', minWidth: 180 },
        {
            title: '用户类型',
            field: 'loadType',
            minWidth: 120,
            formatter: row => {
                return interferenceType.filter(item => item.id == row.cellValue)[0]?.name
            }
        },
        { title: '所属厂站名称', field: 'substationName', minWidth: 180 },

        { title: '用户协议容量(MVA)', field: 'userAgreementCapacity', minWidth: 100 },
        { title: '装机容量(MW)', field: 'installedCapacity', minWidth: 100 },
        {
            title: '操作', fixed: 'right',
            align: 'center',
            width: '180',

            render: 'buttons',
            buttons: [
                {
                    name: 'edit',
                    title: '绑定测点',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton',
                    click: row => {

                        bingRow.value = row
                    }
                },
                {
                    name: 'edit',
                    title: '编辑',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton',
                    click: row => {
                        detail.value.open('编辑', row)
                    }
                },
                {
                    name: 'del',
                    title: '删除',
                    type: 'danger',
                    icon: 'el-icon-Delete',
                    render: 'confirmButton',
                    popconfirm: {
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        confirmButtonType: 'danger',
                        title: '确定删除吗？'
                    },
                    click: row => {
                        deleteUser([row.id]).then(res => {
                            ElMessage.success('删除成功')
                            tableStore.index()
                        })
                    }
                }
            ]
        }
    ],
    loadCallback: () => { }
})

tableStore.table.params.searchValue = ''
provide('tableStore', tableStore)
onMounted(() => {
    tableStore.index()
})

const addMenu = () => {
    detail.value.open('新增')
}
</script>
