<template>
  <div class="market">
    <div class="list">
      <div class="title flex">
        <van-image
          width="14"
          height="14"
          round
          fit="cover"
          :src="`/img/user/${market}.png`"
        />
        <div>{{ title[market] }}</div>
      </div>
      <div class="ul">
        <div class="li flex" v-for="(item, index) in list" :key="item.symbol" @click="checkDetail(item.symbol)">
          <div class="left">
            <div class="number" :class="{active: index < 3}">{{ index+1 }}</div>
            <div>
              <div class="name">{{ item.title }}</div>
              <div class="symbol">{{ item.symbol }}</div>
            </div>
          </div>
          <div class="right" :class="{'down': item.percentage < 0}">
            {{ item.percentage > 0 ? '+' : ''  }}{{ item.percentage }}%
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { getProductShowList, getProductList } from "@/api/product";
export default {
  props: ['market'],
  data () {
    return {
      list: []
    }
  },
  computed: {
    listId() {
      return {
        'US': '13',
        'HK': '15',
        'CN': '14',
        'CFD': '11',
      }
    },
    title() {
      return {
        // 'US': '美股热度榜',
        // 'HK': '港股热度榜',
        // 'CN': 'A股热度榜',
        'US': this.$t('exchange.l55'),
        'HK': this.$t('exchange.l56'),
        'CN': this.$t('exchange.l57'),
        'CFD': this.$t('exchange.l77'),
      }
    }
  },
  created () {
    this.getProductShowList()
  },
  methods: {
    checkDetail(symbol) {
      this.$emit('back', symbol)
    },
    getProductShowList() {
      getProductShowList({
        groupList: JSON.stringify([this.listId[this.market]]),
      }).then(res => {
        var group = ((res.list || [])[0] || {});
        var productList = (group.productList || []).slice(0, 5);
        var symbols = []
        productList.forEach(item => {
          symbols.push(item.symbol)
        })
        getProductList({
          symbol: JSON.stringify(symbols)
        }).then((res) => {
          var list = res.list || []
          list.forEach(item => {
            if (item.close) {
              item.percentage = ((((item.ask || item.close) - item.close) / item.close) * 100).toFixed(2);
            } else {
              item.percentage = 100;
            }
          })
          this.list = list
        })
        .catch(() => {
        })
      }).catch(() => {
      })
    },
  }
}
</script>
<style lang="less" scoped>
.market {
  padding: 20px 18px 30px;
}
.list {
  background-color: #FAFBFC;
  border-radius: 6px;
  padding: 0 11px 11px;
  .title {
    column-gap: 4px;
    height: 40px;
    font-weight: 500;
    color: var(--color-1-);
    .van-image {
      margin-right: 3px;
    }
  }
  .li {
    height: 53px;
    border-top: 1px solid #EBEBEB;
    justify-content: space-between;
    cursor: pointer;
  }
  .left {
    display: flex;
    align-items: center;
    font-size: 14px;
    column-gap: 9px;
  }
  .name {
    color: var(--color-1-);
    margin-bottom: 5px;
  }
  .symbol {
    color: #636465;
    font-size: 12px;
  }
  .number {
    font-weight: 600;
    font-size: 14px;
    color: #636465;
    &.active {
      color: var(--color-);
    }
  }
  .right {
    font-size: 14px;
    color: #E22404;
    &.down {
      color: #22B673;
    }
  }
}
</style>