<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="status">
                            <a-select v-model:value="searchFormData.status" allowClear>
                                <a-select-option value="">全部</a-select-option>
                                <a-select-option value="enabled">启用</a-select-option>
                                <a-select-option value="disabled">停用</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 === 'img'">
                            <a-image :width="60" :src="config('http.apiBasic') + record.img" />
                        </template>
                        <template v-if="column.dataIndex === 'startAt'">
                            <span>{{ dayjs(record.startAt).format('YYYY-MM-DD HH:mm:ss') }}</span>
                        </template>
                        <template v-if="column.dataIndex === 'endAt'">
                            <span>{{ dayjs(record.endAt).format('YYYY-MM-DD HH:mm:ss') }}</span>
                        </template>
                        <template v-if="'isDaily' === column.dataIndex">
                            <a-tag v-if="record.isDaily" :color="'green'">是</a-tag>
                            <a-tag v-if="!record.isDaily" :color="'red'">否</a-tag>
                        </template>
                        <template v-if="'action' === column.key">
                            <x-action-button @click="$refs.editDialogRef.handleEdit(record.id)">
                                <a-tooltip>
                                    <template #title> {{ $t('pages.system.user.edit') }}</template>
                                    <edit-outlined /> </a-tooltip></x-action-button>
                            <x-action-button @click="createQrcode(record)">
                                <a-tooltip>
                                    <template #title>二维码</template>
                                    <QrcodeOutlined />
                                </a-tooltip></x-action-button>
                            <x-action-button @click="handleDelete(record.id)">
                                <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="qropen" :title="'生成二维码'" @ok="qropen = false" :footer="null">
        <a-card class="mb-8-2"
            style="display: flex;align-items: center;flex-direction: column;justify-content: center;">
            <x-qrCode :value="qrValue" :icon="qrlogo" :iconBackgroundColor="'#ffffff'" :size="180"></x-qrCode>
        </a-card>
    </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 { statusUserTypeEnum } from '@/enums/system'
import { usePagination } from '@/hooks'
import EditDialog from './components/EditDialog.vue'
import { PlusOutlined, EditOutlined, DeleteOutlined, QrcodeOutlined } from '@ant-design/icons-vue'
import { useI18n } from 'vue-i18n'
import qrlogo from '@/assets/qrlogo.png'
import dayjs from 'dayjs'
const content = ref('')
const qrValue = ref('')
const qropen = ref(false)
defineOptions({
    name: 'houseProduct',
})
const { t } = useI18n() // 解构出t方法
const columns = [
    { title: '礼品图片', dataIndex: 'img' },
    { title: '案场名称', dataIndex: 'companyName' },
    { title: '礼品名称', dataIndex: 'name' },
    { title: '兑换数量', dataIndex: 'maxNum' },
    { title: '开始时间', dataIndex: 'startAt', width: 180, align: 'center' },
    { title: '结束时间', dataIndex: 'endAt', width: 180, align: 'center' },
    { title: '是否每天', dataIndex: 'isDaily', width: 120, align: 'center' },
    // { title: '状态', dataIndex: 'status', key: 'introduce', width: 100, align: 'center' },
    { title: t('button.action'), key: 'action', fixed: 'right', width: 150, 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.houseProduct
            .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()
    }
}
const createQrcode = (params) => {
    const { name, id,img } = params
    const item = {
        title: name,
        typer: 'product',
        pathUrl: '/pages/product/index.vue',
        relationId: id,
        kvalue1: img,
    }
    qrValue.value = JSON.stringify(item)
    qropen.value = true
}
/**
 * 删除
 */
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.houseProduct.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>
