<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="60px" :insertSlotArr="[1]">
        <div class="scoped-textarea"  slot="OI1">
          <div class="st-text">卓泰后台</div>
          <tinymce ref="refContent" v-model="content" :height="500" />
        </div>
      </base-form>
      <div class="xl-form scoped-bottom">
        <el-button class="xl-form-btn t2" @click="close">关 闭</el-button>
        <el-button class="xl-form-btn t1" @click="close('confirm')">确定</el-button>
      </div>
    </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 (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.content = '<p>这里开始</p>\n<p>&nbsp;</p>\n<p style=\"margin: 10px 16px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; clear: both; min-height: 1em; color: #333333; font-family: -apple-system, BlinkMacSystemFont, Arial, sans-serif; font-size: 17px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.544px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #ffffff; line-height: 2em; text-indent: 2em; text-align: center;\"><span style=\"margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; font-size: 16px;\"><span style=\"margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; letter-spacing: 2px;\">-END-</span></span></p>\n<p><span style=\"margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; color: #333333; font-family: -apple-system, BlinkMacSystemFont, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; orphans: 2; text-align: justify; text-indent: 34px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #ffffff; letter-spacing: 2px;\"><img class=\"wscnph\" src=\"https://img2.honglounews.com/20210603040359-8175.gif\" /></span></p>'
          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, changeHandle: this.cateChange },
        { label: '作者', key: 'author', class: 'c-2' },
        { label: '主图', key: 'pri_image', type: 'cuImg',
          options: {
            w: 375,
            h: 262,
          }, class: 'c-2' },
        { label: '状态', key: 'hide_status', type: 'select', class: 'c-2', options: this.$dictData.hide_status },
        { label: '时间', key: 'create_at', type: 'datePicker', type2: 'date'},
        { 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)
    },
    cateChange (val) {
      let params = {...this.$refs.ruleForm.baseForm}
      let imgURL= ''
      if (val === 'hlzt') imgURL= 'http://icon.honglounews.com/news_hlzt.jpg'
      if (val === 'hljy' || val === 'xqsc') imgURL= 'http://icon.honglounews.com/news_hljy.jpg'
      if (val === 'bkfx') imgURL= 'http://icon.honglounews.com/news_bkfx.jpg'
      if (val === 'lppc') imgURL= 'http://icon.honglounews.com/news_lppc.jpg'
      if (val === 'lpwd') imgURL= 'http://icon.honglounews.com/news_lpwd.jpg'

      if (val === 'hlpd') imgURL= 'http://icon.honglounews.com/news_hlpd.jpg'
      if (val === 'lszt') imgURL= 'http://icon.honglounews.com/news_lszt.jpg'
      if (val === 'zcjd') imgURL= 'http://icon.honglounews.com/news_zcjd.jpg'
      if (val === 'esf') imgURL= 'http://icon.honglounews.com/news_esf.jpg'
      if (val === 'lsdt') imgURL= 'http://icon.honglounews.com/news_lsdt.jpg'
      if (val === 'scfx') imgURL= 'http://icon.honglounews.com/news_scfx.jpg'
      params.pri_image = imgURL
      this.setDefaultValue(params)
    },
    fileChange (file) {
      let reader = new FileReader();
      let rs = reader.readAsArrayBuffer(file);
      let blob = null;
      reader.onload = (e) => {
        if (typeof e.target.result === 'object') {
          blob = new Blob([e.target.result])
        } else {
          blob = e.target.result
        }
        console.log(Object.prototype.toString.call(blob));
      }
    },
    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
            }
            newForm.news_type = 'adminadd'
            this.$api.house[apiStr](newForm).then(data => {
              this.$msgs(newForm.id ? '编辑成功' : '新增成功')
              this.$emit('close', newForm)
              this.$refs.refContent.hasChange = false
            })
          }
        })
      } else {
        this.$emit('close')
        this.$refs.refContent.hasChange = false
        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;
  margin-bottom: 20px;
  .st-text {
    position: absolute;
    bottom: 2px;
    right: 14px;
    background: #fff;
    font-size: 12px;
    z-index: 9;
    height: 30px;
    line-height: 32px;
    padding-left: 10px;
    padding-right: 10px;
    color: #595959;
    user-select: none;
  }
}

.scoped-bottom {
  position: fixed;
  bottom: 0;
  right: 0;
  background: #fff;
  width: 960px;
  box-sizing: border-box;
  padding-left: 380px;
  padding-bottom: 20px;
  padding-top: 20px;
  border-top: 1px solid #dcdcdc;
  z-index: 10;
}

::v-deep .el-drawer__body {
  padding-bottom: 30px;
}
</style>
