<template>
  <view>
    <Navbar title="充值记录" :goTo="paths.remainingSum" :showRightText="true" :isBack="false" :text="'充值日期'" @clickIcon="handleRightText"/>
    <u-datetime-picker
	  ref="datetimePicker"
      :show="show"
      v-model="dateValue"
      title="选择日期"
      :filter="filter"
      mode="date"
      closeOnClickOverlay
      @confirm="confirm"
      @cancel="cancel"
      @change="change"
      @close="close"
    ></u-datetime-picker>
    <view class="recharge-history">
      <!-- 顶部标签导航 -->
      <view class="tab-navigation">
        <Tabs :tabList="tabList" @changeTab="changeTab" />
      </view>

      <!-- 充值记录列表 -->      <!-- <view class="record-list"> -->
	  <scroll-view scroll-y @scrolltolower="onScrollTolower" class="record-list">
        <view v-for="(record, index) in rechargeRecords" :key="index" class="record-item">
          <!-- 左侧图标 -->
          <u-icon :name="getIconClass(record.type)" size="42"></u-icon>
		  <!-- <text>{{index}}</text> -->
          <!-- 中间充值详情 -->
          <view class="record-details">
            <view class="status" v-if="record.payState<=3" style="color: seagreen;">{{ record.payStateName }}</view>
            <view class="status" v-if="record.payState>3" style="color: red;">{{ record.payStateName }}</view>
			<view class="time">充值方式：{{ record.payTypeName }}</view>
            <view class="time">{{ record.tradeTime }}</view>
          </view>

          <!-- 右侧金额信息 -->
          <view class="amount-info">
            <view class="amount">¥{{ (record.amount/100).toFixed(2) }}</view>
            <view class="balance">余额: {{ (record.walletBalTotal/100).toFixed(2) }}</view>
          </view>
		  
        </view>
	  </scroll-view>
		<!-- </view> -->
	  
	  
    </view>
  </view>
</template>

<script>
import { chargeRecordApi } from '../../api/index/index'
import Tabs from '@/pages/components/Tabs.vue'
import UIcon from '../../uni_modules/uview-ui/components/u-icon/u-icon.vue'

export default {
  name: 'rechargeRecord',
  components: { UIcon, Tabs },
  data() {
    return {
      paths: {
        remainingSum: '/pages/remainingSum/index'
      },
      current: 0,
      show: false,
      dateValue: "",
      activeTab: 'all',
      tabs: [
        { id: 'all', name: '全部' },
        { id: 'success', name: '充值成功' },
        { id: 'failed', name: '充值失败' },
        { id: 'timeout', name: '充值超时' }
      ],
      tabList: ['全部', '充值成功', '充值失败', '充值失败'],
	  queryParam:{
		pageNum:1,
		pageSize:10
	  },
	  rechargeState:"",
	  total:0,
      rechargeRecords: []
    }
  },
  onLoad() {
  	this.getRecordList()
  },
  methods: {
	  onScrollTolower(){
		if(this.total>this.rechargeRecords.length){
			this.queryParam.pageNum++
			this.getRecordList()
		}
	  },
	async getRecordList() {
		let params = {
		  custId: uni.getStorageSync('custId'),
		  rechargeState:this.rechargeState,
		  current:this.queryParam.pageNum,
		  size:this.queryParam.pageSize,
		  startDateTime:this.dateValue,
		  endDateTime:this.dateValue
		}
		const res = await chargeRecordApi(params)
		console.log('🚀 ~ getBalance ~ res', res)
		// this.rechargeRecords = res.records;
		this.total=res.total;
		res.records.forEach(item=>{
			item.tradeTime = item.tradeTime.replace('T', ' ')
		})
		if (this.queryParam.pageNum==1) {
		    this.rechargeRecords = res.records
		} else {
			this.rechargeRecords.push(...res.records)
		}
		 
	},
    changeTab(index) {
      console.log('🚀 ~ changeTab ~ index:', index)
      this.tabIndex = index;
	  if(index==0){
		  this.rechargeState="";
		  this.queryParam.pageNum=1;
		  this.getRecordList()
	  }else{
		  this.rechargeState=index;
		  this.queryParam.pageNum=1;
		  this.getRecordList()
	  }
	  
    },
    filter(type, options) {
      // if (type === 'year') {
      //   return options.filter((option) => option % 2 === 0)
      // }
      return options
    },
    confirm(e) {
		this.show = false;
		if (!isNaN(e.value)) {
			const timestamp = parseInt(e.value);
			const selectedDate = new Date(timestamp);      
			const year = selectedDate.getFullYear();
			const month = ("0" + (selectedDate.getMonth() + 1)).slice(-2);
			const day = ("0" + selectedDate.getDate()).slice(-2);
			const hours = ("0" + selectedDate.getHours()).slice(-2);
			const minutes = ("0" + selectedDate.getMinutes()).slice(-2);
			const seconds = ("0" + selectedDate.getSeconds()).slice(-2);
			// this.dateValue = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
			this.dateValue = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
			this.show = false;
			this.queryParam.pageNum=1;
			this.getRecordList()
		}
		console.log(this.dateValue)
    },
    cancel() {
      this.show = false;
    },
    change(e) {
      console.log('change', e)
    },
    close() {
      this.show = false;
    },
    handleRightText() {
      console.log('🚀 ~ handleRightText ~ ')
      this.current = 5
      this.show = true
    },
    setActiveTab(tabId) {
      this.activeTab = tabId
    },
    getIconClass(type) {
      switch (type) {
        case 'wechat':
          return require('../../static/images/successful.png')
        case 'wallet':
          return require('../../static/images/lose.png')
        default:
          return require('../../static/images/delay.png')
      }
    }
  }
}
</script>

<style scoped lang="scss">
page {
  background: #fff;
}

.recharge-history {
  background: #fff;
  //background-color: #f3f4f6;
  min-height: 94vh;
}

.tab-navigation {
  display: flex;
  background-color: white;
  padding: 15px 16px 0 16px;
}

.tab {
  padding: 12px 16px;
  position: relative;
  cursor: pointer;
}

.tab.active {
  color: #f97316;
}

.tab.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 2px;
  background-color: #f97316;
}

.record-list {
	// background-color: #22c55e;
	height: 87vh;
  //padding: 0 16px;
}

.record-item {
  display: flex;
  align-items: center;
  background-color: white;
  margin-top: 8px;
  padding: 16px;
  border-radius: 8px;
}

.icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.inner-circle {
  width: 24px;
  height: 24px;
  background-color: white;
  border-radius: 50%;
}

.default-icon {
  background-color: #3b82f6;
}

.wechat-icon {
  background-color: #22c55e;
}

.wallet-icon {
  background-color: #f97316;
}

.record-details {
  flex: 1;
  margin-left: 12px;
}

.status {
  font-size: 16px;
  font-weight: 500;
}

.time {
  font-size: 14px;
  color: #6b7280;
}

.amount-info {
  text-align: right;
}

.amount {
  color: #f97316;
}

.balance {
  font-size: 14px;
  color: #6b7280;
}
</style>