<!-- 会员机构管理-子机构管理-会员子机构管理 -->
<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(['member_subinstitution_user_add'])"
                    @click="openComponent" />
            </template>
            <!-- 操作 -->
            <template #operate="{ row }">
                <app-operation size="small" :data-list="handleOperateButtons(row)"
                    @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 { useEnum } from '@/hooks/enum'
import { useMarket } from '@/hooks/market'
import { useRequest } from '@/hooks/request'
import { useDataFilter } from '@/hooks/datatable-v2'
import { useOperation } from '@/hooks/operation'
import { queryOrganSon } 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 { i18n } from '@/stores'

const { global: { t } } = i18n


const areastatusEnum = useEnum('areastatus') // 机构状态
const usertype2Enum = useEnum('usertype2') // 机构类型
const userinfotypeEnum = useEnum('userinfotype') // 所有者类型

const { getMarketOptions } = useMarket()

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

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

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'userid', label: 'member.subinstitution.user.userid' },
    { field: 'accountname', label: 'member.subinstitution.user.accountname' },
    { field: 'parentname', label: 'member.subinstitution.user.parentname' },
    { field: 'memberusername', label: 'member.subinstitution.user.memberusername' },
    { field: 'accountstatus', label: 'member.subinstitution.user.accountstatus', formatValue: (val) => areastatusEnum.getEnumTypeName(val) },
    { field: 'usertype', label: 'member.subinstitution.user.usertype', formatValue: (val) => usertype2Enum.getEnumTypeName(val) },
    { field: 'userinfotype', label: 'member.subinstitution.user.userinfotype', formatValue: (val) => userinfotypeEnum.getEnumTypeName(val) },
    { field: 'userName', label: 'member.subinstitution.user.userName' },
    { field: 'modifytime', label: 'member.subinstitution.user.modifytime', formatValue: (val) => formatDate(val) },
    { field: 'operate', label: 'common.operate', width: 200, fixed: 'right' }
])

const { filterOption, getQueryParams, resetFilters } = useDataFilter<Member.OrganSonReq>({
    filters: [
        {
            field: 'accountname',
            label: t('member.subinstitution.user.username1')
        },
        {
            field: 'markets',
            label: t('member.subinstitution.user.markets'),
            options: () => getMarketOptions()
        },
        {
            field: 'accountstatus',
            label: t('member.subinstitution.user.accountstatus'),
            options: () => areastatusEnum.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 handleOperateButtons = (row: Member.OrganSonRsp) => {
    const buttons = ['member_subinstitution_user_details', 'member_subinstitution_user_account', 'member_subinstitution_user_login']

    if (![6, 7].includes(row.accountstatus)) {
        if ([3, 4].includes(row.modifystatus)) {
            buttons.push('member_subinstitution_user_cancel')
        } else {
            buttons.push('member_subinstitution_user_modify')
        }
    }

    if ([0, 1, 6].includes(row.modifystatus)) {
        if (![6, 7].includes(row.accountstatus)) {
            buttons.push('member_subinstitution_user_close')
        }
        if (row.accountstatus === 6) {
            buttons.push('member_subinstitution_user_recover')
        }
    }

    return getActionButtons(buttons)
}

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