<template>
  <div>
    <el-dialog
      v-loading="loading"
      :show-close="false"
      :close-on-click-modal="false"
      :visible.sync="isShow"
      :title="curObj.id ? '编辑楼盘数据' : '添加楼盘数据'"
      :fullscreen="false"
      width="700px"
      custom-class="xl-dialog"
      center
    >
      <base-form ref="ruleForm" class="lib-edit" :data="formData" :is-inline="false" label-width="100px">
        <div slot="footer" style="padding-top: 20px;">
          <el-button class="xl-form-btn t2" @click="close">关 闭</el-button>
          <el-button class="xl-form-btn t1" @click="close('confirm')">确定</el-button>
        </div>
      </base-form>
    </el-dialog>
  </div>
</template>
<script>
import { arrToObj } from '@/utils'
export default {
  components: { },
  mixins,
  props: {
    isShow: Boolean,
    curObj: Object
  },
  inject: ['parentData'],
  data() {
    return {
      formData: [],
      loading: true,
      cObj: {},
      roomAreaList: [],
    }
  },
  watch: {
    isShow: function(val) {
      if (val) {
        if (this.curObj.id) {
          this.cObj = JSON.parse(JSON.stringify(this.curObj))
        } else {
          this.cObj = {}
        }
        this.getDef()
      }
    },
  },
  methods: {
    getDef(fieldStr) {
      let params = { ...this.cObj }
      this.formData = [
        { label: '关联楼盘', rules: 1, key: 'estate_id', type: 'selectRemote', changeHandle: this.estateChange,
          remoteParams: { skey: 'estate_name', api: `house.admestatelist`, opKey: 'estate_name', opVal: 'id' },
          remoteOptions: [{ keyRO: params.estate_name, valRO: params.estate_id }]
        },
        { label: '所属区域', rules: 1, key: 'area_type', type: 'select', options: this.$dictData.area_type},
        { label: `面积产品户型`, label2: `快捷选择工具`, key: `HT`, type: 'select', options: this.roomAreaList, changeHandle: this.htChange,},
        { label: '产品类型', rules: 1, key: 'product_type', type: 'select', options: this.$dictData.product_type, class: 'c-2'},
        { label: '户型面积', rules: 1, key: 'house_square', class: 'c-2', type: 'inputFont', appendFont: '㎡'},
      ]
      this.setDefaultValue(params)
    },
    estateChange (estate_id, op, cur) {
      if (estate_id) {
        this.$api.house.admestatehousearealist({estate_id}).then(res => {
          const list = res.list || []
          const htObj = arrToObj(this.$dictData.house_type)
          const ptObj = arrToObj(this.$dictData.product_type)
          list.map(item => {
            item.key = `${item.area}㎡-${ptObj[item.product_type]}-${htObj[item.house_type]}`
            item.key2 = `${htObj[item.house_type]}`
            item.val = item.id
          })
          this.roomAreaList = [...list]
          this.cObj.estate_id = estate_id
          this.cObj.area_type = cur.area_type
          this.cObj.HT = ''
          this.cObj.product_type = ''
          this.cObj.house_square = ''
          this.getDef('edit')
        })
      } else {
        this.cObj = {}
        this.getDef('edit')
      }
    },
    htChange (val) {
      this.roomAreaList.forEach(ra => {
        if (val === ra.id) {
          this.cObj.product_type = ra.product_type
          this.cObj.house_square = ra.area
          this.cObj.HT = val
          this.getDef('edit')
        }
        return
      })
    },
    close(str) {
      if (str === 'confirm') {
        this.$refs['ruleForm'].$refs['baseForm'].validate((valid) => {
          if (valid) {
            const oldform = this.$refs.ruleForm.baseForm
            const newForm = { ...oldform }
            if (this.curObj.id) newForm.id = this.curObj.id
            let apiStr = 'admestatehousedynamicadd'
            if (newForm.id) apiStr = 'admestatehousedynamicedit'
            this.$api.house[apiStr](newForm).then(data => {
              this.$msgs(newForm.id ? '编辑成功' : '新增成功')
              this.$emit('close', newForm)
            })
          }
        })
      } else {
        this.$emit('close')
        this.setDefaultValue()
      }
    },
  }
}
</script>
<style lang="scss" scoped>
@import '../../../../styles/libEdit.scss';
.lib-edit {
  padding-top: 0;
  ::v-deep .el-form-item {
    margin-bottom: 10px;
  }
  ::v-deep .el-date-editor.el-input {
    width: 100%;
  }
}
::v-deep .img-upload {
  height: 180px;
  overflow: hidden;
  .icon {
    width: 160px;
  }
  .img {
    width: 160px;
  }
}
</style>
