const _Http = getApp().globalData.http;
let editor = null;
Component({
  options: {
    addGlobalClass: true
  },
  data: {
    lookItem: null,
    show: false,
    height: 0,
  },
  methods: {
    getList() {
      _Http.basic({
        id: 20221111090904,
        content: {
          nocache: true,
          "pageNumber": 1,
          "pageSize": 9999,
          "pageTotal": 1,
          "where": {
            "condition": "",
            isread: 0
          }
        }
      }).then(res => {
        console.log("未读通告", res)
        if (res.msg != '成功') return;
        if (res.data.length) {
          res.data = res.data.map(v => {
            v.inquire = false;
            return v
          });
          this.setData({
            lookItem: res.data[0],
            list: res.data,
            show: true
          });
          this.getDetail();
        }
      })
    },
    getDetail() {
      let lookItem = this.data.lookItem;
      _Http.basic({
        "classname": "saletool.notice.notice",
        "method": "queryNoticeMain",
        "content": {
          "sat_noticeid": lookItem.sat_noticeid
        }
      }).then(res => {
        console.log("通告详情", res)
        if (res.msg != '成功') return;
        lookItem.content = res.data.content;
        lookItem.inquire = true;
        this.data.list[lookItem.rowindex - 1].content = res.data.content;
        this.setData({
          lookItem
        });
        this.rander(lookItem.content)
      });
    },
    async rander(html) {
      this.setHeight().then(s => {
        if (editor == null) {
          wx.createSelectorQuery().in(this).select('#editor').context(function (res) {
            editor = res.context;
            this.rander(html)
          }).exec()
        } else {
          editor.setContents({
            html,
            complete: res => {
              console.log("渲染富文本", res)
              if (this.data.height == 0) setTimeout(() => {
                this.rander(html)
              }, 50)
            }
          })
        };
      })

    },
    setHeight() {
      return new Promise(async (resolve) => {
        let boxHeight = await this.queryElement(".box", "height"),
          headHeight = await this.queryElement(".head", "height"),
          bottomHeight = await this.queryElement(".bottom", "height"),
          height = boxHeight - headHeight - bottomHeight - 5;
        this.setData({
          height
        })
        resolve(height)
      })
    },
    queryElement(element, field) {
      return new Promise((resolve, reject) => {
        wx.createSelectorQuery().in(this).select(element).boundingClientRect().exec(res => {
          resolve(field ? res[0][field] : res[0])
        })
      })
    },
    changeLook(e) {
      let lookItem = this.data.lookItem;
      this.rander("")
      switch (e.target.id) {
        case "previous":
          lookItem = this.data.list[lookItem.rowindex - 2];
          this.setData({
            lookItem
          });
          lookItem.inquire ? this.rander(lookItem.content) : this.getDetail();
          break;
        case "next":
          lookItem = this.data.list[lookItem.rowindex];
          this.setData({
            lookItem
          });
          lookItem.inquire ? this.rander(lookItem.content) : this.getDetail();
          break;
        default:
          console.log("关闭")
          this.setData({
            show: false
          })
          break;
      }
    }
  },
  lifetimes: {
    attached() {
      wx.createSelectorQuery().in(this).select('#editor').context(function (res) {
        editor = res.context;
      }).exec()
      this.getList()
    }
  }
})