<template>
	<view style="background: #F1F2F8;">
		<view
			style="background: url('https://hnyea.0rui.cn/uploads/1/20240722/7f5a72050b9cbc409fb60fa8d32db76a.png');background-repeat: no-repeat;background-size: 100% 555rpx;">
			<tn-nav-bar :isBack="false" :bottomShadow="false" backgroundColor="transparent">
				<view class="custom-nav tn-flex tn-flex-col-center tn-flex-row-left">
					<view style="width: 100%;">
						<view
							style="text-align: center;font-size: 34rpx;color: #000000;letter-spacing: 1px;font-weight: bold;">
							<text>我的</text>
						</view>
					</view>
				</view>
			</tn-nav-bar>
			<view :style="{paddingTop: vuex_custom_bar_height + 'px'}">
				<view style="padding: 30rpx;">
					<view class="tn-flex tn-flex-center tn-flex-col-center">
						<view>
							<view v-if="login && userInfo.photo_image">
								<image :src="apiImgUrl+userInfo.photo_image"
									@click="tn_ru('/pages/packageA/user/my_card?id='+userInfo.member_id)"
									style="width: 120rpx;height: 120rpx;border-radius: 50%;"></image>
							</view>
							<view v-if="!login || !userInfo.photo_image">
								<image src="/static/def.png" style="width: 120rpx;height: 120rpx;border-radius: 50%;">
								</image>
							</view>
						</view>
						<view style="margin-left: 20rpx;font-size: 34rpx;color: #181818;font-weight: bold;">
							{{login ? userInfo.nikename:'' }}
							<tn-button v-if="!login " open-type="getPhoneNumber"
								@getphonenumber="PhoneLogin">点击登陆</tn-button>
						</view>
					</view>
					<view style="margin-top: 30rpx;">
						<view style="background-color: #ffffff;padding: 30rpx 0rpx;border-radius: 20rpx;">
							<tn-grid align="center" :col="col">
								<tn-grid-item style="width: 25%" @click="tn('/pages/packageA/user/my_add')">
									<view>
										<image class="no-img" src="/static/ico1.png" style="width: 150rpx;"
											mode="widthFix"></image>
									</view>
									<view style="margin-top: -20rpx;font-size: 30rpx;font-weight: 500;">
										我的发布
									</view>
								</tn-grid-item>
								<tn-grid-item style="width: 25%" @click="tn('/pages/packageA/user/events_my')">
									<view>
										<image class="no-img" src="/static/ico2.png" style="width: 150rpx;"
											mode="widthFix"></image>
									</view>
									<view style="margin-top: -20rpx;font-size: 30rpx;font-weight: 500;">
										我的活动
									</view>
								</tn-grid-item>
								<tn-grid-item style="width: 25%" @click="tn('/pages/packageA/user/my_collection')">
									<view>
										<image class="no-img" src="/static/ico3.png" style="width: 150rpx;"
											mode="widthFix"></image>
									</view>
									<view style="margin-top: -20rpx;font-size: 30rpx;font-weight: 500;">
										我的收藏
									</view>
								</tn-grid-item>
								<tn-grid-item style="width: 25%"
									@click="tn('/pages/packageA/user/apply_in?association_id=1')">
									<view>
										<image class="no-img" src="/static/ico4.png" style="width: 150rpx;"
											mode="widthFix"></image>
									</view>
									<view style="margin-top: -20rpx;font-size: 30rpx;font-weight: 500;">
										入会申请
									</view>
								</tn-grid-item>
							</tn-grid>
						</view>
					</view>
					<view style="margin-top: 30rpx;">
						<view style="background-color: #ffffff;border-radius: 20rpx;padding: 20rpx;">
							<tn-list-view>
								<tn-list-cell padding="35rpx 30rpx" @click="tn('/pages/packageA/user/my_msg')"
									fontColor="#181818" fontSize="30rpx" :arrow="true">站内信</tn-list-cell>
								<!-- 	<tn-list-cell padding="35rpx 30rpx" @click="tn('/pages/packageA/user/my_card_holder')"
									fontColor="#181818" fontSize="30rpx" :arrow="true">好友申请</tn-list-cell> -->
								<tn-list-cell padding="35rpx 30rpx" @click="tn('/pages/packageA/user/my_invoice')"
									fontColor="#181818" fontSize="30rpx" :arrow="true">发票开取</tn-list-cell>
								<tn-list-cell padding="35rpx 30rpx" fontColor="#181818" fontSize="30rpx"
									:arrow="true">数据资料导出</tn-list-cell>
								<tn-list-cell @click="loginAut" padding="35rpx 30rpx" fontColor="#181818"
									fontSize="30rpx" :arrow="true" :unlined="true">退出账号</tn-list-cell>
							</tn-list-view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getUserIndex,
		loginDo,
		wxphoneLogin
	} from "@/util/api";
	import store from "@/store";

	export default {
		data() {
			return {
				topCurrent: 0,
				login: false,
				loginMod: false,
				userInfo: {},
				apiImgUrl: this.$store.state.imgUrl,
				loginData: {
					openid: '',
					phone: '',
					password: '',
					association_id: store.state.Gid
				},
				code: '',
				identifyCode: '',
				identifyCodeName: '',
				colors: ['#00CCFF', '#FF0000', '#FF9933', '#33CC99'], // 可以根据需要增加颜色
			}
		},
		mounted() {
			var uid = uni.getStorageSync('uid');
			this.uid = uid;
			if (uid) {
				this.getUserInfo();
			}

			this.getcheckCode();
		},
		methods: {
			PhoneLogin(d) {
				console.log(d);
				var openid = uni.getStorageSync('openid');
				wxphoneLogin({
						code: d.code,
						openid: openid
					})
					.then(res => {
						console.log(res);
						if (res.code == 1) {
							uni.showToast({
								title: '登陆成功！',
								icon: 'none',
								duration: 2000
							});
							uni.setStorageSync('uid', res.data.id);
							uni.setStorageSync('userInfo', res.data);
							this.userInfo = res.data;
							this.login = true;
							this.loginMod = false;
						} else {
							uni.showToast({
								title: '登陆失败！',
								icon: 'none',
								duration: 2000
							});
						}
					})
					.catch(error => {
						uni.showToast({
							title: error,
							icon: 'none',
							duration: 2000
						});
					})
			},
			getcheckCode() {
				let code = [];
				const codeLength = 4;
				var codeName = '';
				const random = [
					1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
					'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
					'X', 'Y', 'Z',
				];
				for (let i = 0; i < codeLength; i++) {
					let index = Math.floor(Math.random() * 34);
					code.push(random[index]);
					codeName += random[index];
				}
				this.identifyCodeName = codeName;
				this.identifyCode = code;
			},
			msg() {
				return this.$store.state.msgCount;
			},
			getUserInfo() {
				getUserIndex({
						member_b_id: this.uid,
						member_id: this.uid
					})
					.then(res => {
						console.log(res);
						if (res.code == 1) {
							this.userInfo = res.data;
							this.login = true;
						}
					})
					.catch(error => {
						uni.showToast({
							title: error,
							icon: 'none',
							duration: 2000
						});
					})
			},
			submitLogin() {
				console.log(this.code);
				console.log(this.identifyCodeName);
				if (this.code.toLowerCase() !== this.identifyCodeName.toLowerCase()) {
					uni.showToast({
						title: '验证码错误！',
						icon: 'none',
						duration: 2000
					});
					return;
				}
				if (this.loginData.phone == '') {
					uni.showToast({
						title: '请填写手机号！',
						icon: 'none',
						duration: 2000
					});
					return;
				}
				if (this.loginData.password == '') {
					uni.showToast({
						title: '请填写密码！',
						icon: 'none',
						duration: 2000
					});
					return;
				}
				this.loginData.openid = uni.getStorageSync('openid');
				loginDo(this.loginData)
					.then(res => {
						console.log(res);
						if (res.code == 1) {
							uni.showToast({
								title: '登陆成功！',
								icon: 'none',
								duration: 2000
							});
							uni.setStorageSync('uid', res.data.id);
							uni.setStorageSync('userInfo', res.data);
							this.userInfo = res.data;
							this.login = true;
							this.loginMod = false;
						} else {
							uni.showToast({
								title: '帐号或密码错误！',
								icon: 'none',
								duration: 2000
							});
						}
					})
					.catch(error => {
						uni.showToast({
							title: error,
							icon: 'none',
							duration: 2000
						});
					})
			},
			loginAut() {
				var that = this;
				uni.showModal({
					title: '提示',
					content: '确定要退出吗？',
					success: function(res) {
						if (res.confirm) {
							uni.removeStorageSync('uid');
							that.login = false;
							that.loginData.openid = '';
							that.loginData.phone = '';
							that.loginData.password = '';
							that.code = '';
							that.getcheckCode();
						}
					}
				});
			},
			tn_ru(e) {
				var url = "/pages/packageA/user/apply_in?association_id=" + store.state.Gid;
				uni.navigateTo({
					url: e
				})
			},
			tn(e) {
				var uid = uni.getStorageSync('uid');
				console.log(this.userInfo);
				if (!this.login) {
					uni.showToast({
						title: '请登陆后查看',
						icon: 'none',
						duration: 2000
					});
					return;
				}
				if (this.userInfo.if_member == 2) {
					if (e == '/pages/packageA/user/my_edit') {
						uni.showToast({
							title: '请入会后查看',
							icon: 'none',
							duration: 2000
						});
						return;
					}
					if (e.includes("/pages/packageA/user/my_invite")) {
						uni.showToast({
							title: '请入会后查看',
							icon: 'none',
							duration: 2000
						});
						return;
					}
					if (e.includes("/pages/index/pizz_info")) {
						uni.showToast({
							title: '请入会后查看',
							icon: 'none',
							duration: 2000
						});
						return;
					}
					if (e.includes("/pages/packageA/user/my_card_holder")) {
						uni.navigateTo({
							url: e
						})
						return;
					}
					if (e.includes("/pages/packageA/user/my_card")) {
						uni.showToast({
							title: '请入会后查看',
							icon: 'none',
							duration: 2000
						});
						return;
					}
				}
				uni.navigateTo({
					url: e
				})
			}
		}
	}
</script>

<style scoped>
	.my_input view {
		width: 100% !important;
	}

	.my_input input {
		width: 100% !important;
	}
</style>