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


const currentPage = ref(1);
const pageSize = ref(10); // 每页显示的数据条数
const totalItems = ref(0); // 总数据条数
const active = 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: 2
    },
    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; // 假设你希望反转列表
        totalItems.value = res.data.data.total; // 更新总数据条数
        console.log('订单数据:', orderList.value);
      }
    },
    fail() {
      console.log('出错啦');
    }
  });
};

// 删除订单的方法
const deleteOrder = async (orderId: number | string) => {
  try {
    const response = await uni.request({
      url: `${apiImageUrl}/api/orders/delete`,
      method: 'POST',
      header: {
        'Content-Type': 'application/json', 
      },
      data: JSON.stringify({ id: orderId }), 
    });

    if (response.statusCode === 200) {
      console.log(`成功取消订单:${orderId}`);
     
	    uni.showModal({
	      title: '提示',
	      content: '删除订单成功',
	      showCancel: false,
		    success: (res) => {
		       if (res.confirm) {
		         getOrder();
		       }
		     }

	    });
		
    } else {
      console.error(`无法取消订单:${orderId},错误码:${response.statusCode}`);
    }
  } catch (error) {
    console.error(`发生错误:${error.message}`);
  }
};


const onPageChange = (e: any) => {
  currentPage.value = e.current;
  getOrder(e.current);
};

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

const orderDetail=(order)=>{
	console.log(order);
}

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" @click="orderDetail(order)">
        <view class="status">
          <text class="title" @click="merchantDetail(order)">{{ order.businessVO?.businessName }}</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="() => deleteOrder(order.id)">删除订单</view>
            <view class="button primary">在线联系</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="totalItems"
      :pageSize="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>