<template>
    <el-dialog v-model="showDialog" :title="formData.id ? t('updateConfig') : t('addConfig')" width="50%" class="diy-dialog-wrap"
        :destroy-on-close="true">
        <el-form :model="formData" label-width="120px" ref="formRef" :rules="formRules" class="page-form" v-loading="loading">
                <el-form-item :label="t('weChatPayAppid')" >
    <el-input v-model="formData.we_chat_pay_appid" clearable :placeholder="t('weChatPayAppidPlaceholder')" class="input-width" />
</el-form-item>

                <el-form-item :label="t('weChatPayAppId')" >
    <el-input v-model="formData.we_chat_pay_app_id" clearable :placeholder="t('weChatPayAppIdPlaceholder')" class="input-width" />
</el-form-item>

                <el-form-item :label="t('weChatPayMiniappId')" >
    <el-input v-model="formData.we_chat_pay_miniapp_id" clearable :placeholder="t('weChatPayMiniappIdPlaceholder')" class="input-width" />
</el-form-item>

                <el-form-item :label="t('weChatPayMchId')" >
    <el-input v-model="formData.we_chat_pay_mch_id" clearable :placeholder="t('weChatPayMchIdPlaceholder')" class="input-width" />
</el-form-item>

                <el-form-item :label="t('weChatPayKey')" >
    <el-input v-model="formData.we_chat_pay_key" clearable :placeholder="t('weChatPayKeyPlaceholder')" class="input-width" />
</el-form-item>

                <el-form-item :label="t('weChatPayMiniappSecret')" >
    <el-input v-model="formData.we_chat_pay_miniapp_secret" clearable :placeholder="t('weChatPayMiniappSecretPlaceholder')" class="input-width" />
</el-form-item>

                <el-form-item :label="t('weChatPayNotifyUrl')" prop="we_chat_pay_notify_url">
    <el-input v-model="formData.we_chat_pay_notify_url" clearable :placeholder="t('weChatPayNotifyUrlPlaceholder')" class="input-width" />
</el-form-item>

                <el-form-item :label="t('alipayAppId')" >
    <el-input v-model="formData.alipay_appId" clearable :placeholder="t('alipayAppIdPlaceholder')" class="input-width" />
</el-form-item>

                <el-form-item :label="t('alipayRsaPrivateKey')" >
    <el-input v-model="formData.alipay_rsa_private_key" clearable :placeholder="t('alipayRsaPrivateKeyPlaceholder')" class="input-width" />
</el-form-item>

                <el-form-item :label="t('alipayPublicKey')" >
    <el-input v-model="formData.alipay_public_key" clearable :placeholder="t('alipayPublicKeyPlaceholder')" class="input-width" />
</el-form-item>

                <el-form-item :label="t('alipayNotifyUrl')" prop="alipay_notify_url">
    <el-input v-model="formData.alipay_notify_url" clearable :placeholder="t('alipayNotifyUrlPlaceholder')" class="input-width" />
</el-form-item>

        </el-form>

        <template #footer>
            <span class="dialog-footer">
                <el-button @click="showDialog = false">{{ t('cancel') }}</el-button>
                <el-button type="primary" :loading="loading" @click="confirm(formRef)">{{
                    t('confirm')
                }}</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script lang="ts" setup>
import { ref, reactive, computed, watch } from 'vue'
import { useDictionary } from '@/app/api/dict'
import { t } from '@/lang'
import type { FormInstance } from 'element-plus'

import { addConfig, editConfig, getConfigInfo, } from '@/addon/hygl/api/config'

let showDialog = ref(false)
const loading = ref(false)

/**
 * 表单数据
 */
const initialFormData = {
    id: '',
    we_chat_pay_appid: '',
    we_chat_pay_app_id: '',
    we_chat_pay_miniapp_id: '',
    we_chat_pay_mch_id: '',
    we_chat_pay_key: '',
    we_chat_pay_miniapp_secret: '',
    we_chat_pay_notify_url: '',
    alipay_appId: '',
    alipay_rsa_private_key: '',
    alipay_public_key: '',
    alipay_notify_url: '',
}
const formData: Record<string, any> = reactive({ ...initialFormData })

const formRef = ref<FormInstance>()

// 表单验证规则
const formRules = computed(() => {
    return {
    we_chat_pay_appid: [
        { required: true, message: t('weChatPayAppidPlaceholder'), trigger: 'blur' },
        
    ]
,
    we_chat_pay_app_id: [
        { required: true, message: t('weChatPayAppIdPlaceholder'), trigger: 'blur' },
        
    ]
,
    we_chat_pay_miniapp_id: [
        { required: true, message: t('weChatPayMiniappIdPlaceholder'), trigger: 'blur' },
        
    ]
,
    we_chat_pay_mch_id: [
        { required: true, message: t('weChatPayMchIdPlaceholder'), trigger: 'blur' },
        
    ]
,
    we_chat_pay_key: [
        { required: true, message: t('weChatPayKeyPlaceholder'), trigger: 'blur' },
        
    ]
,
    we_chat_pay_miniapp_secret: [
        { required: true, message: t('weChatPayMiniappSecretPlaceholder'), trigger: 'blur' },
        
    ]
,
    we_chat_pay_notify_url: [
        { required: true, message: t('weChatPayNotifyUrlPlaceholder'), trigger: 'blur' },
        
    ]
,
    alipay_appId: [
        { required: true, message: t('alipayAppIdPlaceholder'), trigger: 'blur' },
        
    ]
,
    alipay_rsa_private_key: [
        { required: true, message: t('alipayRsaPrivateKeyPlaceholder'), trigger: 'blur' },
        
    ]
,
    alipay_public_key: [
        { required: true, message: t('alipayPublicKeyPlaceholder'), trigger: 'blur' },
        
    ]
,
    alipay_notify_url: [
        { required: true, message: t('alipayNotifyUrlPlaceholder'), trigger: 'blur' },
        
    ]
,
    }
})

const emit = defineEmits(['complete'])

/**
 * 确认
 * @param formEl
 */
const confirm = async (formEl: FormInstance | undefined) => {
    if (loading.value || !formEl) return
    let save = formData.id ? editConfig : addConfig

    await formEl.validate(async (valid) => {
        if (valid) {
            loading.value = true

            let data = formData

            save(data).then(res => {
                loading.value = false
                showDialog.value = false
                emit('complete')
            }).catch(err => {
                loading.value = false
            })
        }
    })
}

// 获取字典数据
    

    
const setFormData = async (row: any = null) => {
    Object.assign(formData, initialFormData)
    loading.value = true
    if(row){
        const data = await (await getConfigInfo(row.id)).data
        if (data) Object.keys(formData).forEach((key: string) => {
            if (data[key] != undefined) formData[key] = data[key]
        })
    }
    loading.value = false
}

// 验证手机号格式
const mobileVerify = (rule: any, value: any, callback: any) => {
    if (value && !/^1[3-9]\d{9}$/.test(value)) {
        callback(new Error(t('generateMobile')))
    } else {
        callback()
    }
}

// 验证身份证号
const idCardVerify = (rule: any, value: any, callback: any) => {
    if (value && !/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/.test(value)) {
        callback(new Error(t('generateIdCard')))
    } else {
        callback()
    }
}

// 验证邮箱号
const emailVerify = (rule: any, value: any, callback: any) => {
    if (value && !/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.test(value)) {
        callback(new Error(t('generateEmail')))
    } else {
        callback()
    }
}

// 验证请输入整数
const numberVerify = (rule: any, value: any, callback: any) => {
    if (!Number.isInteger(value)) {
        callback(new Error(t('generateNumber')))
    } else {
        callback()
    }
}

defineExpose({
    showDialog,
    setFormData
})
</script>

<style lang="scss" scoped></style>
<style lang="scss">
.diy-dialog-wrap .el-form-item__label{
    height: auto  !important;
}
</style>
