<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('查看')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :disabled="true" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
      <el-form-item label="楼宇类型" prop="buildingType">
          <el-input v-model="dataForm.buildingType" placeholder="楼宇类型"></el-input>
      </el-form-item>
      <el-form-item label="楼宇名称" prop="buildingName">
          <el-input v-model="dataForm.buildingName" placeholder="楼宇名称"></el-input>
      </el-form-item>
      <el-form-item label="用途分类" prop="buildingUse">
          <el-input v-model="dataForm.buildingUse" placeholder="用途分类"></el-input>
      </el-form-item>
      <el-form-item label="楼宇状态" prop="buildingStatus">
          <el-input v-model="dataForm.buildingStatus" placeholder="楼宇状态"></el-input>
      </el-form-item>
      <el-form-item label="楼宇结构" prop="buildingStructure">
          <el-input v-model="dataForm.buildingStructure" placeholder="楼宇结构"></el-input>
      </el-form-item>
      <el-form-item label="使用现状" prop="buildingUseage">
          <el-input v-model="dataForm.buildingUseage" placeholder="使用现状"></el-input>
      </el-form-item>
      <el-form-item label="建筑时间" prop="constructionTime">
          <el-input v-model="dataForm.constructionTime" placeholder="建筑时间"></el-input>
      </el-form-item>
      <el-form-item label="所处位置" prop="buildingAddr">
          <el-input v-model="dataForm.buildingAddr" placeholder="所处位置"></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="楼栋长" prop="buildingLeader">
          <el-input v-model="dataForm.buildingLeader" placeholder="楼栋长"></el-input>
      </el-form-item>
      <el-form-item label="楼层数" prop="layerCount">
          <el-input v-model="dataForm.layerCount" placeholder="楼层数"></el-input>
      </el-form-item>
      <el-form-item label="地下楼层数" prop="basementLayerCount">
          <el-input v-model="dataForm.basementLayerCount" placeholder="地下楼层数"></el-input>
      </el-form-item>
      <el-form-item label="住宅开始层数" prop="houseBeginLayer">
          <el-input v-model="dataForm.houseBeginLayer" placeholder="住宅开始层数"></el-input>
      </el-form-item>
      <el-form-item label="单元数" prop="unitCount">
          <el-input v-model="dataForm.unitCount" placeholder="单元数"></el-input>
      </el-form-item>
      <el-form-item label="每层每单元户数" prop="layerRoomCount">
          <el-input v-model="dataForm.layerRoomCount" placeholder="每层每单元户数"></el-input>
      </el-form-item>
      <el-form-item label="总房屋数" prop="roomCount">
          <el-input v-model="dataForm.roomCount" placeholder="总房屋数"></el-input>
      </el-form-item>
      <el-form-item label="电梯数量" prop="elevatorCount">
          <el-input v-model="dataForm.elevatorCount" placeholder="电梯数量"></el-input>
      </el-form-item>
      <el-form-item label="建筑面积" prop="buildingArea">
          <el-input v-model="dataForm.buildingArea" placeholder="建筑面积"></el-input>
      </el-form-item>
      <el-form-item label="物业公司" prop="buildingPmc">
          <el-input v-model="dataForm.buildingPmc" placeholder="物业公司"></el-input>
      </el-form-item>
      <el-form-item label="介绍" prop="buildingDesc">
          <el-input v-model="dataForm.buildingDesc" 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="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="platCode">
          <el-input v-model="dataForm.platCode" 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: {
        buildingId: '',
        buildingType: '',
        gridId: '',
        buildingName: '',
        buildingUse: '',
        buildingStatus: '',
        buildingStructure: '',
        buildingUseage: '',
        constructionTime: '',
        buildingAddr: '',
        communityName: '',
        buildingLeader: '',
        layerCount: '',
        basementLayerCount: '',
        houseBeginLayer: '',
        unitCount: '',
        layerRoomCount: '',
        roomCount: '',
        elevatorCount: '',
        buildingArea: '',
        buildingPmc: '',
        buildingDesc: '',
        pointStatus: '',
        longitude: '',
        latitude: '',
        platCode: '',
        createBy: '',
        createDate: '',
        updateBy: '',
        updateDate: '',
        deleteFlag: '',
        versions: '',
        communityId: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        buildingType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        buildingName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        buildingUse: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        buildingStatus: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        buildingStructure: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        buildingUseage: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        constructionTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        buildingAddr: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        communityName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        buildingLeader: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        layerCount: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        basementLayerCount: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        houseBeginLayer: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        unitCount: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        layerRoomCount: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        roomCount: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        elevatorCount: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        buildingArea: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        buildingPmc: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        buildingDesc: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        pointStatus: [
          { 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' }
        ],
        platCode: [
          { 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.post(`/opendata/caLoudong/getLouDongDetails`, { buildingId: 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.buildingId ? 'post' : 'put']('/axis/caLoudong/', 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>
