<template>
  <div class="">
    <lucency-header :lang="lang" page-key="solution" :is-phone="isPhone" />
    <item-banner :lang="lang" :title="`解决方案`" :sub-title="`专业,高效,稳定解决方案`"/>
    <solution-types :lang="lang" :type="type"></solution-types>
    <solution-list :lang="lang" :solution-list="solutions"></solution-list>
    <page-select :page="page" :count="nowCount" :total="nowTotal"></page-select>

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

    <site-bar wechat-src="/image/wechat.jpg"></site-bar>
  </div>
</template>

<script>
import qs from "qs"

import productBanner from "~/components/banner/productBanner";
import itemBanner from "~/components/banner/itemBanner";
import solutionTypes from "~/components/solutionTypes";
import solutionList from "~/components/solutionList";

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

const pageLimit = 5;
export default {
  name: "solutionIndex",
  props:['uLang','pType','pKey','pSolution','pPageData'],
  components:{
    DefaultFooter,
    LucencyHeader,
    productBanner,
    itemBanner,
    solutionTypes,
    solutionList
  },
  async asyncData(ctx){
    // 获取数据
    let url = baseUrl + apiMap.searchSolution.path;
    url += `?type=all&p=1&l=5`
    let [err,res] = await handle(axios.get( url));
    if(err){
      console.log(err);
      return {};
    }
    let result = res.data;
    if(result.code === 1){
      let pageData = {
        limit: result.limit,
        page: result.page,
        total: result.total,
        count: result.count,
      }
      return {
        solutions: result.data,
        basePageData: pageData,
      }
    }else{
      console.error(result.msg);
      console.log(result);
      return {solutions:[]}
    }
  },
  data(){
    return {
      lang: this.uLang?this.uLang:langMap.lang.cn,
      type: this.pType?this.pType:'all',
      key: this.pKey?this.pKey:'',
      page: 1,
      nowCount: 199,
      nowTotal: 2,
      limit:pageLimit,
      solutions:this.pSolution?this.pSolution:[],
      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('changeSolutionType',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.searchSolution();
    },
    changePageHandle(nextPage){
      this.page = nextPage;
      this.searchSolution();
    },
    async searchSolution(){
      // 获取数据
      let url = apiMap.searchSolution.path;
      url += `?key=${this.key}&type=${this.type}&p=${this.page}&l=5`
      let [err,res] = await handle(axios.get(url));
      if(err){ console.log(err); return null; }

      let result = res.data;
      if(result.code === 1){
        this.solutions = 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>
