<template>
	<view>
		<view class='search'>
			<u-search v-model='searchKeyWord' placeholder='可按货源查找' search-icon-color='#AFB3BF' bg-color='#F5F6F9'
				:shape='"round"' :clearabled="true" :show-action='false'></u-search>
		</view>

		<view style='justify-content: space-between;background:#fff;padding:20rpx;' class='flex'>
			<view @click='show=true'>{{startDate}}~{{endDate}}></view>
			<view @click='show1=true'>{{warehouse?warehouse:'全部仓库'}}></view>
		</view>
		<u-picker mode="selector" @confirm='confirm' v-model="show1" range-key='warehouseName' :default-selector="[0]"
			:range="warehouseList"></u-picker>
		<u-calendar @change='calendarchange' v-model="show" :mode="mode"></u-calendar>
		<view>
			<view class='contractwrap' v-for='(item,index) in warehouseList'>
				<view class='flex contractwrap-title'>
					<view class='contractNo'>{{item.warehouseName}}</view>
					<view class='contractNo'>{{item.issuingTime}}</view>
				</view>
				<view v-for='(item1,index1) in item.goodsNameList ' style='background:#EFFAF4;border-radius:10rpx;'>
					<view
						style='color:#22C572;padding:20rpx;border-bottom:1rpx solid #D8E8E0;justify-content: space-between;'
						class='flex'>
						<view style='width:33.3333%;text-align:left;'>{{item1.goodsName}}</view>
						<view style='width:33.3333%;'>均水{{item1.weightedMoisture}}</view>
						<view style='width:33.3333%;text-align:right;'>均价{{item1.avgCost}}</view>
					</view>
					<view v-for='(item2,index2) in item1.list' class='flex contractwrap-item'>
						<view class="title">{{item2.goodsSource}}</view>
						<view>{{item2.inWarehouseWeight}}吨</view>
					</view>
				</view>

			</view>
		</view>
		<view v-if='show2' style='text-align:center;background:#F2F6FA;margin-top:20rpx;'>暂无更多数据</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				startDate: '',
				endDate: '',
				mode: 'range',
				show: false,
				show1: false,
				show2: false,
				currentPage: 1,
				pageSize: 10,
				baseId: '',
				warehouse: '',
				searchKeyWord: '',
				warehouseList: [],
				isLoadMore: false
			}
		},
		onLoad() {
			this.$api.doRequest('get', '/warehouseBaseInfo/selectWarehouseSelfApp', {
				compId: uni.getStorageSync('pcUserInfo').compId,
			}).then(res => {
				if (res.data.code == 200) {
					// uni.hideLoading()
					this.warehouseList = res.data.data
				} else {
					// uni.hideLoading()
					uni.showToast({
						title: "系统异常，请联系管理员",
						icon: 'none',
						duration: 2000
					})
				}
			})
		},
		onShow() {
			Date.prototype.format = function(format) {
				var o = {
					"M+": this.getMonth() + 1, //month
					"d+": this.getDate(), //day
					"h+": this.getHours(), //hour
					"m+": this.getMinutes(), //minute
					"s+": this.getSeconds(), //second
					"q+": Math.floor((this.getMonth() + 3) / 3), //quarter
					"S": this.getMilliseconds() //millisecond
				}
				if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
					(this.getFullYear() + "").substr(4 - RegExp.$1.length));
				for (var k in o)
					if (new RegExp("(" + k + ")").test(format))
						format = format.replace(RegExp.$1,
							RegExp.$1.length == 1 ? o[k] :
							("00" + o[k]).substr(("" + o[k]).length));
				return format;
			}
			var day = new Date();
			this.endDate = day.format('yyyy-MM-dd'); //今天
			this.startDate = new Date(new Date().setDate(new Date().getDate() - 1)).format('yyyy-MM-dd'); //昨天
			// this.startDate=date.getFullYear()+'-'+((date.getMonth()+1)<10?('0'+(date.getMonth()+1)):(date.getMonth()+1))+'-'+(date.getDate()-1)
			// this.endDate=date.getFullYear()+'-'+((date.getMonth()+1)<10?('0'+(date.getMonth()+1)):(date.getMonth()+1))+'-'+date.getDate()
			this.getList()
		},
		onReachBottom() { //上拉触底函数
			// if (this.statusFlag == 3) {
			if (!this.isLoadMore) { //此处判断，上锁，防止重复请求
				this.pageSize += 1
				this.currentPage += 1
				this.getList()
			}
			// }
		},
		methods: {
			format(format) {
				var o = {
					"M+": this.getMonth() + 1, //month
					"d+": this.getDate(), //day
					"h+": this.getHours(), //hour
					"m+": this.getMinutes(), //minute
					"s+": this.getSeconds(), //second
					"q+": Math.floor((this.getMonth() + 3) / 3), //quarter
					"S": this.getMilliseconds() //millisecond
				}
				if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
					(this.getFullYear() + "").substr(4 - RegExp.$1.length));
				for (var k in o)
					if (new RegExp("(" + k + ")").test(format))
						format = format.replace(RegExp.$1,
							RegExp.$1.length == 1 ? o[k] :
							("00" + o[k]).substr(("" + o[k]).length));
				return format;
			},
			confirm(e) {
				console.log(e)
				this.warehouse = this.warehouseList[e[0]].warehouseName
				this.baseId = this.warehouseList[e[0]].id
				this.getList()
			},
			calendarchange(e) {
				this.startDate = e.startDate
				this.endDate = e.endDate
				this.getList()
				console.log(e)
			},
			getList() {
				uni.showLoading({
					title: "正在加载"
				})
				// warehouseBaseInfo/selectWarehouseSelf

				this.$api.doRequest('get', '/warehousingOrder/selectInfo', {
					currentPage: this.currentPage,
					pageSize: this.pageSize,
					searchKeyWord: this.searchKeyWord,
					compId: uni.getStorageSync('pcUserInfo').compId,
					baseId: this.baseId,
					startDate: this.startDate,
					endDate: this.endDate
				}).then(res => {
					if (res.data.code == 200) {

						if (res.data.data.records.length > 0) {
							this.show2 = false
							for (var i = 0; i < res.data.data.records.length; i++) {
								var arr = []
								if (res.data.data.records[i].warehousingOrderList) {
									var data = res.data.data.records[i].warehousingOrderList
									for (let q = 0; q < data.length; q++) {
										if (arr.every((item) => {
												return item.goodsName != data[q].goodsName
											})) {
											var list = []
											list.push(data[q])
											arr.push({
												goodsName: data[q].goodsName,
												list: list
											})
										} else {
											for (var t = 0; t < arr.length; t++) {
												if (arr[t].goodsName = data[q].goodsName) {
													arr[t].list.push(data[q])
												}
											}
										}
									}
								}
								for (var q = 0; q < arr.length; q++) {
									var weight = 0
									var weightedMoisture = 0
									var avgCost = 0
									for (var t = 0; t < arr[q].list.length; t++) {
										weight += arr[q].list[t].inWarehouseWeight
										weightedMoisture += (arr[q].list[t].weightedMoisture * arr[q].list[t]
											.inWarehouseWeight)
										avgCost += (arr[q].list[t].avgCost * arr[q].list[t].inWarehouseWeight)
									}
									arr[q].weight = weight
									arr[q].weightedMoisture = (weightedMoisture / weight).toFixed(2)
									arr[q].avgCost = (avgCost / weight).toFixed(2)

								}
								console.log(arr)
								res.data.data.records[i].goodsNameList = arr
							}
							console.log(res.data.data.records)
							if (this.currentPage == 1) {
								this.warehouseList = res.data.data.records
							} else {
								this.warehouseList = this.warehouseList.concat(res.data.data.records)
							}
						} else {
							this.show2 = true
							if (this.currentPage == 1) {
								this.warehouseList = []
							}
						}
						uni.hideLoading()
					} else {
						uni.hideLoading()
						uni.showToast({
							title: "系统异常，请联系管理员",
							icon: 'none',
							duration: 2000
						})
					}

				})
			}
		}
	}
</script>

<style lang='scss' scoped>
	.contractwrap {
		background: #fff;
		margin: 20rpx;
		padding: 20rpx 20rpx 40rpx 20rpx;
		border-radius: 20rpx;

		.contractwrap-title {
			font-size: 28rpx;
			padding: 20rpx;
			justify-content: space-between;
			align-items: center;

		}

		.contractwrap-item {
			display: flex;
			justify-content: space-between;
			padding: 10rpx 20rpx;
			font-size: 28rpx;

			.title {
				font-size: 24rpx;
				color: #8F8F8F;
			}
		}
	}

	.search {
		width: 100vw;
		background: #fff;
	}

	.u-search {
		width: 94%;
		margin: 0 auto !important;
	}
</style>
