<template>
  <div class="null" v-if="data.length === 0" tabindex="0">
    no information...
  </div>
  <div class="SearchLearn" v-else>
    <div
      class="row"
      v-for="item in data[pageSize - 1]"
      :key="item.id"
      @click="skip(item.path)"
      @keydown.enter.passive="skip(item.path)"
      tabindex="0"
      aria-label="Link"
      :aria-description="item.h3"
    >
      <div class="left">
        <img
          :src="item.cover"
          :alt="item.h3"
          tabindex="0"
          aria-label="Image link"
          :aria-description="item.h3"
        />
      </div>
      <div class="right">
        <h3 v-html="item.h3" tabindex="0" aria-label="Link"></h3>
        <p v-html="item.p" tabindex="0" aria-label="Link"></p>
      </div>
    </div>
    <!-- 分页 -->
    <div class="page">
      <span
        :class="{ active: pageSize === i }"
        v-for="i in data.length"
        :key="i"
        @click="pageChange(i)"
        @keydown.enter.passive="pageChange(i)"
        aria-label="Link"
      >
        {{ i }}
      </span>
    </div>
  </div>
</template>

<script>
import { LearnEngage } from "./data";
export default {
  name: "SearchLearn",
  props: {
    txt: {
      type: String,
      default: "",
    },
  },
  components: {},
  data() {
    //这里存放数据
    return {
      data: [],
      pageSize: 1,
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    // 切换分页
    pageChange(val) {
      this.pageSize = val;
      window.scrollTo({ top: 0, behavior: "smooth" });
    },
    // 点击跳转，新窗口打开
    skip(path) {
      window.open(path, "_blank");
    },
    // 封装一个获取数据的方法
    getData(txt) {
      this.pageSize = 1;
      let temp = [];
      if (txt.trim() === "" || txt.trim().length < 4) {
        temp = [...LearnEngage];
      } else {
        temp = LearnEngage.filter((v) => {
          return (
            v.h3.toLowerCase().includes(txt.toLowerCase()) ||
            v.p.toLowerCase().includes(txt.toLowerCase())
          );
        });
        //
        temp = temp.map((v) => {
          return {
            ...v,
            h3: v.h3.replaceAll(txt, `<span style='color:red;'>${txt}</span>`),
            p: v.p.replaceAll(txt, `<span style='color:red;'>${txt}</span>`),
          };
        });
      }
      // 把结果长度给父亲显示
      this.$emit("update:num", temp.length);

      // 手动处理格式分页
      let pageNum = Math.ceil(temp.length / 10);
      let tempArrAll = [];
      for (let i = 0; i < pageNum; i++) {
        tempArrAll.push(temp.slice(i * 10, (i + 1) * 10));
      }
      this.data = tempArrAll;
    },
    searchBtn(txt) {
      this.getData(txt);
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.getData(this.txt);
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang='less' scoped>
.null {
  font-size: 30px;
  height: 514px;
  line-height: 500px;
  text-align: center;
}
.SearchLearn {
  padding-bottom: 20px;
  .row {
    cursor: pointer;
    background-color: #fff;
    border: 1px solid #c8c8c8;
    margin-bottom: 20px;
    padding: 20px;
    zoom: 1;
    overflow: hidden;
    display: flex;
    .left {
      width: 180px;
      text-align: center;
      & > img {
        width: 150px;
      }
    }
    .right {
      flex: 1;
      & > h3 {
        font-weight: 700;
        font-size: 14px;
        line-height: 30px;
      }
      & > p {
        font-size: 14px;
        color: #a6a6a6;
        line-height: 24px;
      }
    }
  }
  .page {
    display: flex;
    justify-content: center;
    padding-bottom: 30px;
    & > span {
      width: 30px;
      height: 30px;
      text-align: center;
      line-height: 30px;
      margin-right: 15px;
      border-radius: 50%;
      cursor: pointer;
    }
    .active {
      background-color: #bf2323;
      color: #fff;
      pointer-events: none;
    }
  }
}
</style>