<template>
	<view style="    padding: 30rpx;">
		<view>
			<img style="    height: 250rpx;
			    width: 100%;" src="/static/image/tihuoguanli.png" alt="预览" @click="scanCode">
		</view>

		<view class="u-demo-block__content" style="margin-top: 15px;">
			<u-input placeholder="输入取货码" class="slot-ipt" v-model="pickupCode">
				<template slot="suffix">
					<u-button class="slot-btn" text="确认核销" type="success" size="mini" @click="confirmOrder"></u-button>
				</template>

			</u-input>
		</view>


		<view class="tool_list">
			<view class="title" style="font-weight: 500;padding-left: 30rpx;">
				&nbsp;&nbsp;核销记录
			</view>
			<mescroll-item ref="MescrollItem" :i="index" :index="tabIndex" :height="height" v-for="(tab, index) in tabsList"
				:key="index">
			</mescroll-item>
		</view>

	</view>
</template>

<script>
import MescrollItem from "./module/mescrollUni-item.vue";
import { confirmOrder } 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: {
		MescrollItem,
		// #ifdef APP-PLUS
		appUpdate,
		// #endif
		// #ifdef MP
		authorize
		// #endif
	},
	mixins: [colors],
	data () {
		return {
			height: '750rpx',
			pickupCode: '',
			tabIndex: 0,
			tabsList: [{ name: "核销记录" }]

		};
	},
	computed: mapGetters(['isLogin']),
	watch: {
		isLogin: {
			handler: function (newV, oldV) {
				if (newV) {
					this.getUserInfo();
				}
			},
			deep: true
		}
	},
	onShow () {
		let sysInfo = uni.getSystemInfoSync()
		this.height = sysInfo.windowHeight - (sysInfo.screenWidth / 750) * (530) + 'px';
		console.log(this.height)
	},
	onLoad () {
		if (this.isLogin) {
			// this.getUserInfo();
			// #ifdef APP-PLUS
			// this.formatSize()
			// 获取版本号
			// plus.runtime.getProperty(plus.runtime.appid, (inf) => {
			// 	console.log(inf.version)
			// 	this.version = inf.version;
			// });
			// #endif
			// this.setLang();
		} else {
			toLogin();
		}
	},
	methods: {

		scanCode () {
			let that = this;
			uni.scanCode({
				onlyFromCamera: true,
				success: function (res) {
					that.pickupCode = res.result;
					console.log("scan code success ...", res.result)
				},
				fail () {
					console.log("scan code fail ...")
				},
				complete () {
					console.log("scan code complete ...")
				}
			})
		},
		swiperChange: function (e) {
			this.currentSwiper = e.detail.current
		},


		confirmOrder () {
		  let that = this;
			confirmOrder(this.pickupCode).then(res => {
				uni.$u.toast(res.msg)
        that.$refs.MescrollItem[that.tabIndex].downCallback();
			}).catch(err => {
				uni.$u.toast(err)
			})
		},

		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  lang="scss">
.cartcolor {
	color: var(--view-theme);
	border: 1px solid var(--view-theme);
}

.personal-data .wrapper {
	margin: 10rpx 0;
	background-color: #fff;
	padding: 36rpx 30rpx 13rpx 30rpx;
}

.personal-data .wrapper .title {
	margin-bottom: 30rpx;
	font-size: 32rpx;
	color: #282828;
}

.group-buying-data {
	position: relative;
	width: 100%;
	height: 200rpx;
	padding: 40rpx;
	margin: 0 auto;
	background-image: url('http://www.gzzzyd.com/groupon/regiment/提货管理@2x.png');
	background-size: 100% auto;
	background-color: var(--view-theme);

	.group-title {
		font-size: 32rpx;
		font-weight: 500;
		color: #B22338;
		line-height: 44rpx;
		height: 44rpx;
	}



	.group-content {
		display: flex;

		.group-content-l {

			text-align: center;
			flex: 1;
			border-color: #333333;
			border-right: 1px solid dashed;
		}

		.group-content-r {
			text-align: center;
			flex: 1
		}

		.content-num {
			margin-top: 20rpx;
			height: 48rpx;
			font-size: 40rpx;
			font-weight: bold;
			color: #B22338;
			line-height: 48rpx;
		}

		.content-info {
			margin-top: 20rpx;
			text-align: center;
			width: 100%;
			height: 36rpx;
			font-size: 24rpx;
			font-weight: 400;
			color: #CF7986;
			line-height: 36rpx;
		}

	}
}

.tool_list,
.footer {
	background: #fff;
	border-radius: 16rpx;
	margin-top: 20rpx;
}

.footer {
	height: 100rpx;

	button {
		height: 100%;
		font-size: 28rpx;
		font-weight: 400;
		color: #999999;
		line-height: 60rpx;
	}
}

/deep/ .tool_list .u-line,
.my_order .u-line {
	display: none !important;
}


.u-slot-title {
	line-height: 60rpx;

	font-size: 28rpx;
	font-weight: 400;
	color: #333333;

	text {
		vertical-align: text-bottom;
	}

	image {

		margin-right: 20rpx;
		transform: translateY(10rpx);
		width: 60rpx;
		height: 60rpx;

	}
}

.slot-ipt {
	background-color: rgba(255, 255, 255, 1.000000);
	border-radius: 46rpx;
	width: 686rpx;
	border: 1px solid rgba(178, 35, 56, 1);
}

.slot-btn {
	background-color: rgba(178, 35, 56, 1.000000);
	border-radius: 46rpx;
	padding: 20rpx 40rpx 20rpx 40rpx;
}

.tool_list {
	.title {
		/*padding: 0 40rpx;*/
		/*padding-top: 20rpx;*/
		height: 100rpx;
		font-size: 36rpx;
		font-weight: bolder;
		color: #111111;
		line-height: 100rpx;
	}
}

/deep/ .tool_list .u-line,
.my_order .u-line {
	display: none !important;
}


.u-slot-title {
	line-height: 60rpx;

	font-size: 28rpx;
	font-weight: 400;
	color: #333333;

	text {
		vertical-align: text-bottom;
	}

	image {

		margin-right: 20rpx;
		width: 40rpx;
		height: 40rpx;
		vertical-align: super;

	}
}

::v-deep .u-input {
	padding-left: 20rpx !important;
	background: white;
	border-radius: 46rpx !important;
	border: 1px solid #B22338 !important;
	padding-top: 0 !important;
	padding-bottom: 0 !important;
	position: relative;
	height: 80rpx !important;
}

::v-deep .u-input button {
	position: relative !important;
	right: 0 !important;
	transform: translateX(20rpx);
	background: #B22338 !important;

	text {
		font-size: 15px !important;
	}

	color: white;
	width: 200rpx !important;
	height: 80rpx !important;
	border: none !important;
	display: flex !important;
	justify-content: flex-start !important;
	padding-left: 40rpx !important;
	border-radius: 0 46rpx 46rpx 0 !important;
	border: 1px solid #B22338 !important;
	padding-top: 0 !important;
	padding-bottom: 0 !important;
}

.bord {
	border: 1px solid red;
}
</style>
