<!--暂降治理历史数据管理界面-->
<template>
    <div class='default-main'>
        <TableHeader showCustomColumn>
            <template v-slot:select>
                <el-form-item label='用户'>
                    <el-select v-model='tableStore.table.params.userId' filterable clearable>
                        <el-option
                            v-for='item in userListData'
                            :key='item.id'
                            :label='item.userName'
                            :value='item.id'
                        />
                    </el-select>
                </el-form-item>
            </template>
            <template v-slot:operation>
                <el-button type='primary' @click='add' class='ml10' :icon='Plus'>新增治理方案</el-button>
            </template>
        </TableHeader>
        <!--表格-->
        <Table ref='tableRef' isGroup></Table>
        <!--弹出表格-->
        <machine-detail-popup ref='machineDetailTable' />
    </div>
</template>

<script setup lang='ts'>
import { Plus } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import TableStore from '@/utils/tableStore'
import Table from '@/components/table/index.vue'
import TableHeader from '@/components/table/header/index.vue'
import { onMounted, provide, ref } from 'vue'
import { useRouter } from 'vue-router'
import { sgUserList } from '@/api/advance-boot/sgGroven/sgUser'
import { deleteSgScheme } from '@/api/advance-boot/sgGroven/sgScheme'

import machineDetailPopup from '@/views/pqs/voltageSags/sagGovern/scheme/machineDetailPopup.vue'

defineOptions({
    name: 'governSchemeHistory'
})

const machineDetailTable = ref()
const machineDetailData = ref()
const { push } = useRouter()
const userListData: any = ref([])
// const showPower = (id: string) => {
//     alert(id)
// }

window.showPower = function(id: string) {
    //根据生产线查询该生产线下的所有终端数据
    machineDetailTable.value.open('终端容量信息', id)
}


const tableStore = new TableStore({
    url: '/advance-boot/sagGovernScheme/list',
    method: 'POST',
    column: [
    { title: '序号',  width: 80,formatter: (row: any) => {
        return (tableStore.table.params.pageNum - 1) * tableStore.table.params.pageSize + row.rowIndex + 1
      } },
        { title: '用户', field: 'userName', minWidth: 160 },
        { title: '生产线名称', field: 'productLineName', minWidth: 160 },
        { title: '期望回报(年)', field: 'payBackPeriod', minWidth: 150 },
        { title: '数据时间范围', field: 'timeScope', minWidth: 170 },
        {
            title: '治理容量(kW)', field: 'governPower', minWidth: 150, type: 'html',
            formatter: (obj: any) => {
                const val = obj.row.governPower
                const id = obj.row.productLineId
                return `<a href='javascript:void(0);'  style='color: #409EFF;text-decoration: none' onclick='window.showPower("${id}")'>${val}</a>`
            }
        },
        {
            title: '最佳方案',
            children: [
                {
                    title: '方案名称', minWidth: '110', field: 'schemeType',
                    formatter: (obj: any) => {
                        let temp
                        switch (obj.row.schemeType) {
                            case 1:
                                temp = 'AVC'
                                break
                            case 2:
                                temp = 'AVC-RTS'
                                break
                            case 3:
                                temp = '快切'
                                break
                            default:
                                temp = 'UPS'
                        }
                        return temp
                    }
                },
                {
                    title: '初期投资(万元)', minWidth: '150', field: 'initialInvest',
                    formatter: (obj: any) => {
                        return (Number(obj.row.schemeEquipment) + Number(obj.row.schemeMaintain)).toFixed(2)
                    }
                },
                { title: '投资回收期(年)', minWidth: '150', field: 'payBackYear' },
                { title: '治理效果(万元/年)', minWidth: '150', field: 'governEffect' }
            ]
        },
        {
             title: '操作',fixed: 'right',
            align: 'center',
            minWidth: '150',
            render: 'buttons',
          
            buttons: [
                {
                    name: 'productSetting',
                    title: '详情',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton',
                    click: row => {
                        push(`/admin/sagGovernScheme/schemeHistory?id=${row.id}`)
                    }
                },
                {
                    name: 'update',
                    title: '删除',
                    type: 'danger',
                    icon: 'el-icon-Delete',
                    render: 'confirmButton',
                    popconfirm: {
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        confirmButtonType: 'danger',
                        title: '确定删除吗？'
                    },
                    click: row => {
                        deleteSgScheme(row.id).then(res => {
                            ElMessage.success('删除成功')
                            tableStore.index()
                        })
                    }
                }
            ]
        }
    ],
    beforeSearchFun: () => {
        if (tableStore.table.params.addr) {
            tableStore.table.params.addrStrOption = tableStore.table.params.addr.map(tempArray => tempArray.join('/'))
        }
        // for (let key in tableStore.table.params) {
        //     if (tableStore.table.params[key] === '') {
        //         delete tableStore.table.params[key]
        //     }
        // }

    }
})


onMounted(() => {
    // 加载数据
    tableStore.index()
})
tableStore.table.params.userId = ''
provide('tableStore', tableStore)

//初始化用户下拉选择框
const initUserSelect = async () => {
    await sgUserList().then(res => {
        if (res.data && res.data.length > 0) {
            userListData.value = res.data
        }
    })
}

initUserSelect()


//新增用户信息
const add = () => {
    let userId = undefined
    if (tableStore.table.params.userId) {
        userId = tableStore.table.params.userId
    }
    push(`/admin/sagGovernScheme/schemeCalc?userId=${userId}`)
}

</script>
