<template>
  <div class="null" v-if="data.length === 0" tabindex="0">no information...</div>
  <div class="SearchEvents" v-else>
    <div
      class="row"
      v-for="item in data"
      :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" v-if="item.cover">
        <img :src="item.cover" alt="item.h3"
          tabindex="0"
          aria-label="Image link"
          :aria-description="item.h3"
        />
      </div>
      <div class="right" :class="{rightAC:!item.p}">
        <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>
</template>

<script>
import { Events } from "./data";
export default {
  name: "SearchEvents",
  props: {
    txt: {
      type: String,
      default: "",
    },
  },
  components: {},
  data() {
    //这里存放数据
    return {
      data: [],
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    // 点击跳转，新窗口打开
    skip(path) {
      window.open(path, "_blank");
    },
    // 封装一个获取数据的方法
    getData(txt) {
      if (txt.trim() === "" || txt.trim().length < 4) {
        this.data = [...Events];
      } else {
        let temp = [];
        temp = Events.filter((v) => {
          return v.h3.includes(txt) || v.p.includes(txt);
        });
        //
        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.data = [...temp];
      }
      this.$emit("update:num", this.data.length);
    },
    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;
  margin-top: 50px;
  text-align: center;
}
.SearchEvents {
  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;
      }
    }
  }
}
</style>