<template>
	<view class="">
		<jobFilter ref="filter" buttontType="issue" @switchChangeToday="switchChangeToday"
			@switchChangeWait="switchChangeWait" @scanAsN="scanAsN" @scanAppro="scanAppro" :checkedToday="checkedToday"
			:checkedWaitTask="checkedWaitTask">
		</jobFilter>
		<com-empty-view v-if="issueList.length==0"></com-empty-view>

		<uni-swipe-action ref="swipeAction">
			<view v-for="(item, index) in issueList" :key="index">
				<uni-swipe-action-item
					:right-options="item.status=='2'?detailGiveupOptions:detailOptions"
					@click="swipeClick($event,item)">
					<com-issue-job-card :dataContent="item" @click='openJobDetail(item)'></com-issue-job-card>
				</uni-swipe-action-item>
			</view>
		</uni-swipe-action>

		<uni-load-more :status="loadingType" v-if="issueList.length>0" />
		<winScanAsnNumber ref="scanAsnNumber" title="ASN" @getScanCode='getByAsnNumber'></winScanAsnNumber>
		<winScanJobNumber ref="scanNumber" title="任务编号" @getScanCode='getByNumber'></winScanJobNumber>
		<comMessage ref="comMessage"></comMessage>
		<job-info-popup ref='jobInfoPopup'></job-info-popup>
	</view>
</template>

<script>
	import {
		getIssueJobList,
		cancelTakeIssueJob,
		getIssueByCode
	} from '@/api/request.js';

	import {
		goHome,
		updateTitle
	} from '@/common/basic.js';

	import {
		getDetailOption,
		getDetailGiveupOption
	} from '@/common/array.js';

	import comEmptyView from '@/mycomponents/common/comEmptyView.vue'
	import comMessage from '@/mycomponents/common/comMessage.vue' 
	import jobFilter from '@/mycomponents/job/jobFilter.vue'
	import comIssueJobCard from '@/pages/issue/coms/comIssueJobCard.vue'
	import winScanJobNumber from "@/mycomponents/scan/winScanJobNumber.vue"
	import winScanAsnNumber from "@/mycomponents/scan/winScanAsnNumber.vue"
	import jobCard from '@/mycomponents/job/jobCard.vue';
	import jobInfoPopup from '@/mycomponents/job/jobInfoPopup.vue'

	export default {
		name: 'issue',
		components: {
			comEmptyView,
			comMessage, 
			jobFilter,
			comIssueJobCard,
			winScanJobNumber,
			winScanAsnNumber,
			jobCard,
			jobInfoPopup
		},
		data() {
			return {
				issueList: [],
				contentText: {
					contentdown: '上拉加载更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				},
				pageSize: this.$pageSize,
				pageIndex: 1,
				totalCount: 0,
				loadingType: "nomore",
				checkedToday: false,
				checkedWaitTask: false,
				status: "",
				creationTimeStart: "",
				creationTimeEnd: "",
				detailOptions: [],
				detailGiveupOptions: []
			};
		},

		onShow() {
			this.getList();
		},

		onReady() {
			const rightButtonEle2 = document.getElementsByClassName('uni-page-head-btn')[2]
			this.detailOptions = getDetailOption();
			this.detailGiveupOptions = getDetailGiveupOption();
		},

		//后退按钮
		onBackPress(options) {
			if (options.from === 'navigateBack') {
				uni.navigateBack({
					delta: 1
				})
				return false;
			}
		},

		//返回首页
		onNavigationBarButtonTap(e) {
			if (e.index === 0) {
				goHome();
			} else if (e.index == 1) {
				this.$refs.filter.openFilter();
			}
		},

		methods: {
			
			getList() {
				let that = this;
				uni.showLoading({
					title: "加载中­....",
					mask: true
				});

				var params = {
					status: this.status,
					CreationTimeStart: this.CreationTimeStart,
					CreationTimeEnd: this.CreationTimeEnd
				}

				getIssueJobList(params).then(res => {
					uni.hideLoading();

					var list = res.data
					this.totalCount = res.data.length
					this.loadingType = "loadmore";
					that.issueList = list;
					updateTitle("发料任务(" + this.totalCount + ")");

				}).catch(error => {
					updateTitle("发料任务");
					uni.hideLoading();
					that.showMessage(error)
				})
			},

			getByAsnNumber(code) {
				let that = this;
				uni.showLoading({
					title: "加载中....",
					mask: true
				});

				getPurchasereceiptByAsnNumber(code).then(res => {
					uni.hideLoading();
					if (res.data.total == 0) {
						that.showMessage('未查找到' + '【' + code + '】的收货任务');
					} else if (res.data.total == 1) {
						this.$refs.scanAsnNumber.closeScanPopup();
						that.openJobDetail(res.data.list[0]);
					} else {
						this.$refs.scanAsnNumber.closeScanPopup();
						that.showItemList(res.data.list);
					}

				}).catch(error => {
					uni.hideLoading();
					that.showMessage(error);
				})
			},

			getByNumber(code) {
				let that = this;
				uni.showLoading({
					title: "加载中....",
					mask: true
				});

				getJobNewCountByNumber(code).then(res => {
					uni.hideLoading();
					if (res.data.total == 0) {
						that.showMessage('未查找到' + '【' + code + '】的收货任务');
					} else if (res.data.total == 1) {
						this.$refs.scanNumber.closeScanPopup();
						that.openJobDetail(res.data.list[0]);
					}
				}).catch(error => {
					uni.hideLoading();
					that.showMessage(error);
				})
			},

			openJobDetail(item) {
				uni.navigateTo({
					url: './issueDetail?id=' + item.id + '&status=' + item.status
				});
			},

			showItemList(itemList) {
				this.$refs.scanList.openPopup(itemList);
			},

			selectedItem(item) {
				this.openJobDetail(item);
			},

			swipeClick(e, dataContent) {
				if (e.content.text == "详情") {
					this.openjobInfoPopup(dataContent);
				} else if (e.content.text == "放弃") {
					this.$refs.comMessage.showQuestionMessage("确定要放弃当前任务?",
						res => {
							if (res) {
								var id = dataContent.id
								this.cancleJob(id);
							}
						});
				}
			},

			openjobInfoPopup(item) {
				let dataList = [{
						title: "申请单号",
						content: item.requestNumber
					},
					{
						title: "车间代码",
						content: item.workshopCode
					},
					{
						title: "允许修改库位",
						content: item.allowModifyLocation == 1 ? "是" : "否"
					},
					{
						title: "允许修改数量",
						content: item.allowModifyQty == 1 ? "是" : "否"
					},
					{
						title: "允许大于推荐数量",
						content: item.allowBiggerQty == 1 ? "是" : "否"
					},
					{
						title: "允许小于推荐数量",
						content: item.allowSmallerQty == 1 ? "是" : "否"
					},
					{
						title: "允许修改库存状态",
						content: item.allowModifyInventoryStatus == 1 ? "是" : "否"
					},
					{
						title: "允许连续扫描",
						content: item.allowContinuousScanning == 1 ? "是" : "否"
					},
					{
						title: "允许部分完成",
						content: item.allowPartialComplete == 1 ? "是" : "否"
					},
					{
						title: "允许修改批次",
						content: item.allowModifyBach == 1 ? "是" : "否"
					}
				]
				this.$refs.jobInfoPopup.openPopup(item, dataList)
			},

			cancleJob(id) {
				cancelTakeIssueJob(id).then(res => {
					if(res.data){
						this.getList("refresh")
						uni.showToast({
							title:"放弃任务成功"
						})
					}else {
						this.showMessage("放弃任务失败")
					}
				}).catch(res => {
					this.showMessage(res)
				})
			},

			switchChangeToday(state) {
				this.checkedToday = state;
				this.todayTime = creationTime;
				this.getList("refresh");
			},

			switchChangeWait(state) {
				this.checkedWaitTask = state;
				this.status = jobStatus;
				this.getList("refresh");
			},

			showMessage(message) {
				this.$refs.comMessage.showMessage(message, res => {
					if (res) {

					}
				});
			},
		}
	}
</script>

<style scoped lang="scss">

</style>
