<template>
  <div class="list">
    <filterBox></filterBox>
    <van-list
      :offset="900"
      v-model="loading"
      :finished="finished"
      :finished-text="''"
      @load="onLoad"
    >
      <div class="li flex" v-for="item in list" :key="item.symbol" @click="$router.push('/product-detail/' + item.symbol)">
        <div class="left" v-if="productDataObj[item.symbol]">
          <div class="name">{{ productDataObj[item.symbol].title }}</div>
          <!-- <div class="name" v-else-if="language == 'zh-TW'">{{ productDataObj[item.symbol].name_cht || productDataObj[item.symbol].title }}</div> -->
          <!-- <div class="name" v-else>{{ productDataObj[item.symbol].name_en || (productDataObj[item.symbol].market == 'CFD' ? productDataObj[item.symbol].symbol : productDataObj[item.symbol].title) }}</div> -->
          <div class="label flex">
            <div class="tag-box">
              <div class="tag" :class="getTagName(productDataObj[item.symbol])">{{ getTagName(productDataObj[item.symbol]) }}</div>
            </div>
            <div>{{ item.symbol }}</div>
          </div>
        </div>
        <div class="right flex" v-if="productDataObj[item.symbol]">
          <div></div>
          <div>
            <div class="flex" :class="productDataObj[item.symbol].spread < 0 ? 'down' : 'up'">
              <div class="table-row">
                {{ $handleDigits(productDataObj[item.symbol].ask, productDataObj[item.symbol].digits) }}
              </div>
              <div class="table-row flex">
                <div class="div">
                  {{
                    productDataObj[item.symbol].spread > 0
                      ? "+"
                      : ""
                  }}{{ productDataObj[item.symbol].percentage }}%
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </van-list>
    <div class="more-box" v-if="list.length">
      <div class="more flex" @click="toMore">
        <div>
          <!-- 查看更多 -->
          {{ $t('product.l15') }}
        </div>
        <img class="img" src="/img/home/more.png" alt="极度证券">
      </div>
    </div>
  </div>
</template>
<script>
import filterBox from "./filter-box.vue";
import { getProductList, getProductShowList } from "@/api/product";
import bus from "@/utils/bus";
export default {
  props: ['market'],
  inject: ['getproductDataObj'],
  components: {
    filterBox,
  },
  data() {
    return {
      targetInfo: null,
      operationPopupShow: false,
      loading: false,
      finished: false,
      swipeShow: true,
      pageNum: 1,
      pageSize: 6,
      list: [],
    };
  },
  computed: {
    productDataObj() {
      return this.getproductDataObj()
    },
  },
  beforeDestroy() {
    bus.$off('selectLang', this.onLoad)
  },
  deactivated () {
    bus.$off('selectLang', this.onLoad)
  },
  activated () {
    this.onLoad()
  },
  created () {
    bus.$on('selectLang', this.onLoad)
  },
  methods: {
    onLoad() {
      getProductShowList({
        groupList: JSON.stringify([this.market]),
      }).then(res => {
        var group = ((res.list || [])[0] || {});
        var productList = (group.productList || []).slice(0, 6);
        var symbols = []
        productList.forEach(item => {
          symbols.push(item.symbol)
        })
        if (symbols.length == 0) {
          this.loading = false;
          this.finished = true;
          return
        }
        getProductList({
          symbol: JSON.stringify(symbols)
        }).then((res) => {
          var list1 = res.list || [];
          var list = list1;
          if (!this.appParams.uncrypto) {
            list = list1.filter(item => item.market !== 'CFD')
          }
          this.list = list.sort((a, b) => symbols.indexOf(a.symbol) - symbols.indexOf(b.symbol));
          this.loading = false;
          this.finished = true;
          bus.$emit("alterProductData", {
            setData: this.list,
            subscribeList: symbols,
          });
        })
        .catch(() => {
          this.loading = false;
          this.finished = true;
        }).finally(() => {
          this.fullscreenLoading = false
        })
      }).catch(() => {
        this.loading = false;
        this.finished = true;
        this.fullscreenLoading = false
      })
    },
    getTagName(item) {
      if (item.market == 'CN') {
        var list = item.symbol.split('.')
        return list[1]
      }
      return item.market
    },
    toMore() {
      var url = {
        '18': '/market/cfd/hot',
        '17': '/market/cfd/5',
      }
      this.$router.push(url[this.market] || '/market/stock/1')
    }
  },
};
</script>
<style lang="less" scoped>
.list {
  -moz-user-select: none;
  -khtml-user-select: none;
  user-select: none;
  background-color: #fff;
  padding: 0 18px;
  min-height: 200px;
  position: relative;
}
.li {
  justify-content: space-between;
  height: 57px;
  border-bottom: 1px solid var(--border-);
  position: relative;
  .mask {
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    z-index: 1;
    background-color: transparent;
  }
  .left {
    flex: 1;
    min-width: 10%;
    max-width: 100%;
  }
  .name {
    color: var(--color-1-);
    font-size: 15px;
    line-height: 21px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .label {
    color: #959697;
    font-size: 12px;
    margin-top: 2px;
  }
  .tag-box {
    margin-right: 6px;
    .tag {
      line-height: 14px;
      padding: 0 3px;
      border-radius: 2px;
      color: #979899;
      border: 1px solid #EBEBEB;
    }
  }
  .down {
    // color: var(--green-);
    .div {
      color: var(--green-);
    }
  }
  .up {
    // color: var(--red-);
    .div {
      color: var(--red-);
    }
  }
  .div {
    font-size: 14px;
  }
  .table-row {
    width: 84px;
    text-align: right;
    color: #4A4A4A;
    &.flex {
      justify-content: flex-end;
    }
  }
  .bottom {
    font-size: 12px;
    margin-top: 5px;
    color: var(--color-1-);
    justify-content: flex-end;
    column-gap: 4px;
    .border {
      width: 4px;
      height: 12px;
      border: 6px solid #e9eff7;
      border-left-color: transparent;
      border-right-color: transparent;
      border-top: transparent;
    }
    .text {
      line-height: 14px;
      font-size: 10px;
      background-color: #e9eff7;
      padding: 0 4px;
    }
  }
}
.more-box {
  display: flex;
  height: 69px;
  background: linear-gradient( 180deg, rgba(255,255,255,0) 0%, rgba(255,255,255,0.84) 61%, #FFFFFF 100%);
  align-items: flex-end;
  justify-content: center;
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 3;
  .more {
    cursor: pointer;
    background: #FFFFFF;
    border-radius: 15px;
    border: 1px solid #E8E8E8;
    justify-content: center;
    height: 26px;
    padding: 0 14px;
    font-size: 12px;
    color: #4A4A4A;
  }
  .img {
    width: 9px;
    margin-left: 3px;
  }
}
</style>
