<template>
	<view>
		<view class='wrap'>
			<view class="c-row">
				<view class="title">打卡原因</view>
				<view style='padding:0;' class="con-list">
					<u-radio-group activeColor="#22C572" v-model="typevalue">
						<u-radio key="3" label="下班" name="3">下班</u-radio>
						<u-radio key="1" label="上班" name="1">上班</u-radio>
					</u-radio-group>
				</view>
			</view>
			<view class="c-row">
				<view class="title">目标位置</view>
				<view class="con-list">
					<view @click='show=true'>{{detailData.targetLocation}}</view>
					<u-picker :range="warehouseBaseInfoList" range-key="warehouseName" @confirm='targetLPicker($event)'
						v-model="show" mode="selector">
					</u-picker>
				</view>
			</view>
			<view class="c-row">
				<view class="title">打卡距离</view>
				<view class="con-list">
					<view>{{clockDistance}}</view>
				</view>
			</view>
		</view>
		<view>
			
		</view>
		<!-- <view class="footer">
			<view @click='clockRecord' class="button">记录</view>
			<view @click='submit' class="button">提交</view>
		</view> -->
		<u-toast ref="uToast" />
		<view class="bottom-btn">
			<u-button type="primary" class="submit" hover-class="none" @click="clockRecord">记录</u-button>
			<u-button type="primary" class="submit" hover-class="none" @click="submit">提交</u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				typevalue: '1',
				show: false,
				detailData: {
					compId: '',
					clockType: '',
					otherReasons: '',
					targetLocation: '请选择目标位置',
					clockDistance: '重新获取',
				},
				clockDistance: "",
				warehouseType: '1',
				warehouseBaseInfoList: [],

			}
		},
		onLoad() {
			if(uni.getStorageSync("clockwerehouse")){
				this.detailData.targetLocation=uni.getStorageSync("clockwerehouse")
			}
			this.getWarehouse()
			this.getLocation()
		},
		methods: {
			calculate() {
				const query = uni.createSelectorQuery().in(this);
				query.selectAll('.left')
				console.log(query)
			},
			getWarehouse() {
				var that = this
				uni.showLoading({
					title: "正在加载"
				})
				this.$api.doRequest('get', '/warehouseBaseInfo/selectWarehouse', {
					compId: uni.getStorageSync("pcUserInfo").compId,
					warehouseType: '1'
				}).then(res => {
					if (res.data.data.length != 0) {
						that.warehouseBaseInfoList = res.data.data
						uni.getLocation({
							type: 'gcj02',
							geocode: true,
							success: function(res) {
								console.log('获取位置数据：', res);
								console.log('当前位置的经度：' + res.longitude);
								console.log('当前位置的纬度：' + res.latitude);
								that.lat2 = res.latitude
								that.lng2 = res.longitude
								if (that.warehouseBaseInfoList.length > 0) {
									if(that.detailData.targetLocation){
										for(var i = 0 ; i < that.warehouseBaseInfoList.length ; i++){
											if(that.warehouseBaseInfoList[i].warehouseName==that.detailData.targetLocation){
												that.clockDistance = that.utils.getDistance(that
												.warehouseBaseInfoList[i].warehousePositioning.split(',')[
													0],
												that.warehouseBaseInfoList[i].warehousePositioning.split(
													',')[1], that.lat2, that.lng2)
											}
										}
									}
									else{
										that.clockDistance = that.utils.getDistance(that
										.warehouseBaseInfoList[0].warehousePositioning.split(',')[
											0],
										that.warehouseBaseInfoList[0].warehousePositioning.split(
											',')[1], that.lat2, that.lng2)
											that.detailData.targetLocation = that.warehouseBaseInfoList[0].warehouseName
									}
								}
							},
							fail: function(req) {
								console.log(req)
							}
						});

					}
					uni.hideLoading()
				})
			},
			targetLPicker(e) {
				uni.setStorageSync("clockwerehouse",this.warehouseBaseInfoList[e[0]].warehouseName)
				this.detailData.targetLocation = this.warehouseBaseInfoList[e[0]].warehouseName
				this.clockDistance = this.utils.getDistance(this.warehouseBaseInfoList[e[0]].warehousePositioning.split(
						',')[0],
					this.warehouseBaseInfoList[e[0]].warehousePositioning.split(',')[1], this.lat2, this.lng2)
					
			},
			submit() {
				if (!this.typevalue) {
					this.$api.msg('打卡原因不能为空')
					return
				}
				if (!this.detailData.targetLocation) {
					this.$api.msg('请选择目标位置')
					return
				}
				var that = this
				uni.showModal({
					content: "确定提交打卡信息？",
					showCancel: true,
					confirmText: '提交',
					success: function(res) {
						if (res.confirm) {
							that.detailData.compId = uni.getStorageSync('pcUserInfo').compId
							that.detailData.commonId = uni.getStorageSync('pcUserInfo').userId
							that.detailData.dept = uni.getStorageSync('pcUserInfo').deptName
							that.detailData.phone = uni.getStorageSync('pcUserInfo').userMobilePhone
							that.detailData.empName = uni.getStorageSync('userInfo').userName
							that.detailData.clockType = that.typevalue
							if(that.clockDistance.indexOf("km") > -1){
								that.detailData.clockDistance = that.clockDistance.replace("km","")*1000
							}else{
								that.detailData.clockDistance = that.clockDistance.replace("m","")
							}
							console.log(that.detailData)
							// if(that.detailData.clockDistance > 1500){
							// 	that.$api.msg('请在目标位置1.5km范围内进行打卡')
							// 	return
							// }else{
								that.$api.doRequest('post', '/clockInfo/api/addClock', that.detailData)
									.then(res => {
										console.log(res.data)
										if (res.data.code == 200) {
											that.$api.msg('提交成功')
										} else {
											that.$api.msg('提交失败')
										}
									})
							// }
						}
					}
				})
			},
			clockRecord() {
				uni.navigateTo({
					url: '/pages/clock/the_clock_record'
				})
			},
		},
	}
</script>

<style lang='scss' scoped>
	page {
		background: #F5F6FA;
	}

	.title_b {
		margin: 20rpx 20rpx 0rpx 20rpx;
		padding: 20rpx 10rpx 20rpx 10rpx;
		font-size: 18px;
		font-weight: 550;
	}

	.c-row {
		display: -webkit-box;
		display: -webkit-flex;
		display: flex;
		-webkit-box-align: center;
		-webkit-align-items: center;
		align-items: center;
		padding: 20rpx 30rpx;
		position: relative;
	}

	.con-list {
		-webkit-box-flex: 1;
		-webkit-flex: 1;
		flex: 1;
		display: -webkit-box;
		display: -webkit-flex;
		display: flex;
		-webkit-box-orient: vertical;
		-webkit-box-direction: normal;
		-webkit-flex-direction: column;
		flex-direction: column;
		color: #303133;
		line-height: 40rpx;
		text-align: right;
		padding-right: 20rpx;
	}

	.wrap {
		padding-bottom: 10px;
		font-size: 14px;
		background: #fff;
		margin: 10px;
		border-radius: 10px;

		input {
			font-size: 14px;
		}

		>.title {
			padding: 10px 16px;
		}

	}

/* 	.footer {
		background: #fff;
		position: fixed;
		bottom: 0;
		width: 100%;
		padding: 20px 10px;
		z-index: 10;

		.button {
			background: #22C572;
			width: 90%;
			margin: 20rpx auto;
			padding: 10px;
			color: #fff;
			text-align: center;
			border-radius: 30px;
		}
	} */
	.submit {
		width: 40%;
		background: #22C572;
		border-radius: 10rpx;
	}
	.bottom-btn {
		padding: 30rpx;
		background: #FFFFFF;
		width: 100%;
		position: fixed;
		bottom: 0rpx;
		display: flex;
		z-index: 9999;
	}
	.buns_item {
		display: flex;
		padding: 80rpx 0 50rpx 0;
		justify-content: space-around;
	}

	.but_css {
		background: #22C572;
		width: 40%;
		padding: 20rpx;
		color: #fff;
		text-align: center;
		border-radius: 20rpx;
	}

	/deep/.u-radio-group {
		flex-direction: row-reverse;
	}
</style>
