<template>
  <div>
    <el-dialog
      v-loading="loading"
      :show-close="false"
      :close-on-click-modal="false"
      :visible.sync="isShow"
      :title="curObj.id ? '编辑活动' : '添加活动'"
      :fullscreen="false"
      width="400px"
      custom-class="xl-dialog"
      center
    >
      <base-form ref="ruleForm" :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,
    }
  },
  watch: {
    isShow: function(val) {
      if (val) {
        this.getDef()
      }
    },
  },
  methods: {
    getDef() {
      let params = { ...this.curObj }
      if (!params.max_enroll) params.max_enroll = 0
      this.formData = [
        { label: '活动楼盘', key: 'estate_id', rules: 1, 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: '截止时间', key: 'end_at', rules: 1, type: 'datePicker', type2: 'date' },
        { label: '分类', key: 'activity_type', rules: 1, type: 'select', options: this.$dictData.activity_type},
        { label: '群聊', key: 'image', type: 'uploads' },
        { label: '备注', key: 'remark', rules: 1, type: 'textarea'},
        { label: '需要报名', key: 'is_sign', type: 'select', options: this.$dictData.sys_yesno},
        { label: '上限人数', key: 'max_enroll'},
      ]
      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 = 'admactivityadd'
            if (newForm.id) apiStr = 'admactivityedit'
            if (!newForm.max_enroll) newForm.max_enroll = 0
            this.$api.other[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: 40px;
  overflow: hidden;
  .el-upload {
    height: 35px;
  }
  .icon {
    width: 110px;
    height: 35px;
    line-height: 35px;
    font-size: 20px;
  }
  .img {
    width: 110px;
    height: 35px;
  }
}
</style>
