<template>
	<view class="container">
		<!-- 头部添加打卡时间区域 -->
		<view class="add">
			<view class="icon" @click="handleAdd">
				<img src="../../static/add.png">
			</view>
			<view class="title" @click="handleAdd">
				添加打卡位置
			</view>
			<view class="none"></view>
		</view>

		<!-- 打卡时间列表 -->
		<view class="list">
			<uni-swipe-action>
				<!-- 每一个时间段区域 -->
				<uni-swipe-action-item :auto-close="true" :right-options="options" @click="onClick(index)"
					v-for="(item,index) in list" :key="index">

					<view class="box">
						<view class="icon">
							<img src="../../static/location2.png">
						</view>
						<view class="place">
							<view class="top">
								{{item.name}}
							</view>
							<view class="center">
								地址：{{item.name}}
							</view>
							<view class="bottom">
								范围：{{item.radius}}米
							</view>
						</view>
						<view class="right">
							<img src="../../static/right.png">
						</view>
					</view>
				</uni-swipe-action-item>
			</uni-swipe-action>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				id: "",
				type: "",
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#D43030'
					}
				}],
			}
		},
		onLoad(options) {
			this.type = options.type
			if (options.id) {
				this.id = options.id
				this.getRuleDetail()
			}
		},
		onShow() {
			let arr = uni.getStorageSync("chooseList")||uni.getStorageSync("chooseList_edit")
			if (arr) {
				this.list = arr
			}
		},
		methods: {
			// 获取打卡规则的详细信息
			async getRuleDetail() {
				let res = await this.$myRequest({
					url: `/attendance/api/settings/rule/detail/${this.id}`
				})
				// console.log(res);
				if (res.code == 200) {
					this.list = res.data.locations
					uni.setStorageSync("chooseList_edit", this.list)
				}
			},

			// 点击添加打卡位置回调
			handleAdd() {
				// 获取用户位置权限
				uni.authorize({
					scope: 'scope.userLocation',
					success:() => {
						uni.navigateTo({
							url: `/pages/addLocation/addLocation?type=${this.type}`
						})
					},
					fail() {
						uni.showModal({
							title: '提示',
							content: '请先开启定位权限，否则将无法使用定位功能',
							cancelText: '不授权',
							confirmText: '授权',
							success: function(res) {
								if (res.confirm) {
									uni.openSetting({
										success(res) {}
									})
								} else if (res.cancel) {}
							}
						});
					}
				})
			},
			// 点击右侧删除按钮回调
			onClick(index) {
				uni.showModal({
					title: '提示',
					content: '确定删除该打卡位置吗？',
					success: (res) => {
						if (res.confirm) {
							this.list.splice(index, 1)
							if (this.type == 1) {
								uni.setStorageSync("chooseList_edit", this.list)
							} else {
								uni.setStorageSync("chooseList", this.list)
							}
							uni.showToast({
								title: "删除成功",
								icon: 'success'
							})
						} else if (res.cancel) {}
					}
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		padding-top: 20rpx;

		.add {
			display: flex;
			margin: 0 auto;
			width: 690rpx;
			height: 87rpx;
			line-height: 87rpx;
			border-radius: 14rpx;
			background-color: #fff;

			.icon {
				flex: 1;
				display: flex;
				justify-content: center;
				align-items: center;

				img {
					width: 36rpx;
					height: 36rpx;
				}
			}

			.title {
				flex: 3;
				font-size: 30rpx;
				color: #0082FC;
			}

			.none {
				flex: 5;
			}
		}

		.list {
			margin-top: 20rpx;
			padding-bottom: 30rpx;

			.box {
				display: flex;
				align-items: center;
				margin: 0 auto;
				margin-bottom: 20rpx;
				width: 690rpx;
				height: 137rpx;
				border-radius: 14rpx;
				background-color: #fff;

				.icon {
					margin-top: 10rpx;
					width: 75rpx;
					text-align: center;

					img {
						width: 40rpx;
						height: 40rpx;
					}
				}

				.place {
					width: 540rpx;

					.top {
						font-size: 32rpx;
						overflow: hidden;
						white-space: nowrap;
						text-overflow: ellipsis;
					}

					.center {
						font-size: 24rpx;
						color: #999999;
						overflow: hidden;
						white-space: nowrap;
						text-overflow: ellipsis;
					}

					.bottom {
						font-size: 24rpx;
						color: #999999;
						overflow: hidden;
						white-space: nowrap;
						text-overflow: ellipsis;
					}
				}

				.right {
					width: 75rpx;
					text-align: center;

					img {
						width: 15rpx;
						height: 28rpx;
					}
				}
			}
		}
	}

	// 解决左滑区域突出问题
	::v-deep .uni-swipe_button-group {
		margin-bottom: 20rpx;
	}
</style>
