<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 (customer.id)" prop="customerId">
          <el-input v-model="dataForm.customerId" placeholder="客户Id (customer.id)"></el-input>
      </el-form-item>
          <el-form-item label="党组织的上级ID,没有上级时为0" prop="orgPid">
          <el-input v-model="dataForm.orgPid" placeholder="党组织的上级ID,没有上级时为0"></el-input>
      </el-form-item>
          <el-form-item label="党组织的所有上级ID,没有上级时为0" prop="orgPids">
          <el-input v-model="dataForm.orgPids" placeholder="党组织的所有上级ID,没有上级时为0"></el-input>
      </el-form-item>
          <el-form-item label="行政组织 机关ID" prop="agencyId">
          <el-input v-model="dataForm.agencyId" placeholder="行政组织 机关ID"></el-input>
      </el-form-item>
          <el-form-item label="行政组织 机关ID" prop="agencyPids">
          <el-input v-model="dataForm.agencyPids" placeholder="行政组织 机关ID"></el-input>
      </el-form-item>
          <el-form-item label="党组织类型 0省委,1市委,2区委,3党工委,4党委,5支部" prop="partyOrgType">
          <el-input v-model="dataForm.partyOrgType" placeholder="党组织类型 0省委,1市委,2区委,3党工委,4党委,5支部"></el-input>
      </el-form-item>
          <el-form-item label="组织名称" prop="partyOrgName">
          <el-input v-model="dataForm.partyOrgName" placeholder="组织名称"></el-input>
      </el-form-item>
          <el-form-item label="组织编码" prop="partyOrgCode">
          <el-input v-model="dataForm.partyOrgCode" placeholder="组织编码"></el-input>
      </el-form-item>
          <el-form-item label="经度" prop="longitude">
          <el-input v-model="dataForm.longitude" placeholder="经度"></el-input>
      </el-form-item>
          <el-form-item label="纬度" prop="latitude">
          <el-input v-model="dataForm.latitude" placeholder="纬度"></el-input>
      </el-form-item>
          <el-form-item label="地址" prop="address">
          <el-input v-model="dataForm.address" placeholder="地址"></el-input>
      </el-form-item>
          <el-form-item label="党组织介绍" prop="introduction">
          <el-input v-model="dataForm.introduction" 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>
    <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: '',
        customerId: '',
        orgPid: '',
        orgPids: '',
        agencyId: '',
        agencyPids: '',
        partyOrgType: '',
        partyOrgName: '',
        partyOrgCode: '',
        longitude: '',
        latitude: '',
        address: '',
        introduction: '',
        delFlag: '',
        revision: '',
        createdBy: '',
        createdTime: '',
        updatedBy: '',
        updatedTime: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        customerId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        orgPid: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        orgPids: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        agencyId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        agencyPids: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        partyOrgType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        partyOrgName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        partyOrgCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        longitude: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        latitude: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        address: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        introduction: [
          { 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(`/${moduleName}/${pathName}/#[[${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']('/${moduleName}/${pathName}/', 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>
