<!-- 通知公告-公告通知管理 -->
<template>
    <app-view>
        <template #header>
            <app-filter :option="filterOption">
                <template #userid="{ item }">
                    <el-form-item :label="item.label">
                        <app-select-investor v-model="item.value" />
                    </el-form-item>
                </template>
            </app-filter>
        </template>
        <app-table :data="dataList" :columns="tableColumns" :loading="loading">
            <template #headerLeft>
                <app-operation :data-list="getActionButtons(['notice_manage_add'])"
                    @click="(code: string) => openComponent(code)" />
            </template>
            <!-- 操作 -->
            <template #operate="{ row }">
                <app-operation size="small" :data-list="handleOperateButtons(row)"
                    @click="(code: string) => openComponent(code, row)" circle />
            </template>
            <template #footer>
                <app-pagination :total="total" v-model:page-size="pageSize" v-model:page-index="pageIndex"
                    @change="onSearch" />
            </template>
        </app-table>
        <component :is="componentMap.get(componentId)" v-bind="{ record }" @closed="closeComponent"
            v-if="componentId" />
    </app-view>
</template>

<script lang="ts" setup>
import { shallowRef } from 'vue'
import { ElMessage } from 'element-plus'
import { formatDate } from '@/filters'
import { useDataFilter } from '@/hooks/datatable-v2'
import { useRequest } from '@/hooks/request'
import { useOperation } from '@/hooks/operation'
import { useEnum } from '@/hooks/enum'
import { query } from '@/services/api/notice'
import { i18n } from '@/stores'
import moment from 'moment'
import AppTable from '@pc/components/base/table/index.vue'
import AppPagination from '@pc/components/base/pagination/index.vue'
import AppOperation from '@pc/components/base/operation/index.vue'
import AppFilter from '@pc/components/base/table-filter-v2/index.vue'
import AppSelectInvestor from '@pc/components/modules/select-investor/index.vue'

const { global: { t } } = i18n
const flagEnum = useEnum('flag')
const msgtypeEnum = useEnum('msgtype') // 消息类型
const sendtypeEnum = useEnum('sendtype') // 发布类型
const sendstatusEnum = useEnum('sendstatus') // 推送状态

const { componentMap, componentId, record, openComponent, closeComponent, getActionButtons } = useOperation<Notice.QueryRsp>({
    onClose: () => onSearch()
})

const { dataList, total, pageSize, pageIndex, loading, run } = useRequest(query, {
    params: {
        pageNum: 1,
        pageSize: 20
    },
    onError: (err) => {
        ElMessage.error(err)
    }
})

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'title', label: 'notice.manage.title' },
    { field: 'msgtype', label: 'notice.manage.msgtype', formatValue: (val) => msgtypeEnum.getEnumTypeName(val) },
    { field: 'sendtype', label: 'notice.manage.sendtype', formatValue: (val) => sendtypeEnum.getEnumTypeName(val) },
    { field: 'sentstatus', label: 'notice.manage.sentstatus', formatValue: (val) => sendstatusEnum.getEnumTypeName(val) },
    { field: 'scheduletime', label: 'notice.manage.scheduletime', formatValue: (val) => formatDate(val) },
    { field: 'endtime', label: 'notice.manage.endtime', formatValue: (val) => formatDate(val) },
    { field: 'recipient', label: 'notice.manage.recipient' },
    { field: 'operate', label: 'common.operate', fixed: 'right', width: 200 }
])

const { filterOption, getQueryParams, resetFilters } = useDataFilter<Notice.QueryReq>({
    filters: [
        {
            field: 'title',
            label: t('notice.manage.title')
        },
        {
            field: 'msgtype',
            label: t('notice.manage.msgtype'),
            options: () => msgtypeEnum.getEnumOptions()
        },
        {
            field: 'content',
            label: t('notice.manage.content')
        },
        {
            field: 'userid',
            label: t('notice.manage.userid')
        },
        {
            field: 'sendtype',
            label: t('notice.manage.sendtype'),
            options: () => sendtypeEnum.getEnumOptions()
        },
        {
            field: 'isforcedisplay',
            label: t('notice.manage.isforcedisplay'),
            options: () => flagEnum.getEnumOptions()
        },
    ],
    buttons: [
        { label: t('operation.search'), className: 'el-button--primary', onClick: () => onSearch() },
        { label: t('operation.reset'), className: 'el-button--primary', validateEvent: false, onClick: () => resetFilters() }
    ]
})

// 处理操作按钮
const handleOperateButtons = (row: Notice.QueryRsp) => {
    const buttons = ['notice_manage_details', 'notice_manage_delete']

    if (row.sentstatus === 0) {
        buttons.push('notice_manage_audit')
        const startDate = moment(row.scheduletime)
        if (moment().isBefore(startDate)) {
            buttons.push('notice_manage_modify')
        }
    }

    if (row.msgtype === 1 && row.sentstatus === 1) {
        const endDate = moment(row.endtime)
        if (moment().isBefore(endDate)) {
            buttons.push('notice_manage_finish')
        }
    }

    if (row.isforcedisplay) {
        buttons.push('notice_manage_unforced')
    } else {
        buttons.push('notice_manage_forced')
    }

    return getActionButtons(buttons)
}

const onSearch = () => {
    const qs = getQueryParams()
    run(qs)
}
</script>