<!-- 分润管理-机构分润配置-子机构分润配置-编辑 -->
<template>
    <app-drawer :title="t('profitshare.institution.config.edit.title')" width="480" v-model:show="show"
        :refresh="refresh" :loading="loading">
        <el-form ref="formRef" class="el-form--vertical" label-width="100px" :model="formData" :rules="formRules">
            <el-form-item :label="t('profitshare.institution.config.edit.feeName')">
                {{ config.feeName }}
            </el-form-item>
            <el-form-item label="市场" v-if="config.marketId">
                {{ config.marketName }}
            </el-form-item>
            <el-form-item :label="t('profitshare.institution.config.edit.subareagroupid')" prop="subareagroupid">
                <el-select v-model="formData.subareagroupid" :empty-values="[null, undefined, 0]">
                    <el-option v-for="item in dataList" :key="item.subareagroupid" :label="item.subareagroupname"
                        :value="item.subareagroupid" />
                </el-select>
            </el-form-item>
            <el-form-item :label="t('profitshare.institution.config.edit.dividetype')" prop="dividetype">
                <el-radio-group v-model="formData.dividetype">
                    <el-radio v-for="item in premiumAutoModeEnum.getEnumOptions(1)" :key="item.value"
                        :value="item.value">
                        {{ item.label }}
                    </el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item :label="t('profitshare.institution.config.edit.dividemode')" prop="dividemode">
                <el-radio-group v-model="formData.dividemode">
                    <el-radio v-for="item in areadividemodeEnum.getEnumOptions()" :key="item.value" :value="item.value">
                        {{ item.label }}
                    </el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item :label="t('profitshare.institution.config.edit.dividevalue')" prop="dividevalue">
                <el-input-number v-model="formData.dividevalue" :placeholder="t('common.pleaseenter')" :min="0"
                    :max="100" />
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="onCancel(false)">{{ t('operation.cancel') }}</el-button>
            <el-button type="primary" @click="onSubmit">{{ t('operation.submit') }}</el-button>
        </template>
    </app-drawer>
</template>

<script lang="ts" setup>
import { ref, PropType, toRaw, onMounted } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { useEnum } from '@/hooks/enum'
import { useRequest } from '@/hooks/request'
import { profitShareQueryPage, profitShareDelSOrganSec } from '@/services/api/profitshare'
import AppDrawer from '@pc/components/base/drawer/index.vue'
import { i18n } from '@/stores'

interface Config {
    feeType: number;
    feeName: string;
    marketId: number;
    marketName: string;
}

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

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

const { global: { t } } = i18n
const formRef = ref<FormInstance>()
const show = ref(true)
const refresh = ref(false)
const loading = ref(false)

// 查询分组
const { dataList } = useRequest(profitShareQueryPage)

const formData = ref<Model.ProfitShareDelSOrganSecReq>({
    dividefeetype: props.config.feeType,
    dividemode: 1,
    dividetype: 1,
    dividevalue: 0,
    isedit: 0,
    marketid: props.config.marketId,
    subareagroupid: 0
})

// 表单验证规则
const formRules: FormRules = {
    subareagroupid: [{
        required: true,
        validator: (rule, value, callback) => {
            if (value) {
                callback()
            } else {
                callback(new Error(t('profitshare.institution.config.edit.tips1')))
            }
        }
    }],
    dividetype: [{
        required: true,
        validator: (rule, value, callback) => {
            if (value) {
                callback()
            } else {
                callback(new Error(t('profitshare.institution.config.edit.tips2')))
            }
        }
    }],
    dividemode: [{
        required: true,
        validator: (rule, value, callback) => {
            if (value) {
                callback()
            } else {
                callback(new Error(t('profitshare.institution.config.edit.tips3')))
            }
        }
    }],
    dividevalue: [{
        required: true,
        validator: (rule, value, callback) => {
            if (value) {
                callback()
            } else {
                callback(new Error(t('profitshare.institution.config.edit.tips4')))
            }
        }
    }],
}

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

const onSubmit = () => {
    formRef.value?.validate((valid) => {
        if (valid) {
            const rawData = toRaw(formData.value)
            loading.value = true
            profitShareDelSOrganSec({
                data: rawData
            }).then(() => {
                ElMessage.success(t('common.tips3'))
                onCancel(true)
            }).catch((err) => {
                ElMessage.error(t('common.tips4') + err)
            }).finally(() => {
                loading.value = false
            })
        }
    })
}

onMounted(() => {
    const record = props.record
    if (record) {
        formData.value = {
            dividefeetype: record.dividefeetype,
            dividemode: record.dividemode,
            dividetype: record.dividetype,
            dividevalue: record.dividevalue * 100,
            isedit: 1,
            marketid: record.marketid,
            subareagroupid: record.subareagroupid
        }
    }
})
</script>