<!-- 交易商管理-交易商管理-交易商批量转移 -->
<template>
    <app-view>
        <template #header>
            <app-filter :option="filterOption">
                <!-- 转出交易商 -->
                <template #outinvestor="{ item }">
                    <el-form-item :label="item.label">
                        <app-select-member v-model="item.value" :params="{ usertype: '5' }" />
                    </el-form-item>
                </template>
                <!-- 转出方 -->
                <template #outmemberuserid="{ item }">
                    <el-form-item :label="item.label">
                        <app-select-member v-model="item.value" :params="{ usertype: '2,3,4' }" />
                    </el-form-item>
                </template>
                <!-- 转入方 -->
                <template #inuserid="{ item }">
                    <el-form-item :label="item.label">
                        <app-select-member v-model="item.value" :params="{ usertype: '2,3,4' }" />
                    </el-form-item>
                </template>
            </app-filter>
        </template>
        <app-table :data="dataList" :columns="tableColumns" :loading="loading">
            <template #headerLeft>
                <app-operation :data-list="getActionButtons(['investor_manage_transfer_add'])" @click="openComponent" />
            </template>
            <!-- 操作 -->
            <template #operate="{ row }">
                <app-operation size="small" :data-list="getActionButtons(['investor_manage_transfer_cancel'])"
                    @click="(code: string) => openComponent(code, row)" circle v-if="row.transferstatus === 1" />
                <span v-else>{{ handleNoneValue() }}</span>
            </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, handleNoneValue } from '@/filters'
import { useEnum } from '@/hooks/enum'
import { useRequest } from '@/hooks/request'
import { useDataFilter } from '@/hooks/datatable-v2'
import { useOperation } from '@/hooks/operation'
import { queryOrganSonTransfer } from '@/services/api/member'
import AppTable from '@pc/components/base/table/index.vue'
import AppFilter from '@pc/components/base/table-filter-v2/index.vue'
import AppPagination from '@pc/components/base/pagination/index.vue'
import AppOperation from '@pc/components/base/operation/index.vue'
import AppSelectMember from '@pc/components/modules/select-member/index.vue'
import { i18n } from '@/stores'

const { global: { t } } = i18n

// 转移状态
const transferstatusEnum = useEnum('transferstatus')
// 转移类型
const investortransfertypeEnum = useEnum('investortransfertype')

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

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

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'transfertype', label: 'investor.manage.transfer.transfertype', formatValue: (val) => investortransfertypeEnum.getEnumTypeName(val) },
    { field: 'outmemberusername', label: 'investor.manage.transfer.outmemberusername' },
    { field: 'outparentusername', label: 'investor.manage.transfer.outparentusername' },
    { field: 'outinvestorname', label: 'investor.manage.transfer.outinvestorname' },
    { field: 'inmemberusername', label: 'investor.manage.transfer.inmemberusername' },
    { field: 'inuseridname', label: 'investor.manage.transfer.inuseridname' },
    { field: 'transferstatus', label: 'investor.manage.transfer.transferstatus', formatValue: (val) => transferstatusEnum.getEnumTypeName(val) },
    { field: 'createtime', label: 'investor.manage.transfer.createtime', formatValue: (val) => formatDate(val) },
    { field: 'operate', label: 'common.operate', fixed: 'right' }
])

const { filterOption, getQueryParams, resetFilters } = useDataFilter<Member.OrganSonTransferReq>({
    filters: [
        {
            field: 'outinvestor',
            label: t('investor.manage.transfer.outinvestor')
        },
        {
            field: 'outmemberuserid',
            label: t('investor.manage.transfer.outmemberuserid')
        },
        {
            field: 'inuserid',
            label: t('investor.manage.transfer.inuserid')
        },
        {
            field: 'transferstatus',
            label: t('investor.manage.transfer.transferstatus'),
            options: () => transferstatusEnum.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 = (clear = false) => {
    const qs = getQueryParams(clear)
    run(qs)
}
</script>