<!-- 通知公告-资讯管理 -->
<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_news_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 { informManquery, informManinit } from '@/services/api/notice'
import { i18n } from '@/stores'
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 columnDetailStatusEnum = useEnum('columndetail_status') // 状态

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

const { data } = useRequest(informManinit)

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

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'columnname', label: 'notice.news.columnname' },
    { field: 'title', label: 'notice.news.title' },
    { field: 'srcname', label: 'notice.news.srcname' },
    { field: 'author', label: 'notice.news.author' },
    { field: 'istop', label: 'notice.news.istop', formatValue: (val) => flagEnum.getEnumTypeName(val) },
    { field: 'isshow', label: 'notice.news.isshow', formatValue: (val) => flagEnum.getEnumTypeName(val) },
    { field: 'publishdate', label: 'notice.news.publishdate', formatValue: (val) => formatDate(val, 'YYYY-MM-DD') },
    { field: 'status', label: 'notice.news.status', formatValue: (val) => columnDetailStatusEnum.getEnumTypeName(val) },
    { field: 'creatoruser', label: 'notice.news.creatoruser' },
    { field: 'creaedate', label: 'notice.news.creaedate', formatValue: (val) => formatDate(val) },
    { field: 'operate', label: 'common.operate', fixed: 'right', width: 180 }
])

const { filterOption, getQueryParams, resetFilters } = useDataFilter<Notice.InformManqueryReq>({
    filters: [
        {
            field: 'columnid',
            label: t('notice.news.columnid'),
            options: () => data.value?.siteColumnconfig.map((e) => ({
                label: e.columnname,
                value: e.id
            })) ?? []
        },
        {
            field: 'status',
            label: t('notice.news.status'),
            options: () => columnDetailStatusEnum.getEnumOptions()
        },
        {
            field: 'title',
            label: t('notice.news.title')
        },
        {
            field: 'isshow',
            label: t('notice.news.isshow'),
            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.InformManqueryRsp) => {
    const buttons = ['notice_news_details', 'notice_news_delete']

    if (row.status === 10) {
        buttons.push('notice_news_audit')
    } else {
        buttons.push('notice_news_modify')
        if (row.status !== 1) {
            if (row.isshow) {
                buttons.push('notice_news_show')
            } else {
                buttons.push('notice_news_hide')
            }
        }
    }

    return getActionButtons(buttons)
}

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