<template>
	<view class="content">
		<view class="row1">
			<text class="tit">头像</text>
			<view class="background-img">
				<image class="background-img" v-model="avatar" :src="userInfo.avatar"></image>
			</view>
		</view>
		<view class="row">
			<text class="tit">用户名</text>
			<input class="input" type="button" v-model="name"  placeholder-class="placeholder" />
		</view>
		<view class="row" style="margin-top: 50rpx;height: 100rpx;" @click="toLogout">
			<text class="tit1">退出登录</text>
			<!-- <input class="input" type="button" v-model="name"  placeholder-class="placeholder" /> -->
		</view>
		<!-- <view class="row">
			<text class="tit">性别</text>
			<picker @change="bindPickerChange" :value="index" :range="array" class="input">
				<text>{{ sex }}</text>
			</picker>
		</view> -->
	<!-- 	<view class="row">
			 <text class="tit">生日</text>
			 <picker @change="bindDateChange" mode="date" :value="date" :start="startDate" :end="endDate" class="input">
				 <text>{{birthday}}</text>
			 </picker>
		</view>
		<view class="row">
			<text class="tit">邀请码</text>
			<input class="input" disabled type="text" v-model="uid"  placeholder-class="placeholder" />
		</view> -->
		<button class="add-btn" @click="confirm">修改</button>
		  
	</view>
</template>

<script>
import { mapState, mapMutations } from 'vuex';
import { logout } from '@/api/set.js';
import { userEdit } from '@/api/set.js';
import  pickerAddress from '@/components/wangding-pickerAddress/wangding-pickerAddress.vue';
import wPicker from '@/components/w-picker/w-picker.vue';
export default {
	data() {
		 
		return {
			name: '',
			sex:'',
			array:['男','女'],
			index:0,
			birthday: '',
			birthdaynum:0,
			date:'',
			uid: '',
			avatar: '',
			resultInfo: {
				result: '2019-12-20 10:00:00'
			},
			year:'',
			
		};
	},
	components: {
		wPicker,
		pickerAddress
	},
	computed: {
		...mapState('user',['userInfo']),
		  startDate() {
		          console.log('start');
		        },
		        endDate() {
		          console.log('end');
		        }
	},
	onShow(option) {
		console.log(this);
// 获取生日日期
		let date = new Date(this.userInfo.birthday * 1000);
		let month = date.getMonth() + 1; //获取月份
		let day = date.getDate();
		this.name = this.userInfo.nickname + '';
		this.uid = this.userInfo.uid || '';
		this.avatar = this.userInfo.avatar || '';
		this.birthday = date.getFullYear() + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day);
		this.year = new Date().getFullYear();
		let sex;
		if (this.userInfo.sex == 0) {
			sex = '未知';
		} else if (this.userInfo.sex == 1) {
			sex = '男';
		} else {
			sex = '女';
		}
		this.sex = sex; //性别赋值
		this.index=this.userInfo.sex;
	},
	methods: {
		//退出登录
		toLogout(){
			let obj = this;
			uni.showModal({
			    content: '确定要退出登录么',
			    success: (e)=>{
			    	if(e.confirm){
						logout({}).then((e) => {
							uni.navigateBack();
						}).catch((e) => {
							console.log(e);
						})
			    		obj.logout();
			    	}
			    }
			});
		},
		bindDateChange:function(e){
			console.log(e.detail.value);
		this.birthday=e.detail.value;
		this.birthdaynum=(new Date(e.detail.value).getTime())/1000;
		console.log(this.birthdaynum);
		},
		bindPickerChange: function(e) {
			this.sex=this.array[e.target.value];
			this.index = e.target.value+1;
			
			
		},
		// 显示日期选择器
		// toggleTab(str) {
		// 	this.$refs[str].show();
		// },
		// 数据处理结果
		onConfirm(val) {
			//如果页面需要调用多个mode类型，可以根据mode处理结果渲染到哪里;
			// switch(this.mode){
			// 	case "date":
			// 		break;
			// }
			this.resultInfo = { ...val };
			this.birthday = this.resultInfo.result;
		},
		switchChange(e) {
			this.addressData.default = e.value;
		},
		confirm() {
			userEdit({ nickname: this.name, avatar: this.avatar, sex: this.index, phone: this.phone ,birthday:this.birthdaynum })
				.then(e => {
					this.$api.msg('修改成功');
					setTimeout(() => {
						uni.switchTab({
							url: '/pages/user/user'
						});
					}, 1000);
					console.log(e);
				})
				.catch(e => {
					console.log(e);
				});
		}
	}
};
</script>

<style lang="scss">
page {
	background: $page-color-base;
}

.row1 {
	display: flex;
	align-items: center;
	justify-content: space-between;
	position: relative;
	padding: 0 30upx;
	height: 110upx;
	background: #fff;
	margin-bottom: 20upx;
	.tit {
		flex-shrink: 0;
		width: 120upx;
		font-size:$font-lg;
		color: $font-color-dark;
	}
	.background-img{
		width:80rpx;
		height:80rpx;
		border-radius:50%;
		background: #F2F2F2;
	}
}
.row {
	display: flex;
	align-items: center;
	justify-content: space-between;
	position: relative;
	padding: 0 30upx;
	height: 110upx;
	background: #fff;
  .tit1{
	  margin: 0 auto;
	  flex-shrink: 0;
	  font-size:$font-lg;
	  color: $font-color-dark;
  }
	.tit {
		flex-shrink: 0;
		width: 120upx;
		font-size:$font-lg;
		color: $font-color-dark;
	}
	.input {
		flex: 1;
		font-size: $font-base;
		color: $color-gray;
	}
	.iconlocation {
		font-size: 36upx;
		color: $font-color-light;
	}
}
.add-btn {
	display: flex;
	align-items: center;
	justify-content: center;
	width: 560upx;
	height: 80upx;
	margin: 100rpx auto;
	font-size: $font-lg;
	color: #fff;
	background: #FF727E;
	border-radius: 40upx;
}
</style>
