<template>
  <el-dialog :visible.sync="visible" :title="$t('deal')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
      <el-form-item label="发布人" prop="nickName">
        <p>{{dataForm.nickName}}</p>
      </el-form-item>
      <el-form-item label="发布时间" prop="distributeTime">
        <p>{{dataForm.distributeTime}}</p>
      </el-form-item>
      <el-form-item label="议题内容" prop="eventContent">
        <p>{{dataForm.issueContent}}</p>
      </el-form-item>
      <el-form-item label="工作反馈" prop="jobFeedback">
        <el-timeline :reverse="false">
          <el-timeline-item v-for="(jobFeedbackDTO, index) in dataForm.jobFeedbackDTOS" :key="index" :timestamp="jobFeedbackDTO.createdTime" placement="top">
            <el-card>
              <p>{{jobFeedbackDTO.stateName}}{{jobFeedbackDTO.createdTime}}</p>
              <p>{{jobFeedbackDTO.advice}}</p>
            </el-card>
          </el-timeline-item>
        </el-timeline>
      </el-form-item>
      <el-form-item label="处理意见" prop="advice">
          <el-input v-model="dataForm.advice" placeholder="不超过50字"></el-input>
      </el-form-item>
      <el-form-item label="操作" prop="state">
        <el-select v-model="dataForm.state" placeholder="请选择">
          <el-option
            v-for="item in verifyOptions"
            :key="item.verifyFlag"
            :label="item.verifyTitle"
            :value="item.verifyFlag">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        issueContent: '',
        nickName: '',
        distributeTime: '',
        advice: '',
        state: '',
        jobFeedbackDTOS: []
      },
      verifyOptions: [{
        verifyFlag: '1',
        verifyTitle: '添加处理意见'
      }, {
        verifyFlag: '2',
        verifyTitle: '关闭'
      }, {
        verifyFlag: '4',
        verifyTitle: '转项目'
      }]
    }
  },
  computed: {
    dataRule () {
      return {
        advice: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        eventState: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/events/issue/waitHandleIssueDetail/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http['post'](
          '/events/issue/issueWaitHandleSubmit', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
