<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.residentId ? $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="gridId">
          <el-input v-model="dataForm.gridId" placeholder="网格ID"></el-input>
      </el-form-item>
          <el-form-item label="人口性质" prop="residentProperty">
          <el-input v-model="dataForm.residentProperty" placeholder="人口性质"></el-input>
      </el-form-item>
          <el-form-item label="居民分类" prop="residentType">
          <el-input v-model="dataForm.residentType" placeholder="居民分类"></el-input>
      </el-form-item>
          <el-form-item label="证件类型" prop="idType">
          <el-input v-model="dataForm.idType" placeholder="证件类型"></el-input>
      </el-form-item>
          <el-form-item label="证件号码（公民身份证号）" prop="idCard">
          <el-input v-model="dataForm.idCard" placeholder="证件号码（公民身份证号）"></el-input>
      </el-form-item>
          <el-form-item label="姓名" prop="residentName">
          <el-input v-model="dataForm.residentName" placeholder="姓名"></el-input>
      </el-form-item>
          <el-form-item label="性别" prop="sex">
          <el-input v-model="dataForm.sex" placeholder="性别"></el-input>
      </el-form-item>
          <el-form-item label="出生日期" prop="birthday">
          <el-input v-model="dataForm.birthday" placeholder="出生日期"></el-input>
      </el-form-item>
          <el-form-item label="民族" prop="nation">
          <el-input v-model="dataForm.nation" placeholder="民族"></el-input>
      </el-form-item>
          <el-form-item label="联系方式" prop="telephone">
          <el-input v-model="dataForm.telephone" placeholder="联系方式"></el-input>
      </el-form-item>
          <el-form-item label="户籍省" prop="householdProv">
          <el-input v-model="dataForm.householdProv" placeholder="户籍省"></el-input>
      </el-form-item>
          <el-form-item label="户籍市" prop="householdCity">
          <el-input v-model="dataForm.householdCity" placeholder="户籍市"></el-input>
      </el-form-item>
          <el-form-item label="户籍县(区)" prop="householdCounty">
          <el-input v-model="dataForm.householdCounty" placeholder="户籍县(区)"></el-input>
      </el-form-item>
          <el-form-item label="户籍镇街" prop="householdTown">
          <el-input v-model="dataForm.householdTown" placeholder="户籍镇街"></el-input>
      </el-form-item>
          <el-form-item label="户籍社区/村" prop="householdVillage">
          <el-input v-model="dataForm.householdVillage" placeholder="户籍社区/村"></el-input>
      </el-form-item>
          <el-form-item label="户籍详址" prop="householdAddressDetail">
          <el-input v-model="dataForm.householdAddressDetail" placeholder="户籍详址"></el-input>
      </el-form-item>
          <el-form-item label="现住省" prop="curliveProv">
          <el-input v-model="dataForm.curliveProv" placeholder="现住省"></el-input>
      </el-form-item>
          <el-form-item label="现住市" prop="curliveCity">
          <el-input v-model="dataForm.curliveCity" placeholder="现住市"></el-input>
      </el-form-item>
          <el-form-item label="现住县(区)" prop="curliveCounty">
          <el-input v-model="dataForm.curliveCounty" placeholder="现住县(区)"></el-input>
      </el-form-item>
          <el-form-item label="现住镇街" prop="curliveTown">
          <el-input v-model="dataForm.curliveTown" placeholder="现住镇街"></el-input>
      </el-form-item>
          <el-form-item label="现住社区/村" prop="curliveVillage">
          <el-input v-model="dataForm.curliveVillage" placeholder="现住社区/村"></el-input>
      </el-form-item>
          <el-form-item label="现住详址" prop="curliveAddressDetail">
          <el-input v-model="dataForm.curliveAddressDetail" placeholder="现住详址"></el-input>
      </el-form-item>
          <el-form-item label="籍贯省" prop="nativeAddressProv">
          <el-input v-model="dataForm.nativeAddressProv" placeholder="籍贯省"></el-input>
      </el-form-item>
          <el-form-item label="籍贯市" prop="nativeAddressCity">
          <el-input v-model="dataForm.nativeAddressCity" placeholder="籍贯市"></el-input>
      </el-form-item>
          <el-form-item label="籍贯县(区)" prop="nativeAddressCounty">
          <el-input v-model="dataForm.nativeAddressCounty" placeholder="籍贯县(区)"></el-input>
      </el-form-item>
          <el-form-item label="曾用名" prop="formerName">
          <el-input v-model="dataForm.formerName" placeholder="曾用名"></el-input>
      </el-form-item>
          <el-form-item label="学历" prop="education">
          <el-input v-model="dataForm.education" placeholder="学历"></el-input>
      </el-form-item>
          <el-form-item label="职业" prop="occupation">
          <el-input v-model="dataForm.occupation" placeholder="职业"></el-input>
      </el-form-item>
          <el-form-item label="职业类别" prop="occupationType">
          <el-input v-model="dataForm.occupationType" placeholder="职业类别"></el-input>
      </el-form-item>
          <el-form-item label="服务处所" prop="serviceAddress">
          <el-input v-model="dataForm.serviceAddress" placeholder="服务处所"></el-input>
      </el-form-item>
          <el-form-item label="婚姻状况" prop="marriageStatus">
          <el-input v-model="dataForm.marriageStatus" placeholder="婚姻状况"></el-input>
      </el-form-item>
          <el-form-item label="政治面貌" prop="party">
          <el-input v-model="dataForm.party" placeholder="政治面貌"></el-input>
      </el-form-item>
          <el-form-item label="宗教信仰" prop="religious">
          <el-input v-model="dataForm.religious" placeholder="宗教信仰"></el-input>
      </el-form-item>
          <el-form-item label="有无皈依（已受洗）" prop="conversionState">
          <el-input v-model="dataForm.conversionState" placeholder="有无皈依（已受洗）"></el-input>
      </el-form-item>
          <el-form-item label="国籍" prop="nationality">
          <el-input v-model="dataForm.nationality" placeholder="国籍"></el-input>
      </el-form-item>
          <el-form-item label="数据来源" prop="platCode">
          <el-input v-model="dataForm.platCode" placeholder="数据来源"></el-input>
      </el-form-item>
          <el-form-item label="创建人" prop="createBy">
          <el-input v-model="dataForm.createBy" placeholder="创建人"></el-input>
      </el-form-item>
          <el-form-item label="创建时间" prop="createDate">
          <el-input v-model="dataForm.createDate" placeholder="创建时间"></el-input>
      </el-form-item>
          <el-form-item label="最后修改人" prop="updateBy">
          <el-input v-model="dataForm.updateBy" placeholder="最后修改人"></el-input>
      </el-form-item>
          <el-form-item label="最后修改时间" prop="updateDate">
          <el-input v-model="dataForm.updateDate" placeholder="最后修改时间"></el-input>
      </el-form-item>
          <el-form-item label="删除状态" prop="deleteFlag">
          <el-input v-model="dataForm.deleteFlag" placeholder="删除状态"></el-input>
      </el-form-item>
          <el-form-item label="乐观锁" prop="versions">
          <el-input v-model="dataForm.versions" placeholder="乐观锁"></el-input>
      </el-form-item>
          <el-form-item label="扩展字段1" prop="attribute1">
          <el-input v-model="dataForm.attribute1" placeholder="扩展字段1"></el-input>
      </el-form-item>
          <el-form-item label="扩展字段2" prop="attribute2">
          <el-input v-model="dataForm.attribute2" placeholder="扩展字段2"></el-input>
      </el-form-item>
          <el-form-item label="扩展字段3" prop="attribute3">
          <el-input v-model="dataForm.attribute3" placeholder="扩展字段3"></el-input>
      </el-form-item>
          <el-form-item label="扩展字段4" prop="attribute4">
          <el-input v-model="dataForm.attribute4" placeholder="扩展字段4"></el-input>
      </el-form-item>
          <el-form-item label="扩展字段5" prop="attribute5">
          <el-input v-model="dataForm.attribute5" placeholder="扩展字段5"></el-input>
      </el-form-item>
          <el-form-item label="扩展字段6" prop="attribute6">
          <el-input v-model="dataForm.attribute6" placeholder="扩展字段6"></el-input>
      </el-form-item>
          <el-form-item label="扩展字段7" prop="attribute7">
          <el-input v-model="dataForm.attribute7" placeholder="扩展字段7"></el-input>
      </el-form-item>
          <el-form-item label="扩展字段8" prop="attribute8">
          <el-input v-model="dataForm.attribute8" placeholder="扩展字段8"></el-input>
      </el-form-item>
          <el-form-item label="扩展字段9" prop="attribute9">
          <el-input v-model="dataForm.attribute9" placeholder="扩展字段9"></el-input>
      </el-form-item>
          <el-form-item label="扩展字段10" prop="attribute10">
          <el-input v-model="dataForm.attribute10" placeholder="扩展字段10"></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: {
        residentId: '',
        gridId: '',
        residentProperty: '',
        residentType: '',
        idType: '',
        idCard: '',
        residentName: '',
        sex: '',
        birthday: '',
        nation: '',
        telephone: '',
        householdProv: '',
        householdCity: '',
        householdCounty: '',
        householdTown: '',
        householdVillage: '',
        householdAddressDetail: '',
        curliveProv: '',
        curliveCity: '',
        curliveCounty: '',
        curliveTown: '',
        curliveVillage: '',
        curliveAddressDetail: '',
        nativeAddressProv: '',
        nativeAddressCity: '',
        nativeAddressCounty: '',
        formerName: '',
        education: '',
        occupation: '',
        occupationType: '',
        serviceAddress: '',
        marriageStatus: '',
        party: '',
        religious: '',
        conversionState: '',
        nationality: '',
        platCode: '',
        createBy: '',
        createDate: '',
        updateBy: '',
        updateDate: '',
        deleteFlag: '',
        versions: '',
        attribute1: '',
        attribute2: '',
        attribute3: '',
        attribute4: '',
        attribute5: '',
        attribute6: '',
        attribute7: '',
        attribute8: '',
        attribute9: '',
        attribute10: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        gridId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        residentProperty: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        residentType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        idType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        idCard: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        residentName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        sex: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        birthday: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        nation: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        telephone: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        householdProv: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        householdCity: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        householdCounty: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        householdTown: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        householdVillage: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        householdAddressDetail: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        curliveProv: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        curliveCity: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        curliveCounty: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        curliveTown: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        curliveVillage: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        curliveAddressDetail: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        nativeAddressProv: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        nativeAddressCity: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        nativeAddressCounty: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        formerName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        education: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        occupation: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        occupationType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        serviceAddress: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        marriageStatus: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        party: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        religious: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        conversionState: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        nationality: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        platCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        createBy: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        createDate: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updateBy: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updateDate: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        deleteFlag: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        versions: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        attribute1: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        attribute2: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        attribute3: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        attribute4: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        attribute5: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        attribute6: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        attribute7: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        attribute8: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        attribute9: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        attribute10: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.residentId) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/axis/caResident/${this.dataForm.residentId}`).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.residentId ? 'post' : 'put']('/axis/caResident/', 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>
