<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 === 'cover'">
                            <a-image :width="60"
                                :src="record.cover ? config('http.apiBasic') + record.cover : errImg" />
                        </template>
                        <template v-if="column.dataIndex === 'content'">
                            <span style="cursor: pointer;color:#1677ff;"
                                @click="content = record.content || ''; type = 2; open = true" type="link">点击查看</span>
                        </template>
                        <template v-if="'status' === column.dataIndex">
                            <a-tag :color="activatyStatus.getColor(record.status)">{{
                                activatyStatus.getName(record.status)
                                }}</a-tag>
                        </template>
                        <template v-if="'action' === column.key">
                            <x-action-button @click="auditHandleEdit(record)" v-if="record.status === 1">
                                <a-tooltip>
                                    <template #title> {{ '审核' }}</template>
                                    <i class='iconfont icon-shenhe' style='font-size:14px;color:#faad14'></i>
                                </a-tooltip>
                            </x-action-button>
                             <x-action-button @click="startHandleEdit(record)" v-if="record.status === 2">
                                <a-tooltip>
                                    <template #title> {{ '开始活动' }}</template>
                                    <i class='iconfont icon-kaishi-yuan' style='font-size:14px;color:#52c41a'></i>
                                </a-tooltip>
                            </x-action-button>
                            <x-action-button @click="$refs.editDialogRef.handleEdit(record)">
                                <a-tooltip>
                                    <template #title> {{ $t('pages.system.user.edit') }}</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">
        <a-card class="mb-8-2">
            <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>
                <p v-html="content"></p>
            </template>
        </a-card>
    </a-modal>
    <a-modal :open="modal.open" :title="'审核'" :width="640" :confirm-loading="modal.confirmLoading" title="审核"
        ok-text="确认" cancel-text="取消" @ok="handleAuditEdit" @cancel="hideModal()">
        <a-card class="mb-8-2">
            <a-form-item :label="'审核'" name="">
                <a-radio-group v-model:value="auditStatus"
                    :options="[{ value: 2, label: '通过' }, { value: 99, label: '不通过' }]"></a-radio-group>
            </a-form-item>
            <a-form-item :label="'备注'" name="">
                <a-textarea v-model:value="remark"></a-textarea>
            </a-form-item>
        </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 dayjs from 'dayjs'
import { usePagination,useModal } from '@/hooks'
import EditDialog from './components/EditDialog.vue'
import { PlusOutlined, EditOutlined, DeleteOutlined, QrcodeOutlined } from '@ant-design/icons-vue'
import { useI18n } from 'vue-i18n'
import { activatyStatus } from '@/enums/index.js'
import errImg from '@/assets/blankSpace.png'
defineOptions({
    name: 'activity',
})
const { t } = useI18n() // 解构出t方法
const open = ref(false)
const imgList = ref([])
const type = ref(1)

const columns = [
    { title: '活动封面', dataIndex: 'cover', align: 'center', width: 100 },
    { title: '活动标题', dataIndex: 'title' },
    { title: '活动时间', dataIndex: 'openAt', align: 'center' },
    { title: '活动内容', dataIndex: 'content', align: 'center', width: 100 },
    { title: '备注', dataIndex: 'remark'},
    { title: '状态', dataIndex: 'status', align: 'center', width: 100 },
    { title: t('button.action'), key: 'action', fixed: 'right', width: 140, align: 'center' },
]
const { modal, showModal, hideModal, showLoading:auditShowLoading, hideLoading:auditHideLoading } = useModal()
const { listData, loading, showLoading, hideLoading, paginationState, resetPagination, searchFormData } =usePagination()

const editDialogRef = ref()
const currentForm=ref({})
const auditStatus=ref(2)
const remark=ref('')
getPageList()
/**
 * 获取表格数据
 * @returns {Promise<void>}
 */
async function getPageList() {
    try {
        showLoading()
        const { pageSize, current } = paginationState
        const { success, data, total } = await apis.activity
            .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 auditHandleEdit = (params) => {
    currentForm.value = params
    showModal()
}
const startHandleEdit=async (obj)=>{
  try {
        const params = {
            ...obj,
            status: 3,
        }
        const result = await apis.activity.updateItem(obj.id, params).catch(() => {
            throw new Error()
        })
        if (config('http.code.success') === result?.success) {
            hideModal()
            getPageList()
            message.success('活动已成功')
        }
    } catch (error) {
        message.error(error.message)
    }
}
const handleAuditEdit = async () => {
    try {
        auditShowLoading()
        const params = {
            ...currentForm.value,
            status: auditStatus.value,
            remark: remark.value
        }
        console.log(params);
        const result = await apis.activity.updateItem(currentForm.value.id, params).catch(() => {
            throw new Error()
        })
        auditHideLoading()
        if (config('http.code.success') === result?.success) {
            hideModal()
            getPageList()
            message.success('审核成功')
        }
    } catch (error) {
        message.error(error.message)
        auditHideLoading()
    }
}
/**
 * 删除
 */
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.activity.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>
