// packageE/healthy/component/healthy_Res/healthy_Res.js

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    resultData: {
      type: Object,
      value: {},
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    query: null,
    mouseText: "",
    bmiText: {
      text:
        "根据WS/T 428-2013成人体重判定，您已经非常苗条了。前往不要盲目节食减肥。根据WS/T 428-2013成人体重判定，您已经非常苗条了。前往不要盲目节食减肥。根据WS/T 428-2013成人体重判定，您已经非常苗条了。前往不要盲目节食减肥。根据WS/T 428-2013成人体重判定，您已经非常苗条了。前往不要盲目节食减肥",
      tempText:
        "根据WS/T 428-2013成人体重判定，您已经非常苗条了。前往不要盲目节食减肥。根据WS/T 428-2013成人体重判定，您已经非常苗条了。前往不要盲目节食减肥。根据WS/T 428-2013成人体重判定，您已经非常苗条了。前往不要盲目节食减肥。根据WS/T 428-2013成人体重判定，您已经非常苗条了。前往不要盲目节食减",
      mouseText: "",
      optFlag: false,
      curOpt: "close", //open / close
    },
    whiText: {
      text:
        "根据WS/T 428-2013成人体重判定，您已经非常苗条了。前往不要盲目节食减肥。根据WS/T 428-2013成人体重判定，您已经非常苗条了。前往不要盲目节食减肥。根据WS/T 428-2013成人体重判定，您已经非常苗条了。前往不要盲目节食减肥。根据WS/T 428-2013成人体重判定，您已经非常苗条了。前往不要盲目节食减肥",
      tempText:
        "根据WS/T 428-2013成人体重判定，您已经非常苗条了。前往不要盲目节食减肥。根据WS/T 428-2013成人体重判定，您已经非常苗条了。前往不要盲目节食减肥。根据WS/T 428-2013成人体重判定，您已经非常苗条了。前往不要盲目节食减肥。根据WS/T 428-2013成人体重判定，您已经非常苗条了。前往不要盲目节食减",
      mouseText: "",
      optFlag: false,
      curOpt: "close", //open / close
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    gotoDetail(event) {
      let item = event.currentTarget.dataset.item;
      wx.navigateTo({
        url: "/packageA/detail_v2/detail_v2?id=" + item.goods_id,
      });
    },
    getViewRect(view) {
      let query = this.data.query;
      return new Promise((resolve, reject) => {
        query
          .select(view)
          .boundingClientRect(function (rect) {
            resolve(rect);
          })
          .exec();
      });
    },
    setBmiTextStatus() {
      let status = this.data.bmiText.curOpt;
      if (status == "close") {
        this.setData({
          "bmiText.tempText": this.data.bmiText.text,
          "bmiText.curOpt": "open",
        });
      } else if (status == "open") {
        this.setData({
          "bmiText.tempText": this.data.bmiText.mouseText,
          "bmiText.curOpt": "close",
        });
      }
    },
    setWhiTextStatus() {
      let status = this.data.whiText.curOpt;
      if (status == "close") {
        this.setData({
          "whiText.tempText": this.data.whiText.text,
          "whiText.curOpt": "open",
        });
      } else if (status == "open") {
        this.setData({
          "whiText.tempText": this.data.whiText.mouseText,
          "whiText.curOpt": "close",
        });
      }
    },
    async setBmiText() {
      let rect1 = await this.getViewRect(".bmiText .text");
      let rect2 = await this.getViewRect(".bmiText .text .content");
      if (rect1.height >= rect2.height) return;

      let chr = this.data.bmiText.text.split(""); //这个方法是将一个字符串分割成字符串数组
      let temp = "";
      let count = chr.length;

      let maxWidth = rect1.width * 2 - rect1.width / 2;
      this.setData({ mouseText: "" });
      for (let i = 0; i < count; i++) {
        let rect2 = await this.getViewRect(".mouseText");
        if (rect2.width < maxWidth) {
          temp += chr[i];
          this.setData({
            mouseText: temp,
          });
        } else break;
      }
      this.setData({
        "bmiText.mouseText": temp + "...",
        "bmiText.tempText": temp + "...",
        "bmiText.optFlag": true,
        "bmiText.curOpt": "close",
      });
    },
    async setWhiText() {
      console.log("执行3");
      let rect1 = await this.getViewRect(".whiText .text");
      let rect2 = await this.getViewRect(".whiText .text .content");
      if (rect1.height >= rect2.height) return;

      let chr = this.data.whiText.text.split(""); //这个方法是将一个字符串分割成字符串数组
      let temp = "";
      let count = chr.length;

      let maxWidth = rect1.width * 2 - rect1.width / 2;
      this.setData({ mouseText: "" });
      for (let i = 0; i < count; i++) {
        let rect2 = await this.getViewRect(".mouseText");
        if (rect2.width < maxWidth) {
          temp += chr[i];
          this.setData({
            mouseText: temp,
          });
        } else break;
      }
      this.setData({
        "whiText.mouseText": temp + "...",
        "whiText.tempText": temp + "...",
        "whiText.optFlag": true,
        "whiText.curOpt": "close",
      });
    },
  },
  async ready() {
    // this.data.query = this.createSelectorQuery();
    // await this.setBmiText();
    // await this.setWhiText();
  },
});
