<!-- 会员机构管理-机构管理-机构开户申请-编辑 -->
<template>
    <app-drawer :title="t('member.institution.open.edit.title')" width="900" v-model:show="show" :refresh="refresh"
        :loading="loading">
        <el-form ref="formRef" label-width="140px" :model="formData" :rules="formRules" :show-message="false">
            <fieldset class="g-fieldset el-form--horizontal">
                <legend class="g-fieldset__legend">{{ t('common.baseinfo') }}</legend>
                <el-form-item :label="t('member.institution.open.edit.username')" prop="username">
                    <el-input v-model="formData.username" maxlength="50" :placeholder="t('common.pleaseenter')" />
                </el-form-item>
                <el-form-item :label="t('member.institution.open.edit.userinfotype')" prop="userinfotype">
                    <app-enum code="userinfotype" type="radio" v-model="formData.userinfotype" />
                </el-form-item>
                <el-form-item :label="t('member.institution.open.edit.referral')" prop="referral">
                    <app-select-member ref="referralRef" v-model="formData.referral" />
                </el-form-item>
            </fieldset>
            <fieldset class="g-fieldset el-form--horizontal">
                <legend class="g-fieldset__legend">{{ isPerson ? t('member.institution.open.edit.person') :
                    t('member.institution.open.edit.companny') }}{{ t('member.institution.open.edit.profile') }}
                </legend>
                <el-form-item :label="t('member.institution.open.edit.customername')" prop="customername">
                    <el-input v-model="formData.customername" maxlength="50" :placeholder="t('common.pleaseenter')" />
                </el-form-item>
                <el-form-item :label="t('member.institution.open.edit.company')" prop="company" v-if="isPerson">
                    <el-input v-model="formData.company" maxlength="50" :placeholder="t('common.pleaseenter')" />
                </el-form-item>
                <el-form-item :label="t('member.institution.open.edit.biznature')" prop="biznature" v-if="!isPerson">
                    <app-enum code="biznature" v-model="formData.biznature" />
                </el-form-item>
                <el-form-item :label="t('member.institution.open.edit.cardtype')" prop="cardtype">
                    <app-enum code="certypecompany" v-model="formData.cardtype" v-if="!isPerson" />
                    <app-enum code="certypeperson" v-model="formData.cardtype" v-else />
                </el-form-item>
                <el-form-item :label="t('member.institution.open.edit.cardnum')" prop="cardnum">
                    <el-input v-model="formData.cardnum" maxlength="50" :placeholder="t('common.pleaseenter')" />
                </el-form-item>
                <el-form-item :label="t('member.institution.open.edit.cardfrontphotourl')" prop="cardfrontphotourl">
                    <app-upload v-model="uploadFiles.cardfrontphotourl" :file-types="['image']"
                        :type-message="t('common.tips21')" :max-size="uploadConfig.maxFileSize" />
                </el-form-item>
                <el-form-item :label="t('member.institution.open.edit.cardbackphotourl')" prop="cardbackphotourl"
                    v-if="isPerson">
                    <app-upload v-model="uploadFiles.cardbackphotourl" :file-types="['image']"
                        :type-message="t('common.tips21')" :max-size="uploadConfig.maxFileSize" />
                </el-form-item>
                <template v-if="!isPerson">
                    <el-form-item :label="t('member.institution.open.edit.legalpersonname')" prop="legalpersonname">
                        <el-input v-model="formData.legalpersonname" maxlength="50"
                            :placeholder="t('common.pleaseenter')" />
                    </el-form-item>
                    <el-form-item :label="t('member.institution.open.edit.legalcardfrontphotourl')"
                        prop="legalcardfrontphotourl">
                        <app-upload v-model="uploadFiles.legalcardfrontphotourl" :file-types="['image']"
                            :type-message="t('common.tips21')" :max-size="uploadConfig.maxFileSize" />
                    </el-form-item>
                    <el-form-item :label="t('member.institution.open.edit.legalcardbackphotourl')"
                        prop="legalcardbackphotourl">
                        <app-upload v-model="uploadFiles.legalcardbackphotourl" :file-types="['image']"
                            :type-message="t('common.tips21')" :max-size="uploadConfig.maxFileSize" />
                    </el-form-item>
                    <el-form-item :label="t('member.institution.open.edit.contactname')" prop="contactname">
                        <el-input v-model="formData.contactname" maxlength="50"
                            :placeholder="t('common.pleaseenter')" />
                    </el-form-item>
                </template>
                <el-form-item :label="t('member.institution.open.edit.sex')" prop="sex">
                    <app-enum code="Sex" type="radio" v-model="formData.sex" />
                </el-form-item>
                <el-form-item :label="t('member.institution.open.edit.mobilephone')" prop="mobilephone">
                    <el-input type="number" v-model="formData.mobilephone" maxlength="50"
                        :placeholder="t('common.pleaseenter')" />
                </el-form-item>
                <el-form-item :label="t('member.institution.open.edit.telphone')" prop="telphone">
                    <el-input v-model="formData.telphone" maxlength="50" :placeholder="t('common.pleaseenter')" />
                </el-form-item>
                <el-form-item class="el-form-item--row" :label="t('member.institution.open.edit.provinceid')"
                    prop="provinceid">
                    <app-region class="el-form-item--col" v-model:country="formData.countryid"
                        v-model:province="formData.provinceid" v-model:city="formData.cityid"
                        v-model:district="formData.districtid" />
                </el-form-item>
                <el-form-item class="el-form-item--row" :label="t('member.institution.open.edit.cardaddress')"
                    prop="cardaddress">
                    <el-input v-model="formData.cardaddress" maxlength="50" :placeholder="t('common.pleaseenter')" />
                </el-form-item>
                <el-form-item :label="t('member.institution.open.edit.postalcode')" prop="postalcode">
                    <el-input v-model="formData.postalcode" maxlength="50" :placeholder="t('common.pleaseenter')" />
                </el-form-item>
                <el-form-item :label="t('member.institution.open.edit.email')" prop="email">
                    <el-input v-model="formData.email" maxlength="50" :placeholder="t('common.pleaseenter')" />
                </el-form-item>
                <el-form-item class="el-form-item--row" :label="t('member.institution.open.edit.remark')" prop="remark">
                    <el-input type="textarea" v-model="formData.remark" maxlength="200" :rows="3"
                        :placeholder="t('common.pleaseenter')" />
                </el-form-item>
            </fieldset>
            <fieldset class="g-fieldset el-form--horizontal">
                <legend class="g-fieldset__legend">{{ t('common.annex') }}</legend>
                <el-form-item :label="t('common.annex1')" prop="attachment1">
                    <app-upload v-model="uploadFiles.attachment1" :file-types="['image', 'pdf']"
                        :type-message="t('common.tips21')" :max-size="uploadConfig.maxFileSize" />
                </el-form-item>
                <el-form-item :label="t('common.annex1')" prop="attachment2">
                    <app-upload v-model="uploadFiles.attachment2" :file-types="['image', 'pdf']"
                        :type-message="t('common.tips21')" :max-size="uploadConfig.maxFileSize" />
                </el-form-item>
            </fieldset>
        </el-form>
        <template #footer>
            <el-button @click="onCancel(false)">{{ t('operation.cancel') }}</el-button>
            <el-button type="primary" @click="onSubmit">{{ t('operation.submitaduit') }}</el-button>
        </template>
    </app-drawer>
</template>

<script lang="ts" setup>
import { ref, reactive, PropType, onMounted, computed } from 'vue'
import { ElMessage, FormInstance, FormRules, UploadUserFile } from 'element-plus'
import { parseFilePaths, extractFilePaths } from '@/filters'
import { uploadConfig } from '@/constants/common'
import { decryptAES } from '@/services/crypto'
import { useRequest } from '@/hooks/request'
import { wskhUserinfoAdd, queryWskhUserinfoDetail } from '@/services/api/member'
import { UserInfoType, Gender } from '@/constants/member'
import { i18n } from '@/stores'
import AppDrawer from '@pc/components/base/drawer/index.vue'
import AppUpload from '@pc/components/base/upload/index.vue'
import AppEnum from '@pc/components/modules/enum/index.vue'
import AppRegion from '@pc/components/modules/region/index.vue'
import AppSelectMember from '@pc/components/modules/select-member/index.vue'

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

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

const referralRef = ref() // 推荐人组件实例

const uploadFiles = reactive<{
    cardfrontphotourl: UploadUserFile[];
    cardbackphotourl: UploadUserFile[];
    legalcardfrontphotourl: UploadUserFile[];
    legalcardbackphotourl: UploadUserFile[];
    attachment1: UploadUserFile[];
    attachment2: UploadUserFile[];
}>({
    cardfrontphotourl: [],
    cardbackphotourl: [],
    legalcardfrontphotourl: [],
    legalcardbackphotourl: [],
    attachment1: [],
    attachment2: []
})


// 是否个人
const isPerson = computed(() => formData.value.userinfotype === UserInfoType.Personal)

const formData = ref<Partial<Member.MemberDetail>>({
    userinfotype: UserInfoType.Company,
    sex: Gender.Male
})

const { loading, run } = useRequest(queryWskhUserinfoDetail, {
    manual: true,
    onSuccess: ((res) => {
        res.data.cardnum = decryptAES(res.data.cardnum)
        formData.value = res.data
        uploadFiles.cardfrontphotourl = parseFilePaths(res.data.cardfrontphotourl)
        uploadFiles.cardbackphotourl = parseFilePaths(res.data.cardbackphotourl)
        uploadFiles.legalcardfrontphotourl = parseFilePaths(res.data.legalcardfrontphotourl)
        uploadFiles.legalcardbackphotourl = parseFilePaths(res.data.legalcardbackphotourl)
        uploadFiles.attachment1 = parseFilePaths(res.data.attachment1)
        uploadFiles.attachment2 = parseFilePaths(res.data.attachment2)
        referralRef.value.onReset(res.data.referral)
    }),
    onError: (err) => {
        ElMessage.error(err)
    }
})

// 表单验证规则
const formRules: FormRules = {
    cardtype: [{ required: true }],
    username: [{ required: true }],
    customername: [{ required: true }],
    cardnum: [{ required: true }],
}

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

const onSubmit = () => {
    const rawData = { ...formData.value }
    rawData.userstate = 2
    rawData.cardfrontphotourl = extractFilePaths(uploadFiles.cardfrontphotourl)
    rawData.cardbackphotourl = extractFilePaths(uploadFiles.cardbackphotourl)
    rawData.legalcardfrontphotourl = extractFilePaths(uploadFiles.legalcardfrontphotourl)
    rawData.legalcardbackphotourl = extractFilePaths(uploadFiles.legalcardbackphotourl)
    rawData.attachment1 = extractFilePaths(uploadFiles.attachment1)
    rawData.attachment2 = extractFilePaths(uploadFiles.attachment2)

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

onMounted(() => {
    const userId = props.record?.userid
    if (userId) {
        run({ userId })
    }
})
</script>