<template>
	<view class="padding-top container">
		<!-- tab选项 -->
		<view class="tabTitle">
			<text :class="{'actived': tabType===1}" @click="switchTab(1)">基本信息</text>
			<text :class="{'actived': tabType===2}" @click="switchTab(2)">邀请信息</text>
		</view>
		<!-- tab内容 -->
		<view class="tabContent">
			<!-- 基本信息 -->
			<view v-if="tabType===1">
				<view class="ulBox">
					<view class="liBox">
						<text>姓名：</text>
						<text><input v-model="dataForm.name" class="uni-input" maxlength="15" placeholder="姓名" /></text>
					</view>
					<view class="liBox">
						<text>手机号：</text>
						<text><input v-model="dataForm.mobileNumber" class="uni-input" type="number" maxlength="11"
								placeholder="手机号" /></text>
					</view>
					<view class="liBox">
						<text>申请身份：</text>
						<text>
							<picker @change="bindPickerChange" :value="applyIdentity" :range="identityArr">
								<view class="uni-input">{{identityArr[applyIdentity]}}</view>
							</picker>
						</text>
					</view>
					<view class="liBox" v-if="applyIdentity == 0">
						<text>工作室名称：</text>
						<text><input v-model="dataForm.b1Name" class="uni-input" placeholder="工作室名称" /></text>
					</view>
					<view class="liBox">
						<text>所属组织：</text>
						<text>
							<input v-if="isCross==true" v-model="dataForm.areaName" class="uni-input"
								placeholder="请填写组织名称" />
							<picker v-else @change="bindPChange" :value="index" :range="orgdataList">
								<view class="uni-input">{{ orgdataList[index]}}</view>
							</picker>
						</text>
					</view>
					<view class="liBox">
						<text>是否跨区邀请：</text>
						<view class="isCrossBox">
							<view :class="{checked:isCross}">
								<i class="circle" @click="isCross=true"></i>
								<text>是</text>
							</view>
							<view :class="{checked:!isCross}">
								<i class="circle" @click="isCross = false"></i>
								<text>否</text>
							</view>
						</view>
					</view>
					<!-- 			<view class="liBox"  v-if="isCross== true">
						<text>所属组织名称：</text>
						<text>
							<picker @change="bindPickerChange" :value="applyIdentity" :range="identityArr">
								<view class="uni-input">{{identityArr[applyIdentity]}}</view>
							</picker>
						</text>
					</view> -->
				</view>
				<share></share>
				<button type="primary" plain="true" class="sendInvite" @click="shareToggle()">发出邀请</button>
			</view>
			<!-- 邀请信息 -->
			<view class="inviteListsBox" v-if="tabType===2">
				<h4 class="noListTip" v-if="!inviteLists || inviteLists.length<=0">暂无邀请记录！</h4>
				<view class="listUl ulBox" v-for="item in inviteLists">
					<view class="liBox">
						<text>姓名：</text>
						<text>{{item.name}}</text>
					</view>
					<view class="liBox">
						<text>手机号：</text>
						<text>{{item.mobileNumber}}</text>
					</view>
					<view class="liBox">
						<text>申请身份：</text>
						<text>{{item.partnerLevel == 'A1'? '事业伙伴' : '工作室'}}</text>
					</view>
					<view class="liBox">
						<text>所属组织：</text>
						<text>{{item.orgName}}</text>
					</view>
					<view class="liBox">
						<text>是否跨区邀请：</text>
						<text>{{item.hasCrossDistrict==1 ? '是' : '否'}}</text>
					</view>
					<view class="liBox">
						<text>邀请状态：</text>
						<text v-if="item.approvalStatus=='0'">邀请中</text>
						<text v-else-if="item.approvalStatus=='1'">审核中</text>
						<text v-else-if="item.approvalStatus=='3'" class="success">邀请成功</text>
						<text v-else class="fail">邀请失败</text>
					</view>
					<view class="liBox-footer" v-if="item.approvalStatus == 0 || item.approvalStatus== '2'"
						@click="reinvite(item.id)">
						<text>重新邀请</text>
					</view>
				</view>
			</view>
		</view>
		<!-- 分享信息 -->
		<view>
			<uni-popup ref="share" type="bottom" backgroundColor="#fff">
				<view class="popup-content">
					<view class="shareBox">
						<view class="shareOptionItem" @click="uniShare(1)">
							<view class="imgBox">
								<image src="../../static/app-plus/sharemenu/wechatfriend.png" mode="widthFix"></image>
							</view>
							<text>微信好友</text>
						</view>
						<view class="shareOptionItem" @click="uniShare(2)">
							<view class="imgBox">
								<image src="../../static/app-plus/sharemenu/wechatmoments.png" mode="widthFix"></image>
							</view>
							<text>分享朋友圈</text>
						</view>
					</view>
				</view>
				<view class="popup-footer" @click="closeShare()">
					<view>取消</view>
				</view>
			</uni-popup>
		</view>
	</view>
</template>

<script>
	import api from "@/api/api"
	import common from '../../common/common';
	export default {
		data() {
			return {
				dataForm: {
					applyType: '1', //申请类型邀请加盟传 1 申请加盟传 2
					operatStep: '1', // 99:最后一步,1:第一步,2:为第二步该obj 为第一步
					name: '', //姓名
					mobileNumber: '', //被邀请人手机号
					b1Name: '', // 工作室
					partnerLevel: '', //被邀请人身份
					hasCrossDistrict: '', //是否跨区邀请
					areaId: '', //所属组织Id
					areaName: '' //所属组织名称
				},
				userId: uni.getStorageSync('cffp_userId'),
				tabType: 1,
				identityArr: ['工作室', '事业伙伴'],
				applyIdentity: 0,
				index: 0,
				isCross: false,
				inviteLists: [],
				orgList: [],
				orgdataList: [],
				queryorgList: [],
				shareId: '',
				realName:''
			};
		},
		onLoad() {
			this.getqueryOrgList()
			let dataForm = uni.getStorageSync('userinfodataForm')
			this.realName = dataForm.realName
		},
		methods: {
			queryorgchange(e) {
				this.orgindex = e.detail.value
			},
			bindPChange(){
				console.log('1')
			},
			// 查询组织list
			getqueryOrgList() {
				let CffpAreaQueryReqVO = {
					userId: this.userId,
					partnerLevel: '',
					// this.applyIdentity == 0 ? 'A1' : 'B1'
				}
				api.queryOrgList(CffpAreaQueryReqVO).then(res => {
					if (res['success']) {
						this.queryorgList = res.data.cffpAreaQuerys
						this.orgList = res.data.cffpAreaQuerys
						this.changeorgkey()

						// this.orgList = res.data.cffpAreaQuerys.map((x) => x.areaName)
						// console.log(this.orgList, 22225)
					}
				})
			},
			switchTab(type) {
				this.tabType = type;
				if (this.tabType === 2) {
					this.queryInviteList()
				}
			},
			bindPickerChange: function(e) {
				this.applyIdentity = e.detail.value
				this.changeorgkey()
			},
			changeorgkey() {
				let that = this
				let orgKey = that.applyIdentity == 0 ? 'B1' : 'A1'
				that.orgdataList = []
			
				console.log(that.orgList, 1115)
				const list = that.orgList.map(item => {
					if(item.partnerLevel == orgKey || item.partnerLevel == 'ALL'){
						return item
					}
				});
				for (var i = 0; i < list.length; i++) {
					if (list[i] == undefined) {
						list.splice(i,1)
					}
				}
				that.orgdataList = list.map((x) => x.areaName)
			},
			// 发起邀请事件
			shareToggle() {
				this.dataForm.partnerLevel = this.applyIdentity == 0 ? 'B1' : 'A1'
				this.dataForm.hasCrossDistrict = this.isCross == true ? '1' : '0'
				this.dataForm.areaId = this.queryorgList[this.index].areaId
				this.dataForm.userId = this.userId
				this.dataForm.areaName = this.isCross == false ? this.queryorgList[this.index].areaName : this.dataForm
					.areaName
				// this.dataForm.b1Name = this.applyIdentity == 'B1' ? this.dataForm.b1Name : null
				// if(this.applyIdentity == 'B1'){
				// 	if(this.dataForm.b1Name ==''){

				// 	}
				// }
				if (this.dataForm.name == "" || this.dataForm.mobileNumber == "" || this.dataForm.areaName == '' || this
					.applyIdentity == 'B1' && this.dataForm.b1Name == '') {
					uni.showToast({
						title: '请将信息补充完整',
						duration: 2000,
						icon: 'none'
					});
					return false
				}
				if (common.mobileNoValid(this.dataForm.mobileNumber)) {
					api.saveApplyInfo(this.dataForm).then(res => {
						if (res['success']) {
							// this.switchTab(2)
							this.shareId = res.data.id
							this.$refs.share.open()
							// uni.navigateTo({
							// 	url: '/pages/invitationRegister/invitationRegister?shareId=' + this.shareId
							// })
						}
						uni.showToast({
							title: res['message'],
							duration: 2000,
							icon: 'none'
						});
					})
				} else {
					common.errorDialog(2, '手机号校验错误')
				}

			},
			closeShare() {
				this.$refs.share.close()
			},
			reinvite(id) {
				this.shareId = id
				this.$refs.share.open()
			},
			uniShare(type) {
				if (type === 1) {
					uni.share({
						provider: "weixin",
						scene: "WXSceneSession",
						type: 0,
						href: "https://mdev.zuihuibi.cn/cffp/pages/invitationRegister/invitationRegister?shareId=" +
							this.shareId,
						title: "CFFP家庭财务策划师联盟邀您加入",
						summary: `我是家庭财务策划师${this.realName}正在使用CFFP财富中心，点击下载即刻加入！`,
						imageUrl: "https://mdev.zuihuibi.cn/cffp/static/cffp_logo.jpg",
						success: function(res) {
							console.log("success:" + JSON.stringify(res));
						},
						fail: function(err) {
							console.log("fail:" + JSON.stringify(err));
						}
					});
				} else if (type === 2) {
					uni.share({
						provider: "weixin",
						scene: "WXSceneTimeline",
						type: 0,
						href: "https://mdev.zuihuibi.cn/cffp/pages/invitationRegister/invitationRegister?shareId=" +
							this.shareId,
						title: "CFFP家庭财务策划师联盟邀您加入",
						summary: `我是家庭财务策划师${this.realName}正在使用CFFP财富中心，点击下载即刻加入！`,
						imageUrl: "https://mdev.zuihuibi.cn/cffp/static/cffp_logo.jpg",
						success: function(res) {
							console.log("success:" + JSON.stringify(res));
						},
						fail: function(err) {
							console.log("fail:" + JSON.stringify(err));
						}
					});
				}
			},
			queryInviteList() {
				api.queryInviteList({
					userId: this.userId
				}).then(res => {
					if (res['success']) {
						this.inviteLists = res['data']['data']
					}
				})
			},


		}
	}
</script>

<style lang="scss">
	.container {
		position: relative;
		height: 100vh;

		.tabTitle {
			color: #666666;
			background-color: #fff;
			display: flex;
			align-items: center;
			justify-content: space-around;
			padding: 30rpx 60rpx 0;

			text {
				&.actived {
					color: #333;
					border-bottom: 4rpx solid #20269B;
				}
			}
		}

		.tabContent {
			margin-top: 10rpx;

			.ulBox {
				width: 95%;
				margin: 0 auto 20rpx;
				padding: 10rpx 20rpx;
				color: #333;
				border-radius: 10rpx;
				background-color: #fff;
				box-sizing: border-box;
				position: relative;

				.liBox {
					border-bottom: 1px solid #F0F0F0;
					height: 76rpx;
					display: flex;
					justify-content: space-between;
					align-items: center;

					&:last-child {
						border: none;
					}

					.uni-input {
						text-align: right;
					}

					text {
						&.success {
							color: #78C06E;
						}

						&.fail {
							color: #FF0000;
						}
					}

					.isCrossBox {
						display: flex;
						align-items: center;

						view {
							display: flex;
							align-items: center;

							&:first-child {
								margin-right: 20rpx;
							}

							.circle {
								display: block;
								width: 28rpx;
								height: 28rpx;
								border: 2rpx solid #20269B;
								border-radius: 50%;
								position: relative;
								margin: 4rpx 10rpx 0 10rpx;
							}

							&.checked {
								.circle {
									&::before {
										position: absolute;
										content: '';
										width: 20rpx;
										height: 20rpx;
										border-radius: 50%;
										left: 50%;
										top: 50%;
										background-color: #20269B;
										transform: translate(-50%, -50%);
									}
								}
							}

						}


					}

				}

				&.listUl::before {
					display: block;
					content: '';
					position: absolute;
					left: 10rpx;
					top: 20rpx;
					width: 8rpx;
					height: 20rpx;
					background-color: #FA882F;
					border-radius: 8rpx;
				}
			}

			.sendInvite {
				position: absolute;
				left: 50%;
				top: 80%;
				transform: translateX(-45%);
				width: 60%;
				border-radius: 80rpx;
				background-color: #20269B;
				color: #fff;
				font-size: 36rpx;
			}
		}

		.popup-content {
			font-size: 32rpx;
			color: #666;
			padding-bottom: 40rpx;

			.shareBox {
				display: flex;
				justify-content: space-around;
				align-items: center;

				.shareOptionItem {
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;

					.imgBox {
						width: 100rpx;
					}
				}

			}
		}

		.liBox-footer {
			width: 200rpx;
			height: 50rpx;
			color: #FFFFFF;
			margin-top: 10rpx;
			text-align: center;
			line-height: 50rpx;
			border-radius: 30rpx;
			background: #20269B;
		}

		.popup-footer {
			color: #666;
			font-size: 36rpx;
			border-top: 2rpx solid #f0f0f0;
			display: flex;
			justify-content: center;
			align-items: center;
			padding-top: 36rpx;
		}

	}
</style>
