<template>
<view class="container">
	<block v-if="isload">
		<dd-tab :itemdata="['全部','待付款','待发货','待收货','已完成']" :itemst="['all','0','1','2','3']" :st="st" :isfixed="true" @changetab="changetab"></dd-tab>
		<view class="pages-con">
			<block v-for="(item, index) in datalist" :key="index">
				<view class="order-item" @tap="goto" :data-url="'detail?id=' + item.id">
					<view class="order-item__hd flex j-between al-item-center">
						<view class="order-item__name flex al-item-center" v-if="item.bid==0" @tap.stop="goto" :data-url="'/pagesA/shop/index?id=' + item.bid"><text class="iconfont icon-dianpuguanli" :style="'color:' + t('color1')"></text> {{item.binfo.name}}</view>
						<view class="order-item__name flex al-item-center" v-else><image class="order-item__logo" :src="item.binfo.logo"></image> {{item.binfo.name}}</view>
						<text class="order-item__state" :style="'color:' + t('color1')" v-if="item.status == 0">{{item.status == 0 ? '待付款':''}}</text>
						<text class="order-item__state" :style="'color:' + t('color1')" v-if="item.status">{{item.status==2?'待收货':(item.status==3?'已完成':(item.status==4?'已关闭':(item.status==1?(item.freight_type==1?'待提货':'待发货'):'')))}}</text>
					</view>
					<view class="order-item__bd">
						<block v-for="(item2, idx) in item.prolist" :key="idx">
							<view class="product-item flex">
								<view class="product-item__hd" @tap.stop="goto" :data-url="'/pages/shop/product?id=' + item2.proid">
									<image class="product-item__img" :src="item2.pic"></image>
								</view>
								<view class="product-item__bd">
									<view class="product-item__info flex">
										<view class="product-item__name">{{item2.name}}</view>
										<view class="product-item__price">￥{{item2.sell_price}}</view>
									</view>
									<view class="product-item__size">规格：{{item2.ggname}}</view>
									<view class="product-item__num">数量：×{{item2.num}}</view>
								</view>
							</view>
						</block>
					</view>
					<view class="order-item__ft">
						<view class="order-item__ft-refund flex" v-if="item.refund_status">
							<view class="refund-hd">退款</view>
							<text class="refund-bd">{{item.refund_status==1?'退款中'+item.refund_money:(item.refund_status==2?'已退款￥'+item.refund_money:(item.refund_status==3?'退款申请已驳回':''))}}</text>
							<text class="refund-bd" v-if="item.refundCount"> 有退款({{item.refundCount}})</text>
						</view>
						<view class="order-item__ft-top flex al-item-center">
							<view class="order-item__ft-piece">共计{{item.procount}}件商品</view>
							<view class="order-item__ft-total">实付:￥{{item.totalprice}}</view>
							<view class="order-item__ft-total" v-if="item.balance_price > 0 && item.balance_pay_status == 0">尾款：￥{{item.balance_price}}</view>
						</view>
						<view class="order-item__ft-bot flex al-item-center">
							<block v-if="([1,2,3]).includes(item.status) && item.invoice">
								<view class="order-item__ft-btn" @tap.stop="goto" :data-url="'invoice?type=shop&orderid=' + item.id">发票</view>
							</block>
							<view @tap.stop="goto" :data-url="'detail?id=' + item.id" class="order-item__ft-btn">详情</view>
							<block v-if="item.status==0">
								<view class="order-item__ft-btn" @tap.stop="toclose" :data-id="item.id">关闭订单</view>
								<view class="order-item__ft-btn btn-light" v-if="item.paytypeid!=5" :style="'color:'+t('color1')" @tap.stop="goto" :data-url="'/pages/pay/pay?id=' + item.payorderid">去付款</view>
								<view class="order-item__ft-btn btn-light" v-if="item.paytypeid==5" :style="'color:'+t('color1')" @tap.stop="goto" :data-url="'/pages/pay/transfer?id=' + item.payorderid">付款凭证</view>
							</block>
							<block v-if="item.status==1">
								<block v-if="item.paytypeid!='4'">
									<view class="order-item__ft-btn" @tap.stop="goto" :data-url="'refundSelect?orderid=' + item.id + '&price=' + item.totalprice" v-if="canrefund==1">退款</view>
								</block>
								<block v-else>
									<view class="order-item__ft-btn">{{codtxt}}</view>
								</block>
							</block>
							<block v-if="item.status==2">
								<block v-if="item.paytypeid!='4'">
									<view class="order-item__ft-btn" @tap.stop="goto" :data-url="'refundSelect?orderid=' + item.id + '&price=' + item.totalprice" v-if="canrefund==1">退款</view>
								</block>
								<block v-else>
								<view class="order-item__ft-btn">{{codtxt}}</view>
								</block>
								<view class="order-item__ft-btn" @tap.stop="logistics" :data-express_com="item.express_com" :data-express_no="item.express_no" :data-express_content="item.express_content" v-if="item.freight_type!=3 && item.freight_type!=4">查看物流</view>
								<view v-if="item.balance_pay_status == 0 && item.balance_price > 0" class="order-item__ft-btn btn-light" :style="'color:'+t('color1')" @tap.stop="goto" :data-url="'/pages/pay/pay?id=' + item.balance_pay_orderid">支付尾款</view>
								<view v-if="item.paytypeid!='4' && (item.balance_pay_status==1 || item.balance_price==0)" class="order-item__ft-btn btn-light" :style="'color:'+t('color1')" @tap.stop="orderCollect" :data-id="item.id">确认收货</view>
							</block>
							<block v-if="(item.status==1 || item.status==2) && item.freight_type==1">
								<view class="order-item__ft-btn" @tap.stop="showhxqr" :data-hexiao_qr="item.hexiao_qr">核销码</view>
							</block>
							<block v-if="item.status==3 || item.status==4">
								<view class="order-item__ft-btn" @tap.stop="todel" :data-id="item.id">删除订单</view>
							</block>
							<block v-if="item.bid>0 && item.status==3">
								<view v-if="item.iscommentdp==0" class="order-item__ft-btn btn-light" :style="'color:'+t('color1')" @tap.stop="goto" :data-url="'/pages/order/commentdp?orderid=' + item.id">评价店铺</view>
							</block>
						</view>
					</view>
				</view>
			</block>
		</view>
		<nomore v-if="nomore"></nomore>
		<nodata v-if="nodata"></nodata>

		
		<uni-popup id="dialogHxqr" ref="dialogHxqr" type="dialog">
			<view class="hxqrbox">
				<image :src="hexiao_qr" @tap="previewImage" :data-url="hexiao_qr" class="img"/>
				<view class="txt">请出示核销码给核销员进行核销</view>
				<view class="close" @tap="closeHxqr">
					<image :src="event_rul +'/static/img/static/img/close2.png'" style="width:100%;height:100%"/>
				</view>
			</view>
		</uni-popup>

		<uni-popup id="dialogSelectExpress" ref="dialogSelectExpress" type="dialog">
			<view style="background:#fff;padding:20rpx 30rpx;border-radius:10rpx;width:600rpx" v-if="express_content">
				<view class="sendexpress-item" v-for="(item, index) in express_content" :key="index" @tap="goto" :data-url="'/pages/order/logistics?express_com=' + item.express_com + '&express_no=' + item.express_no" style="display: flex;border-bottom: 1px solid #f5f5f5;padding:20rpx 0;">
					<view class="flex1">{{item.express_com}} - {{item.express_no}}</view>
					<image :src="event_rul +'/static/img/static/img/arrowright.png'" style="width:30rpx;height:30rpx"/>
				</view>
			</view>
		</uni-popup>

	</block>
	<loading v-if="loading"></loading>
	<dp-tabbar :opt="opt"></dp-tabbar>
	<popmsg ref="popmsg"></popmsg>
	<!-- 悬浮按钮 -->
	<drag-button></drag-button>
</view>
</template>

<script>
var app = getApp();

export default {
  data() {
    return {
		opt:{},
		loading:false,
		isload: false,
		menuindex:-1,

		st: 'all',
		datalist: [],
		pagenum: 1,
		nomore: false,
		nodata:false,
		codtxt: "",
		canrefund:1,
		express_content:'',
		selectExpressShow:false,
		hexiao_qr:'',

		event_rul: app.globalData.event_url,
    };
  },

  onLoad: function (opt) {
		this.opt = app.getopts(opt);
		if(this.opt && this.opt.st){
			this.st = this.opt.st;
		}
		this.getdata();
  },
	onPullDownRefresh: function () {
		this.getdata();
	},
  onReachBottom: function () {
    if (!this.nodata && !this.nomore) {
      this.pagenum = this.pagenum + 1;
      this.getdata(true);
    }
  },
  methods: {
    getdata: function (loadmore) {
			if(!loadmore){
				this.pagenum = 1;
				this.datalist = [];
			}
      var that = this;
      var pagenum = that.pagenum;
      var st = that.st;
			that.nodata = false;
			that.nomore = false;
			that.loading = true;
      app.post('ApiOrder/orderlist', {st: st,pagenum: pagenum}, function (res) {
				that.loading = false;
        var data = res.datalist;
        if (pagenum == 1) {
					that.codtxt = res.codtxt;
					that.canrefund = res.canrefund;
					that.datalist = data;
          if (data.length == 0) {
            that.nodata = true;
          }
					that.loaded();
        }else{
          if (data.length == 0) {
            that.nomore = true;
          } else {
            var datalist = that.datalist;
            var newdata = datalist.concat(data);
            that.datalist = newdata;
          }
        }
      });
    },
    changetab: function (st) {
      this.st = st;
      uni.pageScrollTo({
        scrollTop: 0,
        duration: 0
      });
      this.getdata();
    },
    toclose: function (e) {
      var that = this;
      var orderid = e.currentTarget.dataset.id;
      app.confirm('确定要关闭该订单吗?', function () {
				app.showLoading('提交中');
        app.post('ApiOrder/closeOrder', {orderid: orderid}, function (data) {
					app.showLoading(false);
          app.success(data.msg);
          setTimeout(function () {
            that.getdata();
          }, 1000);
        });
      });
    },
    todel: function (e) {
      var that = this;
      var orderid = e.currentTarget.dataset.id;
      app.confirm('确定要删除该订单吗?', function () {
				app.showLoading('删除中');
        app.post('ApiOrder/delOrder', {orderid: orderid}, function (data) {
					app.showLoading(false);
          app.success(data.msg);
          setTimeout(function () {
            that.getdata();
          }, 1000);
        });
      });
    },
    orderCollect: function (e) {
      var that = this;
      var orderid = e.currentTarget.dataset.id;
      app.confirm('确定要收货吗?', function () {
				app.showLoading('提交中');
        app.post('ApiOrder/orderCollect', {orderid: orderid}, function (data) {
					app.showLoading(false);
          app.success(data.msg);
          setTimeout(function () {
            that.getdata();
          }, 1000);
        });
      });
    },
		logistics:function(e){
			var express_com = e.currentTarget.dataset.express_com
			var express_no = e.currentTarget.dataset.express_no
			var express_content = e.currentTarget.dataset.express_content
			console.log(express_content)
			if(!express_content){
				app.goto('/pages/order/logistics?express_com=' + express_com + '&express_no=' + express_no);
			}else{
				this.express_content = JSON.parse(express_content);
				console.log(express_content);
				this.$refs.dialogSelectExpress.open();
			}
		},
		hideSelectExpressDialog:function(){
			this.$refs.dialogSelectExpress.close();
		},
		showhxqr:function(e){
			this.hexiao_qr = e.currentTarget.dataset.hexiao_qr
			this.$refs.dialogHxqr.open();
		},
		closeHxqr:function(){
			this.$refs.dialogHxqr.close();
		},
  }
};
</script>
<style lang="scss">
.container{
	min-height: 100vh;
	background: #F5F5F5;
	box-sizing: border-box;
	padding-top: 90rpx;	
	padding-bottom: calc(20rpx + constant(safe-area-inset-bottom));
	padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
}
.pages-con{
	padding: 24rpx;
}
.order-item {
	background: #FFF;
	border-radius:12rpx;
	padding: 28rpx 24rpx;
	&:not(:last-child) {
		margin-bottom: 24rpx;
	}
	&__hd {
		display:flex;
	}
	&__name {
		font-size: 28rpx;
		font-weight: bold;
		color: #333333;
		line-height: 40rpx;
	}
	.icon-dianpuguanli {
		font-size: 36rpx;
		flex-shrink: 0;
		margin-right: 4rpx;
	}
	&__logo {
		width: 36rpx;
		height: 36rpx;
		flex-shrink: 0;
		margin-right: 4rpx;
	}
	&__state {
		flex-shrink: 0;
		margin-left: 20rpx;
		font-size: 28rpx;
		line-height: 40rpx;
	}
	&__bd {
		margin-top: 40rpx;
	}
	.product-item {
		margin-bottom: 40rpx;
		&__hd {
			width: 180rpx;
			height: 180rpx;
			flex-shrink: 0;
			margin-right: 24rpx;
			border-radius: 8rpx;
			overflow: hidden;
		}
		&__img {
			width: 100%;
			height: 100%;
		}
		&__bd {
			flex: 1;
		}
		&__info {
			font-size: 28rpx;
			font-weight: bold;
			color: #333333;
			line-height: 36rpx;
		}
		&__name {
			flex: 1;
		}
		&__price {
			flex-shrink: 0;
			margin-left: 32rpx;
			font-size: 28rpx;
			line-height: 40rpx;
		}
		&__size,&__num {
			margin-top: 10rpx;
			font-size: 24rpx;
			font-weight: bold;
			color: #999999;
			line-height: 34rpx;
		}
	}
	&__ft {
		padding-top: 10rpx;
		&-top {
			justify-content: flex-end;
			font-size: 28rpx;
			color: #999999;
			line-height: 30rpx;
		}
		&-total {
			color: #333333;
			font-weight: bold;
			margin-left: 14rpx;
		}
		&-refund {
			padding: 30rpx 24rpx;
			background: #FAFAFA;
			border-radius: 8rpx;
			margin-top: -8rpx;
			font-size: 28rpx;
			line-height: 30rpx;
			.refund-hd {
				font-weight: bold;
				flex-shrink: 0;
				margin-right: 28rpx;
			}
		}
		&-bot {
			margin-top: 30rpx;
			justify-content: flex-end;
		}
		&-btn {
			position: relative;
			min-width: 156rpx;
			height: 60rpx;
			padding: 0 20rpx;
			font-size: 24rpx;
			font-weight: bold;
			color: #666666;
			line-height: 60rpx;
			box-sizing: border-box;
			margin-left: 12rpx;
			text-align: center;
			&::before {
				content: "";
				position: absolute;
				left: 0;
				right: 0;
				top: 0;
				bottom: 0;
				border-radius: 30rpx;
				border: 1rpx solid #CECECE;
			}
			&.btn-light {
				&::before {
					border-color: #0A5D3B;
				}
			}
		}
	}
}
.order-item .order-item__hd .f1{display:flex;align-items:center;color:#333;font-size: 28rpx;line-height: 40rpx;}
.order-item .order-item__hd image{width: 36rpx;height: 36rpx;margin-right: 4rpx;}
.order-item .order-item__hd .order-state {flex-shrink: 0;margin-left: 20rpx;}

.order-item .content{display:flex;width: 100%; padding:16rpx 0px;border-bottom: 1px #f4f4f4 dashed;position:relative}
.order-item .content:last-child{ border-bottom: 0; }
.order-item .content image{ width: 140rpx; height: 140rpx;}
.order-item .content .detail{display:flex;flex-direction:column;margin-left:14rpx;flex:1}
.order-item .content .detail .t1{font-size:26rpx;line-height:36rpx;margin-bottom:10rpx;display:-webkit-box;-webkit-box-orient:vertical;-webkit-line-clamp:2;overflow:hidden;}
.order-item .content .detail .t2{height: 46rpx;line-height: 46rpx;color: #999;overflow: hidden;font-size: 26rpx;}
.order-item .content .detail .t3{display:flex;height:40rpx;line-height:40rpx;color: #ff4246;}
.order-item .content .detail .x1{ flex:1}
.order-item .content .detail .x2{ width:100rpx;font-size:32rpx;text-align:right;margin-right:8rpx}

.order-item .bottom{ width:100%; padding: 10rpx 0px; border-top: 1px #f4f4f4 solid; color: #555;}
.order-item .op{ display:flex;flex-wrap: wrap;justify-content:flex-end;align-items:center;width:100%; padding: 10rpx 0px; border-top: 1px #f4f4f4 solid; color: #555;}

.btn1{margin-left:20rpx; margin-top: 10rpx;width:160rpx;height:60rpx;line-height:60rpx;color:#fff;border-radius:3px;text-align:center;}
.btn2{margin-left:20rpx; margin-top: 10rpx;width:160rpx;height:60rpx;line-height:60rpx;color:#333;background:#fff;border:1px solid #cdcdcd;border-radius:3px;text-align:center;}

.hxqrbox{background:#fff;padding:50rpx;position:relative;border-radius:20rpx}
.hxqrbox .img{width:400rpx;height:400rpx}
.hxqrbox .txt{color:#666;margin-top:20rpx;font-size:26rpx;text-align:center}
.hxqrbox .close{width:50rpx;height:50rpx;position:absolute;bottom:-100rpx;left:50%;margin-left:-25rpx;border:1px solid rgba(255,255,255,0.5);border-radius:50%;padding:8rpx}
</style>