<template>
  <div class="list" v-loading.body.fullscreen="fullscreenLoading" element-loading-background="rgba(255, 255, 255, 0)">
    <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" v-if="language == 'zh-CN'">{{ 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">
            <div></div>
            <div>
              <div class="flex" :class="productDataObj[item.symbol].percentage < 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].percentage > 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("updateSubscribe", this.list, false, false, this.swipeList);
  },

  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
      }
      let 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) => {
          let list1 = res.list || [];
          let list = list1;
          if (!this.appParams.uncrypto) {
            list = list1.filter(item => item.market !== 'CFD')
          }
          // let 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("updateSubscribe", this.list, false, false, this.swipeList);
        })
        .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) => {
          let list1 = res.list || [];
          let list = list1;
          if (!this.appParams.uncrypto) {
            list = list1.filter(item => item.market !== 'CFD')
          }
          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("updateSubscribe", this.list, false, false, this.swipeList);
        })
        .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') {
        let 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 - 216px);
  overflow-y: auto;
  padding: 0 18px;
  background-color: #fff;
}
.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;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .label {
    color: #959697;
    font-size: 12px;
    margin-top: 5px;
  }
  .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 {
      background-color: #22AF6E;
    }
  }
  .up {
    // color: var(--red-);
    .div {
      background-color: #EA4A45;
    }
  }
  .div {
    color: #fff;
    line-height: 23px;
    width: 72px;
    text-align: center;
    border-radius: 2px;
  }
  .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;
    }
  }
}
</style>
