<template>
	<mescroll-body ref="mescrollRef" @init="mescrollInit" :down="downOption" @down="downCallback" @up="upCallback">
		<view class="content">
			<view class="content1" v-for="(item,index) in dataList" :key="index">
				<view class="top">
					<view class="left">{{item.warehouseName}}</view>
					<view class="right">{{item.validityDate}}</view>
				</view>
				<view class="hz-content">
					<view class="row1">
						<view class="hz-name">
							{{item.shipperName}}
						</view>
						<view class="car">
							({{item.carList.length}}车)
						</view>
					</view>
					<view class="car-list">
						<view v-for="(item1,index) in item.carList" class="number">
							{{item1}}
						</view>
					</view>
				</view>
				<view class="del-row">
					<button class="del" @click='del(item)'>删除</button>
				</view>
			</view>
			<u-modal v-model="isShowAlert1" :title-style="{fontSize: '18px',fontWeight:'500'}"
				:content-style="{fontSize: '14px',fontWeight:'400'}" confirm-color='#22C572' confirm-text='确定'
				title='登录提示' showCancelButton='false' :content="content1" @confirm="alertBtn" @cancel="cancelClick">
			</u-modal>
			<u-modal v-model="isShowAlert" :title-style="{fontSize: '18px',fontWeight:'500'}"
				:content-style="{fontSize: '14px',fontWeight:'400'}" confirm-color='#22C572' confirm-text='确定'
				title='登录提示' showCancelButton='false' :content="content" @confirm="alertBtn" @cancel="cancelClick">
			</u-modal>
		</view>
	</mescroll-body>
</template>
<script>
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	import {
		mapState
	} from 'vuex';

	export default {
		mixins: [MescrollMixin], // 使用mixin (在main.js注册全局组件)
		components: {

		},
		data() {
			return {
				downOption: {
					auto: false //是否在初始化后,自动执行downCallback; 默认true
				},
				dataList: [],
				validityDate: '',
				id: '',
				warehouseName: '',
				isShowAlert: false,
				isShowAlert1: false,
				content: '当前登入信息验证失败，是否重新登录？',
				content1: '确定删除送粮记录？',
				dataList: []
			}
		},
		onLoad() {

		},
		// #ifndef MP
		onNavigationBarButtonTap(e) {
			const index = e.index;
			if (index === 0) {
				this.navTo('/pages/set/set');
			} else if (index === 1) {
				// #ifdef APP-PLUS
				const pages = getCurrentPages();
				const page = pages[pages.length - 1];
				const currentWebview = page.$getAppWebview();
				currentWebview.hideTitleNViewButtonRedDot({
					index
				});
				// #endif
				uni.navigateTo({
					url: '/pages/notice/notice'
				})
			}
		},
		// #endif
		computed: {
			...mapState(['hasLogin', 'userInfo']),
		},
		onShow() {
			this.$api.doRequest('get', '/commonUser/api/checkSession').then(res => {
				console.log("checkSession", res)
				if (res.data.data == "INVALID") {
					this.isShowAlert = true;
				}
			})
			console.log("hasLogin", this.hasLogin)
		},
		methods: {
			getData(size, page, type) {
				let _obj = {
					commonId: uni.getStorageSync("userInfo").id,
					pageSize: size,
					currentPage: page
				}
				this.$api.doRequest('get', '/grainDeliveryRegistration/selectGrainDeliveryRegistration', _obj)
					.then(res => {
						if (res.data.code == 200) {
							debugger
							let curPageData = []
							for (let i = 0; i < res.data.data.records.length; i++) {
								let _data = res.data.data.records[i];
								//判断是记录是否失效
								let _isUserd = function() {
									debugger
									let _startTime = new Date((_data.validityDate + ' 00:00:00')).getTime();
									let _endTime = Date.parse(new Date());
									if (_endTime - _startTime > 0) {
										_data.validityDate = '已失效'
									} else {
										_data.validityDate = _data.validityDate + ' 0时前有效'
									}
								}()
								let _carNumberList = _data.carNo.split(',')
								let __obj = {
									warehouseName: _data.warehouseName,
									id: _data.id,
									shipperName: _data.shipperName,
									carList: _carNumberList,
									validityDate: _data.validityDate

								}
								curPageData.push(__obj)
							}
							if (type == 'down') {
								this.mescroll.endSuccess();
								this.dataList = []
							} else {
								this.mescroll.endBySize(curPageData.length, res.data.data
									.total); //必传参数(当前页的数据个数, 总数据量)
							}
							this.dataList = this.dataList.concat(curPageData);
						}
					})
			},
			/*下拉刷新的回调 */
			downCallback() {
				this.getData(10, 1, 'down')
			},
			/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
			upCallback(page) {
				//联网加载数据
				this.getData(page.size, page.num, 'up')
			},
			del(row) {
				this.isShowAlert1 = true
				this.id = row.id
				// 删除成功后删除页面当前行
			},

			navTo(url) {
				if (!this.hasLogin) {
					url = '/pages/public/login';
				}
				uni.navigateTo({
					url
				})
			},
			alertBtn() {
				this.$api.doRequest('post', '/grainDeliveryRegistration/api/deleteInfo', {
					id: this.id
				}).then(res => {
					if (res.data.code == 200) {
						for (let i = 0; i < this.dataList.length; i++) {
							if (this.id == this.dataList[i].id) {
								this.$api.msg('删除成功！')
								this.dataList.splice(i, 1)
							}
						}
					} else {
						this.$api.msg('删除失败！')
					}
				})

			},
			cancelClick() {
				this.isShowAlert = false
			}
		}
	}
</script>
<style lang='scss' scoped>
	page {
		background: #F5F6FA;
	}


	.content1 {
		background: white;
		margin: 20rpx;
		padding: 20rpx;
		border-radius: 20rpx;

		.top {
			display: flex;
			justify-content: space-between;

			.left {
				font-size: 32rpx;
				font-weight: 700;
			}

			.right {
				font-size: 28rpx;
				color: #FD714F;
			}
		}

		.hz-content {
			background: #F5F6FA;
			margin-top: 20rpx;
			padding: 20rpx;

			.row1 {
				display: flex;
				align-items: center;
				margin-bottom: 30rpx;

				.hz-name {
					font-size: 36rpx;
					font-weight: 700;
					margin-right: 4rpx;
				}

				.car {
					font-size: 24rpx;
					color: #676E80;

				}
			}

			.car-list {
				.number {
					display: inline-block;
					width: 23%;
					background: #FFFFFF;
					border-radius: 23px;
					font-size: 24rpx;
					color: #676E80;
					padding: 10rpx 20rpx;
					margin: 1%;
					text-align: center;
					box-sizing: border-box;
				}
			}

		}

		.del-row {
			display: flex;
			justify-content: flex-end;
			align-items: center;
			margin-top: 20rpx;

			.del {
				background: #FFFFFF;
				border-radius: 33px;
				border: 1px solid #CDCDCD;
				font-size: 28rpx;
				color: #333333;
				margin: 0;
				padding: 0 30rpx;
				height: 100%;
			}

			/deep/.uni-button {}
		}

	}
</style>
