<template>
  <div>
    <lucency-header :lang="lang" :page-key="pageKey" :is-phone="isPhone"></lucency-header>
    <item-banner :title="'支持中心'" :sub-title="'需要我们为您提供些什么内容呢?'"></item-banner>
    <div class="conBox">
      <q-tab :headers="qTabs">
        <template v-slot:question>
          <support ></support>
        </template>
        <template v-slot:download>
         <downloads :downloads="downloadsType"></downloads>
        </template>
      </q-tab>
    </div>
    <default-footer :lang="lang"></default-footer>
    <site-bar wechat-src="/image/wechat.jpg"></site-bar>
  </div>
</template>

<script>
import qs from "qs";


import lucencyHeader from "~/components/header/lucencyHeader";
import langMap from "@/map/langMap";
import handle from "@/until/handle";
import {unescapeHtml} from "@/until/unescapeHtml";
import {isMediaView} from "@/until/mediaView";

function getFileIcon(fileName){
  // 获取文件后缀
  if(!fileName){
    return 'unknownfile';
  }
  const fileSuffix = fileName.split('.').pop();
  switch (fileSuffix) {
    case 'doc':
      return 'doc';
    case 'docx':
      return 'doc';
    case 'pdf':
      return 'pdf';
    case 'png':
      return 'img';
    case 'jpg':
      return 'img';
    case 'jpeg':
      return 'img';
    default:
      return 'unknownfile';

  }
}
export default {
  name: "index",
  props:['uLang'],
  components: { lucencyHeader },
  data(){
    return {
      lang: this.uLang?this.uLang:langMap.lang.cn,
      pageKey: 'support',
      qTabs: [
        {
          text: '技术支持',
          content: '技术支持',
          key: 'question'
        },
        {
          text: '下载中心',
          content: '下载相关技术资料',
          key: 'download'
        },
      ],
      downloadsType: [
        {
          title: "G8100",
          key: 1,
          subItems: [
            {
              icon: "doc",
              title: "4G低功耗模块G8100B硬件手册",
              url: "https://gpscore.net/",
              add_time: "1486538906"
            },
            {
              icon: "pdf",
              title: "4G低功耗模块G8100B硬件手册",
              url: "https://gpscore.net/",
              add_time: "1486538906"
            },
            {
              icon: "img",
              title: "4G低功耗模块G8100B硬件手册",
              url: "https://gpscore.net/",
              add_time: "1486538906"
            }
          ]
        },
        {
          title: "枪机摄像头",
          key: 2,
          subItems: [
            {
              icon: "doc",
              title: "4G低功耗模块G8100B硬件手册",
              url: "https://gpscore.net/",
              add_time: "1486538906"
            },
            {
              icon: "pdf",
              title: "4G低功耗模块G8100B硬件手册",
              url: "https://gpscore.net/",
              add_time: "1486538906"
            },
            {
              icon: "img",
              title: "4G低功耗模块G8100B硬件手册",
              url: "https://gpscore.net/",
              add_time: "1486538906"
            }
          ]
        }
      ],
      isPhone: false
    }
  },

  mounted() {
    this.$root.$on('changeLang',this.switchLang);
    this.$root.$on('loadDownloadItem',this.loadDownloadItems);
    this.isPhone = isMediaView(0,1024);
    this.loadDownloads();
  },

  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
        }
      }
    },
    async loadDownloads(){
      let err, res;
      [err, res] = await handle(this.$axios.$get('/api/getDownloads.php'));
      if(err) {
        console.log(err);
        return 0;
      }
      if(res.rcode === 1){

        // this.downloadsType = res.data;
        this.downloadsType = res.data.map(item=>{
          item.key = item.id;
          return item;
        });
      }else{
        console.log(res.msg);
        console.log(res);
      }

    },
    async loadDownloadItems(downloadId){
      console.log(`获取下载项${downloadId}`)
      let err, res;
      const queryData = {
        id: downloadId
      };
      let url = '/api/getDownloadItems.php';
      let data = qs.stringify(queryData);
      [err, res] = await handle(this.$axios.post(
        url,
        data
      ));
      if(err){
        console.log(err);
        return null;
      }
      let result = res.data;
      console.log(result)
      if(result.rcode === 1){
        // 匹配对应的id
        let downloadItem = this.downloadsType.find(item => item.id === downloadId);
        let arr = result.data?result.data:[];
        downloadItem.subItems = arr.map(item=>{
          item.title = unescapeHtml(item.remark);
          item.icon = getFileIcon(item.files);
          item.src = item.files;
          return item;
        });
      }else{
        console.error(result.msg);
        console.log(result);
      }
    }
  },
}
</script>

<style scoped>

</style>
