<template>
	<view class="">
		<com-empty-view v-if="recordList.length==0"></com-empty-view>
		<record-filter ref="filter" @switchChangeToday="switchChangeToday" @onScanNumber="getScanNumber"
			 :checkedToday="checkedToday">
		</record-filter>
		<view v-if="recordList.length>0">

			<uni-swipe-action ref="swipeAction">
				<view v-for="(item, index) in recordList" :key="index">
					<uni-swipe-action-item :right-options="detailOptions" @click="swipeClick($event,item)">
						<comRecordCard :dataContent="item" @click='openJobDetail(item)'></comRecordCard>
					</uni-swipe-action-item>
				</view>
			</uni-swipe-action>

			<uni-load-more :status="loadingType" v-if="recordList.length>0" />
			<recordInfoPopup ref='recordInfoPopup'></recordInfoPopup>
			<recordListPopup ref="recordListPopup" @selectedItem="selectedItem"></recordListPopup>
		</view>
		<com-message ref="comMessage"></com-message>
	</view>
</template>

<script>
	import {
		getSupplierDeliverRecordList,
	} from '@/api/request2.js';

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

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

	import comEmptyView from '@/mycomponents/common/comEmptyView.vue'
	import recordFilter from '@/mycomponents/record/recordFilter.vue'

	import comRecordCard from '@/pages/supplierDeliver/coms/comRecordCard.vue'
	import recordListPopup from '@/pages/supplierDeliver/coms/recordListPopup.vue'
	import recordInfoPopup from '@/pages/supplierDeliver/coms/recordInfoPopup.vue'

	import comMessage from '@/mycomponents/common/comMessage.vue'

	export default {
		name: 'receipt',
		components: {
			comEmptyView,
			recordFilter,
			comRecordCard,
			recordInfoPopup,
			recordListPopup,
			comMessage,
		},
		data() {
			return {
				recordList: [],
				pageNo: 1,
				pageSize: 10,
				totalCount: 0,
				loadingType: "nomore",
				checkedToday: false,
				todayTime: "",
				detailOptions: [],
				detailGiveupOptions: [],
				currentItem: {}
			};
		},

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

		onReady() {
			const rightButtonEle2 = document.getElementsByClassName('uni-page-head-btn')[2]
			this.detailOptions = getDetailOption();
		},
		onReachBottom() {
			//避免多次触发
			if (this.loadingType == 'loading' || this.loadingType == 'nomore') {
				return;
			}
			this.getList("more");
		},

		onPullDownRefresh() {
			this.getList('refresh');
		},

		//后退按钮
		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(type) {
				let that = this;
				uni.showLoading({
					title: "加载中­....",
					mask: true
				});
				this.loadingType = "loading";
				if (type === "refresh") {
					this.pageNo = 1;
					this.recordList = [];
				}
				var filters = []
				if (this.checkedToday) {
					filters.push({
						column: "request_time",
						action: "betweeen",
						value: this.todayTime
					})
				}

				var params = {
					filters: filters,
					pageNo: this.pageNo,
					pageSize: this.pageSize,
				}

				getSupplierDeliverRecordList(params).then(res => {
					uni.hideLoading();
					if (type === "refresh") {
						uni.stopPullDownRefresh();
					}

					var list = res.data.list;
					this.totalCount = res.data.total
					this.loadingType = "loadmore";
					if (list == null || list.length == 0) {
						this.loadingType = "nomore";
						return;
					}
					this.recordList = type === "refresh" ? list : this.recordList.concat(list);
					this.pageNo++;
					updateTitle("供应商发货记录(" + this.totalCount + ")");
				}).catch(error => {
					if (type === "refresh") {
						uni.stopPullDownRefresh();
					}
					updateTitle("供应商发货记录");
					this.loadingType = "";
					uni.hideLoading();
					that.showMessage(error)

				})
			},


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

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

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

			swipeClick(e, dataContent) {
				if (e.content.text == "详情") {
					this.openrecordInfoPopup(dataContent);
				}
			},

			openrecordInfoPopup(item) {
				this.$refs.recordInfoPopup.openPopup(item)
			},
			
			switchChangeToday(state, creationTime) {
				this.checkedToday = state;
				this.todayTime = creationTime;
				this.getList("refresh");
			},

			getScanNumber(code) {
				this.getDataListByType(code)
			},
			getDataListByType(code) {
				let that = this;
				uni.showLoading({
					title: "加载中....",
					mask: true
				});
				var filters = []
				filters.push({
					column: "number",
					action: "==",
					value: code
				})

				var params = {
					filters: filters,
					pageNo: 1,
					pageSize: 100,
				}
				getSupplierDeliverRecordList(params).then(res => {
					uni.hideLoading();
					if (res.data.list.length == 0) {
						that.showMessage('未查找到' + '【' + code + '】的收货任务');
					} else if (res.data.list.length == 1) {
						that.openJobDetail(res.data.list[0]);
					} else {
						if (type == "asnNumber") {
							that.showItemList(res.data.list);
						}
					}
				}).catch(error => {
					uni.hideLoading();
					that.showMessage(error);
				})
			},

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

					}
				});

			},
		}
	}
</script>

<style scoped lang="scss">

</style>
