<template>
	<view class="back_view">
		<Settle :index="2">
			<view class="base-list">
				<view class="tip-text">
					<image class="tip-image" mode="widthFix" src="../../static/settle/anquan@3x.png"></image>
					<text>身份证照片、电工证照片、行驶证/驾驶证照片及车辆照片仅用于申请入驻。</text>
				</view>
				<view class="base-item">
					<view class="item item-select" @click="gopage('/pages/settle/select?selectType=area')">
						<view class="flex-center">
							<image class="item-image" src="../../static/settle/mixing@3x.png"></image>
							<text class="label">接单城市</text>
						</view>
						<p :class="['u-flex-1 u-text-right txt', {before: !cityText}]">{{ cityText ? '已选择' : '去选择' }}
						</p>
						<u-icon name="arrow-right" color="#2272FF" size="28"></u-icon>
					</view>
					<view class="tips">可以服务的城市，可选多个</view>
					<view v-if="">
						<view class="line-view-d"></view>
						<view class="city-text">{{cityText}}</view>
					</view>
				</view>
				<view class="base-item" style="height: 600rpx;">
					<view class="item item-select" @click="gopage('/pages/settle/select?selectType=working_age')">
						<view class="flex-center">
							<image class="item-image" src="../../static/settle/mixing@3x.png"></image>
							<text class="label">电工工龄(年)</text>
						</view>
						<p :class="['u-flex-1 u-text-right area', {before: !baseInfo.working_age}]">
							{{ baseInfo.working_age ? workingAgeTxt : '去选择'}}
						</p>
						<u-icon name="arrow-right" color="#2272FF" size="28"></u-icon>
					</view>
					<view class="tips">从事电工工作的工龄</view>
					<view class="line-view"></view>
					<view class="item itemView">
						<view>
							<text class="itemTitle">师傅数量</text>
						</view>
						<input class="itemInput" @input="countInput" v-model="name" type="number" value=""
							placeholder="请输入" placeholder-style='color:#2272FF;font-size:24rpx;' />
					</view>
					<view class="tips">可投入交付服务的人员数量</view>
					<view class="line-view"></view>
					<view>
						<view class="item item-select" @click="locationClick()">
							<view class="flex-center">
								<image class="item-image" src="../../static/settle/mixing@3x.png"></image>
								<text class="itemTitle">详细地址</text>
							</view>
							<view class="flex-center">
								<image class="item-image" src="../../static/settle/dizhi@3x.png"></image>
								<text class="right-label blueColor">自动定位</text>
							</view>
						</view>
						<view class="address-view">
							<textarea class="address-textarea" :cursor-spacing="40" maxlength="100"
								placeholder-style="color:#999999" v-model="baseInfo.address"
								placeholder="请填写可接收物料的详细地址" />
						</view>
					</view>
				</view>
			</view>
			<view class="seat"></view>
			<template v-slot:bottom>
				<view class="btn-wrap flex-xc">
					<button class="btn-submit" :disabled="!submitStatus" @click="$u.debounce(submitBaseInfo, 500)">
						确定
					</button>
				</view>
			</template>
		</Settle>
	</view>
</template>

<script>
	import Settle from '@/components/settle/index.vue'

	let r = require("../../libs/bmap-wx.min.js");
	let h = new r.BMapWX({
		// ak: "rpVq5d3yxaRCoKzVwmFMo0o4iC524CAG",
		ak: "lF1HoyXdiv4uZx6TrGQEZs48F5y8kEHI"
	});
	export default {
		components: {
			Settle
		},
		data() {
			return {
				baseInfo: {
					name: "",
					mobile: "",
					service_area: [],
					working_age: "",
					worker_num: "",
					address: ''
				},
				saving: false,
				areaTxts: [],
				workingAgeTxt: '',
				protocolContent: '',
				address: '',
				isPosting: false
			}
		},
		onLoad(e) {
			this.initLocation()
		},
		onShow() {
			if (this.isPosting) {
				this.isPosting = false
			}
		},

		computed: {
			submitStatus() {
				const baseInfo = this.baseInfo
				//baseInfo.name && baseInfo.mobile && 
				const flag = baseInfo.service_area && baseInfo.working_age !== '' && baseInfo.address
				return flag && !this.saving
			},
			cityText() {
				return this.areaTxts.join('、')
			}
		},
		components: {

		},
		methods: {
			initLocation() {
				const self = this
				uni.getLocation({
					type: 'wgs84',
					success: function(res) {
						self.baiduLocation(res.longitude, res.latitude)
					}
				});
			},
			baiduLocation(long, lat) {
				let a = this,
					t = function() {
						let t = {
							success: function(res) {
								let e = res.wxMarkerData[0];
								let i = e.address;
								a.baseInfo.address = i
								// a.setData({
								// 	addressValue: i
								// }), o.globalData.addressValue = i, a.initWatermarkBoxHeightAndWidth();
							},
							fail: function(a) {
								// n.error(a), console.log(a);
							}
						};
						if (lat && long) {
							let e = lat + "," + long
							// let e = "113.88308" + ',' + "22.55329"
							// let e = "22.55329" + ',' + "113.88308" 
							t.location = e;
						}
						h.regeocoding(t);
					};
				t()
			},

			//师傅数量
			countInput(e) {
				this.baseInfo.worker_num = e.detail.value
			},

			//定位点击
			locationClick: function() {
				let that = this
				uni.chooseLocation({
					success: function(res) {
						that.baseInfo.address = res.address + res.name
						//that.address = res.address + res.name
					},
					fail: function(res) {
						uni.showToast({
							title: '定位失败'
						})
					}
				});

			},

			gopage(url) {
				uni.navigateTo({
					url,
				});
			},
			initBaseInfo() {
				Indicator.open();
				let params = {
					id: '',
				}
				const res = this.$u.api.getSettleBaseInfo(params)
				uni.hideLoading()
				if (res.status != 200) {
					uni.showToast({
						title: res.message,
						icon: 'success'
					});
					return;
				}
				this.baseInfo = res.data
				uni.showToast({
					title: '入驻基本信息提交成功',
					icon: 'success'
				});
			},
			checkBaseInfo() {
				const baseInfo = this.baseInfo;
				let rulesObj = {
					/*
					name: function(value) {
						if (!value) {
							uni.showToast({
								title: '请输入您的姓名',
								icon: 'none'
							});
							return false;
						}
					},
					mobile: function(value) {
						const mobileReg = /^[1][3,4,5,7,8][0-9]{9}$/;
						if (!value) {
							uni.showToast({
								title: '请输入您的电话',
								icon: 'none'
							});
							return false;
							return false;
						} else if (!mobileReg.test(value)) {
							uni.showToast({
								title: '请输入正确的电话',
								icon: 'none'
							});
							return false;
						}
					},
					*/
					service_area: function(value) {
						if (!value || value.length == 0) {
							uni.showToast({
								title: '请选择您的接单城市',
								icon: 'none'
							});
							return false;
						}
					},
					working_age: function(value) {
						if (value === '') {
							uni.showToast({
								title: '请选择您的工龄',
								icon: 'none'
							});
							return false;
						}
					},
					//address
					address: function(value) {
						if (value === '') {
							uni.showToast({
								title: '请选择您的地址',
								icon: 'none'
							});
							return false;
						}
					},


				};
				const flagArr = Object.keys(baseInfo).some((key) => {
					// 返回验证失败的数组
					let flag = true;
					if (rulesObj.hasOwnProperty(key)) {
						const func = rulesObj[key];
						const value = baseInfo[key];
						if (typeof func === "function") {
							flag = func(value) === false ? false : true;
						}
					}
					return !flag;
				});
				return !flagArr || flagArr.length < 1;
			},

			/*
			shakeCLick: function() {
				let timer = setTimeout(() => {
					clearTimeout(timer)
					this.submitBaseInfo()
				}, 2000)
			},
			*/

			async submitBaseInfo() {

				// this.newSubmitBaseInfo()
				// return
				const baseInfo = this.baseInfo;
				if (!this.checkBaseInfo()) {
					return;
				}

				this.saving = true;
				uni.showLoading({
					title: '加载中'
				});
				const params = {
					...baseInfo,
					recommender: getApp().globalData.settleRecommend
				}
				console.log(params, 'params')
				params.service_area = baseInfo.service_area.join(',')
				const res = await this.$u.api.submitSettleBaseInfo(params)
				this.saving = false;
				uni.hideLoading()

				//this.isPosting = false

				if (res.code != 200) {
					uni.showToast({
						title: res.message,
						icon: 'none'
					});
					return;
				}
				uni.showToast({
					title: '入驻基本信息提交成功',
					icon: 'success'
				});
				let timer = setTimeout(() => {
					clearTimeout(timer)
					uni.navigateTo({
						url: '/pages/settle/file',
						"animationType": "fade-in",
						"animationDuration": 300
					})
				}, 2000)
			},

		},
	}
</script>

<style lang="scss" scoped>
	.back_view {
		background-color: #F4F5F7;
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
	}

	.base-list {
		.base-item {
			background-color: #FFFFFF;
			border-radius: 12rpx;
			padding: 30rpx;
			min-height: 160rpx;
			margin-bottom: 20rpx;
		}

		.line-view {
			background-color: #F4F5F7;
			height: 2rpx;
			width: 100%;
			margin-bottom: 30rpx;
			margin-top: 36rpx;
		}

		.line-view-d {
			background-color: #F4F5F7;
			height: 2rpx;
			width: 100%;
			margin-bottom: 20rpx;
			margin-top: 20rpx;
		}

		.city-text {
			font-size: 28rpx;
			color: #333333;
		}

		.item {
			margin-top: 6rpx;
			display: flex;
			width: 100%;
			font-size: 32rpx;
			background-color: #fff;
			align-items: center;

			.label {
				color: #333;
				font-size: 32rpx;
			}

			.txt {
				line-height: 44rpx;
				color: #999999;
				font-size: 24rpx;
				display: -webkit-box;
				overflow: hidden;
				text-overflow: ellipsis;
				word-wrap: break-word;
				white-space: normal !important;
				-webkit-line-clamp: 2;
				-webkit-box-orient: vertical;

				&.before {
					color: #2272FF;
				}
			}

			.item-image {
				width: 24rpx;
				height: 24rpx;
				margin-right: 10rpx;
			}

			.item-image {
				width: 26rpx;
				height: 30rpx;
				margin-right: 6rpx;
			}

			.required {
				margin: 0 6rpx;
				color: red;
				font-size: 32rpx;
			}

			.iconfont {
				color: #999;
			}
		}

		.tips {
			margin-top: 10rpx;
			font-size: 24rpx;
			color: #666;
		}

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

		.id-identity-box {
			.txt {
				padding-top: 8rpx;
				line-height: 40rpx;
				font-size: 28rpx;
				color: #666;

				&:first-child {
					margin-top: -58rpx;
					padding-top: 12rpx;
				}

				.red {
					color: red;
				}
			}

			// &.tips{
			//     margin-bottom:0!important;
			// }

		}

		/*选择内容*/
		.item-select {
			font-size: 30rpx;
			color: #333;
			font-weight: 500;
			justify-content: space-between;
		}

		.flex1 {
			flex: 1 !important;

			&.txt {
				white-space: nowrap;
				max-width: 450rpx;
				overflow: hidden;
				text-overflow: ellipsis;
			}
		}

		.item-input {
			overflow: hidden;

			input {
				text-align: right;
				line-height: 44rpx;
				width: 100%;
				font-size: 30rpx;
				color: #333;
				font-weight: 400;
			}

			.input-readonly {
				font-size: 30rpx;
				color: #999;
				font-weight: 400;
			}

			input::-webkit-input-placeholder {
				font-size: 30rpx;
				color: #999;
				font-weight: 400;
			}

			input:-moz-placeholder {
				font-size: 30rpx;
				color: #999;
				font-weight: 400;
			}

			input:-ms-input-placeholder {
				font-size: 30rpx;
				color: #999;
				font-weight: 400;
			}
		}
	}

	.right-label {
		font-size: 24rpx;
		color: #2272FF;
		margin-left: 4rpx;
		line-height: 22rpx;

		.slected {
			font-size: 28rpx;
			color: #333333;
		}
	}

	.title {
		padding: 30rpx 0 40rpx;
		text-align: center;
		line-height: 50rpx;
		font-size: 36rpx;
		color: #333;
		font-weight: bold;
	}

	.seat {
		padding-bottom: 168rpx;
	}

	/*提交按钮*/
	.btn-wrap {
		width: 100%;

		.btn-submit {
			width: 600rpx;
			height: 104rpx;
			line-height: 104rpx;
			border-radius: 52rpx;
			background: #2272FF;
			font-size: 32rpx;
			color: #fff;

			&[disabled] {
				background-color: #D1D4D4;
				color: #FFFFFF;
			}
		}
	}

	.itemView {
		background-color: #FFFFFF;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		width: 100%;
	}

	.itemTitle {
		background-color: #FFFFFF;
		font-size: 32rpx;
		font-weight: bold;
		color: #333333;
	}

	.itemInput {
		background-color: #FFFFFF;
		width: 65%;
		text-align: right;
		font-size: 28rpx;
		color: #333333;
	}


	.newAddressView {
		margin-top: 60rpx;
		background-color: #FFFFFF;
		width: 100%;
		display: flex;
		flex-direction: column;
		padding-bottom: 30rpx;
	}

	.newAddressTitleView {
		height: 64rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
	}

	.itemRedStar {
		font-size: 32rpx;
		color: #D02B2B;
	}

	.addressBackView {
		background-color: #FFFFFF;
		border: 2rpx solid #F3F3F3;
		margin-top: 24prx;
	}

	.addressTextAreaView {
		background-color: #FFFFFF;
		width: 640rpx;
		height: 112rpx;
		margin-top: 24rpx;
		margin-left: 24rpx;
		font-size: 28rpx;
		line-height: 40rpx;
		padding-bottom: 20rpx;
	}


	.right-margin-40 {
		margin-right: 40rpx;
	}

	.blueColor {
		color: #007AFF;
	}

	.area {
		max-width: 400rpx;
		color: #333333;
		font-size: 28rpx;

		&.before {
			color: #2272FF;
			font-size: 24rpx;
		}
	}

	.address-view {
		border: 2rpx solid #ECECEC;
		height: 112rpx;
		margin-top: 30rpx;
		padding: 16rpx 30rpx;
	}

	.address-textarea {
		font-size: 26rpx;
	}
	
	.tip-text {
		background-color: #FFFFFF;
		border-radius: 12rpx;
		padding: 38rpx 30rpx 30rpx 30rpx;
		height: 140rpx;
		margin-bottom: 20rpx;
		font-size: 24rpx;
		color: #000000;
		display: flex;
		align-items: flex-start;
	}
	
	.tip-image {
	width: 34rpx;
	margin-right: 8rpx;
	margin-top: 2rpx;
	}
</style>
