<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 />

        <!-- 表格顶部菜单 -->
        <TableHeader
            :buttons="['refresh', 'add', 'edit', 'delete', 'comSearch', 'quickSearch', 'columnDisplay']"
            :quick-search-placeholder="t('quick Search Placeholder', { fields: t('test.quick Search Fields') })"
        />

        <!-- 表格 -->
        <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
        <Table ref="tableRef" />

        <!-- 表单 -->
        <PopupForm />
    </div>
</template>

<script setup lang="ts">
import { ref, provide, onMounted } from 'vue'
import baTableClass from '/@/utils/baTable'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'

const { t } = useI18n()
const tableRef = ref()
const optButtons = defaultOptButtons(['weigh-sort', 'edit', 'delete'])
const baTable = new baTableClass(
    new baTableApi('/admin/test/'),
    {
        pk: 'id',
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: t('test.id'), prop: 'id', align: 'center', width: 70, operator: 'RANGE', sortable: 'custom' },
            { label: t('test.weigh'), prop: 'weigh', align: 'center', operator: 'RANGE', sortable: 'custom' },
            { label: t('test.status'), prop: 'status', align: 'center', render: 'switch', operator: '=', sortable: false, replaceValue: { 0: t('test.status 0'), 1: t('test.status 1') } },
            { label: t('test.update_time'), prop: 'update_time', align: 'center', render: 'datetime', operator: 'RANGE', sortable: 'custom', width: 160, timeFormat: 'yyyy-mm-dd hh:MM:ss' },
            { label: t('test.create_time'), prop: 'create_time', align: 'center', render: 'datetime', operator: 'RANGE', sortable: 'custom', width: 160, timeFormat: 'yyyy-mm-dd hh:MM:ss' },
            { label: t('test.image'), prop: 'image', align: 'center', render: 'image', operator: false },
            { label: t('test.images'), prop: 'images', align: 'center', render: 'images', operator: false },
            { label: t('test.radio'), prop: 'radio', align: 'center', render: 'tag', operator: '=', sortable: false, replaceValue: { opt0: t('test.radio opt0'), opt1: t('test.radio opt1') } },
            { label: t('test.checkbox'), prop: 'checkbox', align: 'center', render: 'tags', operator: 'FIND_IN_SET', sortable: false, replaceValue: { opt0: t('test.checkbox opt0'), opt1: t('test.checkbox opt1') } },
            { label: t('test.select'), prop: 'select', align: 'center', render: 'tag', operator: '=', sortable: false, replaceValue: { opt0: t('test.select opt0'), opt1: t('test.select opt1') } },
            { label: t('test.selects'), prop: 'selects', align: 'center', render: 'tags', operator: 'FIND_IN_SET', sortable: false, replaceValue: { opt0: t('test.selects opt0'), opt1: t('test.selects opt1') } },
            { label: t('test.switch'), prop: 'switch', align: 'center', render: 'switch', operator: '=', sortable: false, replaceValue: { 0: t('test.switch 0'), 1: t('test.switch 1') } },
            { label: t('test.float'), prop: 'float', align: 'center', operator: 'RANGE', sortable: false },
            { label: t('test.number'), prop: 'number', align: 'center', operator: 'RANGE', sortable: false },
            { label: t('test.color'), prop: 'color', align: 'center', render: 'color', operator: false },
            { label: t('test.icon'), prop: 'icon', align: 'center', render: 'icon', operator: false },
            { label: t('test.city'), prop: 'city_text', align: 'center', operator: false },
            { label: t('test.timestamp'), prop: 'timestamp', align: 'center', render: 'datetime', operator: 'RANGE', sortable: 'custom', width: 160, timeFormat: 'yyyy-mm-dd hh:MM:ss' },
            { label: t('test.year'), prop: 'year', align: 'center', operator: 'RANGE', sortable: 'custom' },
            { label: t('test.datetime'), prop: 'datetime', align: 'center', operator: '=', sortable: 'custom', width: 160 },
            { label: t('test.time'), prop: 'time', align: 'center', operator: '=', sortable: 'custom' },
            { label: t('test.date'), prop: 'date', align: 'center', operator: '=', sortable: 'custom' },
            { label: t('operate'), align: 'center', width: 140, render: 'buttons', buttons: optButtons, operator: false },
        ],
        dblClickNotEditColumn: [undefined, 'status', 'switch'],
        defaultOrder: { prop: 'weigh', order: 'desc' },
    },
    {
        defaultItems: { weigh: 0, status: '1', radio: 'opt0', checkbox: ['opt0', 'opt1'], select: 'opt0', switch: '1', float: 0, number: 0, array: [], timestamp: null, year: null, datetime: null, time: null, date: null, editor: null },
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})
</script>

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

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