<template>
  <view class="container">
    <unicloud-db ref="udb" v-slot:default="{data, pagination, loading, hasMore, error}" :collection="collectionList" field="User_id,Order_ID,CommodityName,CommodityPrice,CommodityImg,OrderStatus,CommodityID">
      <view v-if="error">{{error.message}}</view>
      <view v-else-if="data">
        <uni-list>
          <uni-list-item v-for="(item, index) in currentList" :key="index" showArrow :clickable="true" @click="handleItemClick(item._id)">
            <template v-slot:body>
              <text>
                <!-- 此处默认显示为_id，请根据需要自行修改为其他字段 -->
                <!-- 如果使用了联表查询，请参考生成的 admin 项目中 list.vue 页面 -->
                {{item.Order_ID}}
              </text>
            </template>
          </uni-list-item>
        </uni-list>
		
		 <view class="uni-pagination-box">
			<uni-pagination @change="change" show-icon="true" :total="total" :pageSize="pageSize"
				:current="pageCurrent"></uni-pagination>
		</view> 
		
		<button type="primary" @click="fanhui">返回</button> 
      </view>
    </unicloud-db>
  </view>
</template>

<script>
  const db = uniCloud.database()
  export default {
    data() {
      return {
		//总页数
		total: 0,
		// 每页包含数据条数
		pageSize: 10,
		// 当前在第几页
		pageCurrent: 1,
		// 当前页面的数据
		currentList: [],
		// 表格的总体数据
		tableList: [],
        collectionList: "CommodityOrder",
        loadMore: {
          contentdown: '',
          contentrefresh: '',
          contentnomore: ''
        }
      }
    },
    onPullDownRefresh() {
      this.$refs.udb.loadData({
        clear: true
      }, () => {
        uni.stopPullDownRefresh()
      })
    },
    onReachBottom() {
      this.$refs.udb.loadMore()
    },
	created() {
		// 计算一共有多少页
		this.total = Math.ceil(this.tableList.length / this.pageSize);
	    this.getData(1)
		this.getPageData(1);
	},
    methods: {
      handleItemClick(id) {
        uni.navigateTo({
          url: './detail?id=' + id
        })
      },
	  //分页点击事件
	  change(e) {
	  	this.pageCurrent = e.current; //给变量赋值	
	  	this.getPageData(e.current)
	  },
	  getPageData() {
	  	db.collection("CommodityOrder").get() //获取数据表的信息
			.then(res => {
				this.tableList = res.result.data //把数据赋值给本页数组 
				this.currentList = this.tableList.slice( //把每页数据分页查询 赋值给 当页显示的数组
					(this.pageCurrent - 1) * this.pageSize,
					this.pageCurrent * this.pageSize
				);
			}).catch((err) => {})
	  },
	  // 查询数据表中的数据
	  getData() {
	  	db.collection('CommodityOrder')
	  		.count()
	  		.then((res) => {
	  			this.total = res.result.total
	  		})
	  },
	  fanhui(){	  
		uni.reLaunch({
		    url: '/pages/ucenter/ucenter'
		})	
	  }
    }
  }
</script>

<style>
</style>
