<template>
	<view class="content">
		<headerTopsTips></headerTopsTips>
		<!--  -->
		<uni-nav-bar left-icon="left" title="个人信息" backgroundColor="#e6eafa" @clickLeft="handlegoback" />
		<!--  -->
		<view class="tops_info">
			<view class="cicler">
				<image :src="information.avatar" style="width: 100%; height: 100%; " mode="aspectFill"></image>
				<view class="positon_bottom" @click="upload_imgs">
					+上传头像
				</view>
			</view>
			<text style="color:#fff;">ofo</text>
			<view class="number_las">
				信用分: {{information.creditPoint}} >
			</view>
		</view>
		<view class="item_info">
			<text>姓名</text>
			<text>{{information.name}}</text>
			<view class="bot_line"></view>
		</view>
		<view class="item_info">
			<text>性别</text>
			<!-- <hpy-form-select :dataList="hobbyList" text="text" name="value" v-model="formData.hobbySelect"
				@change="change" :hideBorder="true" :hideArrow="true">
				<text>未设置 > </text>
			</hpy-form-select> -->
			<!-- <text></text> -->
			<picker :range="time_picker" @change="bindTimeChange" :value="check_sex">
				<view class="uni-input">{{ time_picker[check_sex] }}</view>
			</picker>
			<view class="bot_line"></view>
		</view>
		<view class="item_info">
			<text>生日</text>
			<text>{{information.birthday}}</text>
			<!-- <uni-datetime-picker v-model="single">{{}}</uni-datetime-picker> -->
		</view>
		<!--  -->
		<view class="item_info" style="margin-top: 20rpx;">
			<text>绑定的手机号</text>
			<text @click="handle_push(1)">{{information.mobile}}</text>
			<view class="bot_line"></view>
		</view>
		<view class="item_info">
			<text>修改密码</text>
			<text @click="handle_push(2)">></text>
			<view class="bot_line"></view>
		</view>
		<view class="item_info">
			<text>是否认证</text>
			<text
				@click="handle_realNameAuthFlag(information.realNameAuthFlag)">{{information.realNameAuthFlag == 1? '已认证': '未认证'}}</text>
		</view>
		<!--  -->
		<view class="item_info" style="margin-top: 20rpx;">
			<text>关于我们</text>
			<text> > </text>
		</view>
		<!--  -->
		<view class="botm_setlogin">
			<view class="btn_login" @click="handle_goback">退出登录</view>
		</view>
	</view>
</template>

<script setup>
	import headerTopsTips from '@/pages/components/header_tops_tips.vue'
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import sheep from '@/sheep';
	import {
		baseUrl,
		apiPath
	} from '@/sheep/config';
	import {
		ref
	} from 'vue'
	const formData = ref({
		hobbySelect: ''
	})
	const information = ref({})
	const hobbyList = ref([{
		text: '男',
		value: 1
	}, {
		text: '女',
		value: 2
	}])
	const time_picker = ref(
		['男', '女', '未知']
	)
	const single = ref('')
	// 
	const check_sex = ref(0)
	onLoad((option) => {
		information.value = JSON.parse(option.information)
		check_sex.value = information.value.sex
	})
	// 
	function handlegoback() {
		uni.navigateBack()
	}
	// 
	function change(e) {
		console.log(e.index);
		console.log(e.value);
		console.log(e.data);
	}

	function handel_sex(data) {
		// console.log(data, 'data')
		switch (data) {
			case 0:
				return '男';
			case 1:
				return '女';
			case 2:
				return '未知'
		}
	}

	function handle_realNameAuthFlag(data) {
		switch (data) {
			case 0:
				uni.navigateTo({
					url: '/pages/index/authentication'
				});
				break;
			case 1:
				return uni.showToast({
					title: '已认证...',
					icon: 'success'
				})
		}
	}

	// 
	function handle_push(val) {
		sheep.$router.go('/pages/home/updatepassword', {
			date: JSON.stringify(information.value),
			type: val
		})
	}

	// 上传头像
	function upload_imgs() {
		uni.chooseImage({
			count: 1, //默认9
			sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album'], //从相册选择
			success: function(res) {
				let imgs_path = res.tempFilePaths
				// console.log(imgs_path,'imgs_path');
				// console.log(res,'res');
				uni.uploadFile({
					url: `${baseUrl + apiPath}/infra/file/upload`,
					filePath: imgs_path[0],
					name: 'file',
					method: 'post',
					header: {
						'tenant-id': 1,
						'Authorization': uni.getStorageSync('token')
					},
					success: (res) => {
						const data_inter = JSON.parse(res.data)
						// console.log(data_inter, '上传成功')
						let data = {
							avatar: data_inter.data
						}
						handle_update_info(data)
						information.value.avatar = data_inter.data
					}
				})
			}
		})
	}
	// 确认修改
	function handle_update_info(data) {
		// console.log(data,'data')
		uni.request({
			url: `${baseUrl + apiPath}/member/user/update`,
			method: 'PUT',
			data,
			header: {
				'tenant-id': 1,
				'Authorization': uni.getStorageSync('token')
			},
			success: (res) => {
				// console.log(res, 'res')
				if (res.data.code == 0) {
					uni.showToast({
						title: '修改成功',
						icon: 'success'
					})
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: 'error'
					})
				}
			},
			fail: (error) => {
				console.log(error, 'error')
			}
		});
	}
	// 选择性别确认
	function bindTimeChange(e) {
		// console.log('picker发送选择改变，携带值为', e.detail)
		check_sex.value = e.detail.value
		let data = {
			sex: parseInt(check_sex.value) + 1
		}
		handle_update_info(data)
	}
	// 退出
	function handle_goback() {
		uni.clearStorageSync();
		// uni.removeStorageSync('token')
		uni.reLaunch({
			url: '/pages/subpackages/login'
		})
	}
</script>

<style lang="scss" scoped>
	.content {
		height: 100vh;
		background: #eee;
	}

	.botm_setlogin {
		height: 150rpx;
		background: #fff;
		width: 100%;
		position: absolute;
		bottom: 0;
		left: 0;
		padding: 0 20px;
		box-sizing: border-box;
		display: flex;
		align-items: center;
	}

	.btn_login {
		width: 100%;
		height: 100rpx;
		background: #000;
		color: #fff;
		font-size: 16px;
		line-height: 100rpx;
		text-align: center;
	}

	.tops_info {
		height: 300rpx;
		background: #555555;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: space-evenly;

		.cicler {
			width: 150rpx;
			height: 150rpx;
			background: #b5b5b5;
			border-radius: 50%;
			overflow: hidden;
			position: relative;

			.positon_bottom {
				position: absolute;
				bottom: 0;
				background: #a8a8a8;
				color: #fff;
				width: 100%;
				line-height: 30px;
				font-size: 10px;
				text-align: center;
				opacity: 0.7;
			}
		}

		.number_las {
			width: 230rpx;
			line-height: 60rpx;
			background: #ffffff;
			border-radius: 30rpx;
			font-size: 12px;
			text-align: center;
		}
	}

	.item_info {
		height: 100rpx;
		padding: 0 20px;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: space-between;
		position: relative;
		background: #fff;

		.bot_line {
			width: 100%;
			height: 2px;
			background: #e5e5e5;
			position: absolute;
			bottom: 0;
			left: 20px;
		}
	}
</style>