<template>
  <div class="content">
    <lucency-header :lang="lang" page-key="news" :is-phone="isPhone"/>
    <item-banner :lang="lang" :title="`新闻中心`" :sub-title="`行业资讯,高新技术一应俱全`"/>
    <show-types :lang="lang" :type="type" :types="types"></show-types>
    <solution-list :lang="lang" :solution-list="news" :parent-type="'news'"></solution-list>
    <page-select :page="page" :count="nowCount" :total="nowTotal"></page-select>

    <default-footer :lang="lang"/>

    <site-bar></site-bar>
  </div>
</template>

<script>
import axios from "axios";
import qs from "qs";

import langMap from "@/map/langMap";
import {handle} from "@/until/handle";
import {isMediaView} from "@/until/mediaView";
import LucencyHeader from "~/components/header/lucencyHeader.vue";
import ItemBanner from "~/components/banner/itemBanner.vue";
import DefaultFooter from "~/components/footer/defaultFooter.vue";
import {apiMap, baseUrl} from "~/map/apiMap";

const pageLimit = 5;
export default {
  name: "newIndex",
  components: {DefaultFooter, ItemBanner, LucencyHeader},
  props:['uLang','pType','pKey','pNews','pPageData'],
  head(){
    let headInfo = {
      meta: []
    }
    if (this.title)
    {
      headInfo.title = this.title;
    }
    if (this.seoDescription)
    {
      headInfo.meta.push({
        hid: "description",
        name: "description",
        content: this.seoDescription,
      })
    }
    if(this.seo_key)
    {
      headInfo.meta.push({
        hid: "keywords",
        name: "keywords",
        content: this.seo_key,
      })
    }
    return headInfo
  },
  async asyncData(ctx){
    let url = baseUrl + apiMap.searchNews.path;
    url += `?type=all&p=1`
    let [err,res] = await handle(axios.get( url));
    if(err){
      console.log(err);
      return {};
    }
    let title = "合方圆-新闻中心"
    let seo_key = "合方圆,合方圆科技,合方圆新闻";
    let seoDescription = "合方圆科技最新新闻,公司动态,产品应用";

    let result = res.data;
    if(result.code === 1){
      let pageData = {
        limit: result.limit,
        page: result.page,
        total: result.total,
        count: result.count,
      }
      return {
        title,
        seo_key,
        seoDescription,
        news: result.data,
        basePageData: pageData,
      }
    }else{
      console.error(result.msg);
      console.log(result);
      return {
        title,
        seo_key,
        seoDescription,
        news:[]
      }
    }
  },
  data(){
    return {
      title: '',
      seo_key: '',
      seoDescription: '',
      lang: this.uLang?this.uLang:langMap.lang.cn,
      type: this.pType?this.pType:'all',
      types: this.$store.getters.newsTypes,
      key: this.pKey?this.pKey:'',
      page: 1,
      nowCount: 199,
      nowTotal: 2,
      limit:pageLimit,
      news: this.pNews?this.pNews:[],
      basePageData: {},
      pageSave: {

      },
      isPhone: false
    }
  },
  beforeMount() {
    let pageData;
    if (this.pPageData){
      this.basePageData = this.pPageData
    }
    pageData = this.basePageData;
    this.updatePageData(pageData);
  },
  mounted() {
    this.isPhone = isMediaView(0,1024);
    this.$root.$on('changeLang',this.switchLang);
    this.$root.$on('changeType',this.selectType);
    this.$root.$on('changePage',this.changePageHandle);
    // this.$root.$on('changeProductType',this.selectType);
  },
  methods: {
    switchLang(nextLang) {
      if (nextLang) {
        this.lang = nextLang;
      } else {
        if (this.lang === langMap.lang.cn) {
          this.lang = langMap.lang.en
        } else {
          this.lang = langMap.lang.cn
        }
      }
    },
    selectType(nextType) {
      console.log(nextType)
      this.type = nextType;
      this.page = 1;
      this.searchNews();
    },
    changePageHandle(nextPage){
      this.page = nextPage;
      this.searchNews();
    },
    async searchNews(){
      // 获取数据
      let url = apiMap.searchNews.path;
      url += `?key=${this.key}&type=${this.type}&p=${this.page}`
      let [err,res] = await handle(axios.get(url));
      if(err){ console.log(err); return null; }

      let result = res.data;
      if(result.code === 1){
        this.news = result.data;
        let pageData;
        if(result.total){
          // 更新页面信息
          pageData = {
            limit: result.limit,
            page: result.page,
            total: result.total,
            count: result.count,
          }
        }else if(this.pageSave[this.type] && this.pageSave[this.type][this.key]){
          pageData = this.pageSave[this.type][this.key]
        }
        this.updatePageData(pageData);
      }else{
        console.error(result.msg);
        console.log(result);
      }
    },
    updatePageData(pageData){
      if(!this.pageSave[this.type]){
        this.pageSave[this.type] = {}
      }
      if(!this.pageSave[this.type][this.key]){
        this.pageSave[this.type][this.key] = pageData;
      }
      let nowTotal = Math.ceil(pageData.total / pageData.limit);
      let nowCount = pageData.total;
      this.nowTotal = nowTotal;
      this.nowCount = nowCount;
    }
  }
}
</script>

<style scoped>

</style>
