<!-- 会员机构管理-机构管理-机构资料管理 -->
<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(['query_internal_liquidation_export'])"
                    @click="(code: string) => openComponent(code)" />
            </template>
            <!-- 操作 -->
            <template #operate="{ row }">
                <app-operation size="small" :data-list="getActionButtons(['query_internal_liquidation_details'])"
                    @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, queryParams }" @closed="closeComponent"
            v-if="componentId" />
    </app-view>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { getBuyOrSellList } from '@/constants/order'
import { useDataFilter } from '@/hooks/datatable-v2'
import { useRequest } from '@/hooks/request'
import { useMarket } from '@/hooks/market'
import { useOperation } from '@/hooks/operation'
import { queryOrganDetailList } from '@/services/api/member'
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 { getMarketOptions } = useMarket()
const { global: { t } } = i18n

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

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

const tableColumns = ref<Model.TableColumn[]>([
    { field: 'orderId', label: '机构代码' },
    { field: 'accountNameId', label: '机构名称' },
    { field: 'goodsCodeName', label: '机构状态' },
    { field: 'buyOrSell', label: '机构角色' },
    { field: 'orderPrice', label: '操作员' },
    { field: 'orderQty', label: '创建时间' },
    { field: 'orderAccount', label: '最后更新时间' },
    { field: 'operate', label: 'common.operate', fixed: 'right' }
])

const { queryParams, filterOption, getQueryParams, resetFilters } = useDataFilter<Model.OrganDetailListReq>({
    filters: [
        {
            field: 'marketId',
            label: '机构',
            required: true,
            options: () => getMarketOptions()
        },
        {
            field: 'goodsId',
            label: '机构状态',
        },
        {
            field: 'buyOrSell',
            label: '市场权限',
            options: () => getBuyOrSellList()
        },
        {
            field: 'accountId',
            label: '创建时间',
        },
        {
            field: 'orderId',
            label: '角色',
        },
    ],
    buttons: [
        { label: t('operation.search'), className: 'el-button--primary', onClick: () => onSearch() },
        { label: t('operation.reset'), className: 'el-button--primary', validateEvent: false, onClick: () => resetFilters() }
    ]
})

// 处理请求参数
const processRequiredParams = () => {
    const qs = getQueryParams()
    const marketId = (qs.marketId ?? 0).toString()
    qs.tradeMode = +marketId.slice(0, 2)
    return qs
}

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