<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', 'delete', 'comSearch']" quick-search-placeholder="通过日志标题模糊搜索"
          @action="baTable.onTableHeaderAction" />
        <!-- 表格 -->
        <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
        <Table @action="baTable.onTableAction" />

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

    <Info />
  </div>
</template>

<script setup lang="ts">
import _ from 'lodash'
import { onMounted, provide } from 'vue'
import baTableClass from '/@/utils/baTable'
import { sysOperationLog } from '/@/api/controllerUrls'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import useCurrentInstance from '/@/utils/useCurrentInstance'

import Info from './info.vue'
import { buildJsonToElTreeData } from '/@/utils/common'

let optButtons: OptButton[] = [
    {
        render: 'tipButton',
        name: 'info',
        title: 'info',
        text: '',
        type: 'primary',
        icon: 'fa fa-search-plus',
        class: 'table-row-edit',
        disabledTip: false,
    },
]

optButtons = _.concat(optButtons, defaultOptButtons(['delete']))

const baTable = new baTableClass(
    new baTableApi(sysOperationLog),
    {
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: 'ID', prop: 'id', align: 'center', operator: 'LIKE', operatorPlaceholder: '模糊查询', width: 70 },
            {
                label: '管理员ID',
                prop: 'accountId',
                align: 'center',
                operator: 'LIKE',
                operatorPlaceholder: '模糊查询',
                width: 180,
            },
            {
                label: '管理员',
                prop: 'accountName',
                align: 'center',
                operator: 'LIKE',
                operatorPlaceholder: '模糊查询',
                width: 160,
            },
            { label: '标题', prop: 'title', align: 'center', operator: 'LIKE', operatorPlaceholder: '模糊查询' },
            {
                show: false,
                label: '请求数据',
                prop: 'data',
                align: 'center',
                operator: 'LIKE',
                operatorPlaceholder: '模糊查询',
                'show-overflow-tooltip': true,
            },
            {
                label: 'URL',
                prop: 'url',
                align: 'center',
                operator: 'LIKE',
                operatorPlaceholder: '模糊查询',
                'show-overflow-tooltip': true,
                render: 'url',
            },
            { label: 'IP', prop: 'ip', align: 'center', operator: 'LIKE', operatorPlaceholder: '模糊查询', render: 'tag' },
            {
                label: 'useragent',
                prop: 'useragent',
                align: 'center',
                operator: 'LIKE',
                operatorPlaceholder: '模糊查询',
                'show-overflow-tooltip': true,
            },
            {
                label: '创建时间',
                prop: 'createtime',
                align: 'center',
                render: 'datetime',
                sortable: 'custom',
                operator: 'RANGE',
                width: 170,
            },
            {
                label: '操作',
                align: 'center',
                width: '100',
                render: 'buttons',
                buttons: optButtons,
                operator: false,
            },
        ],
        dblClickNotEditColumn: [undefined],
    },
    {},
    {
        onTableDblclick: ({ row }: { row: TableRow }) => {
            infoButtonClick(row)
            return false
        },
    }
)

baTable.mount()
baTable.getIndex()

provide('baTable', baTable)

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

    rowClone.data = rowClone.data ? [{ label: '点击展开', children: buildJsonToElTreeData(JSON.parse(rowClone.data)) }] : []
    baTable.form.extend!['info'] = rowClone
    baTable.form.operate = 'info'
}

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 == 'info') {
            infoButtonClick(data.row)
        }
    })
})
</script>

<script lang="ts">
import { defineComponent } from 'vue'
export default defineComponent({
    name: 'auth/adminLog',
})
</script>

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