<template>
  <view class="page">
	  <scroll-view scroll-y class="preview" :style="{ height: scrollHeight }">
	    <!-- 商品详情 -->
	    <view class="tab-menu">
	      <view 
	        class="tab-item" 
	        v-for="(tab, index) in tabs"
	        :key="index"
	        :class="{ active: currentIndex === index }"
	        @click="switchTab(index)"
	      >
	        {{ tab.name }}
	      </view>
	    </view>
	    <view class="content">
	      <block v-if="currentIndex === 0 || currentIndex === 1 || currentIndex === 2">
	        <view class="container">
	          <view 
	            class="orderItem" 
	            v-for="(order, index) in filteredOrders" 
	            :key="index"
	          >
	            <view class="orderImg">
	              <image :src="orderImageUrl" class="img"></image>
	            </view>
	            <view class="orderMessage">
	              <text class="money">+{{ order.totalPrice }}元</text>
	              <br />
	              <text class="time">下单时间:{{ formatDate(order.createTime) }}</text>
	              <br />
	              <text class="time">订单编号:{{ order.id }}</text>
	            </view>
	          </view>
	        </view>
	      </block>
	    </view>
	  </scroll-view>
  	
  </view>
</template>

<script>
import { apiImageUrl } from '../../API/api';

export default {
  data() {
    return {
      currentIndex: 0,
      tabs: [
        { name: '全部' },
        { name: '收入' },
        { name: '退款' }
      ],
      orderList: [],
      orderImageUrl: "https://tse4-mm.cn.bing.net/th/id/OIP-C.cDfrXgI1DKvU7OklwmBfewHaHa?rs=1&pid=ImgDetMain",
	   scrollHeight: 'calc(100vh - 60px)'
    };
  },
  computed: {
    filteredOrders() {
      if (this.currentIndex === 0) {
        return this.orderList;
      } else {
        return this.orderList.filter(order => 
          (this.currentIndex === 1 && order.type === 'income') ||
          (this.currentIndex === 2 && order.type === 'refund')
        );
      }
    }
  },
  methods: {
    switchTab(index) {
      this.currentIndex = index;
    },
    getOrder() {
      uni.request({
        url: `${apiImageUrl}/api/orders/my/page`,
        method: 'POST',
        data: {
          current: 1,
          endTime: "",
          id: "",
          pageSize: 100,
          pickupMethod: 0,
          sortField: "",
          sortOrder: "",
          startTime: "",
          state: 0
        },
        success: (res) => {
          console.log(res);
          this.orderList = res.data.data.records || [];
          console.log(this.orderList);
        },
        fail: () => {
          console.log('出错啦');
        }
      });
    },
    formatDate(dateStr) {
      const date = new Date(dateStr);
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0');
      const day = String(date.getDate()).padStart(2, '0');
      return `${year}-${month}-${day}`;
    }
  },
  mounted() {
    this.getOrder();
  }
};
</script>

<style lang="scss">
.tabs {
  display: flex;
  justify-content: space-around;
  line-height: 60rpx;
  margin: 0 10rpx;
  background-color: #fff;
  box-shadow: 0 4rpx 6rpx rgba(240, 240, 240, 0.6);
  position: relative;
  z-index: 9;

  .item {
    flex: 1;
    text-align: center;
    padding: 20rpx;
    font-size: 28rpx;
    color: #262626;
  }

  .cursor {
    position: absolute;
    left: 0;
    bottom: 0;
    width: 18%;
    height: 6rpx;
    padding: 0 50rpx;
    background-color: #4095e5;
    /* 过渡效果 */
    transition: all 0.4s;
  }
}

.preview image {
  width: 100%;
}
.page {
  height: 100%;
  overflow: hidden;
  background-color: #f5f5f5;
}

.tab-menu {
  display: flex;
  justify-content: space-around;
  padding: 10px 0;
  background-color: #f5f5f5;
}
.tab-item {
  padding: 10px;
  cursor: pointer;
}
.tab-item.active {
  color: #4095e5;
  font-weight: bold;
}

.scroll-view {
  /* #ifndef APP-NVUE */
  width: 100%;
  height: 100%;
  /* #endif */
  flex: 1;
}

.orderItem {
  width: calc(100% - 20px); 
  min-height: 80px;
  margin-bottom: 10px;
  margin-left: auto;
  margin-right: auto;
  background-color: #fff;
  display: flex;
  align-items: center;
  border-radius: 10px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1), 0 2px 10px rgba(0, 0, 0, 0.08); 
  padding: 5px 10px 5px 10px;
}

.orderImg {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 60px; 
  height: 60px; 
  margin-right: 10px;
}

.img {
  width: 100%;
  height: 100%;
  border-radius: 50%; 
  object-fit: cover;
}

.orderMessage {
  flex-grow: 1;
  margin-top: 0; 
}

.money {
  font-size: 30rpx;
  font-weight: 700;
}

.time {
  font-size: 25rpx;
}

.container {
  width: 90%;
  margin: 0 auto;
}
</style>