<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 {order, orderRefund} 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'

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(orderRefund),
    {
        dblClickNotEditColumn: [undefined, 'status'],
        column: [
            {label: '会员姓名', prop: 'memberName', align: 'center', width: 120, operator: false},
            {label: '订单编号', prop: 'orderNumber' ,"min-width":'180px', align: 'left', operator: 'LIKE'},
            {label: '退款原因', prop: 'refundReason',"min-width":'180px', align: 'left', operator: false},
            {label: '申请退款金额', prop: 'requestAmount', align: 'center', width: 120, operator: false},
            {
                label: '退款类型',
                prop: 'refundType',
                align: 'center',
                render: 'tag',
                operator: '=',
                width: 120,
                custom: {0: '', 1: '', 2: '', 3: 'success', 4: 'warning', 5: 'info', 6: 'info'},
                replaceValue: {0: '全额退款', 1: '部分退款',},
            },
            {
                label: '退款状态',
                prop: 'refundStatus',
                align: 'center',
                render: 'tag',
                operator: '=',
                width: 120,
                custom: {0: 'warning', 1: 'info'},
                replaceValue: {0: '待退款', 1: '已退款',},
            },
            {label: '退款金额', prop: 'auditAmount', align: 'center', width: 120, operator: false},
            {
                label: '审核状态',
                prop: 'auditStatus',
                align: 'center',
                render: 'tag',
                operator: '=',
                width: 120,
                custom: {0: 'warning', 1: 'success', 2: 'info'},
                replaceValue: {0: '待审核', 1: '审核通过', 2: '审核驳回',},
            },
            {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>
