<!-- 通知公告-短信查询 -->
<template>
    <app-view>
        <template #header>
            <app-filter :option="filterOption" />
        </template>
        <app-table :data="dataList" :columns="tableColumns" :loading="loading">
            <!-- 操作 -->
            <template #operate="{ row }">
                <app-operation size="small" :data-list="getActionButtons()"
                    @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 { smsquery } 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 { componentMap, componentId, record, openComponent, closeComponent, getActionButtons } = useOperation<Notice.InformManqueryRsp>({
    onClose: () => onSearch()
})

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

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'columnname', label: 'notice.sms.columnname' },
    { field: 'title', label: 'notice.sms.title' },
    { field: 'srcname', label: 'notice.sms.srcname' },
    { field: 'author', label: 'notice.sms.author' },
    { field: 'istop', label: 'notice.sms.istop', formatValue: (val) => formatDate(val) },
    { field: 'operate', label: 'common.operate', fixed: 'right' }
])

const { filterOption, getQueryParams, resetFilters } = useDataFilter<Notice.SmsQueryReq>({
    filters: [
        {
            field: 'status',
            label: t('notice.sms.status'),
            options: () => []
        },
        {
            field: 'recvphone',
            label: t('notice.sms.recvphone')
        },
    ],
    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>