<template>
  <div class="technology-container">
    <div class="nav" stype="height:500px">
      <!--搜索框-->
      <el-row class="search-nav" style="padding: 30px 0 20px 0">
        <el-col :span="6">
          <div class="pull-left" @click="gotoHome" style="cursor: pointer">
            <img class="logo-img" src="../../assets/img/logo-blue.png" />
            <span class="logo-title">群智众测平台</span>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="search-nav">
            <div id="search-block " class="">
              <div class="search-input">
                <el-tabs v-model="searchType" type="card" @tab-click="handleTypeClick">
                  <el-tab-pane v-for="item in searchTypeArr" :label="item.label" :name="item.value" :key="item.value"></el-tab-pane>
                </el-tabs>
                <el-input placeholder="请输入内容" v-model="searchVal" class="input-with-select">
                  <el-button class="search-button" slot="append"  type="primary" @click="loadTechnologyArticles(1)">搜索</el-button>
                </el-input>
              </div>
            </div>
          </div>
        </el-col>
        <el-col :span="6">
          <el-button type="primary pull-right" class="releaseBtn" @click="checkLogin()">免费发布众测需求</el-button>
        </el-col>
      </el-row>
      <!--TabNav-->
      <el-tabs v-model="searchType" @tab-click="handleTabClick" class="square-tab">
        <el-tab-pane v-for="item in searchTypeArr" :name="item.value" :key="item.value">
          <span slot="label" style="font-size: 18px">{{item.name}}</span>
        </el-tab-pane>
      </el-tabs>
      <div>
      </div>
    </div>
    <div class="container" style="margin: 15px auto">
      <el-row>
        <el-col :span="18" class="project-task">
          <TechnologyCard
            :techArticleList = techArticleList.content v-if="searchType=='0'" @refreshList="loadTechnologyArticles(activePage)">
          </TechnologyCard>
          <TechnologyMG :techArticleList = techArticleList.content v-if="searchType=='1'"></TechnologyMG>
          <el-pagination
            v-if="techArticleList.content&&techArticleList.content.length"
            :page-size="10"
            layout="prev, pager, next"
            :total="techArticleList.totalElements"
            @current-change="handlePageChange"
            :current-page = activePage
            class="pull-right"
          >
          </el-pagination>
        </el-col>
        <el-col :span="6"  class="popular-modules">
          <HotActicle :articleArr="articleArr" @refreshHotArticle="loadHotArticles"/>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
  import Http from '@/js/http.js'
  import SearchBar from '../../components/commons/SearchBar'
  import TechnologyCard from './TechnologyCard'
  import TechnologyMG from './TechnologyMG'
  import HotActicle from './HotActicle'
  import PopularProject from '../Square/PopularProject'
  import {storageGet} from '@/js/index.js'
  import {notify} from "../../constants";
  import qs from 'querystring'
  import { mapActions ,mapGetters} from 'vuex';

  export default {
    name: 'Technology2.0',
    components: {
      SearchBar,
      TechnologyCard,
      TechnologyMG,
      PopularProject,
      HotActicle
    },
    data(){
      return {
        isLogin: false,
        searchVal: '',
        searchType:'',
        searchTypeArr:[
          {
            "name": "技术文章",
            "value": "0",
            "label":'文章'
          },
          {
            "name": "论文专著",
            "value": "1",
            "label":'专著'
          }
        ],
        articleArr:[],
        keyword:"",
        activePage:1,
        techArticleList:{},
      }
    },
    methods:{
      loadData(){
          if (storageGet('user') != null) {
              this.isLogin = true;
          }
      },
      checkLogin(){
          if(!this.isLogin){
              console.log("请登录后访问");
              notify('warning','请登录后访问');
          }else{
              console.log("已登录");
              this.$router.push('/project/create');
          }
      },
      gotoHome(){
        this.$router.push('/home');
      },
      handleTypeClick(tab){
        this.handleTabClick(tab);
      },
      handleTabClick(tab){
        this.searchType = tab.name
        this.activePage = 1;
        this.searchVal = '';
        this.loadTechnologyArticles(1);
      },
      loadHotArticles(){
        Http.get('/api/technical/ranking').then((res)=>{
          this.articleArr = res.data.technicalArticlesPage.content;
        })
      },
      loadTechnologyArticles(index){
        // let url=`/api/technical/articles?searchCondition={"keyword":"${this.keyword}","activePage":${this.activePage}}`;
        // Http.get(encodeURI(url))
        let url = '/api/technical/articles';
        this.activePage = index;
        let params = {
          "keyword": this.searchVal,//查询条件，分页展示时就不填
          "activePage": this.activePage,//指定页数
          "columnFilters": [
            {
              "field": "type",
              "type": "enums",
              "enums": this.searchTypeArr,
              "value": this.searchType//展示技术文章value为0
            }
          ]
        }
        Http.post(url,params)
          .then(res=>{
            if(res.data.technicalArticlesPage){
              this.techArticleList = res.data.technicalArticlesPage;
              this.searchType = '0';
            }
            else if(res.data.publicationsPage){
              this.techArticleList = res.data.publicationsPage;
              this.searchType = '1';
            }
          })
      },
      handlePageChange(index){
        this.activePage = index;
        this.loadTechnologyArticles(index);
      }
    },
    mounted() {
      this.loadData();
      this.loadHotArticles();
      this.loadTechnologyArticles(1);
    }
  }
</script>

<style lang="less">
  @import "../../style/search-nav";
  .technology-container {
    /deep/ .search-input,/deep/ .releaseBtn{
      margin: 20px 0 0 0 !important;
    }

    .popular-modules {
      padding-left: 15px;
    }

  }

</style>
