<template>

  <ele-form
    :isShowSubmitBtn=true
    :isShowBackBtn=false
    :isShowResetBtn=true
    :rules="formObj.rules"
    :form-desc="formObj.formDesc"
    v-model="formObj.formData"
    :request-fn="handleRequest"
    @request-success="handleRequestSuccess"
  >
  </ele-form>
</template>
<script>
  import { showForms, commit } from '@/api/activiti/formStart.js'

  export default {
    name: 'FormStart',
    props: {
      instanceId: String
    },
    data() {
      return {
        // 表单数据
        formEntity: {
          fId: {},
          modelKey: {},
          modulId: {},
          fName: {},
          instanceId: {},
          fKey: {},
          fVersion: {},
          applyUser: {},
          jsonData: {}
        },
        //动态表单数据
        formObj: {
          // 表单数据
          formData: {},
          formDesc: {},
          rules: {}
        }
      }
    },
    // 初始化方法
    created() {
    },
    methods: {
      // 重置表单新增使用
      reset() {
        this.formEntity.fId = undefined
        this.formEntity.modelKey = undefined
        this.formEntity.modulId = undefined
        this.formEntity.fName = undefined
        this.formEntity.instanceId = undefined
        this.formEntity.fKey = undefined
        this.formEntity.fVersion = undefined
        this.formEntity.applyUser = undefined
        this.formEntity.jsonData = undefined
        this.formObj.formDesc = {}
        this.formObj.rules = {}
        this.formObj.formData = {}
      },
      showForms(processKey) {
        this.reset()
        showForms(processKey).then(
          response => {
            this.formEntity = response.data
            this.formEntity.fId=undefined
            let jsonData = response.data.jsonData
            let parse = JSON.parse(jsonData)
            this.formObj.formDesc = parse.formDesc
            this.formObj.rules = parse.rules
            let formData = parse.formData
            if (null != formData) {
              this.formObj.formData = parse.formData
            }
            this.open = true
            this.title = '提交申请'
          }
        )
      },
      handleRequest(data) {
        console.log(data)
        this.formObj.formData = data
        let s = JSON.stringify(this.formObj)
        this.formEntity.jsonData = s
        commit(this.formEntity).then(response => {
          if (response.code === 200) {
            this.msgSuccess(response.msg)
            //调用父组件关闭方法
            this.$emit('cancel');
          } else {
            this.msgError(response.msg)
          }
        })
        return Promise.resolve()
      },
      handleRequestSuccess() {
        this.$message.success('发送成功')
      }
    }
  }
</script>

