<!-- 分润管理-机构分润配置-子机构分润配置 -->
<template>
    <app-view>
        <template v-if="dataList.length">
            <fieldset v-for="(item, index) in dataList" :key="index">
                <legend>{{ item.feeName + (item.marketId && ` [${item.marketName}]`) }}</legend>
                <app-table :data="item.configs" :columns="tableColumns">
                    <template #headerLeft>
                        <app-operation :data-list="getActionButtons(['profitshare_institution_config_add'])"
                            @click="(code: string) => showComponent(code, item)" />
                    </template>
                    <!-- 操作 -->
                    <template #operate="{ row }">
                        <app-operation size="small"
                            :data-list="getActionButtons(['profitshare_institution_config_modify', 'profitshare_institution_config_delete'])"
                            @click="(code: string) => showComponent(code, item, row)" circle />
                    </template>
                </app-table>
            </fieldset>
        </template>
        <el-empty v-else />
        <component :is="componentMap.get(componentId)" v-bind="{ config, record }" @closed="closeComponent"
            v-if="componentId" />
    </app-view>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { formatDate, parsePercent } from '@/filters'
import { useEnum } from '@/hooks/enum'
import { useRequest } from '@/hooks/request'
import { useOperation } from '@/hooks/operation'
import { initProfitShare } from '@/services/api/profitshare'
import AppTable from '@pc/components/base/table/index.vue'
import AppOperation from '@pc/components/base/operation/index.vue'

interface Data {
    feeType: number;
    feeName: string;
    marketId: number;
    marketName: string;
    configs: Model.InitProfitShareConfig[];
}

// 分润算法
const premiumAutoModeEnum = useEnum('premiumAutoMode')
// 分润方式
const areadividemodeEnum = useEnum('areadividemode')

const config = ref<Data>()
const dataList = ref<Data[]>([])

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

const { run } = useRequest(initProfitShare, {
    onSuccess: (res) => {
        const { dividefeetype1, dividefeetype6, dividefeetype9, dividefeetype1name, dividefeetype6name, dividefeetype9name, ft1, ft2, ft3, mks } = res.data
        dataList.value = []
        if (dividefeetype1 === 1) {
            mks.forEach((e) => {
                dataList.value.push({
                    feeType: 1,
                    feeName: dividefeetype1name,
                    marketId: e.marketid,
                    marketName: e.marketname,
                    configs: (ft1 && ft1[e.marketid]) || []
                })
            })
        }
        if (dividefeetype6 === 1) {
            dataList.value.push({
                feeType: 6,
                feeName: dividefeetype6name,
                marketId: 0,
                marketName: '',
                configs: ft2 || []
            })
        }
        if (dividefeetype9 === 1) {
            mks.forEach((e) => {
                if (e.trademode !== 17) {
                    dataList.value.push({
                        feeType: 9,
                        feeName: dividefeetype9name,
                        marketId: e.marketid,
                        marketName: e.marketname,
                        configs: (ft3 && ft3[e.marketid]) || []
                    })
                }
            })
        }
    },
    onError: (err) => {
        ElMessage.error(err)
        dataList.value = []
    }
})

const tableColumns = ref<Model.TableColumn[]>([
    { field: 'subareagroupname', label: 'profitshare.institution.config.subareagroupname' },
    { field: 'dividetype', label: 'profitshare.institution.config.dividetype', formatValue: (val) => premiumAutoModeEnum.getEnumTypeName(val) },
    { field: 'dividemode', label: 'profitshare.institution.config.dividemode', formatValue: (val) => areadividemodeEnum.getEnumTypeName(val) },
    { field: 'dividevalue', label: 'profitshare.institution.config.dividevalue', formatValue: (val) => parsePercent(val) },
    { field: 'username', label: 'profitshare.institution.config.username' },
    { field: 'createtime', label: 'profitshare.institution.config.createtime', formatValue: (val) => formatDate(val) },
    { field: 'operate', label: 'common.operate', fixed: 'right' }
])

const showComponent = (code: string, data: Data, row?: Model.InitProfitShareConfig) => {
    config.value = data
    openComponent(code, row)
}
</script>