<template>
	<view style="    padding: 30rpx;">
		<view class="dt">
			<view class="dt1">
				取货码
			</view>
			<view class="dt2">
				{{pickupCode}}
			</view>
		</view>

		<view class="all-products-body">

			<view class="all-products-item" v-for="(item,index) in goodsList" :key="index">
				<image
					:src="item.goodsImage"
					mode="scaleToFill" />

				<view class="all-products-item-content">
					<view class="all-products-item-content-t">
						{{item.goodsName}}
					</view>
					<view class="all-products-item-content-b">
						<view style="display:flex;">
							<text class="red"> 共{{item.goodsNum}}件</text>
						</view>

					</view>
				</view>
			</view>

		</view>
	</view>
</template>

<script>
	import { getOrderGoodsList} from '@/api/groupon.js'
import {
	getUserInfo,
	userEdit,
	getLogout
} from '@/api/user.js';
import {
	switchH5Login
} from '@/api/api.js';
import {
	toLogin
} from '@/libs/login.js';
import {
	mapGetters
} from "vuex";
import dayjs from "@/plugin/dayjs/dayjs.min.js";
// #ifdef MP
import authorize from '@/components/Authorize';
// #endif
import colors from '@/mixins/color.js';
export default {
	components: {
		// #ifdef APP-PLUS
		appUpdate,
		// #endif
		// #ifdef MP
		authorize
		// #endif
	},
	mixins: [colors],
	data () {
		return {
			pickupCode:'',
			goodsList:[]
		};
	},
	computed: mapGetters(['isLogin']),
	watch: {
		isLogin: {
			handler: function (newV, oldV) {
				if (newV) {
					this.getUserInfo();
				}
			},
			deep: true
		}
	},
	onLoad (e) {
		if (e.pickupCode){
			this.pickupCode=e.pickupCode
		}

		if (e.orderId){
			this.getOrderGoodsList(e.orderId)
		}

	},
	methods: {

		getOrderGoodsList(orderId){
			getOrderGoodsList(orderId).then(res =>{
				this.goodsList=res.data
			})
		},
		isNew () {
			this.$util.Tips({
				title: this.$t(`当前为最新版本`)
			});
		},
		setLang () {
			this.array.map((item, i) => {
				if (this.$i18n.locale == item.value) {
					this.setIndex = i
				}
			})
		},
		bindPickerChange (e, item) {
			this.setIndex = e.detail.value
			this.$i18n.locale = this.array[this.setIndex].value;
			uni.setStorageSync('locale', this.array[this.setIndex].value);
		},

		updateApp () {
			this.$refs.appUpdate.update(); //调用子组件 检查更新
		},
		formatSize () {
			let that = this;
			plus.cache.calculate(function (size) {
				let sizeCache = parseInt(size);
				if (sizeCache == 0) {
					that.fileSizeString = "0B";
				} else if (sizeCache < 1024) {
					that.fileSizeString = sizeCache + "B";
				} else if (sizeCache < 1048576) {
					that.fileSizeString = (sizeCache / 1024).toFixed(2) + "KB";
				} else if (sizeCache < 1073741824) {
					that.fileSizeString = (sizeCache / 1048576).toFixed(2) + "MB";
				} else {
					that.fileSizeString = (sizeCache / 1073741824).toFixed(2) + "GB";
				}
			});
		},

		initData () {
			uni.showModal({
				title: this.$t(`清除缓存`),
				content: this.$t(`确定清楚本地缓存数据吗`),
				success: (res) => {
					if (res.confirm) {
						this.clearCache()
						this.formatSize()
					} else if (res.cancel) {
						return that.$util.Tips({
							title: that.$t(`取消`)
						});
					}
				}
			});
		},
		clearCache () {
			let that = this;
			let os = plus.os.name;
			if (os == 'Android') {
				let main = plus.android.runtimeMainActivity();
				let sdRoot = main.getCacheDir();
				let files = plus.android.invoke(sdRoot, "listFiles");
				let len = files.length;
				for (let i = 0; i < len; i++) {
					let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径
					plus.io.resolveLocalFileSystemURL(filePath, function (entry) {
						if (entry.isDirectory) {
							entry.removeRecursively(function (entry) { //递归删除其下的所有文件及子目录
								uni.showToast({
									title: that.$t(`缓存清理完成`),
									duration: 2000
								});
								that.formatSize(); // 重新计算缓存
							}, function (e) {
								console.log(e.message)
							});
						} else {
							entry.remove();
						}
					}, function (e) { });
				}
			} else { // ios暂时未找到清理缓存的方法，以下是官方提供的方法，但是无效，会报错
				plus.cache.clear(function () {
					uni.showToast({
						title: that.$t(`缓存清理完成`),
						duration: 2000
					});
					that.formatSize();
				});
			}
		},
		/**
		 * 授权回调
		 */
		onLoadFun: function () {
			this.getUserInfo();
		},
		// 授权关闭
		authColse: function (e) {
			this.isShowAuth = e
		},
		/**
		 * 小程序设置
		 */
		Setting: function () {
			uni.openSetting({
				success: function (res) { }
			});
		},
		switchAccounts: function (index) {
			let userInfo = this.switchUserInfo[index],
				that = this;
			that.userIndex = index;
			if (that.switchUserInfo.length <= 1) return true;
			if (userInfo === undefined) return that.$util.Tips({
				title: that.$t(`切换的账号不存在`)
			});
			if (userInfo.user_type === 'h5') {
				uni.showLoading({
					title: that.$t(`正在切换中`)
				});
				switchH5Login().then(res => {
					uni.hideLoading();
					that.$store.commit("LOGIN", {
						'token': res.data.token,
						'time': this.$Cache.strTotime(res.data.expires_time) - this.$Cache.time()
					});
					that.getUserInfo();

				}).catch(err => {
					uni.hideLoading();
					return that.$util.Tips({
						title: err
					});
				})
			} else {
				that.$store.commit("LOGOUT");
				uni.showLoading({
					title: that.$t(`正在切换中`)
				});
				toLogin();
			}
		},
		/**
		 * 退出登录
		 *
		 */
		outLogin: function () {
			let that = this;
			if (that.loginType == 'h5') {
				uni.showModal({
					title: that.$t(`提示`),
					content: that.$t(`确认退出登录`),
					success: function (res) {
						if (res.confirm) {
							getLogout()
								.then(res => {
									// uni.clearStorage()
									that.$store.commit("LOGOUT");
									uni.reLaunch({
										url: '/pages/index/index'
									})
								})
								.catch(err => { });
						} else if (res.cancel) { }
					}
				});
			}
		},
		/**
		 * 获取用户详情
		 */
		getUserInfo: function () {
			let that = this;
			getUserInfo().then(res => {
				that.$set(that, 'userInfo', res.data);
				let switchUserInfo = res.data.switchUserInfo || [];
				for (let i = 0; i < switchUserInfo.length; i++) {
					if (switchUserInfo[i].uid == that.userInfo.uid) that.userIndex = i;
					// 切割h5用户；user_type状态：h5、routine（小程序）、wechat（公众号）；注：只有h5未注册手机号时，h5才可和小程序或是公众号数据想通；
					//#ifdef H5
					if (
						!that.$wechat.isWeixin() &&
						switchUserInfo[i].user_type != "h5" &&
						switchUserInfo[i].phone === ""
					)
						switchUserInfo.splice(i, 1);
					//#endif
				}
				that.$set(that, "switchUserInfo", switchUserInfo);
			});
		},
		/**
		 * 上传文件
		 *
		 */
		uploadpic: function () {
			let that = this;
			this.canvasStatus = true
			that.$util.uploadImageChange('upload/image', (res) => {
				let userInfo = that.switchUserInfo[that.userIndex];
				if (userInfo !== undefined) {
					that.userInfo.avatar = res.data.url;
				}
				that.switchUserInfo[that.userIndex] = userInfo;
				that.$set(that, 'switchUserInfo', that.switchUserInfo);
				this.canvasStatus = false
			}, (res) => {
				this.canvasStatus = false
			}, (res) => {
				this.canvasWidth = res.w
				this.canvasHeight = res.h
			});
		},

		/**
		 * 提交修改
		 */
		formSubmit: function (e) {
			let that = this,
				value = e.detail.value,
				userInfo = that.switchUserInfo[that.userIndex];
			if (!value.nickname) return that.$util.Tips({
				title: that.$t(`请输入姓名`)
			});
			value.avatar = this.userInfo.avatar;
			userEdit(value).then(res => {
				return that.$util.Tips({
					title: res.msg,
					icon: 'success'
				}, {
					tab: 3,
					url: 1
				});
			}).catch(msg => {
				return that.$util.Tips({
					title: msg || that.$t(`保存失败`)
				}, {
					tab: 3,
					url: 1
				});
			});
		}
	}
}
</script>

<style scoped lang="scss">
.cartcolor {
	color: var(--view-theme);
	border: 1px solid var(--view-theme);
}

.all-products-body {
	background: white;
	padding-top: 30rpx;
	.all-products-item {
		display: flex;
		margin-bottom: 20rpx;

		image {
			width: 128rpx;
			height: 128rpx;
			margin-right: 20rpx;
			border: 2rpx solid #FFFFFF;
		}

		.all-products-item-content {
			flex-grow: 1;
			display: flex;
			flex-direction: column;


			.all-products-item-content-t {
				flex: 1;
				font-size: 24rpx;
				font-weight: 400;
				color: #333333;
				line-height: 40rpx;
			}

			.all-products-item-content-b {
				flex: 1;
				font-size: 28rpx;
				font-weight: 500;
				line-height: 80rpx;
				display: flex;
				justify-content: space-between;

				.red {
					display: block;
					font-size: 28rpx;
					font-weight: 500;
					color: #999999 !important;
					margin-right: 10rpx;
				}

				.line-thr {
					display: block;
					font-size: 28rpx;
					font-weight: 400;
					color: #999999;
					text-decoration: line-through;
				}

				.count {
					font-size: 24rpx;
					font-weight: 400;
					color: #999999;
					margin-right: 30rpx;
				}
			}
		}
	}
}

.dt {
	height: 148rpx;
	background: #FFFFFF;
	padding: 20rpx;
	text-align: center;
margin-bottom: 20rpx;
	.dt1 {
		height: 44rpx;
		font-size: 28rpx;
		font-weight: 400;
		color: #333333;
		line-height: 44rpx;
		margin-bottom: 20rpx;
	}

	.dt2 {
		height: 48rpx;
		font-size: 32rpx;
		font-weight: 500;
		color: #333333;
		line-height: 48rpx;
	}
}
</style>
