<template>
	<view class="container">
		<view class="top_">
			<view style="width: 100px; height: 100px; border-radius: 50%; overflow: hidden;">
				<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
					:maxCount="1" width="100" height="100">
					<view class="user_t">
						<image :src="baseUrl + userInfo.avatar" mode="" v-if="userInfo.avatar"></image>
							<image src="http://47.94.122.58:83/userPhoto.png" mode="" v-else></image>
					</view>
				</u-upload>
			</view>
		</view>
		<view class="box_">
			<view class="d_lang">
				<view class="left_">Name</view>
				<view class="right_" @click="inputShow('name')">
					<text style="margin-right: 5px;">{{userInfo.userName || 'name'}}</text>
					<u-icon name="arrow-right" size="16"></u-icon>
				</view>
			</view>
			<view class="d_lang">
				<view class="left_">phonenumber</view>
				<view class="right_" @click="inputShow('phonenumber')">
					<text style="margin-right: 5px;">{{userInfo.phonenumber || 'phonenumber'}}</text>
					<u-icon name="arrow-right" size="16"></u-icon>
				</view>
			</view>
			<view class="d_lang">
				<view class="left_">Gender</view>
				<view class="right_" @click="sexShow()">
					<text style="margin-right: 5px;" v-if="userInfo.sex == 1">women</text>
					<text style="margin-right: 5px;" v-else>men</text>
					<u-icon name="arrow-right" size="16"></u-icon>
				</view>
			</view>
			<view class="d_lang">
				<view class="left_">Birthday </view>
				<view class="right_" @click="dateShow()">
					<text style="margin-right: 5px;">{{value1 | formatDate}}</text>
					<u-icon name="arrow-right" size="16"></u-icon>
				</view>
			</view>
		</view>
		<!-- 日期 -->
		<u-datetime-picker :show="timeShow" v-model="value1" mode="date" @confirm="confirmTime"></u-datetime-picker>
		<!-- 选择器 -->
		<u-picker :show="pickerShow" :columns="columns" @confirm="confirm" @cancel="cancel"></u-picker>
		<!-- 输入框 -->
		<u-popup :show="show" mode="center" :round="10" @close="close" @open="open">
			<view class="p_box">
				<view style="margin: 30px auto;">
					<u--input placeholder="Enter " border="surround" v-model="value" @change="change"></u--input>
				</view>

				<view class="P_">
					<view class="an_niu" @click="save()">confirm</view>
					<view class="an_niux" @click="lost()">cancel</view>
				</view>

			</view>
		</u-popup>

	</view>
</template>

<script>
	import request from '../../utils/request'
	import config from '@/config'
	export default {
		data() {
			return {
				baseUrl: config.imagesUrl,
				userInfo: {},
				type: "name",
				columns: [
					['men', 'women']
				],
				fileList1: [],
				timeShow: false,
				pickerShow: false,
				show: false,
				status: 'loading',
				value: '',
				value1: Number(new Date()),
			}
		},
		onLoad() {
			this.userInfo = uni.getStorageSync('user_info')
			console.log(this.userInfo);
		},

		methods: {
			save() {
				if (this.type == "name") {
					this.userInfo.userName = this.value
				} else {
					this.userInfo.phonenumber = this.value
				}
				this.show = false
			
				this.systemUser()
			},
			lost() {
				this.value = ''
				this.show = false
			},
			async systemUser() {
				uni.showLoading({
								title: 'Loading...'
							});
				let res = await request({
					url: 'system/user/editByUser',
					method: 'put',
					data: this.userInfo
				})
				if(res.code == 200){
					uni.hideLoading();
					this.getBaseInfo()
				}
				

			},
			async getBaseInfo() {
				let res = await request({
					url: 'system/user/getUserBaseInfo',
					method: 'get',
				})
				if (res.code == 200) {
					uni.setStorageSync('user_info', res.data);
				}

			},
			confirmTime(e) {
				console.log(e);
				this.value1 = e.value
				this.timeShow = false
				this.systemUser()
			},
			cancel() {
				this.pickerShow = false
			},
			confirm(e) {
				console.log(e);
				if (e.value[0] == 'men') {
					this.userInfo.sex = 0
				} else {
					this.userInfo.sex = 1
				}
				this.systemUser()
				this.pickerShow = false
			},
			dateShow() {
				this.timeShow = true
			},
			sexShow() {
				this.pickerShow = true
			},
			inputShow(name) {
				this.value = ''
				this.show = true
				this.type = name
			},
			open() {
				// console.log('open');
			},
			close() {
				this.show = false
				// console.log('close');
			},
			change(e) {
				console.log('change', e);
			},
			goback() {
				uni.navigateBack()
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: config.baseUrl + 'common/upload',
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							if (!res.data) {
								uni.showToast({
									title: "Upload failed",
									icon: "none"
								})
								return;
							}
							let data = JSON.parse(res.data)
							setTimeout(() => {
								resolve(data.fileName)
								this.userInfo.avatar = data.fileName
								this.systemUser()
							}, 100)
						}
					});
				})
			},
		},
		filters: {
			//过滤器 用于格式化时间
			formatDate(value) {
				let date = new Date(value);
				//时间戳为10位需*1000，时间戳为13位的话不需乘1000
				let y = date.getFullYear();
				let MM = date.getMonth() + 1;
				MM = MM < 10 ? ('0' + MM) : MM; //月补0
				let d = date.getDate();
				d = d < 10 ? ('0' + d) : d; //天补0
				let h = date.getHours();
				h = h < 10 ? ('0' + h) : h; //小时补0
				let m = date.getMinutes();
				m = m < 10 ? ('0' + m) : m; //分钟补0
				let s = date.getSeconds();
				s = s < 10 ? ('0' + s) : s; //秒补0
				return y + '-' + MM + '-' + d; //年月日


			}
		},

	}
</script>

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

	.top_ {
		width: 100%;
		height: 170px;
		box-sizing: border-box;
		padding-top: 20px;
		display: flex;
		justify-content: center;

		background-color: #106f3d;

	}

	.ds_ {
		width: 85%;
		margin: 15px auto;
		display: flex;
		align-items: center;
		color: #fff;

	}

	.user_t {
		width: 100px;
		height: 100px;
		background: #F2F2F2;


		display: flex;
		align-items: center;
		justify-content: center;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.user_title {
		font-weight: bold;
		font-size: 20px;
		color: #FFFFFF;
		margin-bottom: 10px;
	}

	.user_emil {
		font-weight: 500;
		font-size: 16px;
	}

	.box_ {
		width: 90%;
		background: #FFFFFF;
		box-sizing: border-box;

		box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.05);

		margin: 15px auto;
	}

	.d_lang {
		display: flex;
		align-items: center;
		justify-content: space-between;
		border-bottom: 1px solid #F2F2F2;
		box-sizing: border-box;
		padding: 10px 15px;
	}

	.f_ {
		width: 25%;
		font-weight: 500;
		font-size: 12px;
		color: #333333;
		text-align: center;
	}

	.icon_ {
		width: 22px;
		height: 22px;
		margin: 0 auto;
		margin-bottom: 5px;

		image {
			width: 100%;
			height: 100%;

		}
	}

	.left_ {
		font-weight: 500;
		font-size: 16px;
		color: #000000;
	}

	.right_ {
		display: flex;
		align-items: center;
		font-weight: 500;
		font-size: 16px;
		color: #333333;
	}

	.p_box {
		width: 300px;

		border-radius: 10px;
		background: #FFFFFF;
		box-sizing: border-box;
		padding: 15px;
		text-align: center;

	}

	.an_niu {
		width: 45%;
		border-radius: 6px;
		background: #106f3d;
		color: #fff;
		box-sizing: border-box;
		padding: 10px;
		display: flex;
		align-items: center;
		border: 1px solid #106f3d;
		justify-content: center;
	}

	.an_niux {
		width: 45%;
		border-radius: 6px;

		color: #106f3d;
		box-sizing: border-box;
		padding: 10px;
		display: flex;
		align-items: center;
		border: 1px solid #106f3d;
		justify-content: center;
	}

	.P_ {
		width: 100%;
		display: flex;
		align-items: center;

		justify-content: space-between;
		margin-top: 15px;
	}
</style>