<!-- 会员机构管理-机构管理-机构资料管理-编辑 -->
<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('member.institution.user.edit.subtitle') }}</legend>
                <el-form-item :label="t('member.institution.user.edit.userid')">
                    {{ handleNoneValue(data?.rspResult.userid) }}
                </el-form-item>
                <el-form-item :label="t('member.institution.user.edit.userinfotype')" prop="userinfotype">
                    {{ data ? userinfotypeEnum.getEnumTypeName(data.userinfo.userinfotype) : handleNoneValue() }}
                </el-form-item>
                <el-form-item :label="t('member.institution.user.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.user.edit.usertype')">
                    {{ data ? usertype2Enum.getEnumTypeName(data.rspResult.usertype) : handleNoneValue() }}
                </el-form-item>
                <el-form-item :label="t('member.institution.user.edit.refereeuserid')" prop="refereeuserid">
                    <app-select-member ref="refereeuseridRef" v-model="formData.refereeuserid" />
                </el-form-item>
            </fieldset>
            <fieldset class="g-fieldset el-form--horizontal">
                <legend class="g-fieldset__legend">{{ isPerson ? t('member.institution.user.edit.person') :
                    t('member.institution.user.edit.enterprise') }} {{ t('member.institution.user.edit.legend') }}
                </legend>
                <el-form-item :label="t('member.institution.user.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.user.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.user.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.user.edit.cardtypeid')" prop="cardtypeid">
                    <app-enum code="certypecompany" v-model="formData.cardtypeid" v-if="!isPerson" />
                    <app-enum code="certypeperson" v-model="formData.cardtypeid" v-else />
                </el-form-item>
                <el-form-item :label="t('member.institution.user.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.user.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.user.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.user.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.user.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.user.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.user.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.user.edit.sex')" prop="sex">
                    <app-enum code="Sex" type="radio" v-model="formData.sex" />
                </el-form-item>
                <el-form-item :label="t('member.institution.user.edit.mobile')" prop="mobile">
                    <el-input type="number" v-model="formData.mobile" maxlength="50"
                        :placeholder="t('common.pleaseenter')" />
                </el-form-item>
                <el-form-item :label="t('member.institution.user.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.user.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.user.edit.address')"
                    prop="address">
                    <el-input v-model="formData.address" maxlength="50" :placeholder="t('common.pleaseenter')" />
                </el-form-item>
                <el-form-item :label="t('member.institution.user.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.user.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.user.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" v-if="data && data.rspResult.usertype !== 4">
                <legend class="g-fieldset__legend">{{ t('member.institution.user.edit.subtitle1') }}</legend>
                <el-form-item :label="t('member.institution.user.edit.username1')">
                    {{ data.users.username }}
                </el-form-item>
                <el-form-item :label="t('member.institution.user.edit.userinfotype1')" prop="userinfotype"
                    v-if="data.rspResult.usertype === 2">
                    {{ data.rspResult.reckonaccountid }}
                </el-form-item>
            </fieldset>
            <fieldset class="g-fieldset el-form--horizontal">
                <legend class="g-fieldset__legend">{{ t('member.institution.user.edit.attachment') }}</legend>
                <el-form-item :label="t('member.institution.user.edit.attachment1')" 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('member.institution.user.edit.attachment2')" 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" :disabled="!data" @click="onSubmit">{{ t('operation.save') }}</el-button>
        </template>
    </app-drawer>
</template>

<script lang="ts" setup>
import { ref, reactive, PropType, computed } from 'vue'
import { ElMessage, FormInstance, FormRules, UploadUserFile } from 'element-plus'
import { handleNoneValue, parseFilePaths, extractFilePaths } from '@/filters'
import { uploadConfig } from '@/constants/common'
import { useEnum } from '@/hooks/enum'
import { decryptAES } from '@/services/crypto'
import { useRequest } from '@/hooks/request'
import { editOrgan, queryOrganDetail } from '@/services/api/member'
import {  UserInfoType } from '@/constants/member'
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'
import { i18n } from '@/stores'

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


const usertype2Enum = useEnum('usertype2') // 机构类型
const userinfotypeEnum = useEnum('userinfotype') // 所有者类型

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

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

const formData = ref<Partial<Member.EditOrganReq>>({})

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 { data, loading } = useRequest(queryOrganDetail, {
    params: {
        areaId: props.record.userid
    },
    onSuccess: ((res) => {
        const { rspResult, userinfo, users } = res.data

        if (userinfo) {
            uploadFiles.cardfrontphotourl = parseFilePaths(userinfo.cardfrontphotourl)
            uploadFiles.cardbackphotourl = parseFilePaths(userinfo.cardbackphotourl)
            uploadFiles.legalcardfrontphotourl = parseFilePaths(userinfo.legalcardfrontphotourl)
            uploadFiles.legalcardbackphotourl = parseFilePaths(userinfo.legalcardbackphotourl)
            uploadFiles.attachment1 = parseFilePaths(userinfo.attachment1)
            uploadFiles.attachment2 = parseFilePaths(userinfo.attachment2)
        }

        formData.value = {
            accountname: rspResult.accountname,
            address: userinfo.address,
            attachment1: userinfo.attachment1,
            attachment2: userinfo.attachment2,
            autoid: users.autoid,
            biznature: userinfo.biznature,
            cardbackphotourl: userinfo.cardbackphotourl,
            cardfrontphotourl: userinfo.cardfrontphotourl,
            cardnum: decryptAES(userinfo.cardnum),
            cardtypeid: userinfo.cardtypeid,
            cityid: userinfo.cityid,
            company: userinfo.company,
            contactname: userinfo.contactname,
            customername: userinfo.customername,
            districtid: userinfo.districtid,
            email: decryptAES(userinfo.email),
            legalcardbackphotourl: userinfo.legalcardbackphotourl,
            legalcardfrontphotourl: userinfo.legalcardfrontphotourl,
            legalpersonname: userinfo.legalpersonname,
            logincode: userinfo.logincode,
            mobile: decryptAES(userinfo.mobile),
            modifystatus: rspResult.modifystatus,
            parentuserid: rspResult.parentuserid,
            postalcode: userinfo.postalcode,
            provinceid: userinfo.provinceid,
            qq: userinfo.qq,
            reckonaccountid: rspResult.reckonaccountid,
            refereeuserid: rspResult.refereeuserid,
            remark: userinfo.remark,
            sex: userinfo.sex,
            telphone: decryptAES(userinfo.telphone),
            userid: userinfo.userid,
            userinfotype: userinfo.userinfotype,
            username: users.username,
            usertype: userinfo.usertype,
            wechat: userinfo.wechat
        }

        refereeuseridRef.value.onReset(rspResult.refereeuserid)
    }),
    onError: (err) => {
        ElMessage.error(err)
    }
})

// 表单验证规则
const formRules: FormRules = {
    cardtypeid: [{ 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.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
            editOrgan({
                data: rawData
            }).then(() => {
                ElMessage.success(t('common.tips3'))
                onCancel(true)
            }).catch((err) => {
                ElMessage.error(t('common.tips4') + err)
            }).finally(() => {
                loading.value = false
            })
        }
    })
}
</script>