<template>
  <work-form-base ref="baseForm" :editable="editable" title="上报任务">
    <ui-form ref="form" :model="formData" :editable="editable" :rules="formRules" label-position="top">
      <ui-form-item prop="describes" label="问题描述">
        <ui-textarea v-model="formData.describes" :editable="editable" placeholder="请输入问题描述"></ui-textarea>
      </ui-form-item>
      <ui-form-item :label="editable ? '照片（0/3）' : '照片'" v-if="editable || (formData.picList && formData.picList.length)">
        <ui-upload
          v-model="formData.picList"
          :editable="editable"
          type="card"
        ></ui-upload>
      </ui-form-item>
      <ui-toolbar v-if="editable">
        <ui-button type="primary" :click="handleConfirmClick">确定</ui-button>
      </ui-toolbar>
      <template v-else>
        <bi-operate-log
          :data="operateLogData"
          :send-comment-method="handleSendComment"
        ></bi-operate-log>
      </template>
    </ui-form>
  </work-form-base>
</template>
<script>
import WorkFormBase from './report-work-form-base'
import { createWorkflowTask, createWorkflowComment } from '../apis'

export default {
  name: 'WorkFormTask',
  components: {
    WorkFormBase
  },
  props: {
    data: {
      type: Object
    },
    editable: {
      type: Boolean,
      default: true
    },
    success: {
      type: Function
    }
  },
  data() {
    return {
      formData: {
        describes: null, // 原因描述',
        picList: []
      }
    }
  },
  computed: {
    formRules() {
      return {
        describes: [{
          required: true,
          message: '问题描述不能为空',
          trigger: 'blur'
        }]
      }
    },
    operateLogData() {
      return (this.formData ? this.formData.operLogs || [] : []).map(v => {
        return {
          name: v.operatorName,
          content: v.operationContent,
          date: v.createTime
        }
      })
    }
  },
  watch: {
    data() {
      this.syncFormData()
    }
  },
  created() {
    this.syncFormData()
  },
  methods: {
    syncFormData() {
      if (this.data) {
        this.formData = JSON.parse(JSON.stringify(this.data))
      }
    },
    handleSendComment(evt) {
      return createWorkflowComment({
        id: this.formData.id,
        solveContent: evt.content
      }).then(() => {
        return Promise.all(this.success ? [this.success()] : null).finally(() => {
          this.$success('操作成功')
        })
      })
    },
    handleConfirmClick() {
      return Promise.all([
        this.$refs.baseForm.validate(),
        this.$refs.form.validate()
      ]).then(() => {
        const saveData = Object.assign({}, this.$refs.baseForm.getData(), this.formData)
        if (saveData.picList) {
          saveData.picList = saveData.picList.map(item => {
            return {
              realPath: item
            }
          })
        }
        return createWorkflowTask(saveData).then(() => {
          this.$success('上报成功')
          this.$emit('success')
        })
      })
    }
  }
}
</script>
