<template>
    <div class="default-main ">
        <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>


        <auditDialog ref="dialogRef" @close="baTable.onTableHeaderAction('refresh', {loading: true})"></auditDialog>

    </div>
</template>

<script setup lang="ts">
import _ from 'lodash'
import {ref, onMounted, provide} from 'vue'
import {moments} from '/@/api/controllerUrls'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'
import {useRouter} from 'vue-router'
import {defaultOptButtons} from '/@/components/table'
import {baTableApi} from '/@/api/common'
import baTableClass from '/@/utils/baTable'
import useCurrentInstance from '/@/utils/useCurrentInstance'
import {auth} from "/@/utils/common";
import auditDialog from './auditDialog.vue'
import defaultAvatar from '/@/assets/defaultAvatar.svg'

const router = useRouter()

let auditBtn: OptButton[] = [
    {
        render: 'tipButton',
        name: 'audit',
        title: '审核',
        text: '审核',
        type: 'warning',
        icon: '',
        class: 'table-row-edit',
        disabledTip: false,
    },
]

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


const tableRef = ref()
const baTable = new baTableClass(
    new baTableApi(moments),
    {
        dblClickNotEditColumn: [undefined, 'status'],
        column: [
            {
                label: '头像',
                prop: 'headPortrait',
                align: 'center',
                width: '100',
                render: 'image',
                operator: false
            },
            {label: '发布人昵称', prop: 'nickName', align: 'center', width: 120, operator: 'LIKE'},
            {label: '圈子内容', prop: 'momentsContent', align: 'center', operator: false},
            {
                label: '发布状态',
                prop: 'releaseStatus',
                align: 'center',
                render: 'tag',
                operator: '=',
                width: 120,
                custom: {0: 'warning', 1: 'success', 2: 'info'},
                replaceValue: {0: '待发布', 1: '已发布', 2: '审核失败'},
            },
            {
                label: '举报',
                prop: 'momentsStatus',
                align: 'center',
                render: 'tag',
                operator: '=',
                width: 120,
                custom: {0: 'success', 1: 'warning', 2: 'info'},
                replaceValue: {0: '正常', 1: '被举报', 2: '已下架'},
            },
            {label: '点赞数量', prop: 'favorCount', align: 'center', width: '100', operator: false},
            {label: '被举报数量', prop: 'reportCountTotal', align: 'center', width: '100', operator: false},
            {label: '审核失败原因', prop: 'releaseReason', align: 'center', operator: false},
            {label: '创建时间', prop: 'createTime', align: 'center', width: '170', operator: false},
            {
                label: '操作',
                align: 'center',
                width: 120,
                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)

baTable.mount()
baTable.getIndex()


onMounted(() => {

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

let dialogRef = ref()

/** 点击查看详情按钮响应 */
const buttonClick = (row: TableRow) => {
    if (!row) return
// 数据来自表格数据,未重新请求api,深克隆,不然可能会影响表格
    let rowClone = _.cloneDeep(row)
    dialogRef.value.open(rowClone)
}

</script>


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