<!--业务用户管理界面-->
<template>
    <div class='default-main'>
        <TableHeader showCustomColumn>
            <template #select>
                <el-form-item label='筛选数据'>
                    <el-input
                        v-model='tableStore.table.params.searchValue'
                        clearable
                        placeholder='输入用户名、行业'
                         maxlength="32"
                        show-word-limit
                    />
                </el-form-item>
                <el-form-item label='区域'>
                    <area-cascard
                        ref='areaRef'
                        :isMultiple=true
                        v-model='tableStore.table.params.addr'
                    />
                </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'></Table>
        <!--弹框-->
        <user-popup ref='userPopup' />
        <event-upload-popup ref='eventUploadPopup' />
    </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 AreaCascard from '@/components/form/areaCascard/index.vue'
import { onMounted, provide, ref } from 'vue'
import { useRouter } from 'vue-router'
import { deleteSgUser } from '@/api/advance-boot/sgGroven/sgUser'
import EventUploadPopup from '@/views/pqs/voltageSags/sagGovern/businessUser/event/eventUploadPopup.vue'

import UserPopup from './userPopup.vue'

// defineOptions({
//     name: 'businessUser'
// })

const { push } = useRouter()
//区域联级选择
const areaRef = ref()
//用户信息弹出框
const userPopup = ref()
const eventUploadPopup = ref()

const tableStore = new TableStore({
    url: '/advance-boot/sgUser/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' },
        { title: '所属行业', field: 'industry' },
        { title: '所属地区', field: 'addr' },
        { title: '产线数', minWidth: '130', field: 'productCount' },
        { title: '进线数', minWidth: '130', field: 'lineCount' },
        {
            title: '暂降数据',
            align: 'center',
            minWidth: '180',
            render: 'buttons',
            buttons: [
                {
                    name: 'view',
                    title: '查看',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton',
                    click: row => {
                        if (Number(row.productCount) < 1) {
                            ElMessage.error('请创建该用户的生产线')
                        } else {
                            push(`/admin/businessUserRouter/eventView?id=${row.id}`)
                        }
                    }
                },

                {
                    name: 'import',
                    title: '导入',
                    type: 'primary',
                    icon: 'el-icon-Delete',
                    render: 'basicButton',
                    click: row => {
                        eventUploadPopup.value.open('导入暂降数据', row.id)
                    }
                }
            ]
        },

        {
             title: '操作',fixed: 'right',
            align: 'center',
            width: '230',
            render: 'buttons',
            buttons: [
                {
                    name: 'productSetting',
                    title: '生产线管理',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton',
                    click: row => {
                        push(`/admin/businessUserRouter/productLine?id=${row.id}`)
                    }
                },
                {
                    name: 'productSetting',
                    title: '终端管理',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton',
                    click: row => {
                        push(`/admin/businessUserRouter/machine?id=${row.id}`)
                    }
                },
                {
                    name: 'update',
                    title: '编辑',
                    type: 'primary',
                    icon: 'el-icon-EditPen',
                    render: 'basicButton',
                    click: row => {
                        userPopup.value.open('修改用户', row)
                    }
                },
                {
                    name: 'update',
                    title: '删除',
                    type: 'danger',
                    icon: 'el-icon-Delete',
                    render: 'confirmButton',
                    popconfirm: {
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        confirmButtonType: 'danger',
                        title: '确定删除吗？'
                    },
                    click: row => {
                        deleteSgUser(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.orderBy = 'desc'
tableStore.table.params.searchValue = ''
tableStore.table.params.addr = []
provide('tableStore', tableStore)


//新增用户信息
const add = () => {
    userPopup.value.open('新增用户')
}

</script>
