<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 class="tab_box rx-sc">
				<view class="tab_item" v-for="(item, index) in tabList" :key="index" v-text="item.label"
					:class="{active: pickTabIndex == index}" @click="changeChartsTab(index)"></view>

				<view class="more_search">
					<image src="~@/static/moreSearch.svg" mode="" @click="searchVisible = true"></image>
				</view>
			</view>
		</view>
		<card :list="tableList"></card>
		<CommonFooter rightText="新建调拨" :icon="false" @rightClick="addAllocation" />

		<!-- 搜索组件 -->
		<u-popup :show="searchVisible" mode="top" @close="searchVisible = false" @open="openSearch">
			<view class="search_list">
				<u-form labelPosition="left" :model="popupInfo" labelWidth="180" labelAlign="left" class="baseForm">
					<u-form-item label="单号:" class="required-form" borderBottom prop="allotCode">
						<uni-easyinput v-model="popupInfo.allotCode" :inputBorder="false" placeholder="请输入" />
					</u-form-item>
					<u-form-item label="调拨类型:" class="required-form" borderBottom prop="type">
						<zxz-uni-data-select :localdata="[
								{ value: 1, text: '库内调拨' },
								{ value: 2, text: '库外调拨' }
							]" v-model="popupInfo.type" dataValue='value' dataKey="text" filterable format='{text}'></zxz-uni-data-select>
					</u-form-item>
					<u-form-item label="调出仓库:" class="required-form" borderBottom prop="sourceWarehouse">
						<zxz-uni-data-select @change="sourceWarehouseChange" :localdata="sourceWarehouseList"
							v-model="popupInfo.sourceWarehouse" dataValue='value' dataKey="text" filterable
							format='{text}'></zxz-uni-data-select>
					</u-form-item>
					<u-form-item label="调入仓库:" class="required-form" borderBottom prop="targetWarehouse">
						<uni-easyinput v-if="popupInfo.type == 1" :inputBorder="false"
							:value="popupInfo.sourceWarehouseName" :disabled="true" placeholder="请输入"></uni-easyinput>
						<zxz-uni-data-select v-else @change="targetWarehouseChange" :localdata="targetWarehouseList"
							v-model="popupInfo.targetWarehouse" dataValue='value' dataKey="text" filterable
							format='{text}'></zxz-uni-data-select>
					</u-form-item>
					<u-form-item label="调拨状态:" class="required-form" borderBottom prop="status">
						<zxz-uni-data-select :localdata="stutusOptions" v-model="popupInfo.status" dataValue='value'
							dataKey="text" filterable format='{text}'></zxz-uni-data-select>
					</u-form-item>
				</u-form>
			</view>
			<view class="operate_box rx-bc">
				<u-button size="small" class="u-reset-button" @click="handleReset">
					重置
				</u-button>
				<u-button type="success" size="small" class="u-reset-button" @click="handleSearch">
					确定
				</u-button>
			</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_list {
		min-height: 100rpx;

		/deep/ .baseForm {
			padding: 40rpx 20rpx 20rpx 20rpx;
		}
	}

	.operate_box {
		padding: 10rpx 32rpx;

		/deep/ .u-button {
			width: 40%;
		}
	}

	.tab-title {
		display: flex;
		align-items: center;
		background-color: #fff;
		position: relative;

		.tab_box {
			width: 100%;
			height: 68rpx;
			position: relative;
		}

		.tab_item {
			height: 68rpx;
			line-height: 68rpx;
			padding: 0 20rpx;
			font-size: 32rpx;
			// color: #979C9E;

		}

		.more_search {
			// display: flex;
			// align-items: center;
			height: 70rpx;
			line-height: 70rpx;
		}

		image {
			width: 52rpx;
			height: 52rpx;
			position: absolute;
			right: 10px;
			top: 50%;
			transform: translateY(-50%);
			// margin-left: 10rpx;
		}

		.active {
			box-sizing: border-box;
			border-bottom: 6rpx solid $theme-color;
			color: $theme-color;
		}
	}

	.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>