<!-- 通知公告-资讯来源 -->
<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_newssource_add'])"
                    @click="(code: string) => openComponent(code)" />
            </template>
            <!-- 来源LOGO -->
            <template #imageurl="{ value }">
                <el-image style="width: 100px; height: 50px" :src="getFirstPathFullUrl(value)" fit="contain" />
            </template>
            <!-- 操作 -->
            <template #operate="{ row }">
                <app-operation size="small"
                    :data-list="getActionButtons(['notice_newssource_details', 'notice_newssource_modify', 'notice_newssource_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 { getFirstPathFullUrl } from '@/filters'
import { useDataFilter } from '@/hooks/datatable-v2'
import { useRequest } from '@/hooks/request'
import { useOperation } from '@/hooks/operation'
import { columnsourcequery } 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.ColumnSourceQueryRsp>({
    onClose: () => onSearch()
})

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

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'imageurl', label: 'notice.newssource.imageurl' },
    { field: 'membername', label: 'notice.newssource.membername' },
    { field: 'membertags', label: 'notice.newssource.membertags' },
    { field: 'operate', label: 'common.operate', fixed: 'right' }
])

const { filterOption, getQueryParams, resetFilters } = useDataFilter<Notice.ColumnSourceQueryReq>({
    filters: [
        {
            field: 'smembername',
            label: t('notice.newssource.smembername')
        },
    ],
    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>