<!-- 会员机构管理-机构管理-经纪会员管理-子机构 -->
<template>
    <app-drawer title="子机构" width="900" v-model:show="show" :loading="loading" :refresh="refresh">
        <template #header>
            <app-filter :option="filterOption" />
        </template>
        <app-table :data="dataList" :columns="tableColumns" :loading="loading" />
        <component :is="componentMap.get(componentId)" v-bind="{ record }" @closed="closeComponent"
            v-if="componentId" />
    </app-drawer>
</template>

<script lang="ts" setup>
import { ref, shallowRef, PropType } from 'vue'
import { ElMessage } from 'element-plus'
import { useDataFilter } from '@/hooks/datatable-v2'
import { useRequest } from '@/hooks/request'
import { useOperation } from '@/hooks/operation'
import { queryRole } from '@/services/api/member'
import { i18n } from '@/stores'
import { formatDate } from '@/filters'
import { useEnum } from '@/hooks/enum'
import AppTable from '@pc/components/base/table/index.vue'
import AppFilter from '@pc/components/base/table-filter-v2/index.vue'
import AppDrawer from '@pc/components/base/drawer/index.vue'

const props = defineProps({
    record: {
        type: Object as PropType<Member.OrganRoleRsp>
    }
})

const { global: { t } } = i18n
const show = shallowRef(true)
const refresh = shallowRef(false)
// 状态
const areastatusEnum = useEnum('areastatus')

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

const { dataList, total, pageSize, pageIndex, loading, run } = useRequest(queryRole, {
    params: {
        roleid: 7,
        pageNum: 1,
        pageSize: 20,
        parentuserid: props.record?.userid
    },
    onError: (err) => {
        ElMessage.error(err)
    }
})

const tableColumns = ref<Model.TableColumn[]>([
    { field: 'memberuserid', label: 'member.institution.broker.memberuserid' },
    { field: 'accountname', label: 'member.institution.broker.accountname' },
    { field: 'accountid', label: 'member.institution.broker.accountid' },
    { field: 'accountstatus', label: 'member.institution.broker.accountstatus', formatValue: (val) => areastatusEnum.getEnumTypeName(val) },
    { field: 'operation', label: 'member.institution.broker.operation' },
    { field: 'rolemodifytime', label: 'member.institution.broker.rolemodifytime', formatValue:(val) => formatDate(val) },
    { field: 'operate', label: 'common.operate', fixed: 'right' }
])

const { filterOption, getQueryParams } = useDataFilter<Member.OrganRoleReq>({
    filters: [],
    buttons: [
        { label: t('operation.close'), className: 'el-button--primary', onClick: () => onCancel() }
    ]
})

const onCancel = (isRefresh = false) => {
    show.value = false
    refresh.value = isRefresh
}

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