<template>
    <div class="card p-card pb-3 mb-3 mx-2">
      <h4 class="card-custom-title">
        {{ $t(i18nConst.stakingHistoryTitle) }}
      </h4>
      <hr>

      <div class="row">
        <div class="col-12 d-flex flex-column card-bc-item justify-content-between">

          <div class="d-flex flex-row justify-content-between">
            <span class="d-flex w-25 title-history-tab justify-content-center">Date</span>
            <span class="d-flex w-25 title-history-tab justify-content-center">Amount</span>
            <span class="d-flex w-25 title-history-tab justify-content-center">Action</span> 
          </div>
          <div v-for="history in stakingHistory" :key="history[0]">

              <div class="d-flex flex-row justify-content-between">
                <span class="d-flex w-25">{{ getDate(history.from) }}</span>
                <span class="d-flex w-25 justify-content-end"><span v-if="history.action === 'unstaking'">-</span> {{ getAmount(history.amount) }}</span>
                <span class="d-flex w-25 justify-content-end">{{ history.action }}</span>                
              </div>
          </div>
        </div>
      </div>
    </div>
  </template>
  
  <script>
  import i18nConstantes from "@/constantes/i18n.constantes.js";
  import typeActions from "@/store/typeActions";
import { mapGetters } from 'vuex';
import web3Utils from "@/utils/web3.utils.js";

  
  export default {
      name: "StakingHistory",
      data() {
          return {
            utils: web3Utils,
              i18nConst: i18nConstantes,
              amount: 0,
              interestAmount: 0
          };
      },
      computed: {
          ...mapGetters('staking', ['stakingHistory', 'stakingBalance'])
      },
      methods: {
        getDate(timestamp) {
          const date = new Date(parseInt(timestamp) * 1000);
          return date.toLocaleString();
        },
        getAmount(amount) {
          if (amount) {
            const _amount = Number(amount).toLocaleString('fullwide', {useGrouping:false});
            return this.utils.formatDisplayAmountMetadexWithEthers( _amount );
          } else {
            return 0;
          }
        },
        async getInterestHistory() {
          await this.$store.dispatch(typeActions.getStakerInterestReleaseHistory);
        },
        async getStakerStakingHistory() {
          await this.$store.dispatch(typeActions.getStakerStakingHistory);
        }
      },
      mounted() {
        this.getStakerStakingHistory();
        this.getInterestHistory();
      },
  }
  </script>
  
  <style lang="scss" scoped>
  .img-metadex {
    width: 30px;
    margin-right: 0.3rem;
  }
  
  .title-history-tab {
    font-weight: bold;
    padding-bottom: 0.5rem;
  }

  </style>