import { summaryOfPendingPayments } from "../../api/documentManagement";
export default {
  data() {
    return {
      pendingPaymentData: [],
      allPendingData: [],
      currencyList: [
      

    ],
    loading:false,
    pendingPaymentParams: {
      pageNum: 1,
      pageSize: 10,
    },
    total: 0,
    pageSizes: [10, 50, 200],
    currentPage: 1,
    };
  },
  mounted() {
    this.currencyList = JSON.parse(localStorage.getItem("currencyList"))
    window.addEventListener("setItem", () => {
        this.currencyList = JSON.parse(localStorage.getItem("currencyList"))
    });
    this.fetchPendingPaymentData(this.pendingPaymentParams);
  },
  methods: {
    async fetchPendingPaymentData(params) {
        this.setLoading('loading', true);
        const firstRes = await summaryOfPendingPayments(params);
        let allRows = [];
        if (firstRes && firstRes.code == 200) {
          const firstRows = Array.isArray(firstRes.rows) ? firstRes.rows : [];
          const total = Number(firstRes.total || 0);
          // 若后端做了分页，二次拉取全量数据用于前端全局排序与分页
          if (total > params.pageSize) {
            const fullRes = await summaryOfPendingPayments({ ...params, pageNum: 1, pageSize: total });
            allRows = Array.isArray(fullRes && fullRes.rows) ? fullRes.rows : firstRows;
          } else {
            allRows = firstRows;
          }

          const sortedRows = this.sortRows(allRows);
          this.allPendingData = sortedRows;
          this.total = sortedRows.length;
          this.applySlice();
        }
        this.setLoading('loading', false);
    },
    sortRows(rows) {
        const safeRows = Array.isArray(rows) ? rows : [];
        return safeRows.slice().sort((a, b) => {
          const aNeed = Number(a && a.needPayAmount != null ? a.needPayAmount : 0);
          const aStart = Number(a && a.startPayAmount != null ? a.startPayAmount : 0);
          const bNeed = Number(b && b.needPayAmount != null ? b.needPayAmount : 0);
          const bStart = Number(b && b.startPayAmount != null ? b.startPayAmount : 0);

          const aPriority = aNeed >= aStart ? 1 : 0;
          const bPriority = bNeed >= bStart ? 1 : 0;
          if (bPriority !== aPriority) return bPriority - aPriority;
          if (bNeed !== aNeed) return bNeed - aNeed;
          return 0;
        });
    },
    applySlice() {
        const pageSize = Number(this.pendingPaymentParams.pageSize || 10);
        const pageNum = Number(this.pendingPaymentParams.pageNum || 1);
        const start = (pageNum - 1) * pageSize;
        const end = start + pageSize;
        this.pendingPaymentData = (this.allPendingData || []).slice(start, end);
    },
    handelImg(coin) {
        console.log(coin,"coin");
      
        if(coin == "enx"){
            const imgUrl = `${this.$baseApi}img/enx.svg`;
       
            return imgUrl;
        }
        if(this.currencyList &&this.currencyList.length > 0 && coin){
            return this.currencyList.find(item => item.value === coin)?.imgUrl || '';
        }else{
            return '';
          
        }
    },
    handelCoinLabel(coin) {
        if(coin == "enx"){
            return "Entropyx(enx)";
        }
        if(this.currencyList &&this.currencyList.length > 0 && coin){
            return this.currencyList.find(item => item.value === coin)?.label || '';
        }else{
            return coin;
         
        }
    },
    handelTime(time) {
      try {
        return `${time.split("T")[0]} ${time.split("T")[1].split(".")[0]}`
      } catch (error) {
        return '';
      }
    },
    handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.pendingPaymentParams.pageSize = val
        this.pendingPaymentParams.pageNum = 1
        this.currentPage = 1
        if (this.allPendingData && this.allPendingData.length > 0) {
            this.applySlice();
        } else {
            this.fetchPendingPaymentData(this.pendingPaymentParams);
        }
       
    },
    handleCurrentChange(val) {
            console.log(`当前页: ${val}`);
            this.pendingPaymentParams.pageNum = val
            if (this.allPendingData && this.allPendingData.length > 0) {
                this.applySlice();
            } else {
                this.fetchPendingPaymentData(this.pendingPaymentParams);
            }



    },
   
  }
};