<template>
	<view class="page">
		<uni-nav-bar @clickLeft="goto(1, 1)" left-icon="back"  :border="false" :shadow="false" :fixed="true"
		:title="$t('personal.title')" backgroundColor="#fff" :statusBar="true"></uni-nav-bar>
		<view class="cell_list" @click="dialogVisible = true">
			<view class="cell_left txt">{{$t('personal.text1')}}</view>
			<view class="cell_right arrow">
				<image :src="avatar" mode="aspectFill"></image>
			</view>
		</view>
		<z-prompt :value="nickname" options="nickname" text="Please enter a nickname" @confirm="onNameChange" :options="popupOptions">
			<view class="cell_list">
				<view class="cell_left txt">{{$t('personal.text2')}}</view>
				<view class="cell_right arrow">{{ nickname }}</view>
			</view>
		</z-prompt>
		<view class="cell_list">
			<view class="cell_left txt">{{$t('personal.text3')}}</view>
			<view class="cell_right ">{{id}}</view>
		</view>
		<view class="cell_list">
			<view class="cell_left txt">{{$t('personal.text4')}}</view>
			<view class="cell_right">{{account}}</view>
		</view>
		<view class="cell_list">
			<view class="cell_left txt">{{$t('personal.text5')}}</view>
			<view class="cell_right">{{identity}}</view>
		</view>

		<popup v-model="dialogVisible">
			<view class="popup_content">
				<view @click="takePhoto">{{$t('personal.text6')}}</view>
				<view @click="onUnloadImg">{{$t('personal.text7')}}</view>
			</view>
			<view class="popup_content1">
				<view>{{$t('buttonCancel')}}</view>
			</view>
		</popup>
	</view>
</template>

<script>
	import zPrompt from '@/components/common/prompt';
	import Popup from '@/components/common/popup';
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		components: {
			Popup,
			zPrompt
		},
		data() {
			return {
				popupOptions: {
					placeholder: ''
				},
				avatar: '',
				nickname: '',
				id: '',
				account: '',
				identity: '',
				dialogVisible: false,
			};
		},
		computed: {
			...mapState(['userInfo'])
		},
		//第一次加载
		onLoad(e) {
			this.avatar = this.userInfo.avatar || "/static/images/tx.png";
			this.nickname = this.userInfo.nickname || "";
			this.phone = this.userInfo.phone || "";
			this.id = this.userInfo.id || "";
			this.account = this.userInfo.username || "";
			this.identity = this.userInfo.group;
			this.$http.get('/api/role/detail?lang='+this.$i18n.locale).then(res => {
				this.identity =  res.data.name;
			});
		},
		//页面显示
		onShow() {},
		//方法
		methods: {
			...mapMutations(['setUserInfo']),
			//修改昵称
			onNameChange(e) {
				this.nickname = e.value;
				e.close();
				let data = {
					nickname: e.value
				};
				this.$http.post('/api/user/profile', data).then(res => {
					if(res.code == 0){
						this.userInfo.nickname = e.value;
						this.setUserInfo(this.userInfo);
					}
				});
			},
			goto(url, type) {
			    if (type == 2) {
			        return uni.switchTab({ url: url })
			    }
			    if (type == 1) {
			        return uni.navigateBack({ delta: url });
			    }
			    uni.navigateTo({
			        url: url
			    })
			},
			takePhoto() {
			    uni.chooseImage({
			        count: 1, // 默认9, 设置图片的最大选择数，设置为1表示拍一张照片
			        sourceType: ['camera'], // 可以设置拍照或相册选择，这里只设置拍照
			        success: (res) => {
			            const tempFilePaths = res.tempFilePaths; // 临时路径
			            this.onUnloadImg(tempFilePaths[0]); // 上传图片
			        }
			    });
			},
			//修改头像
			onUnloadImg() {
				this.$http.urlImgUpload("/api/user/avatar", {
					count: 1
				}).then(res => {
					this.avatar = res[0].url;
				});
			},
		},
		//页面隐藏
		onHide() {},
		//页面卸载
		onUnload() {},
		//页面下来刷新
		onPullDownRefresh() {},
		//页面上拉触底
		onReachBottom() {},
		//用户点击分享
		onShareAppMessage(e) {
			return this.wxShare();
		}
	};
</script>
<style lang="scss" scoped>
	@import '@/style/mixin.scss';

	.cell_list {
		padding: 30rpx 30rpx;
		margin: 20rpx 30rpx 0 30rpx;
		border-radius: 8rpx;
	}

	.popup_content {
		padding: 20rpx 10rpx;
		background-color: #FFFFFF;
		text-align: center;
		width: 90vw;
		margin-left: 5vw;
		border-radius: 20rpx;
		line-height: 120rpx;
		margin-bottom: 20rpx;
		font-size: 34rpx;
		font-weight: 500;
		color:#333;
	}
	.popup_content1 {
		background-color: #FFFFFF;
		text-align: center;
		width: 90vw;
		margin-left: 5vw;
		border-radius: 20rpx;
		line-height: 120rpx;
		margin-bottom: 40rpx;
		font-size: 34rpx;
		font-weight: 500;
		color:#333;
	}
	.cell_right image {
		width: 140rpx;
		height: 140rpx;
		border-radius: 50%;
	}
</style>