import { getRpc } from "ycc-api/src/service/Rpc";
import { getBlockDetail } from "ycc-api/src/service/blockDetail/index";
import Vue from "vue";
import { getBlockOverviewAndRelativeHeight } from "ycc-api/dist/cmjs/service/blockDetail";
import { message } from "ant-design-vue";
import { tradeAccuracy } from "@/utils/common";
const Rpc = getRpc("/yccApi");
export default Vue.extend({
  data() {
    return {
      searchValue: "",
      focusedTab: "1",

      selectedOption: "1",
      overview: {
        txCount: 0,
        blockTime: 0,
        hash: "", // 区块哈希
        stateHash: "", //状态哈希
        txHash: "", // 默克尔根
        lastBlockHeight: NaN,
        nextBlockHeight: NaN,
      },
      loadingOverview: false,
      loadingRelativeNumber: false,
      loadingTable: false,
      Trades: [] as any[],
      txHashes: [],
      pages: {
        currentPage: 1,
        pageSize: 10,
        total: 0,
      },
    };
  },
  methods: {
    isSuccess(execer: any, ty: any) {
      return !tradeAccuracy(execer, ty);
    },
    fetchTradeFromTxHashes() {
      const searchArr: string[] = [];
      const end =
        this.pages.pageSize * this.pages.currentPage - 1 < this.pages.total
          ? this.pages.pageSize * this.pages.currentPage - 1
          : this.pages.total - 1;
      const start = this.pages.pageSize * (this.pages.currentPage - 1);
      for (let i = start; i <= end; i++) {
        searchArr.push(this.txHashes[i]);
      }
      Rpc.getTxByHashes(searchArr).then((data) => {
        if (data.error == null) {
          // this.searchIcon(data.result.txs);
          this.Trades = this.checkGroup(data.result.txs);
        } else {
          message.warning(this.$t("components.tip") + data.error);
        }
      });
    },
    checkGroup(arr: any[]) {
      arr.map((current, index) => {
        if (current.tx.groupCount > 1) {
          current.tradeG = 1; //0-非交易组，1-交易组，3-交易组末条, 2-交易组首条
          if (current.tx.fee !== 0) {
            current.tradeG = 2;
          } else {
            if (index < arr.length) {
              current.tradeG = arr[index].tx.next ? 1 : 3;
            } else {
              current.tradeG = 3;
            }
          }
        } else {
          current.tradeG = 0;
        }
      });
      return arr;
    },
    selectOption(v: string) {
      this.selectedOption = v;
    },
    clickSearch(v: string) {
      console.log(v, " show search value");
    },
    setSearchValue(v: string) {
      this.searchValue = v;
    },
    setFocusedTab(v: string) {
      this.focusedTab = v;
    },
    async fetchBlockOverviewAndRelativeBlockHeightData() {
      this.loadingOverview = true;
      this.loadingRelativeNumber = true;
      getBlockOverviewAndRelativeHeight("ycc", "/yccApi", {
        height: Number(this.$route.query.height),
      }).then((data) => {
        if (data) {
          const { relativeBlockHeight, overview } = data;
          overview.then((o: any) => {
            this.loadingOverview = false;
            this.overview = {
              ...this.overview,
              blockTime: o.blockTime,
              txCount: o.txCount,
              hash: o.hash,
              stateHash: o.stateHash,
              txHash: o.txHash,
            };
            this.txHashes = o.txHashes;
            this.pages.total = this.txHashes.length;
            this.fetchTradeFromTxHashes();
          });
          relativeBlockHeight.then((r: any) => {
            this.loadingRelativeNumber = false;
            this.overview = {
              ...this.overview,
              lastBlockHeight: r.lastBlockHeight,
              nextBlockHeight: r.nextBlockHeight,
            };
          });
        }
      });
    },
    async init() {
      this.fetchBlockOverviewAndRelativeBlockHeightData();
    },
    // 交易组模块
    groupCellName(val: any) {
      switch (val.row.tradeG) {
        case 0:
          return "";
        case 1:
          return "tx-middle";
        case 2:
          return "tx-start";
        case 3:
          return "tx-end";
        default:
          return "";
      }
    },
    pageChange(page: number) {
      this.pages.currentPage = page;
    },
    sizeChange(size: number) {
      this.pages.pageSize = size;
    },
  },
  computed: {
    tabList(): any[] {
      return [
        { name: this.$t("lang.trade.txRecord"), value: "1" },
        { name: this.$t("lang.trade.consensusNode"), value: "2" },
      ];
    },
    optionList() {
      return [
        { name: this.$t("lang.trade.txRecord"), value: "1" },
        { name: this.$t("lang.trade.consensusNode"), value: "2" },
      ];
    },
  },
  async mounted() {
    this.init();
  },
  watch: {
    "$route.query.height"(newV, oldV) {
      this.pages = {
        currentPage: 1,
        pageSize: 10,
        total: 0,
      };
      this.init();
    },
  },
});
