<template>
	<view style="padding: 15rpx;">
		<uni-segmented-control :current="current" :values="items" @clickItem="onClickItem" styleType="button"
			activeColor="#007AFF"></uni-segmented-control>
		<view class="content">
			<view v-show="current === 0">
				<win-empty-view v-if="dataList.length==0"></win-empty-view>
				<view v-if="dataList.length>0" v-for="(item, index) in dataList" :key="index">
					
					<view class="device-detail" style="font-size: 32rpx;">
						<com-job-top-info :jobContent="item"></com-job-top-info>
						<view>器具类型 : {{item.containerType|getContainerTypeName}} </view>
						<view>器具规格 : {{item.specificationsTypeName}}</view>
						<view>目标库位 : {{item.requestLocationCode}} </view>
						<view>操作人 : {{item.worker}} </view>
						<view>创建时间 : {{item.creationTime| formatDate}} </view>

						<button type="primary" style="width: 140rpx; font-size: 32rpx; float: right;"
							@click="complete(item)">完成</button>
					</view>
				</view>
			</view>
			<view v-show="current === 1">
				<win-empty-view v-if="dataList.length==0"></win-empty-view>
				<view v-if="dataList.length>0" v-for="(item, index) in dataList" :key="index">
					<view class="device-detail" style="font-size: 32rpx;">
						<com-job-top-info :jobContent="item"></com-job-top-info>
						<view>器具类型 : {{item.containerType|getContainerTypeName}} </view>
						<view>器具规格 : {{item.specificationsTypeName}}</view>
						<view>目标库位 : {{item.requestLocationCode}} </view>
						<view>操作人 : {{item.worker}} </view>
						<view>创建时间 : {{item.creationTime| formatDate}} </view>
					</view>
				</view>
			</view>
		</view>
		<uni-load-more :status="loadingType" v-if="dataList.length>0" />
		<com-message ref="comMessage"></com-message>
	</view>
</template>
<script>
	import {
		getContainerList,
		getDictByCode,
		finshContainerJob
	} from '@/api/index.js';
	import {
		goHome,
		dateFormat,
		getContainerTypeName,
		showConfirmMsg
	} from '@/common/basic.js';
	import winEmptyView from '@/mycomponents/wincom/winEmptyView.vue'
	import comJobTopInfo from '@/mycomponents/comjob/comJobTopInfo.vue'
	import comMessage from '@/mycomponents/common/comMessage.vue'
	export default {
		components: {
			winEmptyView,
			comJobTopInfo,
			comMessage
		},
		data() {
			return {
				items: ['未完成', '已完成'],
				current: 0,
				pageSize: this.modelConfig,
				pageIndex: 1,
				loadingType: "nomore",
				dataList: [],
				containerModelList: [],
				isFinished:false
			};
		},
		filters: {
			formatDate: function(val) {
				return dateFormat(val)
			},
			getContainerTypeName: function(val) {
				return getContainerTypeName(val)
			},
		},

		onShow() {
			this.getList('refresh');
		},
		onReachBottom() {
			//避免多次触发
			if (this.loadingType == 'loading' || this.loadingType == 'nomore') {
				return;
			}
			this.getList("more");
		},
		onPullDownRefresh() {
			this.getList('refresh');
		},
		//后退按钮
		onBackPress(options) {
			if (options.from === 'navigateBack') {
				return false;
			}
			goHome();
			return true;
		},
		//返回首页
		onNavigationBarButtonTap(e) {
			if (e.index === 0) {
				goHome();
			} else if (e.index === 1) {
				window.location.reload();
			}
		},
		methods: {
			complete(item) {
				showConfirmMsg("是否确认完成?", callback => {
					if (callback) {
						this.submit(item)
					} else {
						console.log('cancel')
					}
				})
			},
			onClickItem(item) {
				this.current = item.currentIndex;
				console.log("点击", this.current)
				if(this.current==0){
					this.isFinished =false;
				}else {
					this.isFinished =true;
				}
				this.getList("refresh");
			},
			openScanPopup() {
				this.$refs.scanPopup.openScanPopup();
			},

			//查询发货任务
			getList(type) {
				let that = this;
				uni.showLoading({
					title: "加载中....",
					mask: true
				});
				this.loadingType = "loading";
				if (type === "refresh") {
					this.pageIndex = 1;
					this.dataList = [];
				}
				let params = {
					pageSize: that.pageSize,
					pageIndex: that.pageIndex,
					isFinished:that.isFinished
					// isCreationTimeSorting: that.isTimeWindowSorting,
					// isToday: that.isToday
				};
				getContainerList(params)
					.then(res => {
						uni.hideLoading();
						if (type === "refresh") {
							uni.stopPullDownRefresh();
						}
						var list = res.items;
						this.loadingType = "loadmore";
						if (list == null || list.length == 0) {
							//没数据了
							this.loadingType = "nomore";
							return;
						}
						// that.dataList = [...that.dataList, ...list];
						that.dataList = type === "refresh" ? list : this.dataList.concat(list);
						that.pageIndex++;
					})
					.catch(err => {
						this.loadingType = "";
						this.showMessage(err.message);
						uni.hideLoading();
						if (type === "refresh") {
							uni.stopPullDownRefresh();
						}
					});
			},
			showMessage(message) {
				this.$refs.comMessage.showMessage(message);
			},
			showCommitSuccessMessage() {
				this.$refs.comMessage.showCommitSuccess();
			},
			submit(item) {
				uni.showLoading({
					title: "提交中...",
					mask:true
				});
				let params = this.dataList.find(r => r.id == item.id);
				console.log("测试",JSON.stringify(params));
				finshContainerJob(item.id, params)
					.then(res => {
						uni.hideLoading();
						if (res != null) {
							this.showCommitSuccessMessage();
							this.getList("refresh")
						}
					})
					.catch(err => {
						this.showMessage(err.message);
						uni.hideLoading();
					});
			},
		}
	};
</script>