<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="标题" prop="title">
          <el-input v-model="dataForm.title" placeholder="标题"></el-input>
      </el-form-item>
          <el-form-item label="活动头图" prop="headPic">
          <el-input v-model="dataForm.headPic" placeholder="活动头图"></el-input>
      </el-form-item>
          <el-form-item label="报名开始时间" prop="signupStartTime">
          <el-input v-model="dataForm.signupStartTime" placeholder="报名开始时间"></el-input>
      </el-form-item>
          <el-form-item label="报名截止时间" prop="signupEndTime">
          <el-input v-model="dataForm.signupEndTime" placeholder="报名截止时间"></el-input>
      </el-form-item>
          <el-form-item label="活动开始时间" prop="actStartTime">
          <el-input v-model="dataForm.actStartTime" placeholder="活动开始时间"></el-input>
      </el-form-item>
          <el-form-item label="活动结束时间" prop="actEndTime">
          <el-input v-model="dataForm.actEndTime" placeholder="活动结束时间"></el-input>
      </el-form-item>
          <el-form-item label="活动地点" prop="actAddress">
          <el-input v-model="dataForm.actAddress" placeholder="活动地点"></el-input>
      </el-form-item>
          <el-form-item label="活动位置经度" prop="actLongitude">
          <el-input v-model="dataForm.actLongitude" placeholder="活动位置经度"></el-input>
      </el-form-item>
          <el-form-item label="活动位置纬度" prop="actLatitude">
          <el-input v-model="dataForm.actLatitude" placeholder="活动位置纬度"></el-input>
      </el-form-item>
          <el-form-item label="活动签到打卡地点" prop="signinAddress">
          <el-input v-model="dataForm.signinAddress" placeholder="活动签到打卡地点"></el-input>
      </el-form-item>
          <el-form-item label="活动签到打卡位置经度" prop="signinLongitude">
          <el-input v-model="dataForm.signinLongitude" placeholder="活动签到打卡位置经度"></el-input>
      </el-form-item>
          <el-form-item label="活动签到打卡位置纬度" prop="signinLatitude">
          <el-input v-model="dataForm.signinLatitude" placeholder="活动签到打卡位置纬度"></el-input>
      </el-form-item>
          <el-form-item label="活动签到打卡半径(单位：米)" prop="clockRadius">
          <el-input v-model="dataForm.clockRadius" placeholder="活动签到打卡半径(单位：米)"></el-input>
      </el-form-item>
          <el-form-item label="活动名额" prop="actQuota">
          <el-input v-model="dataForm.actQuota" placeholder="活动名额"></el-input>
      </el-form-item>
          <el-form-item label="已报名人数" prop="signupNum">
          <el-input v-model="dataForm.signupNum" placeholder="已报名人数"></el-input>
      </el-form-item>
          <el-form-item label="活动打卡人数" prop="clockNum">
          <el-input v-model="dataForm.clockNum" placeholder="活动打卡人数"></el-input>
      </el-form-item>
          <el-form-item label="活动分享数" prop="actShareNum">
          <el-input v-model="dataForm.actShareNum" placeholder="活动分享数"></el-input>
      </el-form-item>
          <el-form-item label="活动浏览数" prop="actBrowseNum">
          <el-input v-model="dataForm.actBrowseNum" placeholder="活动浏览数"></el-input>
      </el-form-item>
          <el-form-item label="联系人" prop="contacts">
          <el-input v-model="dataForm.contacts" placeholder="联系人"></el-input>
      </el-form-item>
          <el-form-item label="联系电话" prop="tel">
          <el-input v-model="dataForm.tel" placeholder="联系电话"></el-input>
      </el-form-item>
          <el-form-item label="招募要求" prop="requirement">
          <el-input v-model="dataForm.requirement" placeholder="招募要求"></el-input>
      </el-form-item>
          <el-form-item label="活动内容" prop="actContent">
          <el-input v-model="dataForm.actContent" placeholder="活动内容"></el-input>
      </el-form-item>
          <el-form-item label="活动发布状态（0-下架，1上架）" prop="actStatus">
          <el-input v-model="dataForm.actStatus" placeholder="活动发布状态（0-下架，1上架）"></el-input>
      </el-form-item>
          <el-form-item label="活动发布时间" prop="publishTime">
          <el-input v-model="dataForm.publishTime" placeholder="活动发布时间"></el-input>
      </el-form-item>
          <el-form-item label="活动主办方" prop="sponsor">
          <el-input v-model="dataForm.sponsor" placeholder="活动主办方"></el-input>
      </el-form-item>
          <el-form-item label="创建部门ID" prop="deptId">
          <el-input v-model="dataForm.deptId" placeholder="创建部门ID"></el-input>
      </el-form-item>
          <el-form-item label="活动惩罚积分" prop="punishmentPoints">
          <el-input v-model="dataForm.punishmentPoints" placeholder="活动惩罚积分"></el-input>
      </el-form-item>
          <el-form-item label="活动奖励积分" prop="reward">
          <el-input v-model="dataForm.reward" placeholder="活动奖励积分"></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: '',
        title: '',
        headPic: '',
        signupStartTime: '',
        signupEndTime: '',
        actStartTime: '',
        actEndTime: '',
        actAddress: '',
        actLongitude: '',
        actLatitude: '',
        signinAddress: '',
        signinLongitude: '',
        signinLatitude: '',
        clockRadius: '',
        actQuota: '',
        signupNum: '',
        clockNum: '',
        actShareNum: '',
        actBrowseNum: '',
        contacts: '',
        tel: '',
        requirement: '',
        actContent: '',
        actStatus: '',
        publishTime: '',
        sponsor: '',
        deptId: '',
        punishmentPoints: '',
        reward: '',
        revision: '',
        createdBy: '',
        createdTime: '',
        updatedBy: '',
        updatedTime: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        title: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        headPic: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        signupStartTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        signupEndTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        actStartTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        actEndTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        actAddress: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        actLongitude: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        actLatitude: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        signinAddress: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        signinLongitude: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        signinLatitude: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        clockRadius: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        actQuota: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        signupNum: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        clockNum: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        actShareNum: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        actBrowseNum: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        contacts: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        tel: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        requirement: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        actContent: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        actStatus: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        publishTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        sponsor: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        deptId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        punishmentPoints: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        reward: [
          { 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(`/heart/actinfo/${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']('/heart/actinfo/', 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>
