<template>
  <div>
    <el-drawer
      v-loading="loading"
      :show-close="false"
      :title="curObj.id ? '编辑资讯' : '新增资讯'"
      :wrapper-closable="false"
      :close-on-press-escape="false"
      :visible.sync="isShow"
      size="960px"
      custom-class="xl-drawer"
      direction="rtl"
    >
      <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>
        <div class="scoped-textarea" slot="otherItem">
          <div class="st-text">房票科技</div>
          <tinymce v-model="content" :height="500" />
        </div>
      </base-form>
    </el-drawer>
  </div>
</template>
<script>
import Tinymce from '@/components/Tinymce'
export default {
  components: { Tinymce },
  mixins,
  props: {
    isShow: Boolean,
    curObj: Object
  },
  inject: ['parentData'],
  data() {
    return {
      formData: [],
      loading: true,
      cObj: {},
      content: '',
    }
  },
  watch: {
    isShow: function(val) {
      if (val) {
        if (val) {
          if (this.curObj.id) {
            this.$api.house.adminformationdetail({id: this.curObj.id}).then(res => {
              let curData = res || {}
              this.cObj = curData || {}
              this.content = curData.content || ''
              this.getDef()
            })
          } else {
            this.cObj = this.curObj
            this.getDef()
          }
        }
      }
    },
  },
  methods: {
    getDef() {
      const params = { ...this.cObj }
      const disabled = false
      const remoteOptionsIds = []
      if (params.estate_list) {
        params.estate_id_list = params.estate_list.map(item => {
          remoteOptionsIds.push({ keyRO: item.estate_name, valRO: item.id })
          return item.id
        })
      } else {
        params.estate_id_list = []
      }
      // if (params.id) disabled = true
      this.formData = [
        { label: '标题', key: 'title' },
        { label: '分类', key: 'information_category', type: 'select', class: 'c-2', options: this.$dictData.information_category },
        { label: '作者', key: 'author', class: 'c-2' },
        { label: '状态', key: 'hide_status', type: 'select', class: 'c-2', options: this.$dictData.hide_status },
        { label: '主图', key: 'pri_image', type: 'upload', class: 'c-2' },
        { label: '关联楼盘', key: 'estate_id_list', type: 'selectRemote', multiple: true, changeHandle: this.deviceChange,
          remoteParams: { skey: 'estate_name', api: `house.admestatelist?search_EQ_status=1`, opKey: 'estate_name', opVal: 'id' },
          remoteOptions: remoteOptionsIds
        },
      ]
      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
            if (newForm.estate_id_list && newForm.estate_id_list.length > 0) {
              newForm.estate_id_list = newForm.estate_id_list.join(',')
            } else {
              newForm.estate_id_list = ''
            }
            let apiStr = 'adminformationadd'
            if (newForm.id) apiStr = 'adminformationedit'
            if (this.content) {
              newForm.content = this.content
            } else {
              this.$msg('请输入资讯文章内容')
              return
            }
            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 {
  width: 900px;
  padding-top: 0;
  padding-left: 0;
  padding-bottom: 40px;
  ::v-deep .el-form-item {
    margin-bottom: 10px;
  }
  ::v-deep .el-date-editor.el-input {
    width: 100%;
  }
}

.scoped-textarea {
  width: 100%;
  position: relative;
  .st-text {
    position: absolute;
    bottom: 2px;
    right: 14px;
    background: #fff;
    font-size: 12px;
    z-index: 999999;
    height: 30px;
    line-height: 32px;
    padding-left: 10px;
    padding-right: 10px;
    color: #595959;
    user-select: none;
  }
}
</style>
