<template>
	<view class="margin-bottom-lg">
		<!-- 顶部操作条 -->
		<cu-custom :bgColor="'bg-main-color'" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">申请项目加盟</block>
		</cu-custom>
		<view class="padding description">
			上门服务市场是一个规模较大的、家庭常用的及商家高频使用的订单市场，现服务行业内单单家政服务类目据相关行业机构统计为约400项服务类目，市场规模超万亿元。加上电器、家具、废旧品等等服务类型，服务类目繁多而规模更为庞大，呈逐年递增。并且市场就在家的范围，是一项离家最近的行业之一，典型的家门口有红海生意。而服务方式又进行得更多元化，由以前的单师傅单技能单操作演变到现在系统性及模式化，形成了让有能力的、与有资源的、或有技能的不同群体，达成供应链式的无缝合作。现平台可以解决有兴趣服务业而又还没有服务技能的人群，为其提供加盟合作融入窗口；同时提供对各项服务技能学习的窗口加盟。与您一起创建更多城市社区类目的供应体系。
		</view>
		<view class="padding bg-white">
			<radio-group @change="changeOperatorType">
				<view class="flex justify-between align-center">
					<view class="flex justify-start">
						<view class="cu-avatar round middle-avatar first-avatar">
							<text class="cuIcon-activityfill"></text>
						</view>
						<view class="margin-left-sm flex-column-around">
							<view>我无服务技能</view>
							<view>申请加入业务拓展</view>
						</view>
					</view>
					<radio class="main-color" value="businessDevelopment" />
				</view>
				<view class="flex justify-between align-center margin-top-sm">
					<view class="flex justify-start">
						<view class="cu-avatar round middle-avatar second-avatar">
							<text class="cuIcon-activityfill"></text>
						</view>
						<view class="margin-left-sm flex-column-around">
							<view>我无服务技能</view>
							<view>申请加入副业发展</view>
						</view>
					</view>
					<radio class="main-color" value="sideline" />
				</view>
				<view class="flex justify-between align-center margin-top-sm">
					<view class="flex justify-start">
						<view class="cu-avatar round middle-avatar third-avatar">
							<text class="cuIcon-activityfill"></text>
						</view>
						<view class="margin-left-sm flex-column-around">
							<view>我无服务技能</view>
							<view>申请加入技能学习并提供技能</view>
						</view>
					</view>
					<radio class="main-color" value="skillLearning" />
				</view>
				<view class="flex justify-between align-center margin-top-sm">
					<view class="flex justify-start">
						<view class="cu-avatar round middle-avatar fourth-avatar">
							<text class="cuIcon-activityfill"></text>
						</view>
						<view class="margin-left-sm flex-column-around">
							<view>我有社区兴趣</view>
							<view>申请共同打造社区类目的供应链</view>
						</view>
					</view>
					<radio class="main-color" value="communitySupplyChain" />
				</view>
			</radio-group>
		</view>
		<view class="padding-sm bg-white">
			<view class="solid radius padding-bottom">
				<view class="text-xl text-black text-center">
					<text class="float-info padding-lr bg-white">申请信息</text>
				</view>
				<form @submit="submit">
					<view class="cu-form-group">
						<view class="title">您的姓名<text class="text-red">*</text></view>
						<input name="name" placeholder="请输入姓名"></input>
					</view>
					<view class="cu-form-group">
						<view class="title">注册电话<text class="text-red">*</text></view>
						<input name="phone" placeholder="请输入手机号" v-model="formData.phone"></input>
					</view>
					<view class="cu-form-group">
						<view class="title">申请城市<text class="text-red">*</text></view>
						<picker :mode="'multiSelector'" @change="regionChange" @columnchange="regionColChange" :value="multiIndex" :range-key="'areaName'" :range="areaList">
							<view class="picker">
								{{formData.area && formData.area.length ? formData.area[0].areaName + '-' + formData.area[1].areaName : '请选择'}}
							</view>
						</picker>
					</view>
					<view class="cu-form-group">
						<view class="title">验证码<text class="text-red">*</text></view>
						<input class="radius-input" type="number" name="code" placeholder="请输入验证码"></input>
						<view v-if="countDownNum === 0" class="cu-btn bg-blue round-btn margin-left-xs shadow-blur" @click="sendVertifyCode">发送验证码
						</view>
						<view v-else class="cu-btn bg-gray round-btn margin-left-sm shadow-blur">已发送({{countDownNum}})
						</view>
					</view>
					<view class="cu-form-group">
						<view class="title">关联信息</view>
						<input name="remark" placeholder="请输入入驻关联信息备注"></input>
					</view>
					<view class="cu-form-group">
						<view>
							<checkbox style="transform:scale(0.7)" class="main-color" :value="formData.agreeContract" :checked="formData.agreeContract"
								@click="changeAgreeContract">
							</checkbox>
							<text class="margin-left-xs" @click="showContract">合同同意书链接</text>
						</view>
					</view>
					<view class="margin-lr-sm">
						<button class="bg-main-color long-btn" form-type="submit">提交</button>
					</view>
				</form>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				curUserInfo: {},
				areaList: [],
				multiIndex: [0, 0],
				countDownNum: 0,
				formData: {
					subType: '',
					agreeContract: false,
					area: null
				},
			}
		},
		onLoad() {
			this.loadData();
		},
		methods: {
			loadData() {
				this.curUserInfo = this.$request.getCurUserInfo();
				this.loadAreaList();
			},
			changeOperatorType(e) {
				this.formData.subType = e.detail.value;
			},
			changeAgreeContract() {
				this.formData.agreeContract = !this.formData.agreeContract;
			},
			showContract() {
				uni.navigateTo({
					url: '/pages/my/contract'
				})
			},
			async loadAreaList() {
				let areaList = await this.$request.areaListByStep();
				areaList = areaList.data;
				let col1Code = areaList[0].areaCode;
				let subAreaList = await this.$request.areaListByStep({ parentCode: col1Code });
				subAreaList = subAreaList.data;
				// let col2Code = subAreaList[0].areaCode;
				// let subSubAreaList = await this.$request.areaListByStep({ parentCode: col2Code });
				// subSubAreaList = subSubAreaList.data;
				this.areaList.push(areaList);
				this.areaList.push(subAreaList);
				// this.areaList.push(subSubAreaList);
			},
			regionChange(e) {
				this.multiIndex = e.detail.value;
				let chosenArea = [];
				for(let i = 0; i < this.areaList.length; i++) {
					chosenArea.push(this.areaList[i][this.multiIndex[i]]);
				}
				this.formData.area = chosenArea;
			},
			async regionColChange(e) {
				let colObj = e.detail;
				if (colObj.column == 0) {
					let subSubAreaList = [];
					// 通过一级查询二级，通过二级查三级
					let subAreaList = await this.$request.areaListByStep({parentCode: this.areaList[0][colObj.value].areaCode});
					subAreaList = subAreaList.data;
					this.areaList.pop();
					this.areaList.push(subAreaList);
					this.multiIndex = [colObj.value, 0];
				}
			},
			startCountDownNum() {
				this.countDownNum = 180;
				let t = setInterval(() => {
					this.countDownNum -= 1;
					if (this.countDownNum === 0) {
						clearInterval(t);
					}
				}, 1000);
			},
			async sendVertifyCode() {
				console.log(this.formData)
				if (this.$validate.validContactNum(this.formData.phone)) {
					let res = await this.$request.sendVertifyCode({
						to: this.formData.phone
					});
					if (res && res.code === 0) {
						uni.showToast({
							icon: 'none',
							title: '验证码已发送'
						})
						this.startCountDownNum();
					} else if (res && res.msg) {
						uni.showToast({
							icon: 'none',
							title: res.msg
						})
					} else {
						uni.showToast({
							icon: 'none',
							title: '验证码发送失败'
						})
					}
				} else {
					uni.showToast({
						icon: 'none',
						title: '请正确输入手机号码'
					})
				}
			},
			validateForm(confirmFormData) {
				let formInfoValid = Boolean(confirmFormData.name) &&
					Boolean(confirmFormData.phone) &&
					Boolean(confirmFormData.area) &&
					Boolean(confirmFormData.code);
				if (!formInfoValid) {
					uni.showToast({
						title: '请填写完整信息',
						icon: 'none',
						mask: true
					})
				} else if (!confirmFormData.agreeContract) {
					uni.showToast({
						title: '请阅读并同意合同书',
						icon: 'none',
						mask: true
					})
					formInfoValid = false;
				}
				return formInfoValid;
			},
			async submit(e) {
				let formData = {
					...e.detail.value,
					...this.formData,
					city: this.formData.area && this.formData.area.length ? this.formData.area[this.formData.area.length - 1].areaId : null,
					customerId: this.curUserInfo.customerId,
					type: 'joiningProject'
				}
				let formValid = this.validateForm(formData);
				if (formValid) {
					let res = await this.$request.addCustomerPlace(formData);
					if (res && res.code === 0) {
						await this.$request.refreshCurUserCache();
						uni.navigateBack({
							delta: -1
						})
						uni.showToast({
							title: '提交成功',
							icon: 'success',
							duration: 2000
						})
					} else if (res && res.msg) {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						})
					} else {
						uni.showToast({
							title: '提交失败',
							icon: 'error',
							duration: 2000
						})
					}
				}
			}
		}
	}
</script>

<style scoped>
	.description {
		line-height: 200%;
		color: #676666;
	}

	.middle-avatar {
		width: 100rpx;
		height: 100rpx;
	}

	.middle-avatar [class*="cuIcon-"] {
		font-size: 53rpx !important;
	}

	radio-group {
		display: unset;
	}

	.first-avatar {
		background-image: linear-gradient(45deg, #Ec008c, #6739b6);
		color: #ffffff;
	}

	.second-avatar {
		background-image: linear-gradient(45deg, #9000ff, #5e00ff);
		color: #ffffff;
	}

	.third-avatar {
		background-image: linear-gradient(45deg, #0081ff, #1cbbb4);
		color: #ffffff;
	}
	
	.fourth-avatar {
		background-image: linear-gradient(45deg, #39b54a, #8dc63f);
		color: #ffffff;
	}

	.float-info {
		display: inline-block;
		position: relative;
		top: -18rpx;
		z-index: 1;
	}

	.radius {
		border-radius: 37rpx;
	}
</style>
