<template>
	<div>
		<view class="view">
			<view class="header">
				<view class="tabs">
					<view v-for="(item, index) in tabList" :key="index" class="uni-tab-item" :id="item.id"
						:data-current="index" @click="handleChangeTab(item.id)">
						<text class="uni-tab-item-title" :class="
								item.id === curTab
									? 'uni-tab-item-title-active'
									: ''
							">
							{{ item.name }}
						</text>
					</view>
				</view>
				<view class="history">
						<view class="topBtn" @click="toHistory">
							开票历史
						</view>

					<!-- <view class="topBtn" @click="invoiceShow()">
						申请开票
					</view> -->
				</view>
			</view>

			<mescroll-uni ref="mescroll" @getData="getList" top="100rpx">
				<block slot="list">
					<view class="order-list" v-if="dataList.length > 0">
						<view class="order-item" :class="orderItem.changer?'order-item-active':''"
							v-for="(orderItem, orderIndex) in dataList" :key="orderIndex"
							v-if="orderItem.order_status != -1" @click="goInvoiceDetail(orderItem)">
							<view class="order-header">

								<view class="font-size-base">订单号：{{ orderItem.order_no }}</view>
								<view class="" v-if="orderItem.invoice_status==0&&orderItem.is_invoice==1">
									待审核
								</view>
								<view class="apply-ticket" v-if="orderItem.is_invoice === 0" @click="handleApplyTicket(orderItem)">
									申请开票
								</view>
								<view class="" v-else-if="orderItem.invoice_status==1&&orderItem.is_invoice==1">
									已开票
								</view>
								<!-- $$${{orderItem.is_invoice}} -->
							</view>
							<view class="order-body" @click="orderCheck(orderItem.order_id,orderIndex)">
								<view class="goods-wrap" v-for="(goodsItem, goodsIndex) in orderItem.order_goods"
									:key="goodsIndex">
									<view class="goods-img">
										<image :src="$util.img(goodsItem.sku_image, { size: 'mid' })"
											@error="imageError(orderIndex, goodsIndex)" mode="aspectFill"
											:lazy-load="true">
										</image>
									</view>
									<view class="goods-info">
										<view class="pro-info">
											<view class="goods-name" v-if="goodsItem.goods_class == 2">
												{{ goodsItem.goods_name }}
											</view>
											<view class="goods-name" v-else>
												{{ goodsItem.sku_name }}
											</view>

										</view>
										<view class="goods-sub-section">
											<text class="goods-price">
												<text class="unit price-style small">
													{{ $lang('common.currencySymbol') }}
												</text>
												<text class="price-style large">
													{{
														parseFloat(goodsItem.price)
															.toFixed(2)
															.split('.')[0]
													}}
												</text>
												<text class="unit price-style small">
													.{{
														parseFloat(goodsItem.price)
															.toFixed(2)
															.split('.')[1]
													}}
												</text>
											</text>
											<text class="goods-num">
												<text class="iconfont icon-close"></text>
												{{ goodsItem.num }}
											</text>
										</view>
										<view class="goods-action">
											<!-- <view class="action-btn">加购物车</view> -->
										</view>
									</view>
								</view>
							</view>
						</view>
					</view>
					<view v-else>
						<ns-empty :isIndex="false" text="暂无相关订单"></ns-empty>
					</view>
				</block>
			</mescroll-uni>
			<loading-cover ref="loadingCover"></loading-cover>
		</view>
		<u-modal :show="show" :showCancelButton="true" cancelText="取消" confirmText="确认" @cancel="show=false"
			@confirm="confirmDoInvoice()">
			<view class="content">是否申请开票</view>
		</u-modal>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				selectCurrentPage: false,
				selectAll: false,
				arrmn: false,
				dataList: [],
				doInvoice: [],
				show: false,
				tabList: [{
						id: 1,
						name: '已开票',
					},
					{
						id: 0,
						name: '待开票',
					}
				],
				curTab: 1,
				applyTicketOrderId: 0 // 当前要开票的订单id
			}
		},
		onShow() {
			if (this.$refs.mescroll) this.$refs.mescroll.refresh();
		},
		methods: {
			handleApplyTicket(item) {
				this.applyTicketOrderId = item.order_id;
				this.show = true;
			},
			handleChangeTab(id) {
				// 点击当前tab不生效
				if(this.curTab === id){
					return;
				}
				this.curTab = id;
				this.$refs.mescroll.refresh()
			},
			goInvoiceDetail(item) {
				if(this.curTab === 0) return;
				uni.navigateTo({
					url: '/pages_tool/invoice/detail?order_id=' + item.order_id
				})
			},
			getList(mescroll) {
				this.$api.sendRequest({
					url: "/api/order/lists",
					data: {
						page: mescroll.num,
						page_size: mescroll.size,
						order_status: "all",
						invoice_status: this.curTab,
						// invoice: 1
						invoice: 3  // 逻辑改为已申请开票（审核中）与 未申请的都放在同一列表
					},
					success: res => {
						let newArr = [];
						let msg = res.message;
						if (res.code == 0 && res.data) {
							res.data.list.forEach(item => {
								item.changer = false;
							})
							newArr = res.data.list;
						} else {
							this.$util.showToast({
								title: msg
							});
						}
						mescroll.endSuccess(newArr.length);
						console.log(newArr);
						//设置列表数据
						if (mescroll.num == 1) {
							this.dataList = []; //如果是第一页需手动制空列表
							this.related_id = 0;
						}
						this.dataList = this.dataList.concat(newArr); //追加新数据
						if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
						// console.log('dataList', this.dataList);
					}
				})
			},
			orderCheck(order_id, index) {
				this.dataList.forEach(item => {
					item.changer = false
				})
				this.dataList[index].changer = !this.dataList[index].changer;
			},
			confirmDoInvoice() {
				// let arr = []
				// this.dataList.forEach(item => {
				// 	if (item.changer) {
				// 		arr.push(item.order_id)
				// 	}
				// })
				this.$api.sendRequest({
					url: "/api/order/doInvoice",
					data: {
						// order_id: arr[0]
						order_id: this.applyTicketOrderId
					},
					success: res => {
						this.$util.showToast({
							title: res.message
						});
						this.show = false;
						this.$refs.mescroll.refresh();
					}
				})
			},
			currentPageAll() {
				this.data.forEach(arr => {
					if (this.selectCurrentPage) {
						arr.check = false
					} else {
						arr.check = true
					}
				})
				this.selectCurrentPage = !this.selectCurrentPage
			},
			invoiceShow(orderItem) {
				let arr = []
				this.dataList.forEach(item => {
					if (item.changer) {
						arr.push(item.order_id)
					}
				})
				console.log(arr);
				if (arr.length) {
					this.show = true
				} else {
					this.$util.showToast({
						title: "请选择需要开票订单"
					});
				}
			},
			all() {
				this.selectAll = !this.selectAll
			},
			toIssue(index) {
				uni.navigateTo({
					url: '/pages_tool/invoice/issue'
				})
			},

			toHistory() {
				uni.navigateTo({
					url: '/pages_tool/invoice/history'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.view {
		padding: 32rpx 24rpx 0rpx 24rpx;
		height: calc(100vh - 340rpx) !important;

		.header {
			// display: flex;
			// justify-content: space-between;
			position: relative;
			transform: translate(0, -30%);
			// margin-bottom: 20rpx;
			.tabs {
				width: 50%;
				display: flex;
				margin-bottom: 10rpx;

				.uni-tab-item {
					width: 120rpx;
					text-align: center;

					.uni-tab-item-title {
						font-size: $font-size-base;
						display: inline-block;
						height: 86rpx;
						line-height: 90rpx;
						// border-bottom: 1px solid #fff;
						flex-wrap: nowrap;
						/* #ifndef APP-PLUS */
						white-space: nowrap;
						/* #endif */
						text-align: center;
						font-size: 30rpx;
					}

					.uni-tab-item-title-active {
						height: 86rpx;
						border-bottom: 2px solid #53c21d;
						color: #53c21d;
					}
				}
			}
		}

		.history {
			// margin-top: 18rpx;
			// margin-bottom: 24rpx;
			font-size: 28rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #222222;
			display: flex;
			flex-direction: row-reverse;
			text-align: right;
			position: absolute;
			right: 0;
			top: 50%;
			transform: translate(0, -50%);
			view {
				margin: 0 20rpx;
			}
		}

		.card {
			min-height: 240rpx;
			box-sizing: border-box;
			background: #FFFFFF;
			border-radius: 24rpx;
			padding: 32rpx 24rpx;
			margin-bottom: 24rpx;
			display: flex;

			.left {
				display: flex;
				justify-self: center;
				align-items: center;

				.check {
					width: 32rpx;
					height: 32rpx;

				}

				.uncheck {
					width: 32rpx;
					height: 32rpx;
					background: #FFFFFF;
					border: 2rpx solid #BFBFBF;
					border-radius: 50%;
				}
			}

			.center {
				image {
					width: 176rpx;
					height: 176rpx;
					margin: 0rpx 24rpx;
					border-radius: 8rpx;
				}
			}

			.right {
				display: flex;
				justify-content: space-between;
				flex-direction: column;

				.top {
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
					word-break: break-all;

					width: 390rpx;
					font-size: 28rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #333333;
				}

				.middle {
					margin-bottom: 32rpx;
					height: 34rpx;
					font-size: 24rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #999999;
					line-height: 34rpx;
					display: flex;
					justify-content: space-between;

				}

				.bottom {
					display: flex;
					justify-content: space-between;

					.status {
						font-size: 28rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #585858;
					}

					.price {
						.count {
							font-size: 28rpx;
							font-family: PingFangSC-Regular, PingFang SC;
							font-weight: 400;
							color: #333333;
						}

						.number {
							font-size: 38rpx;
							font-family: DINAlternate-Bold, DINAlternate;
							font-weight: 600;
							color: #333333;
							line-height: 1;
						}
					}
				}
			}
		}
	}

	.bottom_bar {

		background-color: #fff;
		height: 220rpx;
		width: 100%;
		padding: 20rpx 48rpx 88rpx 48rpx;
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
		justify-content: space-between;

		.first_row {

			.op_text {
				font-size: 24rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #999999;
			}

			.rmb {
				font-size: 24rpx;
				font-family: DINAlternate-Bold, DINAlternate;
				font-weight: bold;
				color: #EF6154;
			}

			.price {
				font-size: 38rpx;
				font-family: DINAlternate-Bold, DINAlternate;
				font-weight: 600;
				color: #EF6154;
			}

		}

		.second_row {
			margin-top: 16rpx;
			display: flex;

			.check_icon {
				line-height: 1;
				display: flex;

				.check {
					width: 32rpx;
					height: 32rpx;

				}

				.uncheck {
					width: 32rpx;
					height: 32rpx;
					background: #FFFFFF;
					border: 2rpx solid #BFBFBF;
					border-radius: 50%;
				}

				.text {
					margin-left: 16rpx;
				}
			}
		}

		.btnBox {
			position: absolute;
			bottom: 78rpx;
			right: 24rpx;
			width: 240rpx;
			height: 72rpx;
			background: #21BBF3;
			border-radius: 40rpx;

			font-size: 28rpx;
			font-family: PingFangSC-Medium, PingFang SC;
			font-weight: 600;
			color: #FFFFFF;

			display: flex;
			justify-content: center;
			align-items: center;
		}
	}

	/deep/ .uni-scroll-view,
	.uni-scroll-view-content {
		width: 100%;
		height: 100%;

		.order-list {
			margin-top: 104rpx;
		}
	}

	.order-item-active {
		border: 1rpx solid #21BBF3;
	}

	.order-item {
		margin: $margin-updown $margin-both;
		border-radius: $border-radius;
		background: #fff;
		position: relative;
		
		.order-header {
			display: flex;
			justify-content: space-between;
			align-items: center;
			position: relative;
			padding: $padding 30rpx 26rpx 30rpx;

			&.waitpay {
				padding-left: 70rpx;

				.icon-yuan_checked,
				.icon-yuan_checkbox {
					font-size: $font-size-toolbar;
					position: absolute;
					top: 48%;
					left: 20rpx;
					transform: translateY(-50%);
				}

				.icon-yuan_checkbox {
					color: $color-tip;
				}
			}

			.icon-dianpu {
				display: inline-block;
				line-height: 1;
				margin-right: 12rpx;
				font-size: $font-size-base;
			}

			.status-name {
				flex: 1;
				text-align: right;
				font-size: $font-size-base;
				font-weight: 600;
			}
		}

		.order-body {
			.goods-wrap {
				display: flex;
				position: relative;
				padding: 0 30rpx 30rpx 30rpx;

				&:last-of-type {
					margin-bottom: 0;
				}

				.goods-img {
					width: 160rpx;
					height: 160rpx;
					margin-right: 20rpx;

					image {
						width: 100%;
						height: 100%;
						border-radius: $border-radius;
					}
				}

				.goods-info {
					flex: 1;
					position: relative;
					max-width: calc(100% - 180rpx);
					display: flex;
					flex-direction: column;

					.pro-info {
						flex: 1;
					}

					.goods-name {
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;
						overflow: hidden;
						line-height: 1.5;
						font-size: $font-size-base;
						color: $color-title;
					}

					.goods-sub-section {
						width: 100%;
						line-height: 1.3;
						display: flex;
						margin-top: 14rpx;

						.goods-price {
							font-size: $font-size-tag;
							color: var(--price-color);
							flex: 1;
							font-weight: bold;
						}

						.goods-num {
							font-size: $font-size-tag;
							color: $color-tip;
							flex: 1;
							text-align: right;
							line-height: 1;

							.iconfont {
								font-size: $font-size-tag;
							}
						}

						.goods-type {
							font-size: $font-size-tag;
						}

						.unit {
							font-size: $font-size-tag;
							margin-right: 2rpx;
						}

						view {
							flex: 1;
							line-height: 1.3;
							display: flex;
							flex-direction: column;

							&:last-of-type {
								text-align: right;

								.iconfont {
									line-height: 1;
									font-size: $font-size-tag;
								}
							}
						}
					}

					.goods-action {
						text-align: right;

						.action-btn {
							line-height: 1;
							padding: 14rpx 20rpx;
							color: $color-title;
							display: inline-block;
							border-radius: $border-radius;
							background: #fff;
							border: 2rpx solid #999;
							font-size: $font-size-tag;
							margin-left: 10rpx;
						}
					}
				}
			}
		}

		.order-footer {
			.order-base-info {
				.total {
					padding: $padding;
					font-size: $font-size-tag;
					background: rgba(248, 248, 248, 0.5);
					display: flex;

					&>text {
						flex: 1;
						line-height: 1;
						margin-left: 10rpx;
					}
				}

				.order-type {
					padding-top: 20rpx;
					flex: 0.5;

					&>text {
						line-height: 1;
					}
				}
			}

			.order-action {
				text-align: right;
				padding: 30rpx;
				// display: flex;
				position: relative;

				.order-time {
					position: absolute;
					top: 35rpx;
					left: 30rpx;
					display: flex;
					align-items: center;
					font-size: 10px;
					color: #b5b6b9;

					image {
						width: 26rpx;
						height: 26rpx;
						margin-right: 6rpx;
					}
				}

				.action-btn {
					line-height: 1;
					padding: 20rpx 26rpx;
					color: #333;
					display: inline-block;
					border-radius: $border-radius;
					background: #fff;
					border: 2rpx solid #999;
					font-size: $font-size-tag;
					margin-left: 10rpx;
				}
			}
		}
	}

	.topBtn,
	.apply-ticket{
		background-color: #21BBF3;
		color: #fff;
		padding: 5rpx 20rpx;
		border-radius: 50rpx;
	}
</style>