<template>
  <div class="search">
    <div class="tab">
      <div
        :class="['tab-item', activeTab == item ? 'active' : '']"
        @click="handleClickTab(item)"
        v-for="(item, index) in tabs"
        :key="index"
      >
        {{ item }}
      </div>
    </div>
    <div class="search-input">
      <el-input
        placeholder="输入关键词搜索，马上阅读"
        v-model="keyword"
        clearable
        @keyup.native.enter="handleSearch"
      ></el-input>
      <el-button icon="el-icon-search" type="primary" @click="handleSearch"
        >搜索
      </el-button>
    </div>
    <div class="years">
      年份：
      <el-radio-group v-model="currentYear">
        <el-radio
          v-for="(item, index) in years"
          :key="index"
          :label="item"
        ></el-radio>
      </el-radio-group>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    hasType: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      leftTabActiveName: "按类别",
      tabs: ["报纸", "论文", "会议", "期刊"],
      years: [
        "2020",
        "2019",
        "2018",
        "2017",
        "2016",
        "2015",
        "2014",
        "2013",
        "2012",
      ],
      activeTab: "报纸",
      keyword: "",
      currentYear: "2020",
    };
  },
  watch: {
    currentYear(value) {
      this.handleSearch();
    },
  },
  methods: {
    handleClickTab(item) {
      this.activeTab = item;
      this.handleSearch();
    },
    // 搜索关键词、年份、点击类别时需要重置页码
    handleSearch() {
      const refresh = true;
      this.$emit("search", refresh);
    },
  },
};
</script>

<style lang="scss" scoped>
.search {
  background-color: #fff;
  padding: 20px;
  border-radius: 8px;
  .tab {
    display: flex;
    margin-bottom: 20px;
    .tab-item {
      margin-right: 20px;
      padding: 4px 20px;
      cursor: pointer;
      &:hover {
        color: $theme-blue;
        font-weight: bold;
      }
    }
    .active {
      background-color: rgba($color: $theme-blue, $alpha: 0.1);
      border-radius: 4px;
      color: $theme-blue;
      font-weight: bold;
      position: relative;
      &::after {
        position: absolute;
        width: 0;
        height: 0;
        content: "";
        border: 8px solid rgba($color: $theme-blue, $alpha: 0.1);
        border-left: 8px solid transparent;
        border-right: 8px solid transparent;
        border-bottom: 8px solid transparent;
        bottom: -16px;
        left: 50%;
        transform: translateX(-50%);
      }
    }
  }
  .search-input {
    display: flex;
    .el-button {
      margin-left: 20px;
    }
    ::v-deep .el-input {
      input {
        background-color: #f7f8fa;
        border: none;
      }
    }
  }
  .years {
    display: flex;
    align-items: center;
    padding: 20px 10px 10px;
  }
}
</style>
