<template>
	<!-- 收货列表 -->
	<view class="ware-house" @click="closeAll">
		<u-navbar back-icon-color="#333333" background="#FFFFFF" title="盘点" titleColor="#333333" :border-bottom="false" title-bold></u-navbar>
		<inventory-head ref="inventory-head" @toAdd="toAdd" :takeStatus="takeStatus" :value="form.brandId" @pickerchange="pickerchange"></inventory-head>
		<goods-count :signCount="signCount" :takeCount="takeCount"></goods-count>
		<!-- 列表 -->
		<view class="tab-view">
			<scroll-view
				scroll-y
				style="height: 100%;width: 100%;"
				scroll-anchoring
				refresher-enabled="true"
				:refresher-triggered="triggered"
				:refresher-threshold="100"
				refresher-background="#F4F5F7"
				@refresherrefresh="onRefresh"
				@refresherrestore="onRestore"
				enable-back-to-top
			>
				<NoOrder v-if="noParts" imageName="zwdd.png" text="暂无数据" :customStyle="noOrderCustomStyle" />
				<template v-else>
					<uni-swipe-action ref="uni-swipe-action">
						<view class="swipe-action" v-for="(item, index) in list" :key="item.detailId">
							<inventory-info :detail="item" :options="brandList" @handleClick="handleClick" @handleDelete="handleDelete"></inventory-info>
						</view>
					</uni-swipe-action>
				</template>
				<view class="loading-view" v-if="!loaded && !freshing"><u-loading mode="flower"></u-loading></view>
			</scroll-view>
		</view>

		<!-- 底部按钮 -->
		<fixed-button :disabled="disabled" :text="fixedButtonTxt" @handleClick="submit"></fixed-button>
		<!-- 通用提示框 -->
		<view><u-toast ref="uToast" /></view>
	</view>
</template>

<script>
import inventoryHead from './components/inventory/inventoryHead.vue';
import GoodsCount from './components/inventory/goodsCount.vue';
import FixedButton from '@/components/mine/fixed-button.vue';
import InventoryInfo from './components/inventory/inventoryInfo.vue';
import NoOrder from '@/components/order/noOrder.vue';
import ApplyButton from '@/components/parts/applyButton.vue';
import { receiveStatusOptions } from '@/common/optinos/mine/received.js';
import { inventoryStatusOptions } from '@/common/optinos/mine/inventory.js';
import { showToast, showModal, myFilter } from '@/common/common.js';
export default {
	components: {
		inventoryHead,
		InventoryInfo,
		NoOrder,
		ApplyButton,
		FixedButton,
		GoodsCount
	},
	data() {
		return {
			receiveStatusOptions,
			inventoryStatusOptions, // 盘点状态
			list: [], // 物料订单列表
			form: {
				brandId: ''
			},
			show: false,
			loaded: false, // 页面加载
			triggered: true, // true:下拉刷新
			freshing: false, // 防止下拉刷新重复触发
			stock: {
				takeId: '',
				takeStatus: '' // 盘点中 已提交 盘点完成 申请重盘
			},
			signCount: 0, // 库存数量
			takeCount: 0, // 当前盘点数量
			takeStatusOptions: {
				0: '去盘点',
				1: '确认盘点',
				2: '申请重盘',
				3: '已完成'
			}
		};
	},
	computed: {
		// 无数据
		noParts() {
			return this.list.length == 0 && this.loaded;
		},
		// 无数据页面样式
		noPartsCustomStyle() {
			return 'margin-top: 100rpx;background-color: #F4F5F7';
		},
		takeStatus() {
			let status;
			switch (this.stock.takeStatus) {
				case '1':
				case '2':
					status = 2;
					break;
				case '3':
					status = 3;
					break;
				default:
					status = 1;
					break;
			}
			return status;
		},
		fixedButtonTxt() {
			return this.takeStatusOptions[this.takeStatus];
		},
		disabled() {
			return this.stock.takeStatus === '3' ? true : false;
		}
	},
	onLoad(e) {
		// getApp().trackPage('物料订单页');
		// if (e && e.index) {
		// 	// 跳转到第几个tab页
		// 	this.currentIndex = e.index;
		// }
		this.getBrandList();
		this.freshing = false;
		this.reloadList(); // 加载数据
	},
	onShow() {
		let pages = getCurrentPages();
		let page = pages[pages.length - 1];
		if (page.needRefresh) {
			// 需要初始化页面
			page.needRefresh = null;
			this.reloadList();
		}
	},
	methods: {
		pickerchange(data) {
			this.form.brandId = data.id || '';
			if (data.name === '全部') {
				this.form.brandName = '';
			} else {
				this.form.brandName = data.name || '';
			}
			this.reloadList();
		},
		// 获取品牌列表
		getBrandList() {
			this.$refs['inventory-head'].getBrandList();
		},
		// 页面数据：未入库
		async getList(noReset = false) {
			// 状态处理
			this.loaded = false;
			var param = this.form;
			let res = await this.$u.api.inventoryListByInventoryModules(param);
			if (res.code == 200) {
				let { list, count, signCount, stock, takeCount } = res.data;
				if (!noReset) {
					let arr = list || [];
					arr.forEach(item => (item.show = false));
					this.list = arr;
				}
				this.stock = stock;
				this.signCount = signCount;
				this.takeCount = takeCount;
			} else {
				showToast(res.message);
				console.log(res.message);
			}
			this.$nextTick(() => {
				// 页面刷新完之后更新下拉状态
				if (this.freshing) {
					this.triggered = false;
					this.freshing = false;
				}
			});
			this.loaded = true; // 加载完成
		},
		// 跳转到详情
		handleClick(id) {
			if (!this.stock.takeId) {
				return false;
			}
			let url = `pages/mine/warehouse/inventoryDetail?takeId=${this.stock.takeId}`;
			if (id) url += `&id=${id}`;
			uni.navigateTo({
				url
			});
		},
		// 新增收货
		toAdd() {
			this.handleClick();
		},
		// 下拉刷新
		onRefresh() {
			if (this.freshing) return;
			this.freshing = true;
			this.reloadList();
		},
		// 重置下拉状态
		onRestore() {
			this.triggered = 'restore'; // 需要重置
		},
		// 初始化加载
		reloadList() {
			// 第一页数据
			this.getList();
		},
		closeAll() {
			if (this.list.length) {
				this.$refs['uni-swipe-action'].closeOther(undefined);
			}
		},
		async handleDelete(id) {
			if (this.stock.takeStatus !== '0') {
				showToast('盘点已提交,无法删除');
				return false;
			}
			console.log('删除--', id);
			let { code, message } = await this.$u.api.deleteInventoryByInventoryModules(id);
			if (code === 200) {
				showToast('操作成功');
				setTimeout(() => {
					this.reloadList();
				}, 1200);
			} else {
				showToast(message);
			}
		},
		// 提交
		submit() {
			let content = '';
			if (this.stock.takeStatus === '0') {
				content = '是否提交所有盘点?';
			} else if (this.stock.takeStatus === '1') {
				content = '是否申请重新盘点?';
			} else {
				if (this.stock.takeStatus === '2') {
					showToast('已申请重新盘点,请等待审核');
				}
				return false;
			}
			showModal({
				content,
				success: res => {
					if (res.confirm) {
						if (this.stock.takeStatus === '0') {
							this.confirm();
						} else if (this.stock.takeStatus === '1') {
							this.applyReInventory();
						}
					}
				}
			});
		},
		// 确认提交盘点
		async confirm() {
			let { code, message } = await this.$u.api.confirmInventoryByInventoryModules(this.stock.takeId);
			if (code === 200) {
				showToast('操作成功');
				this.getList(true);
			} else {
				showToast(message);
			}
		},
		// 申请重新盘点
		async applyReInventory() {
			let { code, message } = await this.$u.api.confirmInventoryByInventoryModules(this.stock.takeId);
			if (code === 200) {
				showToast('已申请重盘');
			} else {
				showToast(message);
			}
			await this.getList(true);
		}
	}
};
</script>
<style lang="scss" scoped>
.ware-house {
	padding: 0 30rpx;
	height: 100vh;
	// min-height: 100vh;
	display: flex;
	flex-direction: column;
	padding-bottom: constant(safe-area-inset-bottom);
	/* 兼容 iOS < 11.2 */
	padding-bottom: env(safe-area-inset-bottom);
	/* 兼容 iOS >= 11.2 */
	.tab-view {
		height: 100%;
		overflow: auto;
		margin-bottom: 100rpx;
		padding-bottom: constant(safe-area-inset-bottom);
		/* 兼容 iOS < 11.2 */
		padding-bottom: env(safe-area-inset-bottom);
		/* 兼容 iOS >= 11.2 */
		.swipe-action {
			padding-bottom: 20rpx;
		}
		.load-more-view {
			padding-bottom: 64rpx;
		}

		.loading-view {
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: center;
			margin-top: 300rpx;
			position: absolute;
		}
	}
	.fixed-button {
		width: 100%;
		box-sizing: border-box;
		position: fixed;
		left: 0;
		padding: 0 30rpx;
		padding-bottom: constant(safe-area-inset-bottom);
		/* 兼容 iOS < 11.2 */
		padding-bottom: env(safe-area-inset-bottom);
		/* 兼容 iOS >= 11.2 */
		bottom: 20rpx;
	}
}
</style>
