<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>
export default {
  components: { },
  mixins,
  props: {
    isShow: Boolean,
    curObj: Object
  },
  inject: ['parentData'],
  data() {
    return {
      formData: [],
      loading: true,
      cObj: {},
      posTips: '',
    }
  },
  watch: {
    isShow: function(val) {
      if (val) {
        if (val) {
          if (this.curObj.id) {
            this.$api.house.admestatebargaindetail({id: this.curObj.id}).then(res => {
              let curData = res || {}
              curData.startEndTime = [curData.start_at, curData.end_at]
              this.cObj = curData || {}
              this.getDef()
            })
          } else {
            this.cObj = this.curObj
            this.getDef()
          }
        }
      }
    },
  },
  methods: {
    getDef(fieldStr) {
      let params = { ...this.cObj }
      this.formData = [
        { label: '关联楼盘', rules: 1, key: 'estate_id', type: 'selectRemote',
          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: 'pri_image', type: 'uploads', class: 'c-2' },
        { label: '排序', key: 'sort', class: 'c-2' },
        { label: '特价时间', label2: '开始时间', label3: '结束时间', key: 'startEndTime', type: 'datePicker', rules: 1},
        { label: '备注', key: 'remark', type: 'textarea', rules: 1},
      ]
      this.setDefaultValue(params)
    },
    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 = 'admestatebargainadd'
            if (newForm.id) apiStr = 'admestatebargainedit'
            newForm.start_at = newForm.startEndTime[0]
            newForm.end_at = newForm.startEndTime[1]
            delete newForm.startEndTime
            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>
