<template>
  <div>
    <div class="btns flex">
      <div class="btn" :class="{active: market === 'hot'}" @click="tabChange('hot')">
        <!-- 热门 -->
        {{ $t('market.l6') }}
        <div class="line"></div>
      </div>
      <div class="btn" v-for="item in groupList" :key="item.id" :class="{active: market == item.id}" @click="tabChange(item.id)">
        {{ titleName[item.title] || item.title }}
        <div class="line"></div>
      </div>
    </div>
    <marketList :key="market" :swipeList="swipeList" :market="market"/>
  </div>
</template>
<script>
import { getProductGroupList } from "@/api/product";
import marketList from "./components/list.vue";
export default {
  components: {
    marketList,
  },
  data () {
    return {
      market: 'hot',
      groupList: [],
      swipeList: ['000001.SH', '399001.SZ', '399006.SZ', 'XAUUSD', 'BTCUSD', 'XBRUSD', '.DJI.US', '.IXIC.US', 'SPY.US', 'HSI.HK', 'HSCEI.HK', 'HSTECH.HK']
    }
  },
  computed: {
    titleName() {
      return {
        "指数": this.$t('download.l27'),
        "能源": this.$t('market.l19'),
        "其他": this.$t('inform.l22'),
        "加密货币": this.$t('market.l24'),
        "外汇": this.$t('market.l21'),
        "贵金属": this.$t('market.l22')
      }
    }
  },
  activated() {
    if (this.$route.params.market) {
      this.market = this.$route.params.market
    }
  },
  created () {
    if (this.$route.params.market) {
      this.market = this.$route.params.market
    }
    this.getProductGroupList()
  },
  methods: {
    close() {
      this['noticeShow' + this.market] = false
    },
    tabChange(val) {
      if (this.market == val) {
        return
      }
      this.market = val
      this.$router.replace('/market/cfd/' + val)
    },
    getProductGroupList() {
      getProductGroupList({
        visible: 1
      }).then(res => {
        this.groupList = res.list || []
      }).catch(()=> {})
    }
  }
}
</script>
<style lang="less" scoped>
.btns {
  position: sticky;
  top: 50px;
  z-index: 4;
  height: 52px;
  padding: 0 16px;
  align-items: flex-end;
  white-space: nowrap;
  flex-wrap: nowrap;
  overflow-x: auto;
  background-color: #fff;
  &::-webkit-scrollbar {
    display: none;
  }
  .btn {
    cursor: pointer;
    font-size: 14px;
    color: #888888;
    padding-bottom: 18px;
    position: relative;
    &+.btn {
      margin-left: 24px;
    }
    &.active {
      font-size: 16px;
      font-weight: 600;
      color: #000;
      .line {
        display: block;
      }
    }
  }
  .line {
    position: absolute;
    left: 50%;
    bottom: 10px;
    transform: translateX(-50%);
    width: 12px;
    height: 2px;
    border-radius: 1px;
    background-color: #000;
    display: none;
  }
}
</style>