<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" prop="customerId">
          <el-input v-model="dataForm.customerId" placeholder="客户id"></el-input>
      </el-form-item>
          <el-form-item label="小区id" prop="neighborHoodId">
          <el-input v-model="dataForm.neighborHoodId" placeholder="小区id"></el-input>
      </el-form-item>
          <el-form-item label="片区id，neighbor_hood_part.id,可为空。" prop="partId">
          <el-input v-model="dataForm.partId" placeholder="片区id，neighbor_hood_part.id,可为空。"></el-input>
      </el-form-item>
          <el-form-item label="所属楼栋id" prop="buildingId">
          <el-input v-model="dataForm.buildingId" placeholder="所属楼栋id"></el-input>
      </el-form-item>
          <el-form-item label="所属单元id" prop="buildingUnitId">
          <el-input v-model="dataForm.buildingUnitId" 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="doorName">
          <el-input v-model="dataForm.doorName" placeholder="门牌号"></el-input>
      </el-form-item>
          <el-form-item label="房屋类型，1楼房，2平房，3别墅" prop="houseType">
          <el-input v-model="dataForm.houseType" placeholder="房屋类型，1楼房，2平房，3别墅"></el-input>
      </el-form-item>
          <el-form-item label="存储字典value" prop="purpose">
          <el-input v-model="dataForm.purpose" placeholder="存储字典value"></el-input>
      </el-form-item>
          <el-form-item label="1：出租 0：自住  2：闲置" prop="rentFlag">
          <el-input v-model="dataForm.rentFlag" placeholder="1：出租 0：自住  2：闲置"></el-input>
      </el-form-item>
          <el-form-item label="房主姓名" prop="ownerName">
          <el-input v-model="dataForm.ownerName" placeholder="房主姓名"></el-input>
      </el-form-item>
          <el-form-item label="房主电话" prop="ownerPhone">
          <el-input v-model="dataForm.ownerPhone" placeholder="房主电话"></el-input>
      </el-form-item>
          <el-form-item label="房主身份证号" prop="ownerIdCard">
          <el-input v-model="dataForm.ownerIdCard" placeholder="房主身份证号"></el-input>
      </el-form-item>
          <el-form-item label="排序" prop="sort">
          <el-input v-model="dataForm.sort" 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="communityId">
          <el-input v-model="dataForm.communityId" placeholder="社区ID"></el-input>
      </el-form-item>
          <el-form-item label="社区" prop="communityName">
          <el-input v-model="dataForm.communityName" placeholder="社区"></el-input>
      </el-form-item>
          <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="gridName">
          <el-input v-model="dataForm.gridName" placeholder="网格"></el-input>
      </el-form-item>
          <el-form-item label="房屋小区ID" prop="villageId">
          <el-input v-model="dataForm.villageId" placeholder="房屋小区ID"></el-input>
      </el-form-item>
          <el-form-item label="房屋小区" prop="villageName">
          <el-input v-model="dataForm.villageName" placeholder="房屋小区"></el-input>
      </el-form-item>
          <el-form-item label="楼号ID" prop="buildId">
          <el-input v-model="dataForm.buildId" placeholder="楼号ID"></el-input>
      </el-form-item>
          <el-form-item label="楼号" prop="buildName">
          <el-input v-model="dataForm.buildName" placeholder="楼号"></el-input>
      </el-form-item>
          <el-form-item label="单元ID" prop="unitId">
          <el-input v-model="dataForm.unitId" placeholder="单元ID"></el-input>
      </el-form-item>
          <el-form-item label="单元" prop="unitName">
          <el-input v-model="dataForm.unitName" placeholder="单元"></el-input>
      </el-form-item>
          <el-form-item label="房屋ID" prop="homeId">
          <el-input v-model="dataForm.homeId" placeholder="房屋ID"></el-input>
      </el-form-item>
          <el-form-item label="房屋" prop="homeName">
          <el-input v-model="dataForm.homeName" placeholder="房屋"></el-input>
      </el-form-item>
          <el-form-item label="购房日期" prop="purchaseDate">
          <el-input v-model="dataForm.purchaseDate" 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: '',
        neighborHoodId: '',
        partId: '',
        buildingId: '',
        buildingUnitId: '',
        houseName: '',
        doorName: '',
        houseType: '',
        purpose: '',
        rentFlag: '',
        ownerName: '',
        ownerPhone: '',
        ownerIdCard: '',
        sort: '',
        delFlag: '',
        revision: '',
        createdBy: '',
        createdTime: '',
        updatedBy: '',
        updatedTime: '',
        communityId: '',
        communityName: '',
        gridId: '',
        gridName: '',
        villageId: '',
        villageName: '',
        buildId: '',
        buildName: '',
        unitId: '',
        unitName: '',
        homeId: '',
        homeName: '',
        purchaseDate: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        customerId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        neighborHoodId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        partId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        buildingId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        buildingUnitId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        houseName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        doorName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        houseType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        purpose: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        rentFlag: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        ownerName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        ownerPhone: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        ownerIdCard: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        sort: [
          { 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' }
        ],
        communityId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        communityName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        gridId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        gridName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        villageId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        villageName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        buildId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        buildName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        unitId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        unitName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        homeId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        homeName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        purchaseDate: [
          { 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(`/rent/rentHouse/${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']('/rent/rentHouse/', 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>
