<template>
	<view class="page">
		<PageHeader title="" class="PageHeader">
			<template slot="center">
				基本资料
			</template>
		</PageHeader>
		<!-- <view  style="height: 90rpx;"></view> -->
		<view class="list_info">
			<view class="bcenter">
				<view class="avator" @click="upload">
					<!-- <image class="img" :src="avator" mode="aspectFill" /> -->
					<CircleAvatar class="avator" :src="avator" style="width: 200rpx; height: 200rpx;"></CircleAvatar>
					<image class="photo" src="../../static/me/photo.png" mode="widthFix" />
				</view>
			</view>
			<view class="section_title">基本信息</view>
			<view class="info_card">
				<view class="info_item" @click="openNicknamePopUpWindow">
					<text class="label">昵称</text>
					<view class="content">
						<text v-if="nickname" class="textContent">{{ nickname }}</text>
						<text v-else class="textPrompt">请输入昵称</text>
						<image class="arrow" src="../../static/me/arrow_right_gray.png" mode="widthFix" />
					</view>
				</view>

				<view class="info_item" @click="SetSex">
					<text class="label">性别</text>
					<view class="content">
						<input type="text" placeholder="选择你的性别" v-model="sexText" disabled="true" />
						<image class="arrow" src="../../static/me/arrow_right_gray.png" mode="widthFix" />
					</view>
				</view>

				<view class="info_item">
					<text class="label">生日</text>
					<view class="content">
						<picker mode="date" :value="birthday" :end="endDate" @change="onBirthdayChange">
							<input type="text" placeholder="选择你的生日" v-model="birthday" disabled="true" />
						</picker>
						<image class="arrow" src="../../static/me/arrow_right_gray.png" mode="widthFix" />
					</view>
				</view>

				<view class="info_item">
					<text class="label">星座</text>
					<view class="content">
						<input type="text" placeholder="根据你的生日自动匹配" :value="xinzuo_sel" disabled="true"
							style="padding-right: 43rpx;" />
						<!-- <image class="arrow" src="../../static/me/arrow_right_gray.png" mode="widthFix" /> -->
					</view>
				</view>

				<view class="info_item" @click="openContentPopUpWindow">
					<text class="label">简介</text>
					<view class="content">
						<!-- <input type="text" placeholder="做个自我介绍吧~" v-model="content" maxlength="32" /> -->
						<text v-if="content" class="textContent one-omit">{{ content }}</text>
						<text v-else class="textPrompt">做个自我介绍吧</text>
						<image class="arrow" src="../../static/me/arrow_right_gray.png" mode="widthFix" />
					</view>
				</view>

			</view>
			<view class="section_title">个性标签</view>
			<view class="info_card">
				<view class="item_tag">
					<view class="tag" v-for="(item, index) in sel_tags" :key="index">
						{{ item }}
						<image class="close" src="../../static/me/close.png" mode="widthFix" @click="delTag(item)" />
					</view>
					<view class="addNew" @click="showPop = true;">+ 添加</view>
				</view>
			</view>
			<view class="blankHeight"></view>
		</view>

		<view class="btn_submit" @click="submitData(true)">保存</view>
		<DialogBox ref="DialogBox"></DialogBox>

		<view class="popSel" v-if="showPop" @tap.stop="onPreview">
			<PageHeader title="添加标签" @back="showPop = false" :isBack="false" style="background: #f2f6f2;" />
			<view style="height: 90rpx;"></view>
			<view class="headLabel">
				<view class="avator-box">
					<CircleAvatar class="avator" :src="avator"></CircleAvatar>
					<view class="one-omit">{{ nickname }}</view>
				</view>
				<view class="tag-box">
					<view class="tag" v-for="(item, index) in sel_tags" :key="index">
						{{ item }}
						<image class="close" src="../../static/me/close.png" mode="widthFix" @click="delTag(item)" />
					</view>
				</view>
			</view>
			<view class="list_info">
				<view class="name">你的兴趣爱好是？</view>
				<view class="desc">提示：最多选择10个兴趣爱好标签</view>
				<view class="item_tag">
					<view @tap.stop="chkTag(item)" class="tag" :class="selTags(item) ? 'active' : ''"
						v-for="(item, index) in list_tag" :key="index">
						{{ item }}
					</view>
				</view>
			</view>
			<view class="btn_submit" @click="showPop = false;">提交选择</view>
		</view>

		<ToastW3 ref="ToastW3"></ToastW3>

		<view v-if="showRights"
			style="width:100%;height:300rpx;background-color: rgba(255,255,255,0.9);position: fixed;top:0;display: flex;flex-direction: column;justify-content: center;align-items: center;">
			<text
				style="width:90%;color:#000000;font-size:38rpx;text-align: left;padding:10rpx 20rpx;padding-top:10rpx;">正在获取相机、存储权限</text>
			<text
				style="width:90%;color:#666666;font-size:28rpx;text-align: left;padding:10rpx 20rpx;">该权限用于获取设备拍摄或获取本地应用相册，进行头像或图片上传。</text>
		</view>
		<view>
			<NicknamePopup title="编辑昵称" subtitle="" class="openNicknamePopUpWindow" ref="openNicknamePopUpWindow">
				<template slot="content">
					<view class="input-box">
						<uv-input placeholder="请输入昵称" border="surround" v-model="nickname" maxlength="20"></uv-input>
						<view class="length-indicator">{{ nickname.length }}/20</view>
					</view>
					<view class="btn-box" @click="submitData">保存</view>
				</template>
			</NicknamePopup>

			<NicknamePopup title="专属于你的简介" subtitle="好的简介让你在社区更受关注" class="openContentPopUpWindow"
				ref="openContentPopUpWindow">
				<template slot="content">

					<uv-textarea v-model="content" maxlength="50" count placeholder="请输入内容"></uv-textarea>

					<view class="btn-box" @click="submitData">保存</view>
				</template>
			</NicknamePopup>
		</view>
	</view>
</template>

<script>
import {
	requestAndroidPermission,
	gotoAppPermissionSetting
} from '../index/permission.js'
import PageHeader from '@/components/PageHeader/PageHeader.vue';
import CircleAvatar from '@/components/CircleAvatar/CircleAvatar.vue';
import NicknamePopup from '@/components/NicknamePopup/NicknamePopup.vue';
export default {
	components: { PageHeader, CircleAvatar, NicknamePopup },
	data() {
		return {
			showRights: false,
			title: '',
			sel: 1,
			info: {},
			showPop: false,
			nickname: '',
			content: '',
			sex: 1,
			age: 18,
			height: 160,
			weight: 50,
			avator: '../../static/me/avator.png',
			ziye: '',
			qianmin: '',

			xinzuo_sel: '',
			xinzuo: ['水瓶座', '双鱼座', '白羊座', '金牛座', '双子座', '巨蟹座', '狮子座', '处女座', '天秤座', '天蝎座', '射手座', '摩羯座'],

			list_tag: [],
			sel_tags: [],
			sexText: '',
			birthday: '',
			sexOptions: ['男', '女', '其他'],
			endDate: new Date().toISOString().split('T')[0]
		}
	},
	onLoad() {
		let tagStr =
			"看书、做弹弓玩、做木剑玩、做橡皮枪玩、积木、用麻将搭金字塔、拼图、拆装、扑克牌、小汽车、手表、鞋之类的、弹吉他、钢琴、萨克斯、葫芦丝、大号、小号、折纸、剪纸、品茶、涂鸦、英雄联盟、qq堂、cs、cf、地下城勇士、桌面游戏";
		this.list_tag = tagStr.split("、");
		this.getInfoData();
	},
	onShow() { },
	methods: {
		openNicknamePopUpWindow() {
			this.$refs.openNicknamePopUpWindow.open();
		},
		openContentPopUpWindow() {
			this.$refs.openContentPopUpWindow.open();
		},
		closePropUp() {
			this.$refs.openNicknamePopUpWindow.close();
			this.$refs.openContentPopUpWindow.close();
		},
		onBack() { },
		onPreview() { },
		chkSel() {
			if (this.sel == 1) {
				this.sel = 0;
			} else {
				this.sel = 1;
			}
		},
		chkSex(sex) {
			this.sex = sex;
		},
		delTag(tg) {
			let list_tag2 = [];
			for (let i = 0; i < this.sel_tags.length; i++) {
				if (this.sel_tags[i] != tg) {
					list_tag2.push(this.sel_tags[i]);
				}
			}
			this.sel_tags = list_tag2;
		},
		selTags(itm) {
			let that = this;
			let isIn = false;
			for (let entry of this.sel_tags) {
				if (entry == itm) {
					isIn = true;
					break;
				}
			}
			return isIn;
		},
		chkTag(itm) {
			if (this.selTags(itm)) {
				let tmpTags = [];
				for (let entry of this.sel_tags) {
					if (entry != itm) {
						tmpTags.push(entry);
					}
				}
				this.sel_tags = tmpTags;
			} else {
				if (this.sel_tags.length <= 9) {
					this.sel_tags.push(itm);
				} else {
					this.$refs['ToastW3'].showToast({
						title: "最多选择10个标签",
						animation: 0
					});
				}
			}
		},
		sliderChange1(e) {
			this.age = e.detail.value;
		},
		sliderChange2(e) {
			this.height = e.detail.value;
		},
		sliderChange3(e) {
			this.weight = e.detail.value;
		},
		SetSex() {
			let that = this;
			uni.showActionSheet({
				itemColor: '#000000',
				itemList: this.sexOptions,
				success: function (res) {
					that.sexText = that.sexOptions[res.tapIndex];
					that.sex = res.tapIndex + 1;
				},
				fail: function (res) {
					console.log(res.errMsg);
				}
			});
		},
		getInfoData() {
			uni.request({
				url: this.$apiHost + '/Member/getinfoData',
				data: {
					uuid: getApp().globalData.uuid
				},
				header: {
					'content-type': 'application/json'
				},
				success: (res) => {
					console.log(res.data);
					this.nickname = res.data.nickname;
					this.wechat = res.data.wechat;
					this.sex = res.data.sex;
					this.sexText = this.sexOptions[res.data.sex - 1] || '';
					this.birthday = res.data.birthday;
					this.xinzuo_sel = this.getConstellation(this.birthday);
					if (res.data.avator != "") {
						this.avator = res.data.avator;
					}
					if (res.data.aihao) {
						this.sel_tags = res.data.aihao.split(",");
					}
				}
			});
		},
		submitData(isBack) {
			let aihao = this.sel_tags.join(',')
			let obj2 = {
				uuid: getApp().globalData.uuid,
				avator: this.avator,
				nickname: this.nickname,
				content: this.content,
				sex: this.sex,
				birthday: this.birthday,
				xinzuo: this.xinzuo_sel,
				ziye: this.ziye,
				qianmin: this.qianmin,
				aihao: aihao
			}
			uni.request({
				url: this.$apiHost + '/Member/setinfoData',
				data: obj2,
				method: 'POST',
				header: {
					'Content-Type': 'application/x-www-form-urlencoded',
					'sign': getApp().globalData.headerSign
				},
				dataType: 'json',
				success: (res) => {
					console.log("res", res.data)
					this.$refs['ToastW3'].showToast({
						title: res.data.str,
						animation: 0
					});
					if (this.success == 'yes' && isBack == true) {
						if (this.nickname) {
							getApp().globalData.nickname = this.nickname
							uni.removeStorageSync("nickname" );
							uni.setStorageSync("nickname", this.nickname);
						}
						 
						if (this.avator) {
							getApp().globalData.avator = this.avator
							uni.removeStorageSync("avator" );
							uni.setStorageSync("avator", this.avator);
						}
						setTimeout(function () {
							console.log('返回');

							uni.navigateBack()
						}, 800)
					} else {
						this.closePropUp()
					}
					this.getInfoData()
				}
			});
		},
		upload() {
			this.checkRights();
			console.log("----upload");
			var _self = this;
			uni.chooseImage({
				count: 1,
				sizeType: ['compressed'],
				sourceType: ['album', 'camera'],
				success: function (res) {
					console.log('res:', res)
					if (res.tempFilePaths.length > 0) {
						_self.imglocal = res.tempFilePaths[0]
						const tempFilePaths = res.tempFilePaths[0];
						console.log('tempFilePaths:', tempFilePaths);
						const uploadTask = uni.uploadFile({
							url: _self.$apiHost + '/Xweb/upload_img?skey=' + _self.skey,
							filePath: res.tempFilePaths[0],
							name: 'file',
							success: function (uploadFileRes) {
								let resdata = JSON.parse(uploadFileRes.data)
								console.log('Success11:', uploadFileRes);
								console.log('Success21:', resdata);
								if (resdata.success == 'yes') {
									_self.showRights = false;
									_self.avator = resdata.url;
								}
							},
							fail: function (uploadFileFail) {
								console.log('Error:', uploadFileFail.data);
							},
							complete: () => {
								console.log('Complete:');
							}
						});
					}
				},
				error: function (e) {
					console.log(e);
				}
			});
		},
		checkRights() {
			let that = this;
			that.showRights = true;
			requestAndroidPermission('android.permission.CAMERA')
				.then(result => {
					that.showRights = false;
					if (result === 1) { } else if (result === 0) {
						console.log('权限被拒绝');
					} else if (result === -1) {
						console.log('权限被永久拒绝');
					}
				})
				.catch(error => {
					that.showRights = true;
					console.log('权限申请失败：', error);
				});
		},
		onBirthdayChange(e) {
			this.birthday = e.detail.value;
			this.xinzuo_sel = this.getConstellation(this.birthday);
		},
		getConstellation(birthday) {
			if (!birthday) return '';

			const month = parseInt(birthday.split('-')[1]);
			const day = parseInt(birthday.split('-')[2]);

			// 星座日期数组
			const constellationDates = [
				[1, 20, "水瓶座"],
				[2, 19, "双鱼座"],
				[3, 21, "白羊座"],
				[4, 20, "金牛座"],
				[5, 21, "双子座"],
				[6, 22, "巨蟹座"],
				[7, 23, "狮子座"],
				[8, 23, "处女座"],
				[9, 23, "天秤座"],
				[10, 24, "天蝎座"],
				[11, 23, "射手座"],
				[12, 22, "摩羯座"]
			];

			let constellation = "";
			if (day < constellationDates[month - 1][1]) {
				constellation = constellationDates[month - 1][2];
			} else {
				constellation = constellationDates[month % 12][2];
			}

			return constellation;
		}
	}
}
</script>

<style scoped lang="scss">
@import 'normal.scss';

page {
	font-family: PingFang SC-Medium;
}

.popSel {
	position: fixed;
	z-index: 999999;
	top: 0;
	left: 0;
	background-color: #f2f6f2;
	width: 100vh;
	height: 100vh;

	.headLabel {
		width: 686rpx;
		margin: 28rpx 32rpx;
		min-height: 222rpx;
		background: #fff;
		box-sizing: border-box;
		padding: 30rpx 40rpx 46rpx 20rpx;
		border-radius: 20rpx;

		.avator-box {
			display: flex;
			align-items: center;

			.avator {
				width: 80rpx;
				height: 80rpx;
				margin-right: 20rpx;
			}
		}

		.tag-box {
			display: flex;
			flex-direction: row;
			justify-content: flex-start;
			align-items: center;
			flex-wrap: wrap;
			font-size: 24rpx;

			.tag {
				margin-right: 16rpx;
				margin-top: 20rpx;
				padding: 6rpx 10rpx;
				border: 2rpx solid #DCE1DC;
				color: #9E9E9E;
				display: flex;
				justify-content: center;
				align-items: center;
				border-radius: 42rpx;
				line-height: 1;

				.close {
					width: 20rpx;
					margin-left: 12rpx;
					position: relative;
				}
			}
		}
	}
}

.openNicknamePopUpWindow {
	.length-indicator {
		font-size: 24rpx;
		color: #999;
		text-align: right;
		margin-top: 10rpx;
		margin-right: 20rpx;
		position: absolute;
		right: 24rpx;
		top: 24rpx;

	}
}

.popSel {
	.item_tag {
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		align-items: center;
		flex-wrap: wrap;
		width: 690rpx;
		padding: 20rpx;

		.tag {
			border-radius: 94rpx;
			margin-right: 10rpx;
			margin-top: 10rpx;
			padding: 8rpx 16rpx;
			font-size: 28rpx;
			color: #333;
			display: flex;
			flex-direction: row;
			justify-content: center;
			align-items: center;
			background: #fff;
			border-width: 0;

			.close {
				width: 28rpx;
				margin-left: 2rpx;
				position: relative;
			}
		}

		.active {
			border-width: 0;
			background: #ACF934;
		}

		.addNew {
			margin-right: 10rpx;
			margin-top: 10rpx;
			padding: 0rpx 20rpx;
			height: 60rpx;
			border-radius: 12rpx;
			background: #1f1f1f;
			color: #fff;
			display: flex;
			flex-direction: row;
			justify-content: center;
			align-items: center;
		}
	}
}
</style>