<template>
	<view class="mainBox">
		<uni-nav-bar fixed="true" statusBar="true" left-icon="back" title="出库管理" @clickLeft="back"></uni-nav-bar>

		<view class="tab-wrapper">
			<tabs @change="handleTabChange" @handleSearch="handleTabSearch" />
		</view>
		<view class="tab-title__placeholder"></view>

		<CardList :list="tableList" />
		<CommonFooter leftText="新建出库" @leftClick="addStock('hand')" />

		<!-- 搜索组件 -->
		<u-popup :show="searchVisible" mode="right" @close="searchVisible = false">
			<view class="search-container">
				<view class="title">筛选</view>
				<uni-forms ref="customForm" :modelValue="popupInfo" label-position="top">
					<uni-forms-item label="出库时间" name="name">
						<view class="time-wrapper">
							<uni-datetime-picker v-model="popupInfo.range" :border="false" :icon="false" type="daterange" />
						</view>
					</uni-forms-item>
					<uni-forms-item label="单号" name="bizNum">
						<uni-easyinput v-model="popupInfo.bizNum" :inputBorder="false" placeholder="请输入" />
					</uni-forms-item>
					<uni-forms-item label="领料人" name="deliveryName">
						<uni-easyinput v-model="popupInfo.deliveryName" :inputBorder="false" placeholder="请输入" />
					</uni-forms-item>
				</uni-forms>
				<view class="footer">
					<view class="btn reset" @click="handleReset">重置</view>
					<view class="btn search" @click="handleSearch">搜索</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import { post, get } from '@/utils/api.js'
	import tabs from '../components/tabs.vue'
	import CardList from './components/CardList'
	import CommonFooter from '.././components/CommonFooter.vue'
	let [page, size, isEnd] = [1, 10, true]

	export default {
		components: {
			CommonFooter,
			CardList,
			tabs
		},
		data() {
			return {
				status: '',
				pickTabIndex: 0,
				searchVisible: false, //右侧筛选
				tableList: [],
				popupInfo: {
					range: [],
					bizNum: '',
					deliveryName: ''
				} //筛选数据
			}
		},
		onShow() {
			this.getFirstList()
		},
		//触底加载
		onReachBottom: function () {
			if (isEnd) {
				uni.showToast({
					title: '已经没有更多数据了！',
					icon: 'none',
					duration: 1000 //提示时间
				})
				return
			}
			// 显示加载图标
			uni.showLoading({
				title: '数据加载中'
			})
			this.getMoreLists()
		},
		methods: {
			handleReset() {
				this.popupInfo = {
					range: [],
					bizNum: '',
					deliveryName: ''
				}
				this.getFirstList()
			},
			handleSearch() {
				this.getFirstList()
			},
			getFirstList() {
				page = 1
				isEnd = true
				this.searchVisible = false
				this.tableList = []
				uni.showLoading({
					title: '数据加载中'
				})
				this.getList()
			},
			getMoreLists() {
				//获取更多数据
				page++
				this.getList()
			},
			//获取列表信息
			getList() {
				uni.showLoading({
					title: '加载中'
				})
				let data = {
					page,
					size,
					bizStatus: 2,
					auditStatus: this.status.value,
					...this.popupInfo
				}
				if (data.range?.length) {
					data.startTime = data.range[0]
					data.endTime = data.range[1]
				}
				delete data.range
				get(this.apiUrl + '/outInWarehouse/select/inWarehouse', data, true)
					.then(res => {
						if (this.status.value === data.auditStatus) {
							if (page === 1) {
								this.tableList = res.data.records
							} else {
								this.tableList.push(...res.data.records)
							}
							isEnd = this.tableList.length >= res.data.total
						}
					})
					.then(() => {
						uni.hideLoading()
					})
			},
			//切换tab
			handleTabChange(status) {
				this.status = status
				uni.pageScrollTo({ scrollTop: 0, duration: 0 })
				this.getFirstList()
			},
			//筛选
			handleTabSearch() {
				this.searchVisible = true
				// this.getFirstList()
			},
			//新增入库单
			addStock() {
				uni.navigateTo({
					url: '/pages/warehouse/outHouse/addStock'
				})
			},
			//查看详情
			viewDetails(id, code) {
				uni.navigateTo({
					url: '/pages/warehouse/outHouse/details?id=' + id + '&code=' + code
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.search-container {
		width: 70vw;
		padding: 30rpx 36rpx;
		.footer {
			position: absolute;
			bottom: 0;
			left: 0;
			width: 100%;
			display: flex;
			box-shadow: 0 10rpx 30rpx 0 #000;
			.btn {
				width: 50%;
				height: 80rpx;
				border-radius: 0 !important;
				flex: 1;
				display: flex;
				justify-content: center;
				align-items: center;
				&.reset {
					color: $j-primary-border-green;
				}
				&.search {
					color: #fff;
					background-color: $j-primary-border-green;
				}
			}
		}
		.title {
			font-weight: bold;
			font-size: 30rpx;
		}
		.status-wrapper {
			display: flex;
			align-items: center;
			justify-content: space-between;
			view {
				background-color: rgba(242, 242, 242, 1);
				height: 60rpx;
				border-radius: 30rpx;
				line-height: 60rpx;
				text-align: center;
				width: 160rpx;
				border: 1rpx solid rgba(242, 242, 242, 1);
				&.active {
					color: #157a2c;
					border-color: rgba(21, 122, 44, 1);
				}
			}
		}
		/deep/.uni-date {
			.uni-icons {
				display: none;
			}
			.uni-date-x {
				padding: 0;
				view {
					margin: 0 20rpx;
				}
			}
		}
		/deep/.uni-date__x-input,
		/deep/.uni-easyinput__content-input {
			height: 60rpx;
			border-radius: 30rpx;
			overflow: hidden;
			background-color: rgba(242, 242, 242, 1);
		}
	}
	.mainBox {
		padding-bottom: 120rpx;
	}

	.tab-wrapper {
		position: fixed;
		z-index: 99;
		width: 100%;
	}

	.listBox {
		margin-top: 10px;
		padding-top: 5px;
		padding-bottom: 8px;
		.listTit {
			display: flex;
			align-items: center;
			.stuts {
				width: 55px;
				font-size: 12px;
				// border: 1px solid red;
			}
			.name {
				font-size: $uni-font-size-base;
				padding-left: 15rpx;
			}

			.date {
				font-size: $uni-font-size-sm;
				padding-left: 45rpx;
				color: #999;
			}
		}

		.listCont {
			display: flex;
			align-items: center;
			flex-wrap: wrap;
			padding-left: 20rpx;
			flex-direction: row;
			margin-top: 10rpx;
			color: #666;
			// border: 1px solid red;
			.item {
				width: 50%;
				font-size: $uni-font-size-sm;
				overflow: hidden;
				white-space: nowrap;
				text-overflow: ellipsis;
				-o-text-overflow: ellipsis;
			}

			.item text {
				color: #666;
			}
			.item-left {
				font-size: 20px;
				font-weight: 600;
				color: #999;
				position: relative;
				left: 90%;
				top: -55rpx;
			}
		}
		.item-top {
			height: 5rpx;
			width: 96%;
			margin: 0 auto;
			border-bottom: 1px solid #d8d3d3;
		}

		.listbtn {
			margin-top: 20rpx;
			display: flex;
			justify-content: flex-end;
			align-items: center;

			.operBox {
				display: flex;
				align-items: center;
			}
		}

		.listbtn button {
			margin-right: 20rpx;
		}
	}

	.footBox {
		position: fixed;
		left: 0px;
		bottom: 0px;
		height: 100rpx;
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: space-between;
		view {
			width: 100%;
			height: 100%;
			text-align: center;
			color: #fff;
			display: flex;
			align-items: center;
			justify-content: center;
		}

		.add {
			background: $uni-color-primary;
		}

		.reg {
			background: $u-success-dark;
		}

		.uni-icons {
			margin-right: 8rpx !important;
			font-weight: bold;
		}
	}
</style>
