<template>
  <div class="app-container">
    <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-top">
          <div class="t">资讯内容：</div>
          <div :class="isConShow ? 'r t2' : 'r'" @click="isConShow = !isConShow">{{isConShow ? '收起' : '展开'}}</div>
        </div>
        <div :class="isConShow ? 'st-content show' : 'st-content'">
          <div class="st-text">洪楼科技</div>
          <tinymce ref="refContent" v-model="content" :height="800" />
        </div>
      </div>
    </base-form>
    <div class="xl-form scoped-bottom">
      <el-button class="xl-form-btn t4" @click="openSI">从文中选封面图</el-button>
      <el-button class="xl-form-btn t1" @click="close('confirm')">保存</el-button>
      <el-button class="xl-form-btn t2" @click="close">关 闭</el-button>
    </div>
    <select-img 
      :isShow="isSIShow"
      :imgTempArr="imgTempArr"
      @close="closeSI"
    />
  </div>
</template>
<script>
import SelectImg from './components/popup/SelectImg'
import Tinymce from '@/components/Tinymce'
export default {
  components: { Tinymce, SelectImg },
  mixins,
  data() {
    return {
      formData: [],
      loading: true,
      cObj: {},
      content: '',
      isConShow: false,
      imgTempArr: [],
      isSIShow: false,
    }
  },
  created () {
    const query = this.$route.query
    if (query.id) {
      this.$api.house.adminformationdetail({id: query.id}).then(res => {
        let curData = res || {}
        this.cObj = curData || {}
        this.content = curData.content || ''
        this.getDef()
      })
    } else {
      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: {
    openSI () {
      let regex = new RegExp(/src="https:\/\/img2.honglounews.com([^"]*?)(png|jpg)/g)
      let s = this.content.match(regex)
      if (!s) {
        this.$msgw('未找到图片，请先检查~')
        this.isConShow = true
        return
      }
      let imgTempArr = []
      for (var i = 0; i < s.length; i++) {
        imgTempArr.push(s[i].replace('src="', ''))
      }
      this.imgTempArr = imgTempArr
      this.isSIShow = true
    },
    closeSI (str) {
      this.isSIShow = false
      if (str) {
        const oldform = this.$refs.ruleForm.baseForm
        this.setDefaultValue({...oldform, pri_image: str})
      }
    },
    getDef() {
      const params = { ...this.cObj }
      const disabled = false
      if (!params.author) params.author = '洪楼Plus'
      if (!params.hide_status) params.hide_status = '1'
      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', rules: 1 },
        { label: '分类', key: 'information_category', rules: 1, type: 'select', class: 'c-2', options: this.$dictData.information_category, changeHandle: this.cateChange },
        { label: '作者', key: 'author', class: 'c-2', rules: 1 },
        { label: '主图', key: 'pri_image', type: 'cuImg', rules: 1,
          options: {
            w: 375,
            h: 262,
          }, class: 'c-2' },
        { label: '状态', key: 'hide_status', rules: 1, type: 'select', class: 'c-2', options: this.$dictData.hide_status },
        { label: '时间', key: 'create_at', rules: 1, type: 'datePicker', type2: 'date', class: 'c-2'},
        { label: '精选', key: 'select_status', type: 'select', class: 'c-2', options: this.$dictData.select_status },
        { 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.cObj.id) newForm.id = this.cObj.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.$router.push('/news/index')
            })
          }
        })
      } else {
        this.$emit('close')
        this.$router.push('/news/index')
        this.setDefaultValue()
      }
    }
  }
}
</script>
<style lang="scss" scoped>
@import '../../styles/libEdit.scss';
.app-container {
  background: #f9f9f9;
}
.lib-edit {
  width: 900px;
  padding: 20px 20px 60px;
  margin: 0 auto;
  background: #fff;
  ::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: 280px;
    }
    .img {
      width: 280px;
    }
  }
}

.scoped-textarea {
  width: 100%;
  position: relative;
  .st-top {
    padding-bottom: 10px;
    user-select: none;
    .t {
      display: inline-block;
      vertical-align: middle;
      font-size: 14px;
      color: #2d8cf0;
      font-weight: bold;
    }
    .r {
      display: inline-block;
      vertical-align: middle;
      color: #fff;
      padding: 4px 10px;
      border-radius: 6px;
      background: #2d8cf0;
      font-weight: bold;
      font-size: 12px;
      cursor: pointer;
      &.t2 {
        background: #19be6b;
      }
    }
  }
  .st-content {
    height: 0;
    transition: 1s;
    overflow: hidden;
    .st-text {
      display: none;
    }
    &.show {
      height: 800px;
      .st-text {
        display: block;
      }
    }
  }
  .st-text {
    position: absolute;
    bottom: 1px;
    right: 14px;
    background: #fff;
    font-size: 12px;
    z-index: 9;
    height: 16px;
    line-height: 16px;
    padding-left: 10px;
    padding-right: 10px;
    color: #595959;
    user-select: none;
  }
}

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

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