<template><page-meta :page-font-size="fontValue+'px'" :root-font-size="fontValue+'px'"></page-meta>
  <view class="content">
    <view class="top-wrapper"> 
      <view style="padding: 0 16px">
        <Tabs :tabList="tabList" @changeTab="changeTab" />
      </view>
    </view>
	<!-- 待评价 -->
    <scroll-view style="width: 100%;height: 90vh;" @scrolltolower="onScrollTolower" scroll-y="true" v-if="tabIndex==0">
      <view class="order-list" v-for="(item, index) in tableList" :key="index">
        <view class="flex justify-between">
          <!-- <view class="order-source">订单来源：双屏消费机</view> -->
          <view class="order-source" style="font-size: 28rpx;font-weight: bold;">订单来源：{{ item.orderTypeStr }}</view>
          <view style="color: rgba(15, 39, 75, 0.8);font-size: 28rpx;">{{ handleOrderState(item.orderState) }}</view>
        </view>
        <view class="flex justify-between">
          <view>
            <!-- <span style="color: #0f274b">{{ item.canteenName }}/{{item.stallName}}</span> -->
            <span style="color: #0f274b">{{ item.canteenName }}</span>
            <span class="intervalName">{{ item.mealtimeName }}</span>
          </view>
          <view style="color: rgba(15, 39, 75, 0.8)">就餐时间：{{ item.orderDate }}</view>
        </view>
        <view class="img-cont" @click="goDetail(item)">
          <view class="img" v-for="(goods, gindex) in item.orderDetailList" :key="gindex" v-if="gindex<3">
            <u-image :src="goods.goodsImgUrl" width="80px" height="80px" />
            <view>{{ goods.goodsName }}</view>
          </view>
          <view class="price">
            <view>
              <span>￥</span>
              <span style="font-size: 32rpx">{{ (item.realAmount/100).toFixed(2) }}</span>
            </view>
            <view style="color: rgba(15, 39, 75, 0.6)">共{{ item.orderDetailList.length }}件</view>
          </view>
        </view>
        <view class="btn-cont">
          <view v-if="item.orderState==2">
            <u-button size="small" style="background: #ff6816;color: #FFF;font-size: 24rpx;" @click="goEvaluate(item)">评价</u-button>
          </view>
          <!-- <view v-if="item.orderState == 3">
            <u-button size="small" style="background: #ff6816;color: #FFF;">删除订单</u-button>
          </view> -->
        </view>
      </view>
	  <view style="margin: 20px 0" v-if="tableList.length > 0">
	    <u-loadmore :status="status" nomoreText="没有更多数据了" />
	  </view>
	  <view v-else class="flex justify-center align-center" style="height: 50vh">
	    <u-empty icon="../../static/images/not_order.png" text="暂无评价" textColor="#000" />
	  </view>
    </scroll-view>
	
	<!-- 已评价 -->
	<scroll-view style="width: 100%;height: 90vh;" @scrolltolower="onScrollTolower" scroll-y="true" v-if="tabIndex==1">
	  <view class="order-list" v-for="(item, index) in tableList2" :key="index"> 
	    <view class="flex justify-between">
	      <view> 
	        <text style="color: #0f274b;font-weight: bold;font-size:28rpx">{{item.stallName}}</text> 
	      </view>
	      <view style="color: rgba(15, 39, 75, 0.8)">{{ item.createTime }}</view>
	    </view>
		<view class="flex" style="width: 100%;margin: 10rpx 0;">
			<view style="display: flex;gap: 0rpx;">
			  <u-icon v-for="n in 5" :key="n" :name="n <= item.starLevel ? 'star-fill' : 'star-fill'"
				:color="n <= item.starLevel ? '#ff9933' : '#ccc'" size="20"></u-icon>
			</view>
		</view>
		<view style="width: 94%;margin: 10rpx auto;height: auto;word-break: break-all;font-size: 24rpx;">
			{{item.description}}
		</view>
		<view style="width: 94%;margin: 10rpx auto;height: auto;display: flex;flex-wrap: wrap;">
			<view v-for="(imgItem,imgIndex) in item.pictureList" :key="imgIndex">
				<u-image :src="imgItem" width="80px" height="80px" />
			</view>
		</view>
	    
	  </view>
	  <view style="margin: 20px 0" v-if="tableList2.length > 0">
	    <u-loadmore :status="status2" nomoreText="没有更多数据了" />
	  </view>
	  <view v-else class="flex justify-center align-center" style="height: 50vh">
	    <u-empty icon="../../static/images/not_order.png" text="暂无相关订单" textColor="#000" />
	  </view>
	</scroll-view>
	
  </view>
</template>

<script> 
import Tabs from '@/pages/components/Tabs.vue'
import { getDate } from '../../uni_modules/uni-datetime-picker/components/uni-datetime-picker/util'
import { getOrderInfoListAPI,getPageEvaluateListApi,getOrderSourceTypeListAPI } from '../../api/order/index'

export default {
	components: { Tabs },
	props: {
		show: {
		  type: Boolean,
		  default: false
		}
	},
	data() {
		return {
			fontValue:uni.getStorageSync('fontSize') || 8,
			tabIndex: 0,
			tabList: ['待评价', '已评价'],
			startDate: '',
			endDate: '',
			//待评价
			pageNum: 1,
			pageSize: 10,
			total: 0, 
			tableList: [], 
			status: 'loadmore', 
			// sourceTypeList:[],
			//已评价
			pageNum2: 1,
			pageSize2: 10,
			total2: 0, 
			tableList2: [],
			status2:'loadmore',
		}
	}, 
	onLoad(options) {
		// this.getSourceTypeList()
		this.startDate = options.startDate
		this.endDate = options.endDate
		this.getUnEvaluateList() 
	},
	methods: {
		async getSourceTypeList() {
		  try {
		    const res = await getOrderSourceTypeListAPI({}) 
			this.sourceTypeList=res
		  } catch (error) {
		    console.log(error)
		  }
		},
		// 翻页
		onScrollTolower(){
			if(this.tabIndex==0){
				if(this.total>this.tableList.length){ 
					this.pageNum++
					this.getUnEvaluateList()
				}
			}else{
				if(this.total2>this.tableList2.length){ 
					this.pageNum2++
					this.getEvaluatedList()
				}
			}
		},
		//获取待评价列表
		async getUnEvaluateList() {
			console.log('获取列表')
			let params = {
				userId: uni.getStorageSync('userId'),
				pageNum: this.pageNum,
				pageSize: this.pageSize,  
				commentState:"2", 
				orderStateList:[2],   
				searchValue: "", 
			}
			if(this.startDate&&this.endDate){
				params.startDateTime = this.startDate+" 00:00:00"
				params.endDateTime = this.endDate+" 23:59:59"
			}else{
				params.startDateTime = getDate(new Date())+" 00:00:00"
				params.endDateTime = getDate(new Date())+" 23:59:59"
			}
			try {
			const res = await getOrderInfoListAPI(params)
				console.log('?? ~ getUnEvaluateList ~ res:', res)
				this.total = res.total;
				// res.records.forEach(item=>{
				// 	let hasIndex=this.sourceTypeList.findIndex(v=>v.key==item.sourceType)
				// 	item.sourceTypeStr = this.sourceTypeList[hasIndex].value
				// })
				if(this.pageNum==1){
					this.tableList = res.rows
				}else{
					this.tableList.push(...res.rows)
				}
				this.status = this.total == this.tableList.length ? 'nomore' : 'loadmore'
			} catch (error) {
				console.log(error)
			}
		},
		//获取订单列表
		async getEvaluatedList() {
			console.log('获取列表')
			const params = {
				userId: uni.getStorageSync('userId'),
				startDate: this.startDate,
				endDate: this.endDate,
				pageNum: this.pageNum2,
				pageSize: this.pageSize2, 
			} 
			try {
			const res = await getPageEvaluateListApi(params)
				console.log('?? ~ getEvaluatedList ~ res:', res)
				this.total2 = Number(res.total); 
				if(this.pageNum2==1){
					this.tableList2 = res.rows
				}else{
					this.tableList2.push(...res.rows)
				}
				this.status2 = this.total2 == this.tableList2.length ? 'nomore' : 'loadmore'
			} catch (error) {
				console.log(error)
			}
		},
		//翻页
		onReachBottom() {
			console.log('?? ~ onReachBottom ~ ')
			// if (this.total < 10 || this.total == this.tableList.length) {
			//   this.status = 'nomore'
			//   return
			// }
			// this.status = 'loading'
			// setTimeout(() => {
			//   this.pageSize += 10
			//   this.getList()
			//   if (this.tableList.length != this.total) this.status = 'loadmore'
			//   else this.status = 'nomore'
			// }, 500)
			// console.log('加载..', this.pageSize)
		}, 
		//全部/待评价/退款
		changeTab(index) {
			console.log('?? ~ changeTab ~ index:', index)
			this.tabIndex = index;
			if(index==0){
				// this.pageNum=1;
				// this.tableList=[] 
				this.getUnEvaluateList()
			}else{
				// this.pageNum=1;
				// this.tableList=[]
				this.getEvaluatedList()
			} 
			
		},
		
		// 处理订单状态展示
		handleOrderState(state) {
		  switch (state) {
			case 1:
			  return '已下单'
			case 2:
			  return '已完成'
			case 3:
			  return '已退单'
			 
		  }
		}, 
		formatDate(date) {
			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}`
		},
		// 跳转订单详情
		goDetail(item) {
			console.log('跳转订单详情', item)
			uni.navigateTo({
				url: `/pages/work/details?params=${JSON.stringify(item)}`
			})
		},
		//评价按钮
		goEvaluate(item) {
			console.log('跳转订单详情', item)
			uni.navigateTo({
				url: `/pages/work/evaluate?params=${JSON.stringify(item)}`
			})
		},
	}
}
</script>

<style lang="scss" scoped>
.content {
  // background: #f9fbff;
  // height: 100%;
  height: 88vh;
	.top-wrapper {
		width: 100vw;
		height: 60rpx;
		background: #f9fbff;
		z-index: 999;
	}

	.top-cont {
		padding: 0 16px;
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20px;
		.top-title {
			font-weight: 500;
			font-size: 40rpx;
			color: #0f274b;
		}
		.top-ipt {
			height: 31px;
			background: #fff;
			margin-left: 26px;
			margin-right: 12px;
		}
		.top-filter {
			display: flex;
			align-items: center;
		}
	}

	.order-list {
		padding: 8px;
		background: #fff;
		border-radius: 8px;
		font-size: 24rpx;
		font-weight: 400;
		// margin-bottom: 12px; 
		margin: 10px;

		.order-source {
			margin-bottom: 12px;
			font-weight: 500;
			color: #0f274b;
		}

		.intervalName {
			margin-left: 10px;
			color: #ff6816;
			border-radius: 1px 1px 1px 1px;
			border: 1px solid #ff6816;
			padding: 0 4px;
		}

		.img-cont {
			margin: 10px;
			display: flex;
			justify-content: space-between;

			.img {
			display: flex;
			flex-direction: column;
			align-items: center;
				view {
					margin-top: 8px;
					color: #0f274b;
				}
			}
		}
			
		.price {
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			color: #0f274b;
			font-weight: 400;
			font-size: 24rpx;
		}

		.btn-cont {
			display: flex;
			justify-content: flex-end;
			margin-top: 10px;
		}
	}
}
.evaluateCenter{
	height: 30px;width: 100%;display: flex;justify-content: space-between;align-items: center;
	.evaluateCenterLeft{
		width: 25%;display: flex;justify-content: space-between;align-items: center;margin-left: 20px;font-size: 24rpx;
	}
	.evaluateCenterRight{
		width: 60px;height: 24px;line-height:24px;border: 1px solid #ff6816;color: #ff6816;margin-right: 20px;font-size: 24rpx;text-align: center;border-radius: 8px;
	}
}
.filter-popup {
  padding: 30rpx;
  min-height: 60vh;
  max-height: 90vh;
	.section {
		margin-bottom: 40rpx;

		.section-title {
		  font-size: 32rpx;
		  color: #333333;
		  font-weight: 500;
		  margin-bottom: 20rpx;
		}
	}

	.date-quick-select {
		display: flex;
		gap: 20rpx;
		margin-bottom: 20rpx;

		.date-option {
		  flex: 1;
		  height: 80rpx;
		  display: flex;
		  align-items: center;
		  justify-content: center;
		  background-color: #f8f8f8;
		  border-radius: 100rpx;
		  font-size: 28rpx;
		  color: #666666;

		  &.active {
			background-color: #fff2ef;
			color: #ff6633;
		  }
		}
	}

	.date-range {
		display: flex;
		align-items: center;
		gap: 20rpx;

		.date-input {
		flex: 1;
		height: 80rpx;
		background-color: #f8f8f8;
		border-radius: 100rpx;
		padding: 0 20rpx;
		display: flex;
		align-items: center;
		font-size: 28rpx;

		.date-placeholder {
		color: #999999;
		}

		.selected-date {
		color: #333333;
		}
		}

		.date-separator {
		color: #999999;
		font-size: 28rpx;
		}
	}

	.order-type-item {
		width: 200rpx;
		height: 80rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #f8f8f8;
		border-radius: 25px;
		font-size: 28rpx;
		color: #666666;
		margin: 10rpx auto;

		&.active {
		  background-color: #fff2ef;
		  color: #ff6633;
		}
	}

	.bottom-buttons {
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		padding: 28px 16rpx;
		background-color: #ffffff;
		display: flex;
		justify-content: space-between;
		box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
	}
}
</style>
