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

            <el-form-item :label="t('collectionName')" prop="config.collection_name">
                <el-input v-model.trim="formData.config.collection_name" :placeholder="t('collectionNamePlaceholder')" class="input-width" show-word-limit clearable />
            </el-form-item>

            <el-form-item :label="t('collectionBank')" prop="config.collection_bank">
                <el-input v-model.trim="formData.config.collection_bank" :placeholder="t('collectionBankPlaceholder')" class="input-width" clearable />
            </el-form-item>

            <el-form-item :label="t('collectionAccount')" prop="config.collection_account">
                <el-input v-model.trim="formData.config.collection_account" :placeholder="t('collectionAccountPlaceholder')" class="input-width" clearable />
            </el-form-item>

            <el-form-item :label="t('collectionDesc')" prop="config.collection_desc">
                <el-input v-model.trim="formData.config.collection_desc" :placeholder="t('collectionDescPlaceholder')" class="input-width" type="textarea" rows="4" clearable />
            </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 { useVbenModal } from '@vben/common-ui'
import { useVbenForm } from '@/_env/adapter/form'

const [Modal, modalApi] = useVbenModal()
const loading = ref(true)

/**
 * 表单数据
 */
const initialFormData = {
    type: 'offlinepay',
    config: {
        collection_name: '',
        collection_bank: '',
        collection_account: '',
        collection_desc: ''
    },
    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 }
    emit('complete', merged)
    loading.value = false
    modalApi.close()
  },
  layout: 'horizontal',
  schema: [
    { component: 'Input', fieldName: 'config.collection_name', label: t('collectionName'), rules: [{ required: true }] },
    { component: 'Input', fieldName: 'config.collection_bank', label: t('collectionBank'), rules: [{ required: true }] },
    { component: 'Input', fieldName: 'config.collection_account', label: t('collectionAccount'), rules: [{ required: true }] },
    { component: 'Textarea', fieldName: 'config.collection_desc', label: t('collectionDesc'), 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 setFormData = async (data: any = null) => {
  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 cancel = () => { modalApi.close() }

defineExpose({ setFormData })
</script>

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