<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="name">
                            <a-input placeholder="请输入权益名称" v-model:value="searchFormData.name"></a-input>
                        </a-form-item>
                    </a-col>

                    <a-col v-bind="colSpan">
                        <a-form-item label="所属区域" name="areaId">
                            <a-select v-model:value="searchFormData.areaId" allowClear>
                                <a-select-option :value="1">南通</a-select-option>
                                <a-select-option :value="2">盐城</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col class="align-right" 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">
                <x-action-bar class="mb-8-2">
                    <a-button type="primary" @click="$refs.editDialogRef.handleCreate()">
                        <template #icon>
                            <plus-outlined></plus-outlined>
                        </template>
                        新增权益
                    </a-button>
                </x-action-bar>
                <a-table :columns="columns" :data-source="listData" bordered="true" :loading="loading"
                    :pagination="paginationState" :scroll="{ x: 1000 }" @change="onTableChange">
                    <template #bodyCell="{ column, record }">
                        <template v-if="column.dataIndex === 'cover'">
                            <a-image v-if="record.cover" :src="config('http.apiBasic') + record.cover" />
                        </template>
                        <template v-if="column.dataIndex === 'expireAt'">
                            <span>{{ record.expireAt && dayjs(record.expireAt).format('YYYY-MM-DD HH:mm') }}</span>
                        </template>
                        <template v-if="column.dataIndex === 'details'">
                            <span style="cursor: pointer;color:#1677ff;"
                                @click="content = decryptString(record.details) || ''; type = 2; open = true" type="link">点击查看</span>
                        </template>

                        <template v-if="'status' === column.dataIndex">
                            <a-tag v-if="record.status === 'enabled'" :color="'green'">启用</a-tag>
                            <a-tag v-if="record.status === 'disabled'" :color="'red'">停用</a-tag>
                        </template>
                        <!-- <template v-if="'isHot' === column.dataIndex">
                            <a-tag v-if="record.isHot === true" :color="'red'">热门</a-tag>
                            <a-tag v-if="record.isHot === false" :color="'#999999'">非热门</a-tag>
                        </template> -->
                        <template v-if="column.dataIndex === 'areaId'">
                            <span>{{ areaEnum.getName(record.areaId) }}</span>
                        </template>
                        <template v-if="'type' === column.dataIndex">
                            <span>{{ typerEnum.getName(record.type) }}</span>
                        </template>
                        <template v-if="'action' === column.key">
                            <x-action-button @click="$refs.editDialogRef.handleEdit(record)">
                                <a-tooltip>
                                    <template #title> 编辑</template>
                                    <edit-outlined /> </a-tooltip></x-action-button>

                            <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>
    <a-modal v-model:open="open" :title="type === 1 ? '权益图片' : '权益详情'" @ok="open = false">
        <template v-if="type === 1">
            <a-image v-if="imgList.length > 0" :width="200" v-for="item of imgList"
                :src="config('http.apiBasic') + item" />
            <span v-else>
                暂无图片
            </span>
        </template>
        <template v-else>
            <a-card>
                <span v-html="content"></span>
            </a-card>
        </template>

    </a-modal>

    <edit-dialog ref="editDialogRef" @ok="onOk"></edit-dialog>
</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 { typerEnum, areaEnum } from "@/enums/useEnum"
import EditDialog from './components/EditDialog.vue'
import { PlusOutlined, EditOutlined, DeleteOutlined, QrcodeOutlined } from '@ant-design/icons-vue'
import { useI18n } from 'vue-i18n'
import { decryptString } from "@/utils/util"
defineOptions({
    name: 'equiteType',
})
const { t } = useI18n() // 解构出t方法
const open = ref(false)
const imgList = ref([])
const type = ref(1)

const columns = [
    { title: '封面图', dataIndex: 'cover', width: 80 },
    { title: '权益名称', dataIndex: 'name', width: 200 },
    { title: '订单联系人', dataIndex: 'concatName', width: 200 },
    { title: '订单电话', dataIndex: 'concatPhone', width: 200 },
    { title: '所需积分', dataIndex: 'price', width: 100, align: 'center' },
    { title: '已兑', dataIndex: 'stock', width: 100, align: 'center' },
    { title: '排序', dataIndex: 'sequence', width: 100, align: 'center' },
    { title: '兑换数量上限', dataIndex: 'maxNum', width: 120 },
    { title: '单人兑换次数上限', dataIndex: 'canNum', width: 120 },
    { title: '详情', dataIndex: 'details', align: 'center', width: 100 },
    // { title: '是否热门', dataIndex: 'isHot', align: 'center', width: 100 },
    { title: '所属区域', dataIndex: 'areaId', width: 90, align: 'center' },
    { title: '权益到期时间', dataIndex: 'expireAt', width: 120, align: 'center' },
    { title: '礼品有效期/天', dataIndex: 'expire', width: 90, align: 'center' },
    { title: '产品类别', dataIndex: 'type', width: 90, align: 'center' },
    { title: '状态', dataIndex: 'status', key: 'introduce', width: 60, align: 'center' },
    { title: t('button.action'), key: 'action', fixed: 'right', width: 120, 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.equiteMgt
            .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.equiteMgt.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>
