<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="$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)"
          ></div>
          <div class="left">
            <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">{{ getTagName(productDataObj[item.symbol]) }}</div>
              <div>{{ item.symbol }}</div>
            </div>
          </div>
          <div class="right flex">
            <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>
    </van-pull-refresh>
    <!-- <van-popup get-container="body" v-model="operationPopupShow">
      <listOperation></listOperation>
    </van-popup> -->
  </div>
</template>
<script>
import filterBox from "./filter-box.vue";
import longPress from "@/mixins/longPress";
// import listOperation from "./list-operation.vue";
import { getProductList, getProductShowList } from "@/api/product";
import bus from "@/utils/bus";
import { mapGetters } from "vuex";
export default {
  props: ['market', 'swipeList'],
  mixins: [longPress],
  components: {
    filterBox,
    // listOperation,
  },
  inject: ['getproductDataObj'],
  data() {
    return {
      targetInfo: null,
      operationPopupShow: false,
      loading: false,
      finished: false,
      refreshing: false,
      swipeShow: true,
      pageNum: 0,
      pageSize: 30,
      list: [],
    };
  },
  computed: {
    ...mapGetters(['marketCN', 'marketHK', 'marketUS', 'marketCFD', 'marketPopCN', 'marketPopHK', 'marketPopUS', 'marketPopCFD']),
    productDataObj() {
      return this.getproductDataObj()
    },
  },
  watch: {
    'market'() {
      this.onRefresh()
    }
  },
  deactivated () {
    this.swipeShow = false
  },
  activated () {
    this.swipeShow = true
    bus.$emit("alterProductData", {
      setData: this.list,
      subscribeList: this.list.map(item => item.symbol),
    });
  },

  methods: {
    onLoad() {
      if (this.refreshing) {
        this.refreshing = false;
      }
      this.pageNum += 1;
      if (this.pageNum == 1) {
        this.list = [];
        bus.$emit('resetProductDataObj', this.swipeList)
        this.fullscreenLoading = true
      }
      var data = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        groupid: this.market == 'hot' ? '' : this.market,
        market: 'CFD',
        client: 'H5'
      }
      if (this.market == 'hot') {
        this.getProductShowList()
        return
      }
      getProductList(data)
        .then((res) => {
          var list1 = res.list || [];
          var list = list1;
          if (!this.appParams.uncrypto) {
            list = list1.filter(item => item.market !== 'CFD')
          }
          // var symbolList = [];
          // list.forEach((item, index) => {
          //   symbolList.push({
          //     symbol: item.symbol,
          //     num: item.num
          //   });
          // });
          if (this.pageNum == 1) {
            this.list = list;
          } else {
            this.list = this.list.concat(list);
          }
          this.loading = false;
          if (res.endRow == res.total) {
            this.finished = true;
          }
          bus.$emit("alterProductData", {
            setData: this.list,
            subscribeList: this.list.map(item => item.symbol),
          });
        })
        .catch(() => {
          this.loading = false;
          this.finished = true;
        }).finally(() => {
          this.fullscreenLoading = false
        })
    },
    getProductShowList() {
      getProductShowList({
        groupList: JSON.stringify([11]),
      }).then(res => {
        var group = ((res.list || [])[0] || {});
        var productList = (group.productList || []);
        var symbols = []
        productList.forEach(item => {
          symbols.push(item.symbol)
        })
        if (symbols.length == 0) {
          this.loading = false;
          this.finished = true;
          this.fullscreenLoading = false
          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')
          }
          if (this.pageNum == 1) {
            this.list = list.sort((a, b) => symbols.indexOf(a.symbol) - symbols.indexOf(b.symbol));
          } else {
            this.list = this.list.concat(list.sort((a, b) => symbols.indexOf(a.symbol) - symbols.indexOf(b.symbol)));
          }
          this.loading = false;
          if (res.endRow == res.total) {
            this.finished = true;
          }
          bus.$emit("alterProductData", {
            setData: this.list,
            subscribeList: this.list.map(item => item.symbol),
          });
        })
        .catch(() => {
          this.loading = false;
          this.finished = true;
        }).finally(() => {
          this.fullscreenLoading = false
        })
      }).catch(() => {
        this.loading = false;
        this.finished = true;
        this.fullscreenLoading = false
      })
    },
    operationShow(item) {
      this.targetInfo = item;
      this.operationPopupShow = true;
    },
    onRefresh() {
      this.finished = false;
      this.loading = true;
      this.pageNum = 0;
      this.onLoad();
    },
    getTagName(item) {
      if (item.market == 'CN') {
        var list = item.symbol.split('.')
        return list[1]
      }
      return item.market
    },
  },
};
</script>
<style lang="less" scoped>
.list {
  -moz-user-select: none;
  -khtml-user-select: none;
  user-select: none;
  background-color: #F6FAFE;
}
.van-pull-refresh {
  height: calc(100vh - 195px);
  overflow-y: auto;
  background-color: #fff;
}
.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;
  }
  .down {
    color: var(--green-);
    .div {
      background-color: var(--green-);
    }
  }
  .up {
    color: var(--red-);
    .div {
      background-color: var(--red-);
    }
  }
  .div {
    color: #fff;
    line-height: 28px;
    width: 72px;
    text-align: center;
    border-radius: 2px;
  }
  .table-row {
    width: 84px;
    text-align: right;
    font-size: 16px;
    &.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;
    }
  }
}
</style>
