<!--  -->
<template>
  <div class="NNPE-ArticleView" v-if="curQue">
    <AudioLine
      :mp3="curQue.mp3_list[0].url"
      :getCurTime="getCurTime"
      ref="audioLine"
    />
    <template v-if="resArr.length > 0">
      <div class="NPC-sentences-list">
        <div
          :class="['NNPE-detail', item.isTitle ? 'NNPE-detail-title' : '']"
          v-for="(item, index) in resArr"
          :key="'detail' + index"
        >
          <div class="wordsList-box">
            <img :src="articleImg[index]" v-if="articleImg[0] && index == 0" />
            <div>
              <div
                class="NNPE-words"
                v-for="(pItem, pIndex) in item.wordsList"
                :key="'wordsList' + pIndex"
                :class="[pItem.wordIndex == 0 ? 'textLeft' : 'textCenter']"
                @click="showWordDetail($event, pItem.chs)"
              >
                <template v-if="!pItem.width">
                  <template v-if="pItem.isShow">
                    <template
                      v-if="
                        item.wordsList[pIndex + 1].chs &&
                        chsFhList.indexOf(item.wordsList[pIndex + 1].chs) > -1
                      "
                    >
                      <span class="NNPE-words-box">
                        <span
                          class="NNPE-pinyin"
                          :class="[pItem.className ? pItem.className : '']"
                          >{{ pItem.pinyin }}</span
                        >
                        <span
                          :class="[
                            'NNPE-chs',
                            newWordList.indexOf(pItem.chs) > -1 ? 'active' : '',
                          ]"
                          >{{ pItem.chs }}</span
                        >
                      </span>
                      <span class="NNPE-words-box">
                        <span class="NNPE-pinyin" style="text-align: left">{{
                          item.wordsList[pIndex + 1].pinyin
                        }}</span>
                        <span class="NNPE-chs" style="text-align: left">{{
                          item.wordsList[pIndex + 1].chs
                        }}</span>
                      </span>
                    </template>
                    <template v-else>
                      <span
                        class="NNPE-pinyin"
                        :class="[
                          pItem.padding ? 'padding' : '',
                          pItem.className ? pItem.className : '',
                        ]"
                        >{{ pItem.pinyin }}</span
                      >
                      <span
                        class="NNPE-chs"
                        :class="[
                          pItem.padding ? 'padding' : '',
                          newWordList.indexOf(pItem.chs) > -1 ? 'active' : '',
                        ]"
                        >{{ pItem.chs }}</span
                      >
                    </template>
                  </template>
                </template>
                <template v-else>
                  <span
                    :style="{
                      height: pItem.height + 'px',
                      width: pItem.width + 'px',
                    }"
                  ></span>
                </template>
              </div>
            </div>
            <img :src="articleImg[index + 1]" v-if="articleImg[index + 1]" />
          </div>
        </div>
      </div>
    </template>
    <template v-if="isShow">
      <div
        class="NNPE-wordDetail"
        :style="{ top: top + 'px', left: left + 'px' }"
      >
        <Wordcard :word="word" :changeWordCard="changeWordCard" />
      </div>
    </template>
  </div>
</template>

<script>
import { timeStrToSen } from "@/utils/index";
import AudioLine from "../AudioLine.vue";
import Wordcard from "../components/Wordcard.vue"; // 卡片
export default {
  name: "PhraseModelChs",
  props: ["curQue", "bodyLeft", "NNPENewWordList"],
  components: {
    AudioLine,
    Wordcard,
  },
  data() {
    return {
      resArr: [],
      curTime: 0, //单位s
      chsFhList: ["，", "。", "“", "：", "》", "《", "？", "！", "；"],
      enFhList: [",", ".", ";", "?", "!", ":", ">", "<"],
      newWords: ["鱼", "辩礼义"],
      oldHz: "",
      hz: "",
      top: 0,
      left: 0,
      articleImg: {}, // 文章图片
      newWordList: [],
      word: null,
      isShow: false,
    };
  },
  computed: {},
  watch: {
    hz: {
      handler: function (val, oldVal) {
        let _this = this;
        if (val) {
          _this.handleNewWords(val, _this.top, _this.left);
        }
      },
      // 深度观察监听
      deep: true,
    },
  },
  //方法集合
  methods: {
    getCurTime(curTime) {
      this.curTime = curTime;
    },
    handleData() {
      let resArr = [];
      let leg = this.curQue.detail.length;
      let curQue = JSON.parse(JSON.stringify(this.curQue));
      curQue.detail.forEach((dItem, dIndex) => {
        let paraArr = [
          {
            pinyin: "",
            chs: "",
            width: 20,
            height: 20,
          },
          {
            width: 20,
            height: 20,
            pinyin: "",
            chs: "",
          },
        ];
        dItem.wordsList.forEach((sItem, sIndex) => {
          sItem.forEach((wItem, wIndex) => {
            //this.judgePad(sItem, wItem, wIndex);
            this.mergeWordSymbol(sItem, wItem, wIndex);
            let obj = {
              paraIndex: dIndex, //段落索引
              sentIndex: sIndex, //在段落中句子索引
              wordIndex: wIndex, //单词的索引
              pinyin: wItem.pinyin,
              chs: wItem.chs,
              padding: true, //wItem.padding,
              className: wItem.className,
              isShow: wItem.isShow,
              isNewWord: this.newWords.indexOf(wItem.chs) > -1 ? true : false,
            };
            paraArr.push(obj);
          });
        });

        let paraObj = {
          wordsList: paraArr,
        };
        resArr.push(paraObj);
      });
      this.resArr = resArr;
      console.log(this.resArr);

      // 循环文章图片
      if (curQue.img_list) {
        curQue.img_list.forEach((item) => {
          this.articleImg[item.imgNumber] = item.url;
        });
      }
    },
    //词和标点合一起
    mergeWordSymbol(sItem, wItem, curIndex) {
      let leg = sItem.length;
      if (curIndex < leg - 1) {
        let nextIndex = curIndex + 1;
        let chs = sItem[nextIndex].chs;
        let pinyin = sItem[nextIndex].pinyin;
        // if (this.chsFhList.indexOf(chs) > -1) {
        //   wItem.chs = "<a>" + wItem.chs + "</a><a>" + chs + "</a>";
        //   wItem.pinyin = "<a>" + wItem.pinyin + "</a><a>" + pinyin + "</a>";
        // }
        if (this.chsFhList.indexOf(wItem.chs) > -1) {
          wItem.isShow = false;
        } else {
          wItem.isShow = true;
        }
      }

      // if (this.enFhList.indexOf(wItem.pinyin) > -1) {
      //   wItem.className = "textLeft";
      // }
    },
    //判断是否有padding
    judgePad(sItem, wItem, curIndex) {
      let leg = sItem.length;
      if (curIndex < leg - 1) {
        let nextIndex = curIndex + 1;
        let chs = sItem[nextIndex].chs;
        if (
          this.chsFhList.indexOf(chs) > -1 ||
          this.chsFhList.indexOf(wItem.chs) > -1
        ) {
          wItem.padding = false;
        } else {
          wItem.padding = true;
        }
        if (this.enFhList.indexOf(wItem.pinyin) > -1) {
          wItem.className = "textLeft";
        }
      }
    },
    //转化时间
    handleTimeList(list) {
      let listRes = [];
      list.forEach((item) => {
        let res = timeStrToSen(item);
        listRes.push(res);
      });
      return listRes;
    },
    //点击播放某个句子
    handleChangeTime(time) {
      this.curTime = time;
      this.$refs.audioLine.onTimeupdateTime(time);
    },
    handleNewword() {
      let NewWordList = [];
      this.NNPENewWordList.forEach((item) => {
        NewWordList.push(item.new_word);
      });
      this.newWordList = JSON.parse(JSON.stringify(NewWordList));
    },

    showWordDetail(e, word) {
      let _this = this;
      if (_this.oldHz != word) {
        this.isShow = false;
        setTimeout(() => {
          _this.hz = word;
        }, 50);
      }
      _this.top = e.clientY + 20;
      let left = e.clientX;
      let width = 0;
      if (word.length == 1 || word.length == 2) {
        width = 304;
      } else if (word.length == 3 || word.length == 4) {
        width = 432;
      } else if (word.length > 3) {
        width = 560;
      }
      if (left - this.bodyLeft > this.contentWidth / 2) {
        _this.left = left - width;
      } else {
        _this.left = left;
      }
    },
    hideWordDetail() {
      this.isShow = false;
    },
    changeWordCard(isShow) {
      this.isShow = isShow;
      this.oldHz = "";
      this.hz = "";
    },
    // 处理分词数据
    handleNewWords(val, top, left) {
      debugger;
      this.isShow = true;
      this.word = null;
      for (let i = 0; i < this.NNPENewWordList.length; i++) {
        let item = this.NNPENewWordList[i];
        if (item.new_word.trim() == val.trim()) {
          let wordlist = val.split("");
          this.word = { list: wordlist, detail: item, top: top, left: left };
          break;
        }
      }
      this.oldHz = val;
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    console.log(this.curQue);
    if (this.curQue) {
      this.handleData();
    }
    console.log("生词");
    console.log(this.NNPENewWordList);
    if (this.NNPENewWordList && this.NNPENewWordList.length > 0) {
      this.handleNewword();
    }
  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang='scss' scoped>
//@import url(); 引入公共css类
.NNPE-ArticleView {
  width: 100%;
  .NPC-sentences-list {
    padding: 24px 0;
  }
  .NNPE-detail {
    clear: both;
    overflow: hidden;
    .NNPE-words {
      float: left;
      padding: 0 0px 8px 0px;
      &-box {
        float: left;
        > span {
          display: block;
          &.NNPE-pinyin {
            font-family: "GB-PINYINOK-B";
            font-weight: normal;
            font-size: 14px;
            line-height: 150%;
            color: #000000;
            height: 21px;
            &.textLeft {
              text-align: left;
            }
          }
          &.NNPE-chs {
            font-family: "FZJCGFKTK";
            font-size: 24px;
            line-height: 150%;
            color: #000000;
            &.active {
              color: #e35454;
            }
          }
          &.padding {
            padding: 0 3px;
          }
        }
      }
      &.textLeft {
        text-align: left;
      }
      &.textCenter {
        text-align: center;
      }
      > span {
        display: block;
        &.NNPE-pinyin {
          font-family: "GB-PINYINOK-B";
          font-weight: normal;
          font-size: 14px;
          line-height: 150%;
          color: #000000;
          height: 21px;
          &.textLeft {
            text-align: left;
          }
        }
        &.NNPE-chs {
          font-family: "FZJCGFKTK";
          font-size: 24px;
          line-height: 150%;
          color: #000000;
          &.active {
            color: #e35454;
          }
        }
        &.padding {
          padding: 0 3px;
        }
      }
    }
    &.NNPE-detail-title {
      .wordsList-box {
        > div {
          display: flex;
          justify-content: center;
        }
      }
    }
    .index {
      width: 48px;
      box-sizing: border-box;
      padding: 8px;
      text-align: right;
      border-right: 1px solid rgba(0, 0, 0, 0.1);
      b {
        font-weight: 400;
        color: #000000;
        line-height: 1.5;
      }
    }
    .wordsList-box {
      width: 100%;
      padding: 6px 24px 12px 24px;
      > div {
        overflow: hidden;
        clear: both;
      }
      > img {
        width: 100%;
        display: block;
      }
    }
  }
  .NNPE-wordDetail {
    position: fixed;
    //   width: 260px;
    //   height: 200px;
    //   background: #cc0;
  }
}
</style>