<template>
	<view class="mainBox">
		<uni-nav-bar fixed="true" statusBar="true" left-icon="back" title="库存调拨" @clickLeft="back"></uni-nav-bar>

		<view class="tab-title">
			<view v-for="(item, index) in tabList" :key="index" class="tab-item" v-text="item.label" :class="index === pickTabIndex ? 'active' : ''" @click="changeChartsTab(index, item)"></view>
			<view class="search-icon" @click="searchVisible = true">
				筛选
				<image src="~@/static/filter.svg" mode=""></image>
			</view>
		</view>
		<view class="tab-title__placeholder"></view>
		<card :list="tableList"></card>
		<CommonFooter rightText="新建调拨" :icon="false" @rightClick="addAllocation" />

		<!-- 搜索组件 -->
		<u-popup :show="searchVisible" mode="right" @close="searchVisible = false" @open="openSearch">
			<view class="search-container">
				<view class="title">筛选</view>
				<uni-forms ref="customForm" :modelValue="popupInfo" label-position="top">
					<uni-forms-item label="单号" name="allotCode">
						<uni-easyinput v-model="popupInfo.allotCode" :inputBorder="false" placeholder="请输入" />
					</uni-forms-item>
					<!-- <uni-forms-item label="关键词" name="keyWord">
						<uni-easyinput :inputBorder="false" :value="popupInfo.keyWord" placeholder="请输入关键词"></uni-easyinput>
					</uni-forms-item> -->
					<uni-forms-item label="调拨类型" name="type">
						<uni-data-select
							@change="initWarehouse"
							v-model="popupInfo.type"
							:localdata="[
								{ value: 1, text: '库内调拨' },
								{ value: 2, text: '库外调拨' }
							]"></uni-data-select>
					</uni-forms-item>
					<uni-forms-item label="调出仓库" name="sourceWarehouse">
						<uni-data-select @change="sourceWarehouseChange" v-model="popupInfo.sourceWarehouse" :localdata="sourceWarehouseList"></uni-data-select>
					</uni-forms-item>
					<uni-forms-item label="调入仓库" name="targetWarehouse">
						<uni-easyinput v-if="popupInfo.type == 1" :inputBorder="false" :value="popupInfo.sourceWarehouseName" :disabled="true"></uni-easyinput>
						<uni-data-select v-else @change="targetWarehouseChange" v-model="popupInfo.targetWarehouse" :localdata="targetWarehouseList"></uni-data-select>
					</uni-forms-item>
					<uni-forms-item label="调拨状态" name="status">
						<uni-data-select v-model="popupInfo.status" :localdata="stutusOptions"></uni-data-select>
					</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 { getWarehouseList, getAllotApplyPage } from '@/api/warehouseManagement'
	import { get, getJ, post, postJ } from '@/utils/api.js'
	import card from './components/card.vue'
	import CommonFooter from '../components/CommonFooter.vue'
	export default {
		components: {
			card,
			CommonFooter
		},
		data() {
			return {
				page: 1,
				size: 10,
				isEnd: false,
				searchVisible: false,
				stutusOptions: [
					{
						value: 0,
						text: '未提交'
					},
					{
						value: 1,
						text: '审核中'
					},
					{
						value: 2,
						text: '已完成'
					}
				],
				tabList: [
					{
						value: '',
						label: '全部'
					},
					{
						value: 0,
						label: '未提交'
					},
					{
						value: 1,
						label: '审核中'
					},
					{
						value: 2,
						label: '已完成'
					}
				],
				statusList: [
					{ code: 0, label: '未提交' },
					{ code: 1, label: '审核中' },
					{ code: 2, label: '已完成' }
				],
				pickTabIndex: 0,
				popupInfo: {
					sourceWarehouse: '',
					sourceWarehouseName: '',
					targetWarehouse: '',
					allotCode: '',
					type: '',
					keyWord: '',
					status: ''
				}, //筛选数据
				tableList: [],
				warehouseList: [],
				targetWarehouseList: [],
				sourceWarehouseList: []
			}
		},
		onShow() {
			this.getwarehouseList()
			this.getFirstList()
		},
		//触底加载
		onReachBottom: function () {
			if (this.isEnd) {
				uni.showToast({
					title: '已经没有更多数据了！',
					icon: 'none',
					duration: 1000 //提示持续时间
				})
				return
			}
			// 显示加载图标
			this.getMoreLists()
		},
		methods: {
			// 调拨类型改变
			initWarehouse() {
				this.popupInfo.sourceWarehouse = ''
				this.popupInfo.sourceWarehouseName = ''
				this.popupInfo.targetWarehouse = ''
				this.sourceWarehouseList = [...this.warehouseList]
				this.targetWarehouseList = [...this.warehouseList]
			},
			// 调出仓库改变
			sourceWarehouseChange(val) {
				// 清空
				if (!val) {
					this.popupInfo.targetWarehouse = ''
					this.sourceWarehouseList = [...this.warehouseList]
					this.targetWarehouseList = [...this.warehouseList]
				}
				// 库内调拨
				if (this.popupInfo.type == 1) {
					console.log('库内')
					console.log(this.warehouseList.find(item => item.value == val).text)
					this.popupInfo.sourceWarehouseName = this.warehouseList.find(item => item.value == val).text
					this.popupInfo.sourceWarehouse = val
				} else if (this.popupInfo.type == 2) {
					console.log('库外')
					this.popupInfo.sourceWarehouse = val
					this.targetWarehouseList = this.targetWarehouseList.filter(item => item.value != val)
					console.log(this.targetWarehouseList)
				}
			},
			// 调入仓库改变
			targetWarehouseChange(val) {
				// 清空
				if (!val) {
					this.popupInfo.sourceWarehouse = ''
					this.sourceWarehouseList = [...this.warehouseList]
					this.targetWarehouseList = [...this.warehouseList]
				}
				if (this.popupInfo.type == 2) {
					console.log('库外')
					this.popupInfo.targetWarehouse = val
					this.sourceWarehouseList = this.sourceWarehouseList.filter(item => item.value != val)
					console.log(this.sourceWarehouseList)
				}
			},
			//获取仓库
			async getwarehouseList() {
				let res = await getWarehouseList()
				let warehouseList = res.data.map(item => {
					return {
						value: item.id,
						text: item.name
					}
				})
				this.warehouseList = warehouseList
				this.sourceWarehouseList = [...warehouseList]
				this.targetWarehouseList = [...warehouseList]
			},
			back() {
				uni.switchTab({
					url: '/pages/index/index'
				})
			},
			addAllocation() {
				uni.navigateTo({
					url: '/pages/warehouse/inventoryAllocation/edit'
				})
			},
			handleReset() {
				this.popupInfo = {
					sourceWarehouse: '',
					sourceWarehouseName: '',
					targetWarehouse: '',
					allotCode: '',
					type: '',
					keyWord: '',
					status: ''
				}
				this.pickTabIndex = 0
				this.tableList = []
				this.getFirstList()
			},
			handleSearch() {
				this.getFirstList()
			},
			// 搜索弹窗
			openSearch() {
				// console.log('open');
			},
			getFirstList() {
				this.page = 1
				this.searchVisible = false
				this.isEnd = true
				this.getList()
			},
			getMoreLists() {
				//获取更多数据
				this.page++
				this.getList()
			},
			//获取列表信息
			getList() {
				uni.showLoading({
					title: '加载中'
				})
				let data = {
					pageNum: this.page,
					size: this.size,
					...this.popupInfo
				}
				getAllotApplyPage(data)
					.then(res => {
						if (this.page === 1) {
							this.tableList = res.list
						} else {
							this.tableList = this.tableList.concat(res.list)
						}
						this.isEnd = this.tableList.length >= res.count
					})
					.finally(err => {
						uni.hideLoading()
					})
			},
			//切换tab
			changeChartsTab(index, item) {
				this.pickTabIndex = index
				this.popupInfo.status = item.value
				this.handleSearch()
			},
			//查看详情
			viewDetails(id, code) {
				uni.navigateTo({
					url: '/pages/warehouse/enterHouse/details?id=' + id + '&code=' + code
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.mainBox {
		padding-bottom: 120rpx;
	}
	.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);
		}
	}
	.tab-title {
		position: fixed;
		z-index: 99;
		width: 100%;
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: $tab-height;
		line-height: $tab-height;
		background-color: #ffffff;
		border-bottom: 1px solid #f2f2f2;
		box-sizing: border-box;
		.search-icon {
			display: flex;
			align-items: center;
			justify-content: space-around;
			font-size: 28rpx;
			white-space: nowrap;
			margin-left: 10rpx;
			image {
				width: 34rpx;
				height: 34rpx;
			}
		}

		.tab-item {
			width: 25%;
			text-align: center;
			font-size: 32rpx;
			color: $uni-text-color-grey;
		}

		.tab-item.active {
			color: $j-primary-border-green;
			border-bottom: 1px solid $j-primary-border-green;
			font-weight: bold;
		}

		.tab-item.filter {
			flex: 1;
			padding: 0px 30rpx;

			.uni-icons {
				display: flex;
				padding-top: 5px;
			}
		}
	}

	.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;
		}
	}
</style>
