<template>
  <div class="list">
    <filterBox></filterBox>
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <!-- 没有更多了 -->
      <van-list
        :offset="900"
        v-model="loading"
        :finished="finished"
        :finished-text="list.length ? $t('common.l5') : ''"
        @load="onLoad"
      >
        <div class="li flex" v-for="item in list" :key="item.symbol">
          <div
            class="mask"
            @click="$router.push('/product-detail/' + item.symbol + '/6')"
          ></div>
          <div class="left" v-if="productDataObj[item.symbol]">
            <div class="name">{{productDataObj[item.symbol].title }}</div>
            <div class="label flex">
              <div class="tag">{{ getTagName(item.symbol) }}</div>
              <div>{{ item.symbol }}</div>
            </div>
          </div>
          <div class="right flex">
            <div></div>
            <div>
              <div class="flex">
                <div class="table-row" v-if="productDataObj[item.symbol]" :class="productDataObj[item.symbol].percentage < 0 ? 'down' : 'up'">
                  {{
                    productDataObj[item.symbol].percentage > 0
                      ? "+"
                      : ""
                  }}{{ productDataObj[item.symbol].percentage }}%
                </div>
                <div class="table-row">
                  <div class="div">{{ getPrice(item, 100) }}</div>
                </div>
                <div class="table-row">
                  <div class="div">{{ getPrice(item, 105) }}</div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <NoData v-if="!loading && list.length == 0"/>
      </van-list>
    </van-pull-refresh>
  </div>
</template>
<script>
import filterBox from "./filter-box.vue";
import { getProductShowList } from "@/api/product";
import bus from "@/utils/bus";
export default {
  props: ['market', 'mounth'],
  components: {
    filterBox,
  },
  data() {
    return {
      targetInfo: null,
      operationPopupShow: false,
      loading: false,
      finished: false,
      refreshing: false,
      pageNum: 0,
      pageSize: 30,
      list: [],
      symbolList: [],
      loaded: true
    };
  },
  inject: ['getproductDataObj'],
  computed: {
    productDataObj() {
      return this.getproductDataObj()
    },
  },
  watch: {
    'market'() {
      this.onRefresh()
    }
  },
  created () {
  },
  activated () {
    bus.$emit("alterProductData", {
      getData: this.symbolList,
      subscribeList: this.symbolList,
    });
  },
  methods: {
    getProductShowList() {
      getProductShowList({
        groupList: JSON.stringify([5]),
      }).then(res => {
        var list = res.list || []
        var list1 = []
        var symbolList = []
        list.forEach(item => {
          if (item.productList) {
            item.productList.forEach((a, index) => {
              list1.push(a)
              symbolList.push(a.symbol);
            })
          }
        })
        this.list = list1
        this.loading = false;
        this.finished = true;
        this.symbolList = symbolList;
        bus.$emit("alterProductData", {
          getData: this.symbolList,
          subscribeList: this.symbolList,
        });
      }).catch(() => {
        this.loading = false;
        this.finished = true;
      }).finally(() => {
        this.fullscreenLoading = false
      })
    },
    onLoad() {
      if (this.refreshing) {
        this.list = [];
        this.refreshing = false;
      }
      this.fullscreenLoading = true
      this.getProductShowList()
    },
    operationShow(item) {
      this.targetInfo = item;
      this.operationPopupShow = true;
    },
    onRefresh() {
      this.finished = false;
      this.loading = true;
      this.pageNum = 0;
      this.onLoad();
    },
    getPrice(item, index) {
      return item.info[index][`price_${this.mounth}`] + '%'
    },
    getTagName(code) {
      if (code) {
        if (this.market == 'US') {
          return 'US'
        }
        if (this.market == 'HK') {
          return 'HK'
        }
        var list = code.split('.')
        return list[1]
      }
    }
  },
};
</script>
<style lang="less" scoped>
.list {
  -moz-user-select: none;
  -khtml-user-select: none;
  user-select: none;
}
.van-pull-refresh {
  height: calc(100vh - 195px);
  overflow-y: auto;
}
.li {
  justify-content: space-between;
  height: 60px;
  border-bottom: 1px solid var(--border-);
  position: relative;
  padding: 0 16px;
  .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 {
    font-size: 16px;
    line-height: 22px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .label {
    color: #B6B6B6;
    font-size: 12px;
    margin-top: 3px;
  }
  .tag {
    color: #888888;
    margin-right: 6px;
  }
  .div {
    color: #4A4A4A;
    font-size: 14px;
  }
  .table-row {
    width: 70px;
    text-align: right;
    color: var(--red-);
    font-size: 16px;
    &.flex {
      justify-content: flex-end;
    }
    &.down {
      color: var(--green-);
    }
    &.up {
      color: var(--red-);
    }
  }
  .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;
    }
  }
}
</style>
