<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="groupName">
          <el-input v-model="dataForm.groupName" placeholder="社群名称"></el-input>
      </el-form-item>
          <el-form-item label="社群头像" prop="groupAvatar">
          <el-input v-model="dataForm.groupAvatar" placeholder="社群头像"></el-input>
      </el-form-item>
          <el-form-item label="社群介绍" prop="groupIntroduction">
          <el-input v-model="dataForm.groupIntroduction" placeholder="社群介绍"></el-input>
      </el-form-item>
          <el-form-item label="社群类别 0：党员群，1：自建群" prop="groupCategory">
          <el-input v-model="dataForm.groupCategory" placeholder="社群类别 0：党员群，1：自建群"></el-input>
      </el-form-item>
          <el-form-item label="物业项目ID" prop="propertyProjectId">
          <el-input v-model="dataForm.propertyProjectId" placeholder="物业项目ID"></el-input>
      </el-form-item>
          <el-form-item label="网格" prop="deptName">
          <el-input v-model="dataForm.deptName" 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="状态 0：待审核，5：审核不通过，10：审核通过，15：禁言，20：已解散" prop="state">
          <el-input v-model="dataForm.state" placeholder="状态 0：待审核，5：审核不通过，10：审核通过，15：禁言，20：已解散"></el-input>
      </el-form-item>
          <el-form-item label="处理意见" prop="processingOpinions">
          <el-input v-model="dataForm.processingOpinions" placeholder="处理意见"></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-item label="所有部门ID" prop="allDeptIds">
          <el-input v-model="dataForm.allDeptIds" placeholder="所有部门ID"></el-input>
      </el-form-item>
          <el-form-item label="所有部门名称" prop="allDeptNames">
          <el-input v-model="dataForm.allDeptNames" placeholder="所有部门名称"></el-input>
      </el-form-item>
          <el-form-item label="父所有部门" prop="parentDeptIds">
          <el-input v-model="dataForm.parentDeptIds" placeholder="父所有部门"></el-input>
      </el-form-item>
          <el-form-item label="父所有部门" prop="parentDeptNames">
          <el-input v-model="dataForm.parentDeptNames" placeholder="父所有部门"></el-input>
      </el-form-item>
          <el-form-item label="关联业务id 如:物业项目ID" prop="referenceBusinessId">
          <el-input v-model="dataForm.referenceBusinessId" placeholder="关联业务id 如:物业项目ID"></el-input>
      </el-form-item>
          <el-form-item label="所属版块编码" prop="sectionCode">
          <el-input v-model="dataForm.sectionCode" placeholder="所属版块编码"></el-input>
      </el-form-item>
          <el-form-item label="社群类别编码" prop="categoryCode">
          <el-input v-model="dataForm.categoryCode" placeholder="社群类别编码"></el-input>
      </el-form-item>
          <el-form-item label="社区部门类别" prop="typeKey">
          <el-input v-model="dataForm.typeKey" 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: '',
        groupName: '',
        groupAvatar: '',
        groupIntroduction: '',
        groupCategory: '',
        propertyProjectId: '',
        deptName: '',
        deptId: '',
        state: '',
        processingOpinions: '',
        delFlag: '',
        revision: '',
        createdBy: '',
        createdTime: '',
        updatedBy: '',
        updatedTime: '',
        allDeptIds: '',
        allDeptNames: '',
        parentDeptIds: '',
        parentDeptNames: '',
        referenceBusinessId: '',
        sectionCode: '',
        categoryCode: '',
        typeKey: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        groupName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        groupAvatar: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        groupIntroduction: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        groupCategory: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        propertyProjectId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        deptName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        deptId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        state: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        processingOpinions: [
          { 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' }
        ],
        allDeptIds: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        allDeptNames: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        parentDeptIds: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        parentDeptNames: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        referenceBusinessId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        sectionCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        categoryCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        typeKey: [
          { 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(`//group/${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']('//group/', 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>
