<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { apiImageUrl } from '../../API/api';

const currentPage = ref(1);
const pageSize = ref(10); // 每页显示的数据条数
const totalPage = ref(1); // 总页数
const orderList = ref([]);

// 获取订单列表函数
const getOrder = (page: number) => {
  uni.request({
    url: `${apiImageUrl}/api/orders/my/page`,
    method: 'POST',
    data: {
      current: page,
      endTime: "",
      id: "",
      pageSize: pageSize.value,
      pickupMethod: "",
      sortField: "createTime",
      sortOrder: "dscend",
      startTime: "",
      state: 0
    },
    header: {
      'Content-Type': 'application/json',
      'cookie': uni.getStorageSync("cookie") || ''
    },
    success(res) {
      if (res.data.code === 0) {
        const records = res.data.data.records;
        orderList.value = records; // 假设你希望反转列表
        totalPage.value = Math.ceil(res.data.data.total / pageSize.value);
        console.log('订单数据:', orderList.value);
      }
    },
    fail() {
      console.log('出错啦');
    }
  });
};

// 组件挂载时获取第一页订单数据
onMounted(() => {
  getOrder(currentPage.value);
});

// 分页器页面变化事件处理
const onPageChange = (e: any) => {
  currentPage.value = e.current;
  getOrder(e.current);
};

const closeOrder=(order)=>{
	console.log(order);
	console.log(order.id);
	uni.request({
		url:apiImageUrl+'/api/orders/cancel',
		method:'POST',
		data:{
			id:order.id
		},
		header: {
		  'Content-Type': 'application/json',
		  'cookie': uni.getStorageSync("cookie") || ''
		},
		success(res) {
			console.log("成功");
			console.log(res);
		},
		fail() {
			console.log("失败");
		}
	})
}

const goToPay=(order)=>{
	console.log(order);
	uni.request({
		url:apiImageUrl+'/api/Alipay/payment/create',
		method:'GET',
		data:{
			id:order
		},
		header: {
			'Content-Type': 'application/json', // 确保设置正确的 Content-Type
			'cookie': uni.getStorageSync("cookie") || ''
		},
		success: function (result) {
			console.log(result);
			console.log(result.data.data);
			uni.setStorageSync("tradeNo",result.data.data)
			my.tradePay({
				tradeNO: result.data.data,
				success: (res) => {
					console.log('成功调用')
					console.log(res);
					console.log(res.resultCode);
					if(res.resultCode==6001){
						console.log("我是6001");
						uni.setStorageSync("notPay",orderId);
						 uni.navigateTo({
						    url: '/pages/goToPay/goToPay'
						 });
					}
					else if(res.resultCode==9000){
						uni.setStorageSync('orderId', orderId);
						 uni.navigateTo({
							url: '/pages/testFive/testFive'
						 });
					}
				},
				fail: (res) => {
					my.alert({
						content: JSON.stringify(res),
					})
					console.log('失败');
					console.log(res);
				}
			});
		}
	})
}

const merchantDetail=(order)=>{
	console.log(order.businessVO?.id);
	uni.setStorageSync("businessItem",order.businessVO?.id)
	const merchantId = order.businessVO?.id;
	
	uni.navigateTo({
	   url: `/pages/merchant/merchant?merchantId=${merchantId}`,
	});
}
</script>


<template>
  <view class="viewport">
        <!-- 订单列表 -->
        <scroll-view scroll-y class="orders">
          <view class="card" v-for="(order, index) in orderList" :key="index">
            
            <view class="status">
              <text class="title" @click="merchantDetail(order)"> {{ order.businessVO?.businessName}}</text>
              
             <!-- <text>{{ order.totalPrice}}</text> -->
              <text>{{ order.id}}</text>
              <text class="icon-delete"></text>
            </view>
           
            <navigator
              v-for="sku in 1"
              :key="sku"
              class="goods"
              :url="`/pagesOrder/detail/detail?id=1`"
              hover-class="none"
            >
              <view class="cover">
                <image
                  mode="aspectFit"
                  :src="order.businessVO?.businessAvatar"
                ></image>
              </view>
              <view class="meta" v-for="(item,index) in order.orderDetailsVOList">
				    <view class="type">{{item.dishesVO?.dishesName}}--{{item.attributeNames}}</view>
                <text class="type">下单即送20积分</text>
              </view>
            </navigator>
			
            <view class="action">
              
              <template v-if="true">
				  <view class="button primary" @click="() => closeOrder(order)">取消订单</view>
				  <view class="button primary" @click="goToPay(order.id)">
				  	去支付
				  </view>
              </template>
              <template v-else>
                <navigator
                  class="button secondary"
                  :url="`/pagesOrder/create/create?orderId=id`"
                  hover-class="none"
                >
                  再次购买
                </navigator>
                
                <view v-if="false" class="button primary">确认收货</view>
              </template>
            </view>
          </view>
          
		  <view class="loading-text" :style="{ paddingBottom: safeAreaInsets?.bottom + 'px' }">
            {{ true ? '没有更多数据~' : '正在加载...' }}
          </view>
        </scroll-view>
		 <!-- 分页组件 -->
		    <uni-pagination
		      title="订单列表"
		      show-icon
		      :current="currentPage"
		      :total="totalPage * pageSize"
		      @change="onPageChange"
		    />
     </view>
</template>


<style lang="scss">
page {
  height: 100%;
  overflow: hidden;
}

.viewport {
  height: 100%;
  display: flex;
  flex-direction: column;
  background-color: #fff;
}

.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: 20%;
    height: 6rpx;
    padding: 0 50rpx;
    background-color: #4095e5;
    
    transition: all 0.4s;
  }
}


.swiper {
  background-color: #4095e5;
}


.orders {
  .card {
    min-height: 100rpx;
    padding: 20rpx;
    margin: 20rpx 20rpx 0;
    border-radius: 10rpx;
    background-color: #fff;

    &:last-child {
      padding-bottom: 40rpx;
    }
  }

  .status {
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 28rpx;
    color: #999;
    margin-bottom: 15rpx;

    .title {
      color: #343434;
	  font-weight: 700;
      flex: 1;
    }

    .primary {
      color: #ff9240;
    }

    .icon-delete {
      line-height: 1;
      margin-left: 10rpx;
      padding-left: 10rpx;
      border-left: 1rpx solid #e3e3e3;
    }
  }

  .goods {
    display: flex;
    margin-bottom: 20rpx;

    .cover {
      width: 170rpx;
      height: 170rpx;
      margin-right: 20rpx;
      border-radius: 10rpx;
      overflow: hidden;
      position: relative;
    }

    .quantity {
      position: absolute;
      bottom: 0;
      right: 0;
      line-height: 1;
      padding: 6rpx 4rpx 6rpx 8rpx;
      font-size: 24rpx;
      color: #fff;
      border-radius: 10rpx 0 0 0;
      background-color: rgba(0, 0, 0, 0.6);
    }

    .meta {
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: center;
    }

    .name {
      height: 80rpx;
      font-size: 26rpx;
      color: #444;
    }

    .type {
      line-height: 1.8;
      padding: 0 15rpx;
      margin-top: 10rpx;
      font-size: 24rpx;
      align-self: flex-start;
      border-radius: 4rpx;
      color: #888;
      background-color: #f7f7f8;
    }

    .more {
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 22rpx;
      color: #333;
    }
  }

  

  .action {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    padding-top: 20rpx;

    .button {
      width: 180rpx;
      height: 60rpx;
      display: flex;
      justify-content: center;
      align-items: center;
      margin-left: 20rpx;
      border-radius: 60rpx;
      border: 1rpx solid #ccc;
      font-size: 26rpx;
      color: #444;
    }

    .secondary {
      color: #4095e5;
      border-color: #4095e5;
    }

    .primary {
      color: #fff;
      background-color: #4095e5;
    }
  }

  .loading-text {
    text-align: center;
    font-size: 28rpx;
    color: #666;
    padding: 20rpx 0;
  }
}
image {
	width: 100%;
	height: 100%;
	border-radius: 20px;
}
.status-btn {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		justify-content: center;
		height: 92rpx;
		margin: 30rpx;
		background-color: #007AFF;
	}

	.example-body {
		/* #ifndef APP-NVUE */
		display: block;
		/* #endif */
		padding: 15px;
		flex-direction: row;
	}
	.tab-menu {
	  display: flex;
	  justify-content: space-around;
	  padding: 10px 0;
	  background-color: #f5f5f5;
	}
	.tab-item {
	  padding: 10px;
	  cursor: pointer;
	}
	.tab-item.active {
	  color: #007aff;
	  font-weight: bold;
	}

</style>