<template>
	<view>
		<view class="tabs-view">
			<view class="keyword-view clearfix">
				<view class="float_left">
					<u-search
						:show-action="false"
						@clear="searchData()"
						@search="searchData()"
						:clearabled="true"
						placeholder="调拨单号/商品名称"
						v-model="search_form.no"
					></u-search>
				</view>
				<view class="float_right" @click="openSel('search_show')"><text class="custom-icon custom-icon-shaixuan"></text></view>
			</view>
		</view>
		<view class="list-ul">
			<view class="list-li" @click="details(item.id)" v-for="(item, index) in outgoing_merge_list" :key="index">
				<view class="title">
					{{ item.warehouseName }}
					<view class="icon"><u-icon name="arrow-rightward" color="#B8C0C8"></u-icon></view>
					{{ item.inWarehouseName }}
				</view>
				<view class="list-cont">
					<view class="list-cont-li clearfix">
						<text class="label">日期：</text>
						{{ $u.timeFormat(item.createTime) }}
					</view>
					<view class="list-cont-li">
						<text class="label">单号：</text>
						{{ item.no }}
					</view>
					<view class="list-cont-li">种类{{ item.skuNum }}, 数量{{ item.numT }}</view>
					<view class="more-icon">
						<text v-if="item.auditStatus === 1" class="warning-status">待审核</text>
						<text v-else class="success-status">已审核</text>
						<u-icon name="arrow-right" color="#879BBA" size="28"></u-icon>
					</view>
				</view>
			</view>
			<addBtn v-if="$accessCheck($Access.AllocationFormAddAllocate)" url="/pagesT/stock/Allocationedit"></addBtn>
			<u-popup v-model="search_show" mode="right">
				<view class="search-pop">
					<view class="form-view">
						<u-form label-width="160rpx" label-position="left">
							<u-form-item label-position="top" label="制单日期">
								<view class="date-li">
									<picker mode="date" @change="bindDateStartChange">
										<text class="date-li">{{ search_form.startTime ? $u.timeFormat(search_form.startTime, 'yyyy-mm-dd') : '开始日期' }}</text>
									</picker>
								</view>
								<view class="date-line">-</view>
								<view class="date-li">
									<picker mode="date" @change="bindDateEndChange">
										<text class="date-li">{{ search_form.endTime ? $u.timeFormat(search_form.endTime, 'yyyy-mm-dd') : '结束日期' }}</text>
									</picker>
								</view>
							</u-form-item>
							<u-form-item label="调出仓库">
								<view class="clearfix form-val" @click="goWarehouse(2)">
									<text class="float_left ellipsis">{{ search_form.warehouseId ? warehouse_name : '请选择' }}</text>
									<view class="float_right" @click.stop="clearValue('warehouseId')">
										<u-icon :name="!search_form.warehouseId ? 'arrow-right' : 'close-circle-fill'" size="28" color="#999999"></u-icon>
									</view>
								</view>
							</u-form-item>
							<u-form-item label="调入仓库">
								<view class="clearfix form-val" @click="goWarehouse(1)">
									<text class="float_left ellipsis">{{ search_form.inWarehouseId ? inwarehouse_name : '请选择' }}</text>
									<view class="float_right" @click.stop="clearValue('inWarehouseId')">
										<u-icon :name="!search_form.inWarehouseId ? 'arrow-right' : 'close-circle-fill'" size="28" color="#999999"></u-icon>
									</view>
								</view>
							</u-form-item>
							<u-form-item label="审核状态">
								<view class="clearfix form-val" @click="openFrom">
									<text class="float_left ellipsis">{{ search_form.auditStatus ? search_form.auditStatusName : '请选择' }}</text>
									<view class="float_right" @click.stop="clearValue('auditStatus')">
										<u-icon :name="!search_form.auditStatus ? 'arrow-right' : 'close-circle-fill'" size="28" color="#999999"></u-icon>
									</view>
								</view>
							</u-form-item>
						</u-form>
					</view>

					<view class="search-btn">
						<view class="btn-li" @click="clearValue()">重置</view>
						<view class="btn-li" @click="searchConfirm">确定</view>
					</view>
				</view>
			</u-popup>
			<u-select @confirm="fromChange" v-model="from_show" :list="order_status"></u-select>
			<view v-if="!outgoing_list.length" class="empty-view"><u-empty text="暂无数据" mode="list"></u-empty></view>
			<u-loadmore v-if="outgoing_list.length" :status="load_status" />
		</view>
	</view>
</template>
<script>
export default {
	data() {
		return {
			search_show: false,
			order_status: [
				{
					value: 2,
					label: '已审核'
				},
				{
					value: 1,
					label: '待审核'
				},
				{
					value: 3,
					label: '审核未通过'
				},
				{
					value: 4,
					label: '审核中'
				}
			],
			load_status: 'nomore',
			tabs_current: 0,
			page: 1,
			pageSize: 10,
			total: 0,
			source: 5, // 调拨出库 8; 销售出库 5; 采购退货 14;
			search_form: {
				shopId: '',
				shopName: '',
				inWarehouseId: '', //调出仓库id
				auditStatus: '', //审核状态id
				auditStatusName: '', //审核状态名称
				goodsName: '', // 商品名称
				warehouseId: '', // 仓库id
				startTime: '', // 制单开始时间
				endTime: '' // 制单结束时间
			},
			warehouse_name: '',
			inwarehouse_name: '',
			outgoing_list: [],
			outgoing_merge_list: [],
			werahouseData: '',
			tag: '',
			from_show: false
		};
	},
	watch: {
		werahouseData(val) {
			if (this.tag === 2) {
				if (val) {
					this.search_form.warehouseId = val.id;
					this.warehouse_name = val.warehouseName;
				}
			} else if (this.tag === 1) {
				if (val) {
					this.search_form.inWarehouseId = val.id;
					this.inwarehouse_name = val.warehouseName;
				}
			}
		}
	},
	onShow() {
		this.getAllAllocate();
	},
	onReachBottom() {
		if (this.total / this.pageSize > this.page) {
			this.page += 1;
			this.getData();
		}
	},
	onPullDownRefresh() {
		this.searchData();
	},
	methods: {
		fromChange(e) {
			this.search_form.auditStatus = e[0].value;
			this.search_form.auditStatusName = e[0].label;
		},
		openFrom() {
			this.from_show = true;
		},
		goWarehouse(tag) {
			this.tag = tag;
			this.goPage('/pagesT/werahouse/selWerahouse');
		},
		details(id) {
			uni.navigateTo({
				url: '/pagesT/stock/AllocationDetails?id=' + id
			});
		},
		openSel(key) {
			this[key] = true;
		},
		bindDateStartChange(e) {
			this.search_form.startTime = this.$utils.timeByTimestamp(e.detail.value + ' 00:00:00');
		},
		bindDateEndChange(e) {
			this.search_form.endTime = this.$utils.timeByTimestamp(e.detail.value + ' 23:59:59');
		},
		// 搜索确定
		searchConfirm() {
			this.search_show = false;
			this.searchData();
		},
		// 重置搜索
		clearValue(params) {
			if (!params) {
				this.search_form = {
					no: '',
					shopId: '',
					shopName: '',
					inWarehouseId: '', //调出仓库id
					auditStatus: '', //审核状态id
					auditStatusName: '', //审核状态名称
					goodsName: '', // 商品名称
					warehouseId: '', // 仓库id
					startTime: '', // 制单开始时间
					endTime: '' // 制单结束时间
				};
				this.searchConfirm();
			} else {
				this.search_form[params] = '';
			}
		},
		getAllAllocate() {
			this.load_status = 'loading';
			this.$u.api
				.getAllAllocate({
					page: this.page,
					pageSize: this.pageSize
				})
				.then(res => {
					if (this.page === 1) {
						this.outgoing_list = res.data;
					} else {
						this.outgoing_list = this.outgoing_list.concat(res.data);
					}
					let arr = [];
					this.outgoing_list.forEach(item => {
						const index = arr.findIndex(ia => ia.id === item.id);
						if (index === -1) {
							arr.push({
								...item,
								skuNum: 1,
								numT: Number(item.num)
							});
						} else {
							arr[index].skuNum += 1;
							arr[index].numT = this.$NP.plus(arr[index].numT, Number(item.num));
						}
					});
					this.outgoing_merge_list = arr;
					this.total = res.pageTotal;
					this.load_status = this.$utils.loadStatus(this.page, this.pageSize, this.total);
				});
		},
		searchAllocate() {
			this.load_status = 'loading';
			this.$u.api
				.searchAllocate({
					page: this.page,
					pageSize: this.pageSize,
					search: this.search_form.no,
					star: this.search_form.startTime,
					end: this.search_form.endTime,
					inWarehouseId: this.search_form.inWarehouseId,
					auditStatus: this.search_form.auditStatus,
					warehouseId: this.search_form.warehouseId
				})
				.then(res => {
					if (this.page === 1) {
						this.outgoing_list = res.data;
					} else {
						this.outgoing_list = this.outgoing_list.concat(res.data);
					}
					let arr = [];
					this.outgoing_list.forEach(item => {
						const index = arr.findIndex(ia => ia.id === item.id);
						if (index === -1) {
							arr.push({
								...item,
								skuNum: 1,
								numT: Number(item.num)
							});
						} else {
							arr[index].skuNum += 1;
							arr[index].numT = this.$NP.plus(arr[index].numT, Number(item.num));
						}
					});
					this.outgoing_merge_list = arr;
					this.total = res.pageTotal;
					this.load_status = this.$utils.loadStatus(this.page, this.pageSize, this.total);
				});
		},
		//  判断
		getData() {
			const isKey = this.$utils.isSerch(this.search_form);
			if (isKey) {
				this.searchAllocate();
			} else {
				this.getAllAllocate();
			}
		},
		searchData() {
			this.page = 1;
			this.getData();
		}
	}
};
</script>

<style lang="scss" scoped>
.tabs-view {
	position: fixed;
	width: 100%;
	top: 0;
	left: 0;
	z-index: 99;
	.keyword-view {
		padding: 20rpx 24rpx 0;
		background-color: #ffffff;
		z-index: 9;
		padding-bottom: 20rpx;

		.float_left {
			width: 640rpx;
		}

		.float_right {
			line-height: 64rpx;
			width: 50rpx;
			text-align: center;
			color: #666666;
		}
	}
}

.list-ul {
	padding-top: 100rpx;

	.list-li {
		width: 710rpx;
		margin: 20rpx auto;
		padding: 0 24rpx 20rpx;
		background-color: #ffffff;
		border-radius: 20rpx;

		.title {
			line-height: 80rpx;
			border-bottom: 1px solid #eeeeee;
			font-size: 26rpx;
			.icon {
				display: inline-block;
				margin: 0 10rpx;
			}
		}
		.list-cont {
			padding-top: 20rpx;
			font-size: 24rpx;
			line-height: 40rpx;
			color: #62738e;
			position: relative;
			.more-icon {
				position: absolute;
				top: 50%;
				transform: translateY(-50%);
				right: 20rpx;
			}
		}
	}
}
</style>
