<template>
  <j-form-container :disabled='disabled'>
    <a-form-model ref='form' :model='model' :rules='validatorRules' slot='detail'>
      <a-row>
        <a-col :span='24'>
          <a-form-model-item label='回复内容' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='content'>
            <j-popup
              v-model='model.content'
              field='content'
              org-fields=''
              dest-fields=''
              code=''
              :multi='true'
              @input='popupCallback' />
          </a-form-model-item>
        </a-col>
        <a-col :span='24'>
          <a-form-model-item label='悄悄话id' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='whisperingsId'>
            <a-input v-model='model.whisperingsId' placeholder='请输入悄悄话id'></a-input>
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
  </j-form-container>
</template>
<script>
import { getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import { VALIDATE_NO_PASSED } from '@/utils/JEditableTableUtil'

export default {
  name: 'WhisperingsReplyForm',
  components: {},
  props: {
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      labelCol2: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol2: {
        xs: { span: 24 },
        sm: { span: 20 }
      },
      validatorRules: {
        content: [
          { required: true, message: '请输入回复内容!' }
        ]
      },
      confirmLoading: false
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    initFormData(url, id) {
      this.clearFormData()
      if (!id) {
        this.edit(this.modelDefault)
      } else {
        getAction(url, { id: id }).then(res => {
          if (res.success) {
            let records = res.result
            if (records && records.length > 0) {
              this.edit(records[0])
            }
          }
        })
      }
    },
    edit(record) {
      this.model = Object.assign({}, record)
      console.log('WhisperingsReplyForm-edit', this.model)
    },
    getFormData() {
      let formdata_arr = []
      this.$refs.form.validate(valid => {
        if (valid) {
          let isNullObj = true
          Object.keys(this.model).forEach(key => {
            if (this.model[key]) {
              isNullObj = false
            }
          })
          if (!isNullObj) {
            formdata_arr.push(this.model)
          }
        } else {
          this.$emit('validateError', '悄悄话回复表单校验未通过')
        }
      })
      return formdata_arr
    },
    validate(index) {
      return new Promise((resolve, reject) => {
        // 验证主表表单
        this.$refs.form.validate(valid => {
          !valid ? reject({ error: VALIDATE_NO_PASSED, index }) : resolve()
        })
      }).then(values => {
        return Promise.resolve()
      }).catch(error => {
        return Promise.reject(error)
      })
    },
    popupCallback(value, row) {
      this.model = Object.assign(this.model, row)
    },
    clearFormData() {
      this.$refs.form.clearValidate()
    }
  }
}
</script>
