<!-- 分润管理-机构分润配置-子机构分组管理 -->
<template>
    <app-view>
        <template #header>
            <app-filter :options="filterOptons" />
        </template>
        <app-table :data="dataList" :columns="tableColumns" :loading="loading">
            <template #headerLeft>
                <app-operation :data-list="getActionButtons(['profitshare_institution_group_add'])"
                    @click="openComponent" />
            </template>
            <!-- 操作 -->
            <template #operate="{ row }">
                <app-operation size="small"
                    :data-list="getActionButtons(['profitshare_institution_group_modify', 'profitshare_institution_group_member', 'profitshare_institution_group_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 { getProfitShareGroupName } from '@/constants/profitshare'
import { useRequest } from '@/hooks/request'
import { useDataFilter } from '@/hooks/datatable'
import { useOperation } from '@/hooks/operation'
import { profitShareQueryPage } from '@/services/api/profitshare'
import AppTable from '@pc/components/base/table/index.vue'
import AppFilter from '@pc/components/base/table-filter/index.vue'
import AppPagination from '@pc/components/base/pagination/index.vue'
import AppOperation from '@pc/components/base/operation/index.vue'
import { i18n } from '@/stores'

const { global: { t } } = i18n

const { filterOptons, getQueryParams } = useDataFilter<Model.ProfitShareQueryPageReq>()

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

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

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'subareagroupname', label: 'profitshare.institution.group.subareagroupname' },
    { field: 'subareagrouptype', label: 'profitshare.institution.group.subareagrouptype', formatValue: (val) => getProfitShareGroupName(val) },
    { field: 'remark', label: 'profitshare.institution.group.remark' },
    { field: 'username', label: 'profitshare.institution.group.username' },
    { field: 'createtime', label: 'profitshare.institution.group.createtime', formatValue: (val) => formatDate(val) },
    { field: 'operate', label: 'common.operate', fixed: 'right' }
])

filterOptons.inputList = [
    {
        label: t('profitshare.institution.group.subareagroupname'), keys: ['subareagroupname']
    }
]

filterOptons.buttonList = [
    { label: t('operation.search'), className: 'el-button--primary', onClick: () => onSearch() },
    { label: t('operation.reset'), className: 'el-button--primary', onClick: () => onSearch(true) }
]

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