<!-- 分润管理-机构分润配置-子机构分组管理-编辑 -->
<template>
    <app-drawer :title="t('profitshare.institution.group.edit.title')" width="480" v-model:show="show"
        :refresh="refresh" :loading="loading">
        <el-form ref="formRef" class="el-form--vertical" label-width="80px" :model="formData" :rules="formRules">
            <el-form-item :label="t('profitshare.institution.group.edit.subareagrouptype')" prop="subareagrouptype">
                <el-select v-model="formData.subareagrouptype">
                    <el-option :label="getProfitShareGroupName(ProfitShareGroup.SubInstitution)"
                        :value="ProfitShareGroup.SubInstitution" />
                </el-select>
            </el-form-item>
            <el-form-item :label="t('profitshare.institution.group.edit.subareagroupname')" prop="subareagroupname">
                <el-input v-model="formData.subareagroupname" :placeholder="t('common.pleaseenter')" />
            </el-form-item>
            <el-form-item :label="t('profitshare.institution.group.edit.remark')" prop="remark">
                <el-input type="textarea" v-model="formData.remark" maxlength="200" :rows="3"
                    :placeholder="t('common.pleaseenter')" />
            </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 { ProfitShareGroup, getProfitShareGroupName } from '@/constants/profitshare'
import { profitShareDeal } from '@/services/api/profitshare'
import AppDrawer from '@pc/components/base/drawer/index.vue'
import { i18n } from '@/stores'

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

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

const formData = ref<Model.ProfitShareDealReq>({
    remark: '',
    subareagroupname: '',
    subareagrouptype: ProfitShareGroup.SubInstitution
})

// 表单验证规则
const formRules: FormRules = {
    subareagrouptype: [{ required: true }],
    subareagroupname: [{ required: true }],
}

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
            profitShareDeal({
                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 = {
            subareagroupid: record.subareagroupid,
            remark: record.remark,
            subareagroupname: record.subareagroupname,
            subareagrouptype: record.subareagrouptype
        }
    }
})
</script>