<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.rentalId ? $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="房屋ID" prop="houseId">
          <el-input v-model="dataForm.houseId" placeholder="房屋ID"></el-input>
      </el-form-item>
          <el-form-item label="房屋编号" prop="houseName">
          <el-input v-model="dataForm.houseName" placeholder="房屋编号"></el-input>
      </el-form-item>
          <el-form-item label="房屋地址" prop="houseAddress">
          <el-input v-model="dataForm.houseAddress" placeholder="房屋地址"></el-input>
      </el-form-item>
          <el-form-item label="建筑用途" prop="houseUse">
          <el-input v-model="dataForm.houseUse" placeholder="建筑用途"></el-input>
      </el-form-item>
          <el-form-item label="建筑面积（平方米）" prop="houseArea">
          <el-input v-model="dataForm.houseArea" 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="telephone">
          <el-input v-model="dataForm.telephone" placeholder="房主联系方式"></el-input>
      </el-form-item>
          <el-form-item label="房主现居详址" prop="curliveAddress">
          <el-input v-model="dataForm.curliveAddress" placeholder="房主现居详址"></el-input>
      </el-form-item>
          <el-form-item label="出租用途" prop="rentUse">
          <el-input v-model="dataForm.rentUse" placeholder="出租用途"></el-input>
      </el-form-item>
          <el-form-item label="隐患类型" prop="troubleType">
          <el-input v-model="dataForm.troubleType" placeholder="隐患类型"></el-input>
      </el-form-item>
          <el-form-item label="承租人ID" prop="renterId">
          <el-input v-model="dataForm.renterId" placeholder="承租人ID"></el-input>
      </el-form-item>
          <el-form-item label="承租人公民身份证号码" prop="renterCardNumber">
          <el-input v-model="dataForm.renterCardNumber" placeholder="承租人公民身份证号码"></el-input>
      </el-form-item>
          <el-form-item label="承租人证件类型" prop="renterCardType">
          <el-input v-model="dataForm.renterCardType" placeholder="承租人证件类型"></el-input>
      </el-form-item>
          <el-form-item label="承租人姓名" prop="renterName">
          <el-input v-model="dataForm.renterName" placeholder="承租人姓名"></el-input>
      </el-form-item>
          <el-form-item label="承租人联系方式" prop="renterPhone">
          <el-input v-model="dataForm.renterPhone" 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="pointStatus">
          <el-input v-model="dataForm.pointStatus" 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="承租单位" prop="attribute1">
          <el-input v-model="dataForm.attribute1" placeholder="承租单位"></el-input>
      </el-form-item>
          <el-form-item label="承租日期" prop="attribute2">
          <el-input v-model="dataForm.attribute2" placeholder="承租日期"></el-input>
      </el-form-item>
          <el-form-item label="承租期限" prop="attribute3">
          <el-input v-model="dataForm.attribute3" placeholder="承租期限"></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: {
        rentalId: '',
        gridId: '',
        houseId: '',
        houseName: '',
        houseAddress: '',
        houseUse: '',
        houseArea: '',
        idType: '',
        idCard: '',
        residentName: '',
        telephone: '',
        curliveAddress: '',
        rentUse: '',
        troubleType: '',
        renterId: '',
        renterCardNumber: '',
        renterCardType: '',
        renterName: '',
        renterPhone: '',
        longitude: '',
        latitude: '',
        pointStatus: '',
        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' }
        ],
        houseId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        houseName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        houseAddress: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        houseUse: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        houseArea: [
          { 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' }
        ],
        telephone: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        curliveAddress: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        rentUse: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        troubleType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        renterId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        renterCardNumber: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        renterCardType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        renterName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        renterPhone: [
          { 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' }
        ],
        pointStatus: [
          { 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.rentalId) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/axis/caRental/${this.dataForm.rentalId}`).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.rentalId ? 'post' : 'put']('/axis/caRental/', 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>
