<!-- 通知公告-资讯栏目 -->
<template>
    <app-view>
        <template #header>
            <app-filter :option="filterOption" />
        </template>
        <app-table :data="dataList" :columns="tableColumns" :loading="loading">
            <template #headerLeft>
                <app-operation :data-list="getActionButtons(['notice_newscolumn_add'])"
                    @click="(code: string) => openComponent(code)" />
            </template>
            <!-- 操作 -->
            <template #operate="{ row }">
                <app-operation size="small"
                    :data-list="getActionButtons(['notice_newscolumn_details', 'notice_newscolumn_modify', 'notice_newscolumn_delete'])"
                    @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 { columnquery } 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'

const { global: { t } } = i18n
const flagEnum = useEnum('flag')
const columntypeEnum = useEnum('columntype') // 栏目类型

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

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

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'columnname', label: 'notice.newscolumn.columnname' },
    { field: 'sort', label: 'notice.newscolumn.sort' },
    { field: 'isshow', label: 'notice.newscolumn.isshow', formatValue: (val) => flagEnum.getEnumTypeName(val) },
    { field: 'creatoruser', label: 'notice.newscolumn.creatoruser' },
    { field: 'creaedate', label: 'notice.newscolumn.creaedate', formatValue: (val) => formatDate(val) },
    { field: 'operate', label: 'common.operate', fixed: 'right' }
])

const { filterOption, getQueryParams, resetFilters } = useDataFilter<Notice.ColumnQueryReq>({
    filters: [
        {
            field: 'columntype',
            label: t('notice.newscolumn.columntype'),
            options: () => columntypeEnum.getEnumOptions()
        },
        {
            field: 'columnname',
            label: t('notice.newscolumn.columnname'),
        },
        {
            field: 'isshow',
            label: t('notice.newscolumn.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 onSearch = () => {
    const qs = getQueryParams()
    run(qs)
}
</script>