<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="name">
          <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
      </el-form-item>
          <el-form-item label="工作单位" prop="employer">
          <el-input v-model="dataForm.employer" placeholder="工作单位"></el-input>
      </el-form-item>
          <el-form-item label="职称" prop="job">
          <el-input v-model="dataForm.job" placeholder="职称"></el-input>
      </el-form-item>
          <el-form-item label="邮箱" prop="email">
          <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
      </el-form-item>
          <el-form-item label="会员" prop="member">
          <el-input v-model="dataForm.member" placeholder="会员"></el-input>
      </el-form-item>
          <el-form-item label="关系会员名称" prop="withMember">
          <el-input v-model="dataForm.withMember" placeholder="关系会员名称"></el-input>
      </el-form-item>
          <el-form-item label="与会员关系" prop="withRelationship">
          <el-input v-model="dataForm.withRelationship" placeholder="与会员关系"></el-input>
      </el-form-item>
          <el-form-item label="手机号" prop="phone">
          <el-input v-model="dataForm.phone" placeholder="手机号"></el-input>
      </el-form-item>
          <el-form-item label="身高" prop="height">
          <el-input v-model="dataForm.height" placeholder="身高"></el-input>
      </el-form-item>
          <el-form-item label="体重" prop="weight">
          <el-input v-model="dataForm.weight" placeholder="体重"></el-input>
      </el-form-item>
          <el-form-item label="衣服型号" prop="ctModel">
          <el-input v-model="dataForm.ctModel" placeholder="衣服型号"></el-input>
      </el-form-item>
          <el-form-item label="就餐" prop="dinner">
          <el-input v-model="dataForm.dinner" placeholder="就餐"></el-input>
      </el-form-item>
          <el-form-item label="创建时间" prop="createTime">
          <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
      </el-form-item>
          <el-form-item label="修改时间" prop="updateTime">
          <el-input v-model="dataForm.updateTime" 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: '',
        name: '',
        employer: '',
        job: '',
        email: '',
        member: '',
        withMember: '',
        withRelationship: '',
        phone: '',
        height: '',
        weight: '',
        ctModel: '',
        dinner: '',
        createTime: '',
        updateTime: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        name: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        employer: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        job: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        email: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        member: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        withMember: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        withRelationship: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        phone: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        height: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        weight: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        ctModel: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        dinner: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        createTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updateTime: [
          { 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(`/competition/wluploadinfo/${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']('/competition/wluploadinfo/', 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>
