<template>
    <x-search-bar class="mb-8-2">
        <template #default="{ gutter }">
            <a-form :model="searchFormData" layout="inline">
                <a-row :gutter="gutter">
                    <a-col :span="6">
                        <a-form-item :label="$t('pages.system.announcement.form.name')" name="title">
                            <a-input :placeholder="$t('pages.system.role.form.code.placeholder')"
                                v-model:value="searchFormData.title"></a-input>
                        </a-form-item>
                    </a-col>

                    <a-col :span="6">
                        <a-form-item :label="'状态'" name="status">
                            <a-input :placeholder="'请输入状态'" v-model:value="searchFormData.status"></a-input>
                        </a-form-item>
                    </a-col>

                    <a-col :span="6">
                        <a-form-item :label="'地区'" name="areaId">
                            <a-input :placeholder="'请输入地区'" v-model:value="searchFormData.areaId"></a-input>
                        </a-form-item>
                    </a-col>

                    <!-- <a-col v-bind="colSpan">
                        <a-form-item name="id">
                            <template #label>
                                {{ $t('pages.system.role.form.code') }}
                                <a-tooltip :title="$t('pages.system.announcement.form.id')">
                                    <question-circle-outlined class="ml-4-1 color-placeholder" />
                                </a-tooltip>
                            </template>
<a-input :placeholder="$t('pages.system.role.form.code.placeholder')" v-model:value="searchFormData.code"></a-input>
</a-form-item>
</a-col> -->

                    <a-col class="align-right" :span="6">
                        <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 v-action="'add'" type="primary" @click="$refs.addDialogRef.handleCreate()">
                        <template #icon>
                            <plus-outlined></plus-outlined>
                        </template>
                        {{ $t('pages.system.announcement.add') }}
                    </a-button>
                </x-action-bar>
                <a-table :columns="columns" :data-source="listData" :loading="loading" bordered="true" :pagination="paginationState"
                    :scroll="{ x: 1000 }" @change="onTableChange">
                    <template #bodyCell="{ column, record }">
                        <template v-if="'statusType' === column.key">
                            <!--状态-->
                            <a-tag v-if="statusTypeEnum.is('enabled', record.status)" color="processing">
                                {{ statusTypeEnum.getDesc(record.status) }}
                            </a-tag>
                            <!--状态-->
                            <a-tag v-if="statusTypeEnum.is('disabled', record.status)" color="processing">
                                {{ statusTypeEnum.getDesc(record.status) }}
                            </a-tag>
                        </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="column.dataIndex === 'areaId'">
                            <span>{{ areaEnum.getName(record.areaId) }}</span>
                        </template>
                        <template v-if="'createdAt' === column.dataIndex">
                            {{ dayjs(record.createdAt).format('YYYY-MM-DD HH:mm:ss') }}
                        </template>

                        <template v-if="'action' === column.key">
                            <x-action-button @click="$refs.addDialogRef.handleEdit(record)">
                                <a-tooltip>
                                    <template #title> {{ '编辑公告' }}</template>
                                    <edit-outlined />
                                </a-tooltip>
                            </x-action-button>
                            <x-action-button @click="handleRemove(record)">
                                <a-tooltip>
                                    <template #title> {{ '删除公告' }}</template>
                                    <delete-outlined style="color: #ff4d4f" />
                                </a-tooltip>
                            </x-action-button>
                        </template>
                    </template>
                </a-table>
            </a-card>
        </a-col>
    </a-row>

    <add-dialog ref="addDialogRef" @ok="onOk"></add-dialog>
</template>

<script setup>
import { message, Modal } from 'ant-design-vue'
import { ref } from 'vue'
import apis from '@/apis'
import { config } from '@/config'
import { statusTypeEnum} from '@/enums/system'

import { usePagination } from '@/hooks'
import AddDialog from './components/AddDialog.vue'
import { PlusOutlined, EditOutlined, DeleteOutlined } from '@ant-design/icons-vue'
import { useI18n } from 'vue-i18n'
import dayjs from 'dayjs'
import { customersEnum, areaEnum } from "@/enums/useEnum"

defineOptions({
    name: 'systemRole',
})
const { t } = useI18n() // 解构出t方法
const columns = [
    { title: t('pages.system.announcement.form.id'), dataIndex: 'id', width: 100 },
    { title: t('pages.system.announcement.form.createdAt'), dataIndex: 'createdAt', width: 200 },
    { title: t('pages.system.announcement.form.title'), dataIndex: 'title', key: 'title', width: 240 },
    { title: t('pages.system.announcement.form.content'), dataIndex: 'content', width: 240 },
    { title: t('pages.system.announcement.form.status'), key: 'status', dataIndex: 'status', width: 120 },
    { title: t('pages.system.announcement.form.areaId'), key: 'areaId', dataIndex: 'areaId', width: 120 },
    { title: t('button.action'), key: 'action', fixed: 'right', width: 120 },
]

const { listData, loading, showLoading, hideLoading, paginationState, searchFormData, resetPagination } =
    usePagination()
// const { resetForm } = useForm()
const addDialogRef = ref()

getPageList()

/**
 * 获取公公告列表
 * @returns {Promise<void>}
 */
async function getPageList() {
    try {
        showLoading()
        const { pageSize, current } = paginationState
        const { success, data, total } = await apis.announcement
            .getNoticesList({
                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 handleRemove({ id }) {
    Modal.confirm({
        title: t('pages.system.role.delTip'),
        content: t('button.confirm'),
        okText: t('button.confirm'),
        onOk: () => {
            return new Promise((resolve, reject) => {
                ; (async () => {
                    try {
                        const { success } = await apis.announcement.delNotices(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 handleResetSearch() {
    searchFormData.value = {}
    resetPagination()
    getPageList()
}
/**
 * 搜索
 */
function handleSearch() {
    resetPagination()
    getPageList()
}

/**
 * 编辑完成
 */
async function onOk() {
    await getPageList()
}
</script>

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