<!-- 分润管理-机构分润配置-子机构分组管理-会员列表 -->
<template>
    <teleport to="#appPageTeleport">
        <app-view>
            <app-table :data="dataList" :columns="tableColumns">
                <template #headerLeft>
                    <span>{{ t('profitshare.institution.group.member.subareagroupname') }} {{ record.subareagroupname }}</span>
                </template>
                <template #headerRight>
                    <div style="display: flex;">
                        <el-select v-model="formData.userid" :empty-values="[null, undefined, 0]" filterable
                            style="width: 180px;margin-right: 12px;">
                            <el-option v-for="item in userList" :key="item.userid" :label="item.accountname"
                                :value="item.userid">
                                <div style="display: flex;justify-content: space-between;">
                                    <span>{{ item.userid }}</span>
                                    <span>{{ item.accountname }}</span>
                                </div>
                            </el-option>
                        </el-select>
                        <el-button type="primary" :loading="loading" @click="onSubmit">{{ t('operation.add1') }}</el-button>
                        <el-button @click="emit('closed')">{{ t('operation.close') }}</el-button>
                    </div>
                </template>
                <!-- 操作 -->
                <template #operate="{ row }">
                    <el-button size="small" icon="Delete" @click="onDelete(row)" circle plain />
                </template>
                <template #footer>
                    <app-pagination :total="total" v-model:page-size="pageSize" v-model:page-index="pageIndex"
                        @change="run" />
                </template>
            </app-table>
        </app-view>
    </teleport>
</template>

<script lang="ts" setup>
import { ref, PropType } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRequest } from '@/hooks/request'
import { profitShareMemberQuery, profitShareGroupOrgan, profitShareMemberAdd, profitShareDeleteMember } from '@/services/api/profitshare'
import AppTable from '@pc/components/base/table/index.vue'
import AppPagination from '@pc/components/base/pagination/index.vue'
import { i18n } from '@/stores'

const props = defineProps({
    record: {
        type: Object as PropType<Model.ProfitShareQueryPageRsp>,
        required: true
    }
})

const { global: { t } } = i18n
const emit = defineEmits(['closed'])
const loading = ref(false)

const formData = ref<Model.ProfitShareMemberAddReq>({
    subareagroupid: props.record.subareagroupid,
    userid: 0
})

const { dataList: userList, run: getUserList } = useRequest(profitShareGroupOrgan)

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

const tableColumns = ref<Model.TableColumn[]>([
    { field: 'accountname', label: 'profitshare.institution.group.member.accountname' },
    { field: 'operate', label: 'common.operate', fixed: 'right' }
])

// 添加
const onSubmit = () => {
    if (formData.value.userid) {
        loading.value = true
        profitShareMemberAdd({
            data: formData.value
        }).then(() => {
            ElMessage.success('profitshare.institution.group.member.tips1')
            formData.value.userid = 0
            run()
            getUserList()
        }).catch((err) => {
            ElMessage.error('profitshare.institution.group.member.tips2' + err)
        }).finally(() => {
            loading.value = false
        })
    } else {
        ElMessage.warning('profitshare.institution.group.member.tips3')
    }
}

// 删除
const onDelete = (row: Model.ProfitShareMemberQueryRsp) => {
    ElMessageBox({
        title: t('common.alert'),
        message: 'profitshare.institution.group.member.tips4',
        showCancelButton: true,
        beforeClose: (action, instance, done) => {
            if (action === 'confirm') {
                instance.confirmButtonLoading = true
                profitShareDeleteMember({
                    data: {
                        userid: row.userid
                    }
                }).then(() => {
                    ElMessage.success(t('common.tips5'))
                    run()
                    getUserList()
                }).catch((err) => {
                    ElMessage.error(t('common.tips6') + err)
                }).finally(() => {
                    done()
                })
            } else {
                done()
            }
        }
    })
}
</script>