<!-- 会员机构管理-机构管理-机构开户申请-审核通过 -->
<template>
    <app-drawer :title="t('member.institution.open.details.title2')" width="480" v-model:show="show" :loading="loading" :refresh="refresh">
        <el-form ref="formRef" label-width="130px" :model="formData" :rules="formRules" :show-message="false">
            <el-form-item :label="t('member.institution.open.details.areacode')" prop="areacode">
                <el-input type="number" v-model="formData.areacode" :placeholder="t('member.institution.open.details.tips1')" />
            </el-form-item>
            <el-form-item :label="t('member.institution.open.details.smlogincode')" prop="smlogincode">
                <el-input v-model="formData.smlogincode" maxlength="50" :placeholder="t('member.institution.open.details.tips2')" />
            </el-form-item>
            <el-form-item :label="t('member.institution.open.details.loginname')" prop="loginname">
                <el-input v-model="formData.loginname" maxlength="50" :placeholder="t('common.pleaseenter')" />
            </el-form-item>
            <el-form-item :label="t('member.institution.open.details.smpassword')" prop="smpassword">
                <el-input v-model="formData.smpassword" maxlength="50" :placeholder="t('common.pleaseenter')" disabled />
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="onCancel(false)">{{ t('operation.close') }}</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 } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { wskhUserinfoApproved } from '@/services/api/member'
import AppDrawer from '@pc/components/base/drawer/index.vue'
import { i18n } from '@/stores'

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

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

const formData = ref<Partial<Member.MemberDetail>>({
    userid: props.record.userid,
    auditflag: 1,
    smpassword: '123456'
})

// 表单验证规则
const formRules: FormRules = {
    areacode: [{ required: true, min: 4, max: 4 }],
    smlogincode: [{
        required: true,
        validator: (rule, value, callback) => {
            if (value?.length > 2 && /[a-zA-Z]/.test(value)) {
                callback()
            } else {
                callback(new Error(t('member.institution.open.details.tips2')))
            }
        }
    }],
    loginname: [{ required: true }]
}

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

const onSubmit = () => {
    formRef.value?.validate((valid) => {
        if (valid) {
            loading.value = true
            wskhUserinfoApproved({
                data: toRaw(formData.value)
            }).then(() => {
                ElMessage.success(t('common.tips7'))
                onCancel(true)
            }).catch((err) => {
                ElMessage.error(t('common.tips8') + err)
                //onCancel()
            }).finally(() => {
                loading.value = false
            })
        }
    })
}
</script>