<template>
  <div class="app-container">
    <my-form ref="form" v-bind="formProps" :disabled="type === 'detail'" style="width: 710px" />
    <el-button
      v-if="type === 'create' || type === 'update'"
      class="create-btn"
      type="primary"
      round
      @click="handerCreate"
    >{{ type === 'create' ? '创建' : '修改' }}</el-button>
  </div>
</template>

<script>
import RouteParams from '@/mixins/routeParams'
import MyForm from '@/quickComponents/coreCmp/MyForm.vue'
import { createFields, businessInfoFields } from '@/define/question'
import { add, info } from '@/api/question'
export default {
  components: { MyForm },
  mixins: [RouteParams],
  data() {
    return {
      type: '',
      parentType: '',
      formProps: {
        fields: createFields,
        defaultValue: {}
      },
      businessInfoFormProps: {
        fields: businessInfoFields,
        defaultValue: {}
      },
      showInfo: false,
      showCreate: false,
      disabled: false
    }
  },
  mounted() {
    console.log(this)
    this.initData()
  },
  methods: {
    initData() {
      const { path } = this.$route
      const pathArr = path.split('/')
      const type = pathArr[pathArr.length - 1]
      this.type = type
      if (type !== 'create') {
        // this.getInfo(query.id)
        this.formProps.defaultValue = this.$params
      }
    },
    getInfo(id) {
      info({ id }).then(res => {
        this.formProps.defaultValue = res.d
      })
    },
    handerCreate() {
      const form = this.$refs['form']
      form.validate((vaild) => {
        if (vaild) {
          const formData = form.getFormData()
          add({
            ...formData,
          }).then((res) => {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            this.$router.back()
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.info-title{
  margin-top: 40px;
  margin-left: 40px;
}
.create-btn{
  margin-top: 20px;
  margin-bottom: 120px;
  margin-left: 240px;
}
</style>
