<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="110px">
      </base-form>
      <div class="xl-form">
        <div class="xl-form-footer">
          <el-button class="xl-form-btn t2" @click="close">关 闭</el-button>
          <el-button class="xl-form-btn t1" @click="close('confirm')">确定</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { arrToObj } from '@/utils'
export default {
  components: { },
  mixins,
  props: {
    isShow: Boolean,
    curObj: Object
  },
  inject: ['parentData'],
  data() {
    return {
      loading: false,
      formData: [],
    }
  },
  watch: {
    isShow: function(val) {
      if (val) {
        this.getDef()
      }
    },
  },
  methods: {
    getDef (str) {
      let params = { ...this.curObj }
      if (str === 'price') {
        params = {...this.$refs.ruleForm.baseForm}
        if (params.area && params.unit_price) {
          params.price = (params.area * params.unit_price / 10000).toFixed(1)
          if (params.price.charAt(params.price.length - 1) === '0') {
            params.price = parseInt(params.price)
          }
        }
      }
      // if (!params.company) params.company = '贝壳'
      this.formData = [
        { label: '签约中介', key: 'company', type: 'select', options: this.$dictData.zjjg, rules: 1}, 
        { label: '签约日期', key: 'sign_at', type: 'datePicker', type2: 'date', class: 'c-2', rules: 1},
        { label: '面积', key: 'area', class: 'c-2', type: 'inputFont', appendFont: '㎡', changeHandle: this.priceChange, rules: 1},
        { label: '单价', key: 'unit_price', class: 'c-2', type: 'inputFont', appendFont: '元', changeHandle: this.priceChange, rules: 1},
        { label: '总价', key: 'price', class: 'c-2', type: 'inputFont', appendFont: '万元', rules: 1},
        { label: '层数', class: 'c-2', key: 'cur_layer', rules: [
          { validator: (rule, value, callback) => {
            if (Number(value) < 0 || isNaN(Number(value))) {
              callback(new Error('请输入数字'))
            } else {
              callback()
            }
          }, trigger: 'blur' },
        ] },
        { label: '总层数', class: 'c-2', key: 'layer', rules: [
          { validator: (rule, value, callback) => {
            if (Number(value) < 0 || isNaN(Number(value))) {
              callback(new Error('请输入数字'))
            } else {
              callback()
            }
          }, trigger: 'blur' },
        ] },
      ]
      this.setDefaultValue(params)
    },
    priceChange () {
      this.getDef('price')
    },
    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
            newForm.estate_id = this.parentData.searchForm.estate_id
            if (this.imagesArr && this.imagesArr.length > 0) newForm.images = this.imagesArr.join(',')
            let apiStr = 'admpriceadd'
            if (this.curObj.id) apiStr = 'admpriceedit'
            this.$api.house[apiStr](newForm).then(data => {
              this.$msgs(newForm.estate_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-date-editor.el-input {
    width: 100%;
  }
  ::v-deep .el-textarea__inner {
    height: 300px;
  }
}
</style>
