<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :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="事件ID" prop="eventId">
          <el-input v-model="dataForm.eventId" placeholder="事件ID"></el-input>
      </el-form-item>
          <el-form-item label="议题ID" prop="issueId">
          <el-input v-model="dataForm.issueId" placeholder="议题ID"></el-input>
      </el-form-item>
          <el-form-item label="用户ID" prop="userId">
          <el-input v-model="dataForm.userId" placeholder="用户ID"></el-input>
      </el-form-item>
          <el-form-item label="用户昵称" prop="nickName">
          <el-input v-model="dataForm.nickName" placeholder="用户昵称"></el-input>
      </el-form-item>
          <el-form-item label="用户头像" prop="userFace">
          <el-input v-model="dataForm.userFace" placeholder="用户头像"></el-input>
      </el-form-item>
          <el-form-item label="议题内容" prop="issueContent">
          <el-input v-model="dataForm.issueContent" placeholder="议题内容"></el-input>
      </el-form-item>
          <el-form-item label="区" prop="area">
          <el-input v-model="dataForm.area" placeholder="区"></el-input>
      </el-form-item>
          <el-form-item label="区ID" prop="areaId">
          <el-input v-model="dataForm.areaId" placeholder="区ID"></el-input>
      </el-form-item>
          <el-form-item label="街道" prop="street">
          <el-input v-model="dataForm.street" placeholder="街道"></el-input>
      </el-form-item>
          <el-form-item label="街道ID" prop="streetId">
          <el-input v-model="dataForm.streetId" placeholder="街道ID"></el-input>
      </el-form-item>
          <el-form-item label="社区" prop="community">
          <el-input v-model="dataForm.community" placeholder="社区"></el-input>
      </el-form-item>
          <el-form-item label="社区ID" prop="communityId">
          <el-input v-model="dataForm.communityId" placeholder="社区ID"></el-input>
      </el-form-item>
          <el-form-item label="网格" prop="grid">
          <el-input v-model="dataForm.grid" placeholder="网格"></el-input>
      </el-form-item>
          <el-form-item label="网格ID" prop="gridId">
          <el-input v-model="dataForm.gridId" placeholder="网格ID"></el-input>
      </el-form-item>
          <el-form-item label="议题位置地址" prop="issueAddress">
          <el-input v-model="dataForm.issueAddress" placeholder="议题位置地址"></el-input>
      </el-form-item>
          <el-form-item label="议题分类ID" prop="issueCategoryId">
          <el-input v-model="dataForm.issueCategoryId" placeholder="议题分类ID"></el-input>
      </el-form-item>
          <el-form-item label="议题位置纬度" prop="issusLatitude">
          <el-input v-model="dataForm.issusLatitude" placeholder="议题位置纬度"></el-input>
      </el-form-item>
          <el-form-item label="议题位置经度" prop="issueLongitude">
          <el-input v-model="dataForm.issueLongitude" placeholder="议题位置经度"></el-input>
      </el-form-item>
          <el-form-item label="友邻社区ID" prop="friendlyCommunityId">
          <el-input v-model="dataForm.friendlyCommunityId" placeholder="友邻社区ID"></el-input>
      </el-form-item>
          <el-form-item label="评价打分" prop="issueScore">
          <el-input v-model="dataForm.issueScore" placeholder="评价打分"></el-input>
      </el-form-item>
          <el-form-item label="项目状态 0-待网格长处理，5-待社区处理，10-待街道党建办处理，15-待街道处理，20-待区直部门党建办处理，25-待区直部门处理，30-处理，35-关闭，40-结束议题" prop="itemState">
          <el-input v-model="dataForm.itemState" placeholder="项目状态 0-待网格长处理，5-待社区处理，10-待街道党建办处理，15-待街道处理，20-待区直部门党建办处理，25-待区直部门处理，30-处理，35-关闭，40-结束议题"></el-input>
      </el-form-item>
          <el-form-item label="删除标识 0：未删除，1：已删除" prop="delFlag">
          <el-input v-model="dataForm.delFlag" placeholder="删除标识 0：未删除，1：已删除"></el-input>
      </el-form-item>
          <el-form-item label="乐观锁" prop="revision">
          <el-input v-model="dataForm.revision" placeholder="乐观锁"></el-input>
      </el-form-item>
          <el-form-item label="创建人" prop="createdBy">
          <el-input v-model="dataForm.createdBy" placeholder="创建人"></el-input>
      </el-form-item>
          <el-form-item label="创建时间" prop="createdTime">
          <el-input v-model="dataForm.createdTime" placeholder="创建时间"></el-input>
      </el-form-item>
          <el-form-item label="更新人" prop="updatedBy">
          <el-input v-model="dataForm.updatedBy" placeholder="更新人"></el-input>
      </el-form-item>
          <el-form-item label="更新时间" prop="updatedTime">
          <el-input v-model="dataForm.updatedTime" placeholder="更新时间"></el-input>
      </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: '',
        eventId: '',
        issueId: '',
        userId: '',
        nickName: '',
        userFace: '',
        issueContent: '',
        area: '',
        areaId: '',
        street: '',
        streetId: '',
        community: '',
        communityId: '',
        grid: '',
        gridId: '',
        issueAddress: '',
        issueCategoryId: '',
        issusLatitude: '',
        issueLongitude: '',
        friendlyCommunityId: '',
        issueScore: '',
        itemState: '',
        delFlag: '',
        revision: '',
        createdBy: '',
        createdTime: '',
        updatedBy: '',
        updatedTime: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        eventId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        issueId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        userId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        nickName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        userFace: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        issueContent: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        area: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        areaId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        street: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        streetId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        community: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        communityId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        grid: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        gridId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        issueAddress: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        issueCategoryId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        issusLatitude: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        issueLongitude: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        friendlyCommunityId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        issueScore: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        itemState: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        delFlag: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        revision: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        createdBy: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        createdTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updatedBy: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updatedTime: [
          { 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/item/${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[!this.dataForm.id ? 'post' : 'put']('/events/item/', 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>
