<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info"
                  show-icon/>
        <el-row>
            <el-col :span="24">
                <!-- 表格顶部菜单 -->
                <TableHeader :buttons="['refresh', 'add', 'edit', 'delete', 'comSearch']"
                             quick-search-placeholder="通过标题模糊搜索"
                             @action="baTable.onTableHeaderAction"/>
                <!-- 表格 -->
                <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
                <Table @action="baTable.onTableAction"/>

            </el-col>
        </el-row>


    </div>
</template>

<script setup lang="ts">
import {ref, onMounted, provide, nextTick} from 'vue'
import {advertisement, coupon, document, member, order, recommend} from '/@/api/controllerUrls'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'
import {defaultOptButtons} from '/@/components/table/index'
import {baTableApi} from '/@/api/common'
import baTableClass from '/@/utils/baTable'
import _ from "lodash";
import {useRouter} from "vue-router";
import useCurrentInstance from "/@/utils/useCurrentInstance";
import {auth} from '/@/utils/common'
import {ElMessageBox} from "element-plus";
import request from "/@/api/request";
import {ElNotification} from "element-plus/es";


const router = useRouter()

//详情按钮
let detailBtn: OptButton[] = [
    {
        render: 'tipButton',
        name: 'detail',
        title: '详情',
        text: '详情',
        type: 'primary',
        icon: '',
        class: 'table-row-edit',
        disabledTip: false,
    },
]

//注销按钮
let cancelBtn: OptButton[] = [
    {
        render: 'tipButton',
        name: 'cancel',
        title: '注销',
        text: '注销',
        type: 'danger',
        icon: '',
        class: '',
        disabledTip: false,
    },
]

//根据权限增加按钮
function optButtons() {
    let newBtns = defaultOptButtons([])
    if (auth('cancel') == true) {
        newBtns = _.concat(cancelBtn, newBtns)
    }
    if (auth('detail') == true) {
        newBtns = _.concat(detailBtn, newBtns)
    }
    return newBtns
}


const tableRef = ref()
const baTable = new baTableClass(
    new baTableApi(member),
    {
        dblClickNotEditColumn: ['all'],
        column: [
            {
                label: '头像',
                prop: 'headPortrait',
                align: 'center',
                width: '120',
                render: 'image',
                operator: false
            },
            {label: '用户名', prop: 'nickName', align: 'center', operator: 'LIKE'},
            {label: '手机号', prop: 'mobileNumber', align: 'center', operator: 'LIKE'},
            {label: '最后一次登录', prop: 'lastLoginTime', align: 'center', operator: false},
            {
                label: '状态',
                prop: 'memberStatus',
                align: 'center',
                render: 'tag',
                operator: '=',
                custom: {0: 'success', 1: 'warning', 2: 'info'},
                replaceValue: {0: '正常', 1: '待注销', 2: '已注销'}
            },
            {label: '创建时间', prop: 'createTime', align: 'center', operator: false},
            {
                label: '操作',
                align: 'center',
                width: '160',
                render: 'buttons',
                buttons: optButtons(),
                operator: false
            },
        ],
    },
    {
        defaultItems: {},
    },
    {
        // 获得编辑数据后
        requestEdit: () => {
            if (baTable.form.items && !baTable.form.items.icon) baTable.form.items.icon = 'el-icon-Minus'
        },
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.mount()
    baTable.getIndex()

    const {proxy} = useCurrentInstance()
    /**
     * 表格内的按钮响应
     * @param name 按钮name
     * @param row 被操作行数据
     */
    proxy.eventBus.on('onTableButtonClick', (data: { name: string; row: TableRow }) => {
        if (!baTable.activate) return
        if (data.name == 'detail') {
            infoButtonClick(data.row)
        } else {
            cancelBunClick(data.row)
        }
    })
})


/** 点击详情按钮响应 */
const infoButtonClick = (row: TableRow) => {
    if (!row) return
// 数据来自表格数据,未重新请求api,深克隆,不然可能会影响表格
    let rowClone = _.cloneDeep(row)
    router.push({name: 'members/memberinfo/detail', params: rowClone})
}

/** 点击详情按钮响应 */
const cancelBunClick = (row: TableRow) => {
    if (!row) return
// 数据来自表格数据,未重新请求api,深克隆,不然可能会影响表格
    let rowClone = _.cloneDeep(row)
    ElMessageBox.confirm('是否确认注销该用户?', {type: 'warning'}).then(() => {
        request.edit(member, {id: rowClone.id}, 'cancel/confirm').then(res => {
            ElNotification({
                message: '注销成功',
                type: 'success',
            })
            baTable.onTableHeaderAction('refresh', {loading: true})//调用组件刷新功能
        })
    })


}

</script>


<style scoped lang="scss"></style>
