<template>
	<page-meta root-font-size="18px"></page-meta>
	<view class="">
		<uni-segmented-control :current="current" :values="taps" @clickItem="onClickItem" styleType="button"
			activeColor="#007AFF"></uni-segmented-control>

		<win-empty-view v-if="issueList.length==0"></win-empty-view>

		<view hover-class="uni-list-cell-hover" v-for="(item, index) in issueList" :key="item.id"
			@click="openDetail(item)">
			<comKittingIssue :dataContent="item"></comKittingIssue>
		</view>
		<uni-load-more :status="loadingType" v-if="issueList.length>0" />
		<win-scan-button @goScan='openScanPopup' title="箱码"></win-scan-button>
		<win-scan-by-pack ref="scanPopup" @getScanResult='getScanResult' title="箱码"></win-scan-by-pack>
		<com-scan-issue-list ref="scanList" @selectedItem="selectedItem"></com-scan-issue-list>
		<!-- com-message必须放在最下层 -->
		<com-message ref="comMessage"></com-message>
	</view>
</template>

<script>
	import {
		getSparePartIssueList,
		getSparePartIssueJobByNumber,
		getSparePartIssueListByRequest,
		getSparePartIssueJobByPackingCode
	} from '@/api/index.js';

	import {
		goHome,
		getIssueJobState,
		scanSuccessAudio,
		scanFailedAudio,
	} from '@/common/basic.js';

	import winEmptyView from '@/mycomponents/wincom/winEmptyView.vue'
	import issueDetail from './issue_detail.vue';
	import comKittingIssue from '@/mycomponents/coms/task/comKittingIssue.vue';
	import comMessage from '@/mycomponents/common/comMessage.vue'
	import winScanButton from '@/mycomponents/wincom/winScanButton.vue'
	import comScanIssueList from '@/mycomponents/scan/comScanIssueList.vue'
	import winScanByPack from '@/mycomponents/wincom/winScanByPack.vue'
	
	export default {
		name: 'issue',
		components: {
			winEmptyView,
			comKittingIssue,
			issueDetail,
			comMessage,
			winScanButton,
			comScanIssueList,
			winScanByPack
		},
		data() {
			return {
				//popup
				type: '',
				issueList: [],
				reload: false,
				status: '',
				contentText: {
					contentdown: '上拉加载更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				},
				pageSize: this.modelConfig,
				pageIndex: 1,
				isIssueTimeChange: false,
				isToday: false,
				titleArray: ['任务编号'],
				loadingType: "nomore",
				taps:["全部","待处理"],
				current:0
			};
		},
		props: {
			datacontent: {
				type: Object,
				value: null
			},
			isByFIFO: {
				type: Boolean,
				value: false
			}
		},
		onShow() {
			this.current = 0;
			var jobState =getIssueJobState(this.current)
			this.getList('refresh',jobState);
		},
		onNavigationBarButtonTap(e) {
			if (e.index === 0) {
				goHome();
			}else if(e.index === 1){
				window.location.reload();
			}
		},
		methods: {
			onClickItem(item) {
				this.current = item.currentIndex;
				var jobState =getIssueJobState(this.current)
				console.log("点击", this.current)
				this.getList("refresh",jobState);
			},
			openScanPopup() {
				this.$refs.scanPopup.openScanPopup();
			},

			//查询发料任务
			getList(type,jobState) {
				let that = this;
				uni.showLoading({
					title: "加载中....",
					mask: true
				});
				this.loadingType = "loading";
				if (type === "refresh") {
					this.pageIndex = 1;
					this.issueList = [];
				}
				getSparePartIssueList(that.pageIndex,that.pageSize,jobState)
					.then(res => {
						uni.hideLoading();
						if (type === "refresh") {
							uni.stopPullDownRefresh();
						}
						res.items.forEach(item=>{
							item.requestNumber =item.sparePartRequestNumber
						})
						var list = res.items;
						this.loadingType = "loadmore";
						if (list == null || list.length == 0) {
							//没数据了
							this.loadingType = "nomore";
							return;
						}
						that.issueList = type === "refresh" ? list : this.issueList.concat(list);
						that.pageIndex++;
					})
					.catch(err => {
						this.showMessage(err.message);
						uni.hideLoading();
					});
			},

			getScanResult(result) {
				uni.showLoading({
					title: "加载中....",
					mask: true
				});
				getSparePartIssueJobByPackingCode(result.data.code).then(res=>{
					uni.hideLoading();
					if(res){
						scanSuccessAudio()
						this.$refs.scanPopup.closeScanPopup();
						this.openDetail(res)
					}else {
						scanFailedAudio()
						this.showMessage("按箱码["+result.data.code+"]没有查找到发料任务");
					}
				}).catch(error=>{
					scanFailedAudio()
					uni.hideLoading();
					this.showMessage(error.message);
				})
			},

			//扫描申请单号
			scanByRequesNumber(code, type) {
				let that = this;
				uni.showLoading({
					title: "加载中....",
					mask: true
				});
				getSparePartIssueListByRequest(code).then(res => {
					uni.hideLoading();
					let items = res.items;
					if (res.totalCount > 0) {
						if (res.totalCount == 1) {
							that.openDetail(items[0]);
						} else {
							that.showItemList(items);
						}
					} else {
						that.showMessage('未查找到' + type + '为【' + code + '】的发料任务');
					}
				}).catch(err => {
					that.showMessage(err.message);
					uni.hideLoading();
				});
			},

			//扫描申请单号
			scanByNumber(code, type) {
				let that = this;
				uni.showLoading({
					title: "加载中....",
					mask: true
				});
				getSparePartIssueJobByNumber(code).then(res => {
					uni.hideLoading();
					if (res != null) {
						that.openDetail(res);
					} else {
						that.showMessage('未查找到' + type + '为【' + code + '】的发料任务');
					}
				}).catch(err => {
					that.showMessage(err.message);
					uni.hideLoading();
				});
			},

			openDetail(item) {
				if(item.enumIssueSendType==1){
					uni.navigateTo({
						url: './sparePartIssueJobDetailByQty?id=' + item.id + '&jobStatus=' + item.jobStatus
					});
				}else {
					uni.navigateTo({
						url: './sparePartIssueJobDetail?id=' + item.id + '&jobStatus=' + item.jobStatus
					});
				}
				
			},

			showMessage(message) {
				this.$refs.comMessage.showMessage(message);
			},
			onPull() {
				this.getList('refresh');
			},
			onReachBottom() {
				//避免多次触发
				if (this.loadingType == 'loading' || this.loadingType == 'nomore') {
					return;
				}
				var jobState =getIssueJobState(this.current)
				this.getList('more',jobState);
			},
			showItemList(itemList) {
				this.$refs.scanList.openPopup(itemList);
			},
			selectedItem(item) {
				this.openDetail(item);
			}
		}
	};
</script>
<style scoped lang="scss">
</style>
