<template>
	<view>
		<form @submit="formSubmit">
			<view class='personal-data' :style="colorStyle">
				<view class='list'>
					<view class="item avatar-area" >
						<view class="avater_button_view">
							<button class="avater_button" open-type="chooseAvatar" @chooseavatar="chooseavatar">
								<u-avatar :src="userInfo.avatar" style="margin: auto;" size="82" />
							</button>
						</view>
<!--						<u-avatar :src="userInfo.avatar" style="margin: auto;" size="82" @click="albumClick" />-->
						<text class="help_info" selectable="false" space="false" decode="false">
							点击修改头像
						</text>
					</view>

					<view class='item acea-row row-between-wrapper'>
						<view>昵称</view>
						<view class='input'>
							<!-- <text>{{ userInfo.realName }}</text> -->
							<input type="nickname"  name='name' :value='userInfo.name' />
						</view>
					</view>
					<view class='item acea-row row-between-wrapper'>
						<view>性别</view>
						<view class='input'>


							<radio-group style="height: 39rpx;line-height: 39rpx;" name="sex">
								<label style="margin-right: 30rpx;">
									<radio style="margin-right: 10rpx;" value="1" :checked="sex === '1'" /><text>男</text>
								</label>
								<label>
									<radio style="margin-right: 10rpx;" value="0" :checked="sex === '0'" /><text>女</text>
								</label>
							</radio-group>
						</view>
					</view>
					<view class='item acea-row row-between-wrapper'>
						<view>手机号码</view>
<!--						<navigator url="/pages/users/user_phone/index" hover-class="none" class=" input acea-row row-between-wrapper" v-if="!userInfo.phone">-->
<!--							{{ $t(`点击绑定手机号`) }}<text class="  iconfont icon-xiangyou"></text>-->
<!--						</navigator>-->
						<view class='input acea-row row-between-wrapper '>
							<input type='text' disabled='true' name='phone' :value='userInfo.phone' class='id ' />
<!--							<text class='iconfont icon-suozi'></text>-->
						</view>
					</view>

<!--					<view class="item acea-row row-between-wrapper" v-if="userInfo.phone">-->
<!--						<view>{{ $t(`更换手机号码`) }}</view>-->
<!--						<navigator url="/pages/users/user_phone/index?type=1" hover-class="none" class="input  acea-row row-between-wrapper">-->
<!--							{{ $t(`点击更换手机号码`) }}<text class="iconfont icon-xiangyou"></text>-->
<!--						</navigator>-->
<!--					</view>-->

					<view class='item acea-row row-between-wrapper' v-if="currentRole == 'leader'">
						<view>二维码</view>

						<view v-if="userInfo.wxQrcode" class='input    acea-row row-between-wrapper'>
							<view class="qr-cord-box">
								<uni-badge class="uni-badge-left-margin" text="X" absolute="rightTop" size="small" @click="clickBadge">
									<image class="image" :src="userInfo.wxQrcode" ></image>
								</uni-badge>
							</view>

						</view>

						<view v-else class='input    acea-row row-between-wrapper'>
							<view class="qr-cord-box flex-col">
								<image class="add-file-image " src="../../../static/images/addFile.png"  @click="uploadQrCord"></image>
								<text>点击上传二维码</text>
							</view>

						</view>


					</view>



					<!-- #ifdef APP-PLUS -->
					<view class="item acea-row row-between-wrapper" v-if="userInfo.phone">
						<view>{{ $t(`密码`) }}</view>
						<navigator url="/pages/users/user_pwd_edit/index" hover-class="none" class="input">
							{{ $t(`点击修改密码`) }}<text class="iconfont icon-xiangyou"></text>
						</navigator>
					</view>
					<view class="item acea-row row-between-wrapper" @click="initData">
						<view>{{ $t(`缓存大小`) }}</view>
						<view class="input">
							{{ fileSizeString }}<text class="iconfont icon-xiangyou"></text>
						</view>
					</view>
					<view class="item acea-row row-between-wrapper" @click="updateApp">
						<view>{{ $t(`当前版本`) }}</view>
						<view class="input">
							{{ version }}<text class="iconfont icon-xiangyou"></text>
						</view>
					</view>
					<!-- #endif -->

				</view>
				<button class='modifyBnt' formType="submit">保存</button>

			</view>
		</form>
		<canvas canvas-id="canvas" v-if="canvasStatus"
			:style="{ width: canvasWidth + 'px', height: canvasHeight + 'px', position: 'absolute', left: '-100000px', top: '-100000px' }"></canvas>

	</view>
</template>

<script>
import {
	getUserInfo,
	userEdit,
	getLogout
} from '@/api/user.js';
import {
	switchH5Login
} from '@/api/api.js';
import {
	toLogin
} from '@/libs/login.js';
import {
	mapState,
	mapGetters,
	mapMutations
} 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 {
			currentRole: '',
			sex: '1',
			userInfo: {},
			loginType: 'h5', //app.globalData.loginType
			userIndex: 0,
			switchUserInfo: [],
			isAuto: false, //没有授权的不会自动授权
			isShowAuth: false, //是否隐藏授权
			canvasWidth: "",
			canvasHeight: "",
			canvasStatus: false,
			fileSizeString: '',
			version: '',
			array: [{
				name: '中文',
				value: 'zh_cn'
			},
			{
				name: 'English',
				value: 'en_us'
			},
			{
				name: '日本語',
				value: 'ja_jp'
			},
			{
				name: 'Français',
				value: 'fr_fr'
			},
			{
				name: 'ไทย',
				value: 'th_th'
			},
			{
				name: 'Italiano',
				value: 'it_it'
			},
			{
				name: 'Монгол',
				value: 'mn_mn'
			},
			{
				name: '한국인',
				value: 'ko_kr'
			},
			{
				name: 'Tiếng Việt',
				value: 'vi_vn'
			},
			{
				name: '中文(繁体)',
				value: 'zh_ft'
			},
			],
			setIndex: 0,
		};
	},
	computed: {
		...mapGetters({
			cartNum: 'cartNum',
			isLogin: 'isLogin',
			newCurrentRole:'newCurrentRole'
		})
	},
	watch: {
		isLogin: {
			handler: function (newV, oldV) {
				if (newV) {
					this.getUserInfo();
				}
			},
			deep: true
		}
	},
	onShow: function () {
		this.currentRole=this.$store.getters.newCurrentRole;
	},
	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: {

		chooseavatar(e){
			let _this = this;
			console.log(e)
			this.$util.weixinAvatarUpload(e.detail.avatarUrl,
					function (res) {
						_this.userInfo.avatar = res.data.link
				console.log("上传文件成功+++++++++++++++++++",res)
			})
		},

		/**
		 * 上传微信二维码
		 */
		uploadQrCord(){
			let _this = this;
			this.$util.uploadImageOne({
				name: "file"
			}, function(res) {
				console.log("上传文件成功+++++++++++++++++++",res.data.link)
				_this.userInfo.wxQrcode = res.data.link
			});

		},

		clickBadge(){
			this.userInfo.wxQrcode ='';
			console.log("点击了XX")
		},
		// 相册选择图片
		albumClick () {
			let _this = this;
			this.$util.uploadImageOne({
				name: "file"
			}, function(res) {
				console.log("上传文件成功+++++++++++++++++++",res.data.link)
				_this.userInfo.avatar = res.data.link
			});
		},
		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();
			}
		},
		/**
		 * 获取用户详情
		 */
		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);
			});
		},

		/**
		 * 提交修改
		 */
		formSubmit: function (e) {
			let that = this,
				value = e.detail.value;
			if (!value.name) return that.$util.Tips({
				title: that.$t(`请输入昵称`)
			});
			value.avatar = this.userInfo.avatar;
			value.wxQrcode = this.userInfo.wxQrcode;
			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">
	.avater_button_view{
		display: flex;
		flex-direction: row;
		justify-content: center;
	}
	.avater_button{
		width: 82px;
		height: 82px;
		border-radius: 41px;
	}
.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;
}

.personal-data .wrapper .wrapList .item {
	width: 690rpx;
	height: 160rpx;
	background-color: #f8f8f8;
	border-radius: 20rpx;
	margin-bottom: 22rpx;
	padding: 0 30rpx;
	position: relative;
	border: 2rpx solid #f8f8f8;
	box-sizing: border-box;
}

.personal-data .wrapper .wrapList .item.on {
	border-color: #e93323;
	border-radius: 20rpx;
	background-image: url("data:image/png;base64,[encoded data, 4916 characters]");
	background-size: 100% 100%;
	background-color: #fff9f9;
	background-repeat: no-repeat;
}

.personal-data .wrapper .wrapList .item .picTxt {
	width: 445rpx;
}

.personal-data .wrapper .wrapList .item .picTxt .pictrue {
	width: 96rpx;
	height: 96rpx;
	position: relative;
}

.personal-data .wrapper .wrapList .item .picTxt .pictrue image {
	width: 100%;
	height: 100%;
	border-radius: 50%;
}

.personal-data .wrapper .wrapList .item .picTxt .pictrue .alter {
	width: 30rpx;
	height: 30rpx;
	border-radius: 50%;
	position: absolute;
	bottom: 0;
	right: 0;
}

.personal-data .wrapper .wrapList .item .picTxt .text {
	width: 325rpx;
}

.personal-data .wrapper .wrapList .item .picTxt .text .name {
	width: 100%;
	font-size: 30rpx;
	color: #282828;
}

.personal-data .wrapper .wrapList .item .picTxt .text .phone {
	font-size: 24rpx;
	color: #999;
	margin-top: 10rpx;
}

.personal-data .wrapper .wrapList .item .bnt {
	font-size: 24rpx;
	background-color: #fff;
	border-radius: 27rpx;
	width: 140rpx;
	height: 54rpx;
	border: 2rpx solid #e93323;
}

.personal-data .wrapper .wrapList .item .currentBnt {
	position: absolute;
	right: 0;
	top: 0;
	font-size: 26rpx;
	background-color: rgba(233, 51, 35, 0.1);
	width: 140rpx;
	height: 48rpx;
	border-radius: 0 20rpx 0 20rpx;
}

.personal-data .list {
	margin-top: 15rpx;
	background-color: #fff;
}

.personal-data .list .item {
	padding: 30rpx 30rpx 30rpx 0;
	border-bottom: 1rpx solid #f2f2f2;
	margin-left: 30rpx;
	font-size: 32rpx;
	color: #282828;
}

.personal-data .list .item .phone {
	width: 160rpx;
	height: 56rpx;
	font-size: 24rpx;
	color: #fff;
	line-height: 56rpx;
	border-radius: 32rpx
}

.personal-data .list .item .pictrue {
	width: 88rpx;
	height: 88rpx;
}

.personal-data .list .item .pictrue image {
	width: 100%;
	height: 100%;
	border-radius: 50%;
}

.personal-data .list .item .input {
	width: 480rpx;
	text-align: left;
	color: #868686;
}

.personal-data .list .item .input .id {
	width: 365rpx;
}

.personal-data .list .item .input .iconfont {
	font-size: 35rpx;
}

.personal-data .modifyBnt {
	width: 85%;

	margin: auto;
	margin-top: 50rpx;
	height: 84rpx;
	background: #B42A3E;
	border-radius: 8rpx;
	font-size: 32rpx;
	color: #fff;
	text-align: center;
	line-height: 84rpx;
}

.personal-data .logOut {
	font-size: 32rpx;
	text-align: center;
	width: 690rpx;
	height: 90rpx;
	border-radius: 45rpx;
	margin: 30rpx auto 0 auto;
}

.avatar-box {
	width: 96rpx;
	height: 96rpx;

	image {
		width: 100%;
		height: 100%;
		border-radius: 50%;
	}
}

.avatar-area {
	background: rgb(249, 249, 249);
	padding: 0;
	margin: 0 !important;
	width: 100%;
	text-align: center;
	height: 300rpx;

	image {}

	/deep/ .u-avatar {
		margin: auto !important;
	}

	.help_info {
		display: block;
		margin: 20rpx 0;
		height: 34rpx;
		font-size: 24rpx;
		font-weight: 400;
		color: #999999;
		line-height: 34rpx;
	}
}

	.bord{
		border:1px solid red;
	}
	.qr-cord-box{
		width: 200rpx;
		height: 200rpx;
		display: flex;
		flex-direction: column;
		border: 1px solid rgba(238, 238, 238, 1);
		.image{
			width: 200rpx;
			height: 200rpx;
		}
		.add-file-image{
			width: 100rpx;
			height: 100rpx;
			margin-left: 50rpx;
			margin-top: 20rpx;
		}
		text{
			overflow-wrap: break-word;
			color: rgba(153, 153, 153, 1);
			font-size: 28rpx;
			font-weight: NaN;
			text-align: center;
			white-space: nowrap;
			line-height: 40rpx;
			margin-top: 20rpx;
		}
	}
</style>
