<template>
	<view class="container record clearfix" :style="{ paddingTop: CustomBar + 'px' }">
		<cd-custom
			:systeminfo="systeminfo"
			:navbar-data="nvabarData"
			:headerBtnPosi="headerBtnPosi"
			:page="1"
			@pageDetails="hanldPageDetails"
		></cd-custom>
		<view
			class="record-content-empty"
			v-if="isEmpty"
			:style="{ height: windowHeight ? windowHeight - CustomBar + 'px' : 'auto' }"
		>
			<view class="record-container clearfix">
				<image
					class="club-empty-image"
					src="https://img.caimei365.com/group1/M00/03/B7/Cmis2178OfaAEjhLAABqsz9OXM0847.png"
					mode="aspectFit"
				></image>
				<view class="txt">此订单暂无发货记录~</view> <view class="btn" @click="pageGoLink">去发货</view>
			</view>
		</view>
		<view class="record-content" v-else>
			<scroll-view scroll-y="true" class="scroll-view">
				<view class="record-list clearfix" v-for="(item, index) in RecordList" :key="index">
					<view class="record-top clearfix">
						<view class="top-view">
							<view class="top-viewlabel">发货时间:</view>
							<view class="top-viewtext">{{ item.deliveryTime }}</view>
						</view>
						<view class="top-view" v-for="(logist, idx) in item.logisticsInformationList" :key="logist.id">
							<view class="top-viewlabel">{{ logist.logisticsCompanyName }}:</view>
							<view class="top-viewtext">
								<text>{{ logist.nu }}</text>
							</view>
						</view>
					</view>
					<view class="goods-pros">
						<view class="goods-pros-t" v-for="(pros, idx) in item.logisticsRecordList" :key="pros.id">
							<view class="pros-img"
								><image :src="pros.image ? pros.image : ''" alt="" mode="aspectFill"
							/></view>
							<view class="pros-product">
								<view class="producttitle">{{ pros.productName }}</view>
								<view class="productspec">规格:{{ pros.unit ? pros.unit : '' }}</view>
								<view class="productspec">商品编码:{{ pros.productCode ? pros.productCode : '' }}</view>
								<view class="product-view">
									<view class="view-num">数量：{{ pros.buyNum }}</view>
								</view>
								<view class="product-view">
									<view class="view-num">本次发货：{{ pros.num }}</view>
									<view class="view-num">已发货：{{ pros.shipmentsNum }}</view>
								</view>
								<view class="product-view">
									<view class="view-num">未发货：{{ pros.notShippedNum }}</view>
									<view class="view-num">已退货：{{ pros.returnedNum }}</view>
								</view>
							</view>
						</view>
						<view class="goods-pros-b clearfix" v-show="item.remarkImages.length > 0">
							<view class="b-label">拍照备注</view>
							<view class="b-photo">
								<view class="b-imageView" v-for="(imgItem, imgIdx) in item.remarkImages" :key="imgIdx">
									<image
										:src="imgItem"
										mode="aspectFill"
										@click="previewImg(imgIdx, item.remarkImages)"
									></image>
								</view>
							</view>
						</view>
						<view class="goods-pros-b clearfix" v-if="item.remark != ''">
							<view class="b-label">文字备注</view>
							<view class="b-photo">
								<view class="text">{{ item.remark ? item.remark : '' }}</view>
							</view>
						</view>
					</view>
					<!-- 底部button -->
					<order-button ref="orderButton" :hanldData="item" @buttonConfirm="handButtonConfirm">
					</order-button>
				</view>
				<!--加载loadding-->
				<tui-loadmore :visible="loadding" :index="3" type="black"></tui-loadmore>
				<tui-nomore :visible="!pullUpOn" :backgroundColor="'#ffffff'" :text="nomoreText"></tui-nomore>
				<!--加载loadding-->
			</scroll-view>
		</view>
	</view>
</template>
<script>
import { mapState, mapMutations } from 'vuex'
import orderButton from '@/components/cm-module/orderDetails/supplierRecorgButton.vue' //底部按钮
import tuiLoadmore from '@/components/tui-components/loadmore/loadmore'
import tuiNomore from '@/components/tui-components/nomore/nomore'
import cdCustom from '@/components/cm-custom/custom-d'

var isPreviewImg
export default {
	components: {
		orderButton,
		tuiLoadmore,
		tuiNomore,
		cdCustom
	},
	data() {
		return {
			nvabarData: {
				//顶部自定义导航
				showCapsule: 1, // 是否显示左上角图标   1表示显示    0表示不显示
				title: '发货记录' // 导航栏 中间的标题
			},
			headerBtnPosi: this.setHeaderBtnPosi(), //获取设备顶部胶囊高度
			systeminfo: this.setSysteminfo(), //获取设备信息
			CustomBar: this.CustomBar, // 顶部导航栏高度
			isIphoneX: this.$store.state.isIphoneX,
			listQuery: {
				shopOrderId: 0,
				pageNum: 1,
				pageSize: 10
			},
			lgisList: [],
			ramkesList: [],
			RecordList: [], //发货记录
			hasNextPage: false,
			loadding: false,
			pullUpOn: true,
			pullFlag: true,
			isEmpty: false,
			nomoreText: '上拉显示更多',
			scrollHeight: '',
			windowHeight: '',
			mobile: 0
		}
	},
	onLoad(option) {
		this.listQuery.shopOrderId = option.shopOrderId
		this.setScrollHeight()
		this.initShopShipmentsRecord()
	},
	computed: {
		...mapState(['hasLogin', 'userInfo'])
	},
	methods: {
		initShopShipmentsRecord() {
			//初始化发货记录 index:1
			this.ShopService.ShopShipmentsRecord(this.listQuery)
				.then(response => {
					console.log(response)
					let data = response.data
					let logisticsBatchPage = data.logisticsBatchPage
					this.mobile = data.userInfo.mobile
					if (logisticsBatchPage.list && logisticsBatchPage.list.length > 0) {
						this.isEmpty = false
						this.hasNextPage = logisticsBatchPage.hasNextPage
						this.RecordList = logisticsBatchPage.list
						this.pullFlag = false
						setTimeout(() => {
							this.pullFlag = true
						}, 500)
						if (this.hasNextPage) {
							this.pullUpOn = false
							this.nomoreText = '上拉显示更多'
						} else {
							if (this.RecordList.length < 2) {
								this.pullUpOn = true
								this.loadding = false
							} else {
								this.pullUpOn = false
								this.loadding = false
								this.nomoreText = '已至底部'
							}
						}
					} else {
						this.isEmpty = true
					}
				})
				.catch(error => {
					this.$util.msg(error.msg, 2000)
				})
		},
		getOnReachBottomData() {
			this.listQuery.pageNum += 1
			this.ShopService.ShopShipmentsRecord(this.listQuery)
				.then(response => {
					let data = response.data.logisticsBatchPage
					if (data.list && data.list.length > 0) {
						this.hasNextPage = data.hasNextPage
						this.RecordList = this.RecordList.concat(data.list)
						this.pullFlag = false // 防上拉暴滑
						setTimeout(() => {
							this.pullFlag = true
						}, 500)
						if (this.hasNextPage) {
							this.pullUpOn = false
							this.nomoreText = '上拉显示更多'
						} else {
							this.pullUpOn = false
							this.loadding = false
							this.nomoreText = '已至底部'
						}
					}
				})
				.catch(error => {
					this.$util.msg(error.msg, 2000)
				})
		},
		setHeaderBtnPosi() {
			// 获得胶囊按钮位置信息
			let headerBtnPosi = uni.getMenuButtonBoundingClientRect()
			return headerBtnPosi
		},
		setSysteminfo() {
			// 获取设备信息
			let systeminfo
			uni.getSystemInfo({
				success: res => {
					systeminfo = res
				}
			})
			return systeminfo
		},
		setScrollHeight() {
			const { windowHeight, pixelRatio } = wx.getSystemInfoSync()
			this.windowHeight = windowHeight - 1
			this.scrollHeight = windowHeight - 1
		},
		previewImg(index, previewUrls) {
			//顶部商品图片预览
			isPreviewImg = true
			uni.previewImage({
				current: index, //图片索引
				urls: previewUrls, //必须是http图片，本地图片无效
				longPressActions: ''
			})
		},
		handButtonConfirm(data) {
			//监听点击时间的按钮类型并执行...
			// console.log(data)
			this.handShowAlert(data)
		},
		handShowAlert(data) {
			//判断点击的按钮类型并执行...
			let hanldType = data.hanldType
			let hanldData = data.hanldData
			console.log(hanldData)
			switch (hanldType) {
				case 'cancel':
					this.handCenceConfirm(hanldData)
					break
				case 'add':
					let setProductList = []
					let repleArray = hanldData.logisticsRecordList
					repleArray.forEach(el => {
						let elObject = {
							orderProductId: el.orderProductId,
							num: el.num
						}
						setProductList.push(elObject)
					})
					let data = {
						logisticsBatchId: hanldData.id,
						logisticsBatchData: setProductList
					}
					this.$api.navigateTo(
						`/pages/supplier/deliver/add-logistics?type=reple&mobile=${this.mobile}&shopOrderId=${
							this.listQuery.shopOrderId
						}&data=${JSON.stringify(data)}`
					)
					break
				case 'qualifications':
					this.$api.navigateTo(
						`/pages/supplier/deliver/qualifications-details?logisticsBatchId=${hanldData.id}&shopOrderId=${
							this.listQuery.shopOrderId
						}`
					)
					break
			}
		},
		handCenceConfirm(data) {
			//取消订单
			this.$util.modal('提示', '确定要撤销本次发货吗？撤销后需重新发货', '确定', '取消', true, () => {
				this.ShopService.ShopCancelShipment({ logisticsBatchId: data.id })
					.then(response => {
						this.$util.msg('撤销成功', 2000, true, 'success')
						setTimeout(() => {
							this.initShopShipmentsRecord()
						}, 2000)
					})
					.catch(error => {
						this.$util.msg(error.msg, 2000)
					})
			})
		},
		pageGoLink() {
			this.$api.navigateTo('/pages/supplier/deliver/deliver-goods?shopOrderId=' + this.listQuery.shopOrderId)
		},
		hanldPageDetails() {
			this.$api.navigateTo('/pages/supplier/order/order-details?shopOrderId=' + this.listQuery.shopOrderId)
		}
	},
	onPullDownRefresh() {
		//下拉刷新
		this.initShopShipmentsRecord()
		uni.stopPullDownRefresh()
	},
	onReachBottom() {
		if (this.hasNextPage) {
			this.loadding = true
			this.pullUpOn = true
			this.getOnReachBottomData()
		}
	},
	onShow() {}
}
</script>

<style lang="scss">
page {
	background: #f7f7f7;
	height: auto;
}
.record-content {
	position: relative;
	border-top: 1px solid #ebebeb;
	.scroll-view {
		height: 100%;
	}
}
.record-content-empty {
	width: 100%;
	height: 100%;
	display: flex;
	align-items: center;
	justify-content: center;
	.record-container {
		width: 400rpx;
		height: auto;
		image {
			width: 400rpx;
			height: 400rpx;
			display: block;
		}
		.txt {
			font-size: $font-size-28;
			text-align: center;
			color: #333;
			line-height: 30rpx;
			margin-bottom: 20rpx;
		}
		.btn {
			width: 400rpx;
			height: 80rpx;
			background: $btn-confirm;
			border-radius: 40rpx;
			line-height: 80rpx;
			font-size: $font-size-28;
			text-align: center;
			color: #ffffff;
		}
	}
}
.record-list {
	width: 100%;
	height: auto;
	background-color: #f7f7f7;
	margin-bottom: 24rpx;
	.record-top {
		width: 702rpx;
		padding: 20rpx 24rpx;
		height: auto;
		background-color: #fff;
		margin-bottom: 20rpx;
		.top-view {
			width: 100%;
			height: auto;
			float: left;
			line-height: 50rpx;
			font-size: $font-size-28;
			color: #333333;
			display: flex;
			.top-viewlabel {
				width: 130rpx;
				margin-right: 10rpx;
				color: #999999;
			}
			.top-viewtext {
				width: 562rpx;
				height: auto;
				text {
					display: inline-block;
					margin-right: 30rpx;
				}
			}
		}
	}
	.goods-pros {
		width: 702rpx;
		padding: 0 24rpx;
		background: #ffffff;
	}
	.goods-pros-t {
		display: flex;
		width: 100%;
		height: auto;
		padding: 20rpx 0;
		.pros-img {
			width: 210rpx;
			height: 210rpx;
			border-radius: 10rpx;
			margin: 0 20rpx;
			border: 1px solid #f3f3f3;
			margin-left: 0;
			image {
				width: 100%;
				height: 100%;
				border-radius: 10rpx;
			}
		}
		.pros-product {
			width: 492rpx;
			height: 100%;
			line-height: 36rpx;
			font-size: $font-size-28;
			position: relative;
			.producttitle {
				width: 100%;
				display: inline-block;
				height: auto;
				text-overflow: ellipsis;
				display: -webkit-box;
				word-break: break-all;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 2;
				overflow: hidden;
				margin-bottom: 8rpx;
			}
			.productspec {
				height: 44rpx;
				color: #999999;
				line-height: 44rpx;
				font-size: $font-size-26;
			}
			.product-view {
				width: 100%;
				height: auto;
				display: flex;
				.view-num {
					flex: 1;
					text-align: left;
					font-size: $font-size-26;
					color: #666666;
					line-height: 44rpx;
				}
			}
		}
	}
	.goods-pros-b {
		width: 100%;
		height: auto;
		padding: 10rpx 0;
		border-top: 1px solid #f7f7f7;
		border-bottom: 1px solid #f7f7f7;
		position: relative;
		.b-label {
			width: 100%;
			line-height: 70rpx;
			font-size: $font-size-28;
			color: $text-color;
		}
		.b-photo {
			width: 100%;
			height: auto;
			padding: 10rpx 0;
			.b-imageView {
				width: 112rpx;
				height: 112rpx;
				margin: 0 20rpx 20rpx 0;
				float: left;
				image {
					width: 112rpx;
					height: 112rpx;
					display: block;
				}
			}
			.text {
				line-height: 44rpx;
				font-size: $font-size-28;
				color: $text-color;
			}
		}
		.goods-pros-m {
			width: 100%;
			height: auto;
			line-height: 76rpx;
			font-size: $font-size-26;
			color: $text-color;
			float: left;
			padding: 10rpx 0;
			border-top: 1px solid #f7f7f7;
			border-bottom: 1px solid #f7f7f7;
			.m-text {
				width: 62rpx;
				float: left;
				padding-right: 20rpx;
				font-weight: bold;
			}
			.m-input {
				display: -webkit-box;
				display: -webkit-flex;
				display: flex;
				-webkit-box-align: center;
				-webkit-align-items: center;
				align-items: center;
				position: relative;
				width: 620rpx;
				height: auto;
				padding: 20rpx 0 10rpx 0;
				background: #ffffff;
				.text {
					width: 100%;
					height: 100%;
					font-size: $font-size-26;
					line-height: 36rpx;
					color: #333333;
				}
			}
		}
	}
}
</style>
