<template>
	<page-meta root-font-size="16px"></page-meta>
	<view class="">
	<!-- 	<comscanlocation ref="comLocation" @locationDetail="getLocation" placeholder="请扫描盘点库位" :boxFocus='false'
			:clearResult='false'>
		</comscanlocation> -->

		<view class="scanType">
			<view class="uni-flex">
				<view>
					库存状态</view>
				<!-- 	<view style="margin-left: 10rpx;">
					<checkbox style="transform:scale(0.9)" @click="checkAllClick" :checked="checkAll" >全选</checkbox>
				</view> -->
			</view>
		</view>
		<checkbox-group @change="checkboxChange" style="transform:scale(0.9)">
			<label v-for="item in inventoryStatusArray" :key="item.value">
				<checkbox :value="item.value" :checked="item.checked">{{item.text}}</checkbox>
			</label>
		</checkbox-group>

		<scroll-view scroll-y="true" class="scrollView">
			<view hover-class="uni-list-cell-hover" v-for="(item, index) in itemList" :key="item.id">
				<view class="uni-flex uni-row">
					<view>
						<checkbox :checked="item.checked" style="transform:scale(0.7);margin-top:80rpx"
							@click="checkBox($event, item)">
						</checkbox>
					</view>
					<view style="-webkit-flex: 1;flex: 1;">
						<view class="device-detail">
							<view class="list-style">
								<view class="uni-flex uni-row space-between">
									<view class="text-bolder">ERP料号:{{ item.code }}</view>
								</view>
								<view>名称:{{ item.name }}</view>
								<view>描述:{{ item.desc2 }}</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>

		<view class="uni-flex bottom">
			<button class="accept-button" @click="deleteItem()">删除</button>
			<button type="primary" class="save-button" @click="submit()">提交</button>
		</view>
		<comMessage ref="comMessage"></comMessage>
	</view>
</template>

<script>
	import {
		getItemsByLocation,
		createCountJob
	} from '@/api/index.js'
	import {
		getInventoryStatusArray
	} from '@/common/array.js'

	// import comscanlocation from '../../mycomponents/scan/comscanlocation.vue'
	import comMessage from '@/mycomponents/common/comMessage.vue'
	import {
		getBalancesByFilter,
		byContainercode,
		byPackingcode,
		checkjobbypacking,
		checkjobbycontainer
	} from '@/api/index.js';
	import {
		showConfirmMsg,
		showConfirmMsg1,
		goHome
	} from '@/common/basic.js';

	export default {
		name: 'countByLocation',
		components: {
			// comscanlocation,
			comMessage
		},
		data() {
			return {
				itemList: [],
				inventoryStatusArray: [],
				locationCode: '',
				submitting: false,
				checkAll: false
			};
		},
		props: {
			datacontent: {
				type: Object,
				value: null
			}
		},
		filters: {

		},
		//返回首页
		onNavigationBarButtonTap(e) {
			if (e.index === 0) {
				goHome();
			}else if(e.index === 1){
				window.location.reload();
			}
		},
		onLoad() {
			this.inventoryStatusArray = getInventoryStatusArray();
			this.inventoryStatusArray.forEach(r => {
				if (r.value == 1 || r.value == 2 || r.value == 3) {
					r.checked = true
				} else {
					r.checked = false
				}
				r.value = String(r.value)
			})
		},
		mounted() {

		},

		methods: {
			getLocation(location) {
				if (this.itemList.length > 0) {
					showConfirmMsg('是否要清空现有零件列表?', confirm => {
						if (confirm) {
							this.getItems(location);
						}
					})
				} else {
					this.getItems(location);
				}
			},

			getItems(location) {
				uni.showLoading({
					title: '加载中...'
				})
				if (location != null) {
					let that = this;
					that.locationCode = location.code;
					let param = {
						locationCode: location.code
					};
					getItemsByLocation(param).then(res => {
						this.itemList = res;
						if (this.itemList.length == 0) {
							this.showMessage("未查找到零件信息");
						}
						uni.hideLoading();
					}).catch(ex => {
						this.showMessage(ex.message);
						uni.hideLoading();
					});
				}
			},

			checkBox(e, item) {
				item.checked = !item.checked;
			},

			checkAllClick() {
				this.checkAll = !this.checkAll;
				for (var i = 0; i < this.inventoryStatusArray.length; i++) {
					const item = this.inventoryStatusArray[i]
					this.$set(item, 'checked', this.checkAll)
				}
			},

			checkboxChange: function(e) {
				var items = this.inventoryStatusArray,
					values = e.detail.value;
				for (var i = 0; i < items.length; i++) {
					const item = items[i]
					if (values.includes(item.value)) {
						this.$set(item, 'checked', true)
					} else {
						this.$set(item, 'checked', false)
					}
				}
			},

			submit() {
				let checkedItems = this.inventoryStatusArray.filter(r => r.checked);

				if (this.locationCode == '') {
					this.showMessage('请扫描库位');
					return;
				} else if (checkedItems.length == 0) {
					this.showMessage('请选择库存状态');
					return;
				} else if (this.itemList.length == 0) {
					this.showMessage('没有要盘点的零件');
					return;
				}

				this.submitting = true;
				uni.showLoading({
					title: "提交中..."
				});

				let statusMap = new Map();
				checkedItems.filter((r) => !statusMap.has(r.value) && statusMap.set(Number(r.value), 1));
				let sList = [];
				statusMap.forEach((i, m) => {
					sList.push(m)
				})

				let itemMap = new Map();
				this.itemList.filter((r) => !itemMap.has(r.code) && itemMap.set(r.code, 1))
				let iList = [];
				itemMap.forEach((i, m) => {
					iList.push(m)
				})

				let lList = [this.locationCode];
				let params = {
					"countMethod": 2, //按库位按零件
					"countStage": 4, //循环盘点
					"company": localStorage.company,
					"partCondition": iList,
					"locCondition": lList,
					"statusList": sList,
					"worker": localStorage.userName_CN ==""?localStorage.userName:localStorage.userName_CN,
					"warehouseCode": localStorage.warehouseCode,
				}

				createCountJob(params).then(res => {
					this.afterSubmit();
					this.hideLoading();
				}).catch(ex => {
					this.showMessage(ex.message);
					this.hideLoading();
				})

				this.afterSubmit();
				this.hideLoading();
			},

			afterSubmit() {
				this.itemList = [];
				this.$refs.comLocation.clear();
			},

			hideLoading() {
				this.submitting = false;
				uni.hideLoading();
			},

			//删除
			deleteItem() {
				let checkItems = this.itemList.filter(r => {
					return r.checked
				});

				if (checkItems.length > 0) {
					showConfirmMsg('是否要删除选择中的零件?', confirm => {
						if (confirm) {
							for (var i = this.itemList.length - 1; i >= 0; i--) {
								let item = this.itemList[i];
								if (item.checked) {
									this.itemList.splice(i, 1);
								} else {
									item.checked = false;
								}
							}
						}
					});
				} else {
					this.showMessage('没有要删除的零件')
				}
			},

			showMessage(message) {
				this.$refs.comMessage.showMessage(message);
			},
		}
	}
</script>

<style>
	.scanType {
		padding: 5rpx 30rpx;
		font-size: 16px;
		color: gray;
	}
</style>
