<template>
	<view class="main">
		<u-navbar
			title="合作申请"
			:safeAreaInsetTop="true"
			:fixed="true"
			bgColor="#FFFFFF00"
			@leftClick="onTouchBack"
		>
		</u-navbar>
		<image class="top_banner_global" src="../../static/images/kehu/bg_2.png"></image>
		<view style="width: 750rpx;height: 1300rpx;position: relative;">
			<view class="body">
				<view style="width:100%;height:50rpx;position: relative;top: 0;left: 0;right: 0;display: flex;flex-direction: row;align-items: center;justify-content: flex-start;">
					<view style="width: 8rpx;height: 22rpx;background: #D49B4B;opacity: 1;border-radius: 1px 5px 5px 1px;margin-left: 20rpx;"/>
					<text class="title_text">{{getTitleText()}}</text>
				</view>
				<view class="u-form-body">
					<u-form
						labelPosition="left"
						ref="form1"
						:model="person"
						:rules="rules"
						:labelStyle="form_tile_title"
						customStyle="position: relative;"
					>
						<u-form-item
							prop="person.name"
							borderBottom
							ref="name"
							label="姓名*"
						>
							<input
								:placeholder="rules['person.name'].message"
								class="form_input"
								:value="person.name"
								placeholder-class="form_holder"
								@blur="rules['person.name'].validator"
							/>
						</u-form-item>
						<u-form-item
							label="电话*"
							prop="person.phone"
							borderBottom
							ref="phone"
						>
							<input
								:placeholder="rules['person.phone'].message"
								class="form_input"
								placeholder-class="form_holder"
								:value="person.phone"
								@blur="rules['person.phone'].validator"
							/>
						</u-form-item>
						<u-form-item
							label="所在地址"
							prop="person.address"
							borderBottom
							ref="city"
							@click="onClickToSelectCity"
						>
							<pick-regions :defaultRegion="defaultRegion" @getRegion="handleGetRegion">
								<input
									:placeholder="rules['person.address'].message"
									class="form_input"
									:disabled="true"
									:value="person.address"
									placeholder-class="form_holder"
								/>
							</pick-regions>
							<u-icon
								slot="right" 
								name="arrow-right"
							></u-icon>
						</u-form-item>
						<u-form-item
							label="推荐人电话"
							prop="person.referrer"
							borderBottom
							ref="referrer"
						>
							<input
								:placeholder="rules['person.referrer'].message"
								class="form_input"
								placeholder-class="form_holder"
								:value="person.referrer"
								@blur="rules['person.referrer'].validator"
							/>
						</u-form-item>
					</u-form>
					<view class="checkboxgroup" :key="isAgree.toString()">
						<checkbox :checked="isAgree" value="1" @click="onChechBoxChange"/>
						<text class="label" @click="onClickAreement">
							我已阅读并同意 [协议条款]
						</text>
					</view>
					<view style="width: 702rpx;height: 120rpx;position: relative;top: 24rpx;">
						<text class="attentionText">注意事项</text>
						<text class="attentionInfo">
							{{pageData.note}}
						</text>
					</view>
				</view>
			</view>
			<button class="button" @click="onTouchConfirm">提交</button>
			<u-toast ref='uToast'/>
			<u-popup 
				:show="isShowUpload"
				:closeOnClickOverlay="false"
				mode="center"
				bgColor="transparent"
			>
				<UploadSuccess @confirm="onTouchBack" />
			</u-popup>
		</view>
	</view>
</template>

<script>
	import {post} from '@/common/api.js'
	import UploadSuccess from "@/components/UploadSuccess/UploadSuccess.vue";
	
	var Player 	= getApp().globalData.Player;
	var Define	= getApp().globalData.Define;
	var pageData = {
		note : '',
		partnerinfo : {},
		//0->未申请，1->已申请，未审核。2->通过。3->已拒绝
		partnerstatus : 0,
		toppic : {},
	};
	export default {
		components:{
			UploadSuccess,
		},
		onLoad() {
			uni.showLoading({
				
			});
			var agreementData = getApp().globalData.agreementData;
			if(agreementData.textmsg == ''){
				this.$utils.getAgreementData();
			}
			let self	= this;
			post('pages/getbecpartner',{}).then(
				res=>{
					uni.hideLoading();
					var info = Define.onNetMessage(res);
					if(!info){
						//没有取到数据
						return;
					}
					self.pageData 	= info;
					console.log("合作申请页面信息：",info);
					//这是用户数据
					var partnerinfo = info.partnerinfo;
					var person		= self.person;
					person.name		= partnerinfo.name ?? '';
					person.phone	= partnerinfo.phone ?? '';
					person.referrer	= partnerinfo.recommender ?? '';
					var province	= partnerinfo.province;
					var city		= partnerinfo.city;
					var area		= partnerinfo.area;
					if(province && province!=''){
						self.defaultRegion 	= [province,city,area];
						person.address		= province + " " + city + " " + area;
					}
					if(info.partnerstatus > 0){
						self.isAgree	= true;
						console.log("强制改变当前选中状态");
					}
				}
			);
		},
		data() {
			return {
				pageData,
				///是否同意协议条款
				isAgree : false,
				///是否显示提交成功的界面
				isShowUpload : false,
				person : {
					///名字
					name : '',
					///电话号码
					phone : Player.phone,
					///所在地址
					address : '',
					///推荐人电话
					referrer : '',
				},
				rules : {
					"person.name" : {
						type: 'string',
						required: true,
						message : '请输入您的姓名',
						validator : (object)=>{
							this.person.name = object.detail.value;
						}
					},
					'person.phone' : {
						type: 'string',
						required: true,
						message : '请输入您的联系电话',
						///校验用户是否输入了正确的手机号码
						validator : (object)=>{
							var suc = uni.$u.test.mobile(object.detail.value);
							if(!suc){
								uni.$u.toast("请输入正确的电话号码！");
							}else{
								this.person.phone = object.detail.value;
							}
							
							return suc;
						}
					},
					'person.address' : {
						type : 'string',
						required : false,
						message: '请选择您的地址',
					},
					'person.referrer' : {
						type : 'string',
						required : false,
						message : '请输入推荐人电话号码',
						///校验用户是否输入了正确的手机号码
						validator : (object)=>{
							var suc = uni.$u.test.mobile(object.detail.value);
							if(!suc){
								uni.$u.toast("请输入正确的电话号码！");
							}else{
								this.person.referrer = object.detail.value;
							}
							return suc;
						},
					},
				},
				form_tile_title : {
					width: '170rpx',
					height: '37rpx',
					fontSize: 26,
					fontWeight: 500,
					color: 'black',
				},
				defaultRegion:['四川省','成都市','武侯区'],
				region:[],
				
			};
		},
		methods:{
			onTouchBack(){
				uni.navigateBack({
					
				});
			},
			///点击并选择城市
			onClickToSelectCity(){
				uni.hideKeyboard();
			},
			// 获取选择的地区
			handleGetRegion(region){
				this.region = region;
				this.person.address = this.regionName();
				this.$refs.form1.validateField('person.address');
			},
			regionName(){
				return this.region.map(item=>item.name).join(' ');
			},
			onChechBoxChange(e){
				console.log(e);
				if(e.detail.value.length > 0){
					this.isAgree = true;
				}else{
					this.isAgree = false;
				}
				getApp().globalData.agreementData.isAgreed = this.isAgree;
			},
			/**
			 * 点击了协议字体，要去看看协议内容
			 */
			onClickAreement(){
				uni.navigateTo({
					url:'/pages/Agreement/Agreement',
				})
			},
			showToast(content){
				this.$refs.uToast.show(
					{
						message : content,
						type : 'default',
					}
				);
			},
			/**
			 * 点击了提交按钮
			 */
			async onTouchConfirm(){
				if(!this.isAgree){
					this.showToast('请阅读并同意协议条款');
					return;
				}
				var name = this.person.name;
				if(name == ''){
					this.showToast('请输入您的名字');
					return;
				}
				var phone = this.person.phone;
				if(phone == ''){
					this.showToast('请输入您的电话号码');
					return;
				}
				var data = {
					name : name,
					phone : phone,
					status : 0,
					provincecode : 0,
					citycode : 0,
					areacode : 0,
					province : '',
					city : '',
					area : '',
					recommender : this.person.referrer,
				};
				if(this.region.length > 0){
					//如果当前有地址
					var p = this.region[0].name ?? '';
					var c = this.region[1].name ?? '';
					var a = this.region[2].name ?? '';
					data['province'] 	= p;
					data['city']		= c;
					data['area']		= a;
				}
				console.log(data);
				uni.showLoading();
				var res = await post("pages/becpartner",data);
				uni.hideLoading();
				var out = getApp().globalData.Define.onNetMessage(res);
				if(!out){
					return;
				}
				console.log("申请结果：",out);
				getApp().globalData.mainPageData.partnerstatus = 1;
				this.isShowUpload = true;
			},
			/**
			 * 获取显示最上面的文字
			 */
			getTitleText(){
				var partnerstatus = this.pageData.partnerstatus;
				var str	= '';
				if(partnerstatus == 0){
					str = '尚未申请';
				}else if(partnerstatus == 1){
					str = '请等待审核';
				}else if(partnerstatus == 2){
					str = '您已是官方合作人员';
				}else{
					str = '您的申请已被拒绝，请重新申请';
				}
				str = "个人信息 (" + str + ")";
				return str;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.body{
		width: 702rpx;
		height: 900rpx;
		position: relative;
		left: 24rpx;
		right: 24rpx;
		top: 295rpx;
		background: #FFFFFF;
		opacity: 1;
		border-radius: 20rpx;
	}
	.title_text{
		//width: 192rpx;
		font-size: 32rpx;
		font-weight: bold;
		color: #333333;
		opacity: 1;
		margin-left: 10rpx;
	}
	.u-form-body{
		position: relative;
		width: 662rpx;
		left: 20rpx;
		right: 20rpx;
		height: 840rpx;
		//background-color: #1CBBB4;
		border-radius: 20rpx;
	}
	.form_input{
		//background-color: #0081FF;
		width: 100%;
		height: 90rpx;
		text-align: right;
		font-size: 28rpx;
		font-weight: 400;
		color: #333333;
	}
	.form_holder{
		width: 100%;
		height: 90rpx;
		text-align: right;
		font-size: 24rpx;
		font-weight: 400;
		color: #999999;
		margin-right: 10rpx;
	}
	.form_label_style{
		height: 37rpx;
		font-size: 26rpx;
		font-weight: 400;
		color: #666666;
		opacity: 1;
	}r
	///注意事项
	.attentionText{
		width: 104rpx;
		height: 37rpx;
		font-size: 26rpx;
		font-weight: 400;
		color: #666666;
		opacity: 1;
	}
	.attentionInfo{
		width: 648rpx;
		font-size: 24rpx;
		font-weight: 400;
		color: #999999;
		opacity: 1;
		max-height: 120rpx;
	}
	
	.label{
		text-decoration: underline;
		color: #39B54A;
		font-size: 26rpx;
		font-weight: 400;
	}
	.checkboxgroup{
		width: 662rpx;
		height: 100rpx;
		//background-color: #9000FF;
		position: relative;
		top: 10rpx;
		display: flex;
		align-items: center;
		flex-direction: row;
		justify-content: flex-start
	}
	.button{
		width: 702rpx;
		height: 90rpx;
		background: #D49B4B;
		opacity: 1;
		border-radius: 93rpx;
		position: absolute;
		left: 24rpx;
		bottom: -10rpx;
		font-size: 36rpx;
		font-weight: 400;
		color: #FFFFFF;	
	}
</style>
