<template>
	<view class="recordContainer">
		<view class="search">
			<view class="time">
				<picker mode="date" @change="toChangeStartTime">
					<view class="value">{{ searchParams.start_time | dateFormatter('yyyy-MM-dd') }}</view>
				</picker>
				<view class="fixed">至</view>
				<picker mode="date" @change="toChangeEndTime">
					<view class="value">{{ searchParams.end_time | dateFormatter('yyyy-MM-dd') }}</view>
				</picker>
			</view>
			<view class="btn" @click="toSearch">查询</view>
		</view>
		<view class="Header">
			<view>盘库记录</view>
			<view>
				<text>入库：<text class="spec">{{ infoList.reduce((a, b) => a + Number(b.man), 0) }}套</text></text>
				<text>出库：<text class="spec">{{ infoList.reduce((a, b) => a + Number(b.woman), 0) }}套</text></text>
			</view>
		</view>
		<view v-for="item in infoList" :key="item.type" class="typeCard">
			<view class="header" :class="item.toggle ? 'open' : ''" @click="toGetDetail(item)">
				<view class="icon"></view>
				<view class="name">{{ item.type_name }}</view>
			</view>
			<template v-if="item.toggle">
				<scroll-view scroll-y="true" class="scrollList">
					<template v-if="item.children.length > 0">
						<view v-for="btem in item.children" :key="btem.id" class="scrollItem">
							<view class="sex">{{ btem.sex ? '男款' : '女款' }}</view>
							<view class="size">{{ btem.size }}</view>
							<view class="time">{{ btem.created_at | dateFormatter('yyyy/MM/dd hh:mm:ss') }}</view>
							<view class="num spec">
								{{ +btem.storage_type === 1 ? '-' : '+' }}{{ btem.num }}
							</view>
						</view>
						<view v-if="!item.isMore" class="scrollItem noMore">没有更多记录了</view>
					</template>
					<template v-else>
						<view class="noTip">暂无记录</view>
					</template>
				</scroll-view>
			</template>
			<view class="all panku">
				<view class="ku">
					入库：<text class="spec">{{ item.man }}套</text>
				</view>
				<view class="ku">
					出库：<text class="spec">{{ item.woman }}套</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		panKuRecord,
		panKuRecordDetail
	} from "@/apis/stock.js"
	import {
		dateFormatter
	} from "@/filters/index.js"
	export default {
		data() {
			return {
				recoedParams: {
					start_time: '',
					end_time: ''
				},
				infoList: [],
				searchParams: {
					start_time: dateFormatter(Date.now(), 'yyyy-MM-dd'),
					end_time: dateFormatter(Date.now(), 'yyyy-MM-dd')
				}
			}
		},
		onLoad() {
			this.recoedParams.start_time = dateFormatter(Date.now(), 'yyyy-MM-dd')
			this.recoedParams.end_time = dateFormatter(Date.now(), 'yyyy-MM-dd')
			this.getStockRecord()
		},
		methods: {
			// 查询
			toSearch() {
				let {
					start_time,
					end_time
				} = this.searchParams
				if (new Date(end_time).getTime() < new Date(start_time).getTime()) {
					uni.showModal({
						content: "结束时间不能大于开始时间",
						showCancel: false
					})
					return false
				}
				this.recoedParams.start_time = start_time
				this.recoedParams.end_time = end_time
				this.getStockRecord()
			},
			// 结束时间
			toChangeEndTime(e) {
				this.searchParams.end_time = e.detail.value
			},
			// 开始时间
			toChangeStartTime(e) {
				this.searchParams.start_time = e.detail.value
			},
			// 记录详情
			toGetDetail(data) {
				if (data.total && data.isMore) {
					this.getRecordDetail(data.page_index, data.type, list => {
						data.children = [...data.children, ...list]
						data.toggle = !data.toggle
						if (list.length < 20) {
							data.isMore = false
						}
					})
				} else {
					data.toggle = !data.toggle
				}
				console.log(data, 'data')
			},
			getRecordDetail(page, type, cb) {
				uni.showLoading()
				panKuRecordDetail({
					page_index: page,
					type,
					start_time: this.recoedParams.start_time + ' 00:00:00',
					end_time: this.recoedParams.end_time + ' 00:00:00'
				}).then(res => {
					uni.hideLoading()
					if (res.code === 200) {
						cb && cb(res.data.list)
						console.log(res.data.list, 'list')
					} else {
						uni.showModal({
							content: res.message || '查看记录详情失败',
							showCancel: false
						})
					}
				}).catch(() => {
					uni.hideLoading()
					uni.showModal({
						content: '查看记录详情失败',
						showCancel: false
					})
				})
			},
			// 记录
			getStockRecord() {
				uni.showLoading()
				const _this = this
				let {
					start_time,
					end_time
				} = this.recoedParams
				panKuRecord({
					start_time: start_time + ' 00:00:00',
					end_time: end_time + ' 00:00:00'
				}).then(res => {
					if (res.code === 200) {
						_this.infoList = Object.keys(res.data).map(k => {
							let item = Object.assign({}, res.data[k])
							item.type = this.changeKey(k).type
							item.type_name = this.changeKey(k).name
							item.children = []
							item.total = Number(item.man) + Number(item.woman)
							item.toggle = false
							item.page_index = 1
							item.isMore = true
							return item
						})
						console.log(this.infoList, 'infoList')
					} else {
						uni.showModal({
							content: res.message || "获取库存记录失败",
							showCancel: false
						})
					}
				}).catch(() => {
					uni.hideLoading()
					uni.showModal({
						content: "获取库存记录失败",
						showCancel: false
					})
				})
			},
			changeKey(k) {
				let m = new Map([
					['simple', {
						name: '简约版',
						type: '1'
					}],
					['new_old', {
						name: '纯棉版',
						type: '3'
					}],
					['old', {
						name: '高腰版',
						type: '2'
					}],
					['hard', {
						name: '精装版',
						type: '0'
					}],
					['youth', {
						name: '青春版',
						type: '4'
					}]
				])
				return m.get(k)
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		display: flex;
		flex-direction: column;
	}

	.recordContainer {
		width: 100%;
		flex: 1;
		background-color: #F9F9FB;
		padding: 30rpx;
		box-sizing: border-box;

		.search {
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.time {
				width: 468rpx;
				height: 68rpx;
				border-radius: 68rpx;
				background-color: #ffffff;
				padding: 0 34rpx;
				box-sizing: border-box;
				line-height: 68rpx;
				font-size: 28rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.value {
					color: #999999;
					letter-spacing: 1px;
				}

				.fixed {
					color: #333333;
				}
			}

			.btn {
				width: 192rpx;
				height: 68rpx;
				border-radius: 68rpx;
				text-align: center;
				line-height: 68rpx;
				color: #FFFFFF;
				font-size: 28rpx;
				background: linear-gradient(to right, #F97C55 0%, #F44545 100%);
			}
		}

		.Header {
			width: 100%;
			height: 110rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			color: #333333;
			font-size: 32rpx;
			line-height: 44rpx;

			.spec {
				color: #EA4A41 !important;
				font-weight: bold;
			}
		}

		.typeCard {
			width: 100%;
			border-radius: 24rpx;
			background-color: #ffffff;
			margin-bottom: 30rpx;
			overflow: hidden;

			.header {
				width: 100%;
				height: 104rpx;
				padding: 0 30rpx;
				box-sizing: border-box;
				display: flex;
				align-items: center;
				justify-content: flex-start;
				border-bottom: 1px solid #EEEEEE;

				.icon {
					width: 30rpx;
					height: 30rpx;
					border-radius: 50%;
					background-color: rgba(234, 74, 65, 0.36);
					margin-right: 10rpx;
					display: flex;
					align-items: center;
					justify-content: center;

					&::before {
						content: "";
						display: block;
						width: 18rpx;
						height: 18rpx;
						border-radius: 50%;
						background-color: #EA4A41;
					}
				}

				.name {
					color: #333333;
					font-size: 32rpx;
					line-height: 44rpx;
				}

				&::after {
					display: block;
					content: "";
					margin-left: auto;
					width: 30rpx;
					height: 30rpx;
					background-image: url(../../static/new_my/arrow.png);
					background-position: center;
					background-size: cover;
					background-repeat: no-repeat;
				}

				&.open {
					&::after {
						transform: rotate(90deg) !important;
					}
				}
			}

			.scrollList {
				width: 100%;
				height: 416rpx;

				.scrollItem {
					width: 100%;
					height: 104rpx;
					border-bottom: 1px solid #EEEEEE;
					box-sizing: border-box;
					padding: 0 30rpx;
					color: #333333;
					font-size: 28rpx;
					line-height: 40rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;

					.spec {
						font-size: 32rpx !important;
						color: #EA4A41 !important;
					}

					&.noMore {
						justify-content: center !important;
						border-bottom: none !important;
					}

					.sex,
					.size,
					.time,
					.num {
						width: 100rpx;
						overflow: hidden;
						text-overflow: ellipsis;
					}

					.time {
						width: 300rpx !important;
					}

					.num {
						flex: 1 !important;
						text-align: right;
					}
				}

				.noTip {
					width: 100%;
					height: 100%;
					display: flex;
					align-items: center;
					justify-content: center;
					color: #333333;
					font-size: 28rpx;
				}
			}

			.all {
				width: 100%;
				height: 116rpx;
				padding: 0 30rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.sex {
					color: #333333;
					font-size: 28rpx;
					line-height: 40rpx;
				}

				.num {
					color: #999999;
					font-size: 28rpx;

					.spec {
						color: #EA4A41;
						font-size: 40rpx;
						line-height: 56rpx;
						font-weight: bold;
					}
				}
			}

			.panku {
				justify-content: center !important;

				.ku {
					color: #333333;
					font-size: 28rpx;

					.spec {
						color: #EA4A41 !important;
						font-size: 36rpx !important;
						line-height: 50rpx !important;
					}

					&:nth-last-of-type(1) {
						margin-left: 20rpx;
					}
				}

			}
		}
	}
</style>
