<template>
    <!-- <x-search-bar class="mb-8-2">
        <template #default="{ gutter, colSpan }">
            <a-form :model="searchFormData" layout="inline">
                <a-row :gutter="gutter">
                    <a-col v-bind="colSpan">
                        <a-form-item label="客户姓名" name="customerName">
                            <a-input placeholder="请输入姓名" v-model:value="searchFormData.customerName"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col class="align-left" v-bind="colSpan">
                        <a-space>
                            <a-button @click="handleResetSearch">{{ $t('button.reset') }}</a-button>
                            <a-button ghost type="primary" @click="handleSearch">
                                {{ $t('button.search') }}
                            </a-button>
                        </a-space>
                    </a-col>
                </a-row>
            </a-form>
        </template>
</x-search-bar> -->
    <a-row :gutter="8" :wrap="false">
        <a-col flex="auto">
            <a-card type="flex">
                <a-table :columns="columns" :data-source="listData" bordered="true" :loading="loading"
                    :pagination="paginationState" :scroll="{ x: 800 }" @change="onTableChange">
                    <template #bodyCell="{ column, record }">

                        <template v-if="'status' === column.dataIndex">
                            <a-tag v-if="record.status === 'success'" color="#87d068">已使用</a-tag>
                            <a-tag v-if="record.status === 'checking'" color="#2db7f5">确认中</a-tag>
                            <a-tag v-if="record.status === 'checked'" color="#108ee9">待使用</a-tag>
                        </template>
                        <template v-if="'action' === column.key">
                            <x-action-button @click="handleDelete(record)">
                                <a-tooltip>
                                    <template #title>{{ $t('pages.system.delete') }}</template>
                                    <delete-outlined style="color: #ff4d4f" /> </a-tooltip></x-action-button>
                        </template>
                    </template>
                </a-table>
            </a-card>
        </a-col>
    </a-row>
</template>

<script setup>
import { message, Modal } from 'ant-design-vue'
import { ref } from 'vue'
import apis from '@/apis'
import { formatUtcDateTime } from '@/utils/util'
import { config } from '@/config'
import dayjs from 'dayjs'
import { usePagination } from '@/hooks'
// import { rightEnum} from "@/enums/useEnum"
import { PlusOutlined, EditOutlined, DeleteOutlined } from '@ant-design/icons-vue'
import { useI18n } from 'vue-i18n'
defineOptions({
    name: 'points',
})
const { t } = useI18n() // 解构出t方法
const columns = [
    { title: '客户姓名', dataIndex: 'customerName', align: 'center',width:120 },
    { title: '客户手机', dataIndex: 'customerPhone', align: 'center',width:120 },
    { title: '变动前积分', dataIndex: 'before', align: 'center',width:100 },
    { title: '变动后积分', dataIndex: 'before', align: 'center',width:100 },
    { title: '变动积分', dataIndex: 'change', align: 'center',width:100 },
    { title: '变动原因', dataIndex: 'reason', key: 'introduce',align: 'center' },
    // { title: t('button.action'), key: 'action', width: 60, align: 'center' },
]

const { listData, loading, showLoading, hideLoading, paginationState, resetPagination, searchFormData } =
    usePagination()

const editDialogRef = ref()
getPageList()
/**
 * 获取表格数据
 * @returns {Promise<void>}
 */
async function getPageList() {
    try {
        showLoading()
        const { pageSize, current } = paginationState
        const { success, data, total } = await apis.points
            .getProjectList({
                pageSize,
                current: current,
                ...searchFormData.value,
            })
            .catch(() => {
                throw new Error()
            })
        hideLoading()
        if (config('http.code.success') === success) {
            listData.value = data
            paginationState.total = total
        }
    } catch (error) {
        hideLoading()
    }
}

/**
 * 删除
 */
function handleDelete({ id }) {
    Modal.confirm({
        title: t('pages.system.user.delTip'),
        content: t('button.confirm'),
        okText: t('button.confirm'),
        onOk: () => {
            return new Promise((resolve, reject) => {
                ; (async () => {
                    try {
                        const { success } = await apis.points.delItem(id).catch(() => {
                            throw new Error()
                        })
                        if (config('http.code.success') === success) {
                            resolve()
                            message.success(t('component.message.success.delete'))
                            await getPageList()
                        }
                    } catch (error) {
                        reject()
                    }
                })()
            })
        },
    })
}

/**
 * 分页
 */
function onTableChange({ current, pageSize }) {
    paginationState.current = current
    paginationState.pageSize = pageSize
    getPageList()
}

/**
 * 搜索
 */
function handleSearch() {
    resetPagination()
    getPageList()
}
/**
 * 重置
 */
function handleResetSearch() {
    searchFormData.value = {}
    resetPagination()
    getPageList()
}
/**
 * 编辑完成
 */
async function onOk() {
    await getPageList()
}
</script>

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

