<template>
  <div class="list">
    <van-list
      :offset="900"
      v-model="loading"
      :finished="finished"
      :finished-text="list.length ? $t('common.l5') : ''"
      @load="onLoad"
    >
      <div class="li" v-for="item in list" :key="item.ticket">
        <div class="box">
          <div class="left">
            <div class="name-box flex">
              <div class="title" v-if="productDataObj[item.symbol]">
            {{ productDataObj[item.symbol].title }}
          </div>
              <div>({{ item.symbol }})</div>
              <div class="cmd" :class="'cmd' + item.cmd">
                <!-- 买 -->
                {{ item.cmd == 0 ? $t('follow.l32') : $t('follow.l33') }}
              </div>
            </div>
            <div class="price-box flex">
              <div>{{ $handleDigits(item.open_price, productDataObj[item.symbol] ? productDataObj[item.symbol].digits : 2) }}</div>
              <div>&gt;</div>
              <div :class="{up: item.cmd == 0, down: item.cmd == 1}">{{ $handleDigits(item.close_price, productDataObj[item.symbol] ? productDataObj[item.symbol].digits : 2) }}</div>
            </div>
          </div>
          <div class="profit">
            <div class="lab">
              <!-- 盈亏 -->
              {{ $t('account.l7') }}
            </div>
            <div class="number" :class="item.profit > 0 ? 'up' : 'down'">{{ item.profit > 0 ? '+' : '' }}{{ $handleDigits(item.profit) }}({{ getRateReturn(item) }}%)</div>
          </div>
        </div>
        <van-row>
          <van-col span="8">
            <div class="lab">
              <!-- 购买数量 -->
              {{ $t('follow.l35') }}
            </div>
            <div v-if="productDataObj[item.symbol]">
              {{ $handleDigits($bigDecimal.multiply(item.volume, productDataObj[item.symbol].contract_size)) }}
            </div>
          </van-col>
          <van-col span="8">
            <div class="lab">
              <!-- 分润金额(USD) -->
              {{ $t('follow.l28') }}(USD)
            </div>
            <div>
              2,000
            </div>
          </van-col>
        </van-row>
        <div class="time flex">
          <div>{{ item.open_time.replace(/-/g, '/') }}</div>
          <img class="icon" src="/img/copy/icon1.png" alt="">
          <div>{{ item.close_time.replace(/-/g, '/') }}</div>
        </div>
      </div>
      <NoData v-if="!loading && list.length == 0" />
    </van-list>
  </div>
</template>
<script>
import { getFollowHistoryTradeList } from "@/api/copy";
import bus from '@/utils/bus'
export default {
  inject: ['getproductDataObj'],
  data () {
    return {
      pageNum: 0,
      pageSize: 20,
      finished: false,
      loading: false,
      list: [],
      symbolList: [],
      id: '',
    }
  },
  computed: {
    productDataObj() {
      return this.getproductDataObj()
    },
  },
  created () {
    this.id = this.$route.params.id
  },
  methods: {
    onLoad() {
      this.pageNum += 1;
      if (this.pageNum == 1) {
        this.list = [];
      }
      this.getFollowHistoryTradeList()
    },
    getFollowHistoryTradeList() {
      getFollowHistoryTradeList({
        eid: this.id,
        pageNum: this.pageNum,
        pageSize: this.pageSize,
      }).then(res => {
        var list = res.list || [];
        var symbolList = []
        list.forEach((item) => {
          if (this.symbolList.indexOf(item.symbol) === -1 && !this.productDataObj[item.symbol]) {
            symbolList.push(item.symbol)
            this.symbolList.push(item.symbol)
          }
        })
        bus.$emit("alterProductData", {
          getData: symbolList
        });
        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;
        }
      }).catch(() => {})
    },
    getRateReturn(item) {
      return (((item.close_price - item.open_price) / item.open_price) * 100).toFixed(2)
    }
  }
}
</script>
<style lang="less" scoped>
.van-list {
  padding: 0 16px 10px;
}
.li {
  border-radius: 8px;
  border: 1px solid #F6F6F6;
  padding: 13px 9px;
  position: relative;
  margin-bottom: 10px;
}
.box {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  column-gap: 6px;
  margin-bottom: 10px;
  .left {
    flex: 1;
    min-width: 0px;
  }
  .cmd {
    padding: 2px;
    font-size: 12px;
    font-weight: 600;
    font-size: 10px;
    background-color: var(--red-);
    color: #fff;
    margin-left: 8px;
    border-radius: 2px;
  }
  .cmd1 {
    background-color: var(--green-);
  }
  .title {
    min-width: 0px;
    max-width: 100%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .name-box {
    margin-bottom: 6px;
  }
  .price-box {
    font-size: 12px;
    color: #B6B6B6;
    column-gap: 4px;
  }
  .profit {
    text-align: right;
    .lab {
      font-size: 12px;
      color: #888888;
      margin-bottom: 6px;
    }
    .number{
      font-weight: 600;
    }
  }
}
.price {
  padding-top: 11px;
  font-size: 12px;
  color: #333333;
  column-gap: 4px;
}
.van-row {
  font-size: 10px;
  word-break: break-all;
  .lab {
    color: #B6B6B6;
    margin-bottom: 4px;
  }
}
.time {
  font-size: 10px;
  color: #B6B6B6;
  margin-top: 10px;
  column-gap: 4px;
  .icon {
    width: 10px;
  }
}
.up {
  color: var(--red-);
}
.down {
  color: var(--green-);
}
</style>