<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">
        <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>
export default {
  components: { },
  mixins,
  props: {
    isShow: Boolean,
    curObj: Object
  },
  inject: ['parentData'],
  data() {
    return {
      formData: [],
      loading: true,
      cObj: {},
      isShowMap: false
    }
  },
  watch: {
    isShow: function(val) {
      if (val) {
        if (val) {
          if (this.curObj.id) {
            this.$api.house.admlanddetail({id: this.curObj.id}).then(res => {
              let curData = res || {}
              this.cObj = curData || {}
              this.getDef()
            })
          } else {
            this.cObj = this.curObj
            this.getDef()
          }
        }
      }
    },
  },
  methods: {
    getDef(str) {
      let params = { ...this.cObj }
      if (str === 'final_price') {
        params = {...this.$refs.ruleForm.baseForm}
        if (params.final_price && params.plot_ratio) {
          params.floor_price = (Number(params.final_price) * 10000 / (666.6666666667 * Number(params.plot_ratio))).toFixed(2)
        }
        if (params.start_total_price && params.final_price) {
          params.premium_rate = ((Number(params.final_price) - Number(params.start_total_price)) / Number(params.start_total_price) * 100).toFixed(2)
        }
      }
      if (str === 'floor_price') {
        params = {...this.$refs.ruleForm.baseForm}
        if (params.floor_price && params.plot_ratio) {
          params.final_price = ((666.6666666667 * Number(params.plot_ratio)) * Number(params.floor_price) / 10000).toFixed(2)
        }
        if (params.start_unit_price && params.floor_price) {
          params.premium_rate = ((Number(params.floor_price) - Number(params.start_unit_price)) / Number(params.start_unit_price) * 100).toFixed(2)
        }
      }
      if (str === 'start_total_price') {
        params = {...this.$refs.ruleForm.baseForm}
        if (params.start_total_price && params.plot_ratio) {
          params.start_unit_price = (Number(params.start_total_price) * 10000 / (666.6666666667 * Number(params.plot_ratio))).toFixed(2)
        }
        if (params.start_total_price && params.final_price) {
          params.premium_rate = ((Number(params.final_price) - Number(params.start_total_price)) / Number(params.start_total_price) * 100).toFixed(2)
        }
      }
      if (str === 'start_unit_price') {
        params = {...this.$refs.ruleForm.baseForm}
        if (params.start_unit_price && params.plot_ratio) {
          params.start_total_price = ((666.6666666667 * Number(params.plot_ratio)) * Number(params.start_unit_price) / 10000).toFixed(2)
        }
        if (params.start_unit_price && params.floor_price) {
          params.premium_rate = ((Number(params.floor_price) - Number(params.start_unit_price)) / Number(params.start_unit_price) * 100).toFixed(2)
        }
      }
      this.formData = [
        { label: '地块编号', key: 'no', class: 'c-2' },
        { label: '区域', key: 'area_type', type: 'select', class: 'c-2', options: this.$dictData.area_type },
        { label: '竞得单位', key: 'company', class: 'c-2' },
        { label: '成交日期', key: 'deal_time', class: 'c-2', type: 'datePicker', type2: 'date'},
        { label: '容积率', key: 'plot_ratio', class: 'c-2' },
        { label: '用途', key: 'purpose', class: 'c-2' },
        { label: '成交价', key: 'final_price', class: 'c-2', type: 'inputFont', appendFont: '万元/亩', changeHandle: this.finalPriceChange},
        { label: '成交楼面价', key: 'floor_price', class: 'c-2', type: 'inputFont', appendFont: '元', changeHandle: this.floorPriceChange },
        { label: '起拍价', key: 'start_total_price', class: 'c-2', type: 'inputFont', appendFont: '万元/亩', changeHandle: this.startTotalPriceChange },
        { label: '起拍楼面价', key: 'start_unit_price', class: 'c-2', type: 'inputFont', appendFont: '元', changeHandle: this.startUnitPriceChange },
        { label: '溢价率', key: 'premium_rate', class: 'c-2', type: 'inputFont', appendFont: '%' },
        { label: '出让年限', key: 'sell_ownership', class: 'c-2', type: 'inputFont', appendFont: '年' },
        { label: '出让面积', key: 'sell_area', class: 'c-2', type: 'inputFont', appendFont: '亩'},
        { label: '毛坯限价', key: 'built_area', class: 'c-2', type: 'inputFont', appendFont: '元/㎡' },
        { label: '地块位置', key: 'land_name', class: 'c-2', type: 'textarea' },
        { label: '土拍主图', key: 'pri_image', type: 'upload', class: 'c-2' },
      ]
      params.pri_image = this.IMadd(params.pri_image)
      this.setDefaultValue(params)
    },
    finalPriceChange () {
      this.getDef('final_price')
    },
    floorPriceChange () {
      this.getDef('floor_price')
    },
    startTotalPriceChange () {
      this.getDef('start_total_price')
    },
    startUnitPriceChange () {
      this.getDef('start_unit_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
            let apiStr = 'admlandadd'
            if (newForm.id) apiStr = 'admlandedit'
            newForm.pri_image = this.IMdel(newForm.pri_image)
            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 .xl-form .el-textarea__inner {
  height: 180px;
}
</style>
