<template>
  <div class="topForm">
    <parser :form-conf="formData" :relation-conf="[]" :idKey="idKey" @submit="sumbitForm" :key="key" ref="dynamicForm" v-if="!loading" />
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading"> {{ formData.confirmButtonText || '提 交' }}</el-button>
    </span>
  </div>
</template>

<script>
import { createModel, updateModel, getModelInfo } from '@/api/onlineDev/visualDev'
import Parser from '@/components/Generator/parser/Parser'
import { useDateList } from '@/components/Generator/generator/comConfig'
import { deepClone } from '@/utils'
import moment from 'moment'
export default {
  components: { Parser },
  name: 'topForm',
  data() {
    return {
      dataForm: {
        id: '',
        data: ''
      },
      formData: {},
      idKey: '',
      key: +new Date(),
      loading: true,
      btnLoading: false
    }
  },
  created() {},
  methods: {
    init(formData, modelId, isPreview, idKey) {
      this.formData = deepClone(formData)
      this.formData.popupType = 'topEntry'
      this.formData.fields.forEach((i, k) => {
        i.__config__.span = 8
      })
      this.idKey = idKey
      this.modelId = modelId
      this.isPreview = isPreview
      this.loading = true
      this.$nextTick(() => {
        this.dataForm.oldData = null
        this.formValue = {}
        this.visible = true
        this.loading = false
        this.key = +new Date()
      })
    },
    sumbitForm(data) {
      if (!data) return
      this.btnLoading = true
      this.dataForm.data = JSON.stringify(data)
      const formMethod = this.dataForm.id ? updateModel : createModel
      formMethod(this.modelId, this.dataForm)
        .then((res) => {
          this.$message({
            message: res.data.msg,
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.visible = false
              this.btnLoading = false
              let flagInit = this.dataForm.id ? true : false
              this.$emit('refreshDataList', this.dataForm.id, res.data.data)
            }
          })
        })
        .catch(() => {
          this.btnLoading = false
        })
    },
    dataFormSubmit() {
      if (this.isPreview) return this.$message({ message: '功能预览不支持数据保存', type: 'warning' })
      this.$refs.dynamicForm && this.$refs.dynamicForm.submitForm()
    }
  }
}
</script>

<style lang="scss" scoped>
.topForm {
  background: #fff;
  margin-bottom: 10px;
  padding: 15px 10px 0;
}
</style>
