<template>
    <Modal :class="'w-[550px]'" :title="t('updateAlipay')">
        <BaseForm />

            <el-form-item :label="t('appId')" prop="config.app_id">
                <el-input v-model.trim="formData.config.app_id" :placeholder="t('appIdPlaceholder')" class="input-width" maxlength="32" show-word-limit clearable />
                <div class="form-tip">{{ t('appIdTips') }}</div>
            </el-form-item>
            <el-form-item :label="t('appSecretCert')" prop="config.app_secret_cert">
                <el-input v-model.trim="formData.config.app_secret_cert" :placeholder="t('appSecretCertPlaceholder')" class="input-width" type="textarea" rows="4" clearable />
            </el-form-item>

            <el-form-item :label="t('appPublicCertPath')">
                <div class="input-width">
                    <upload-file v-model.trim="formModel.config.app_public_cert_path" api="sys/document/aliyun" />
                </div>
            </el-form-item>

            <el-form-item :label="t('alipayPublicCertPath')">
                <div class="input-width">
                    <upload-file v-model="formModel.config.alipay_public_cert_path" api="sys/document/aliyun" />
                </div>
            </el-form-item>

            <el-form-item :label="t('alipayRootCertPath')">
                <div class="input-width">
                    <upload-file v-model="formModel.config.alipay_root_cert_path" api="sys/document/aliyun" />
                </div>
            </el-form-item>

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

<script lang="ts" setup>
import { ref, reactive } from 'vue'
import { t } from '@/lang'
import Test from '@/utils/test'
import { cloneDeep } from 'lodash-es'
import { useVbenModal } from '@vben/common-ui'
import { useVbenForm } from '@/_env/adapter/form'

const [Modal, modalApi] = useVbenModal()
const loading = ref(true)
const initData = ref<any>(null)
/**
 * 表单数据
 */
const initialFormData = {
    type: 'alipay',
    app_id: '',
    config: {
        app_secret_cert: '',
        app_public_cert_path: '',
        alipay_public_cert_path: '',
        alipay_root_cert_path: ''
    },
    channel: '',
    status: 0,
    is_default: 0
}
const formModel: Record<string, any> = reactive({ ...initialFormData })
const [BaseForm, formApi] = useVbenForm({
  commonConfig: { componentProps: { class: 'w-full' } },
  handleSubmit: async (values) => {
    loading.value = true
    const merged = { ...values, config: { ...formModel.config } }
    emit('complete', merged)
    loading.value = false
    modalApi.close()
  },
  layout: 'horizontal',
  schema: [
    { component: 'Input', fieldName: 'config.app_id', label: t('appId'), rules: [{ required: true }] },
    { component: 'Textarea', fieldName: 'config.app_secret_cert', label: t('appSecretCert'), rules: [{ required: true }], componentProps: { rows: 4 } }
  ],
  wrapperClass: 'grid-cols-1'
})

const emit = defineEmits(['complete'])

/**
 * 确认
 * @param formEl
 */
const confirm = async () => { if (loading.value) return; formApi.submit() }

const cancel = () => {
    Object.assign(formModel, initialFormData)
    if (initData.value) {
        Object.keys(formModel).forEach((key: string) => {
            if (initData.value[key] != undefined) formModel[key] = initData.value[key]
        })
        formModel.channel = initData.value.redio_key.split('_')[0]
        formModel.status = Number(formModel.status)
    }
    emit('complete', formModel)
    modalApi.close()
}
const setFormData = async (data: any = null) => {
    initData.value = cloneDeep(data)
    loading.value = true
    Object.assign(formModel, initialFormData)
    if (data) {
        Object.keys(formModel).forEach((key: string) => {
            if (data[key] != undefined) formModel[key] = data[key]
        })
        formModel.channel = data.redio_key.split('_')[0]
        formModel.status = Number(formModel.status)
    }
    formApi.setModel({ ...formModel })
    loading.value = false
    modalApi.open()
}

const enableVerify = () => {
    let verify = true
    if (Test.empty(formModel.config.app_id) || Test.empty(formModel.config.app_secret_cert) || Test.empty(formModel.config.app_public_cert_path) || Test.empty(formModel.config.alipay_public_cert_path) || Test.empty(formModel.config.alipay_root_cert_path)) verify = false
    return verify
}

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

<style lang="scss" scoped></style>
