<template>
  <div>
    <!-- 没有更多了 -->
    <van-list
      v-model="loading"
      :finished="finished"
      :finished-text="list.length == 0 ? '' : $t('common.l46')"
      @load="onLoad"
    >
      <div class="item" v-for="item in list" :key="item.id" @click="checkDetail(item)">
        <div class="top flex">
          <div class="left flex">
            <img class="icon" src="/img/list/icon1.svg" alt="极度证券">
            <div>
              <!-- 货币兑换 -->
              {{ $t('account.l38') }}
            </div>
          </div>
          <div class="right">
            -{{ fromAmount(item) }}
          </div>
        </div>
        <div class="bottom">
          <div class="time-box flex">
            <div>{{ item.insert_datetime }}</div>
            <div>+{{ toAmount(item) }}</div>
          </div>
          <div class="flex type">
            <!-- 美元钱包账户 -->
            <div>{{ serverName[item.from.server] || $t('account.l1') }}</div>
            <van-icon name="down" />
            <div>{{ serverName[item.to.server] || $t('account.l1') }}</div>
          </div>
        </div>
      </div>
      <!-- 暂无记录 -->
      <NoData :title="$t('common.l45')" v-if="list.length == 0 && !loading"/>
    </van-list>
  </div>
</template>
<script>
import { getCashRecordList } from "@/api/list";
export default {
  props: ['accountInfo'],
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      pageNum: 0,
      pageSize: 10,
      type: [1, 2, 3, 4, 5],
    }
  },
  methods: {
    reset() {
      this.finished = false
      this.loading = true
      this.pageNum = 0
      this.onLoad()
    },
    onLoad() {
      this.pageNum += 1
      getCashRecordList({
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        server: this.accountInfo.server,
        type: JSON.stringify(this.type),
      }).then(res => {
        var list = res.list || []
        list.forEach(item => {
          item.from = item.from ? JSON.parse(item.from) : {}
          item.to = item.to ? JSON.parse(item.to) : {}
        })
        if (this.pageNum == 1) {
          this.list = list
        } else {
          this.list = this.list.concat(list)
        }
        // 加载状态结束
        this.loading = false;
        // 数据全部加载完成
        if (this.list.length >= res.total) {
          this.finished = true;
        }
      }).catch(() => {
        this.loading = false;
        this.finished = true;
      })
    },
    arriveAmount(item) {
      return this.$bigDecimal.multiply(item.amount, item.arrive_rate)
    },
    checkDetail(item) {
      this.$router.push('/account-records/detail/2/' + item.ordernum)
    },
    fromAmount(item) {
      if (item.path == 2) {
        return this.$handleDigits(item.amount) + ' USD'
      }
      return this.$numToThousand((this.$bigDecimal.add(item.amount, item.fee) * item.pay_rate).toFixed(2)) + ' ' + item.pay_currency
    },
    toAmount(item) {
      if (item.path == 1) {
        return this.$handleDigits(item.amount) + ' USD'
      }
      return this.$numToThousand((this.$bigDecimal.subtract(item.amount, item.fee) * item.arrive_rate).toFixed(2)) + ' ' + item.arrive_currency
    }
  },
}
</script>
<style lang="less" scoped>
.van-list {
  padding: 17px 17px 0;
  .item {
    border-radius: 6px;
    border: 1px solid #E3E3E3;
    padding: 15px 12px 18px;
    &+.item {
      margin-top: 10px;
    }
  }
  .top {
    justify-content: space-between;
    margin-bottom: 9px;
    .icon {
      width: 17px;
      margin-right: 3px;
    }
    .right {
      font-size: 16px;
      font-weight: 600;
    }
  }
  .bottom {
    padding-left: 20px;
    font-size: 12px;
    color: #979797;
  }
  .time-box {
    justify-content: space-between;
    margin-bottom: 13px;
    .radius {
      width: 4px;
      height: 4px;
      border-radius: 50%;
      background: #FB4136;
      margin-left: 3px;
    }
    .right {
      color: #FB4136;
      &.green {
        color: #00B960;
        .radius {
          background-color: #00B960;
        }
      }
    }
  }
  .type {
    column-gap: 4px;
    .van-icon {
      transform: rotate(-90deg);
    }
  }
}
</style>