<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.title}}
							</view>
							<view class="center">
								地址：{{item.address}}
							</view>
							<view class="bottom">
								范围：{{item.scope}}米
							</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: 1,
					// 	title: "操场",
					// 	place: "江西省南昌市经开区广兰大道899号",
					// 	scope: "300米"
					// },
					// {
					// 	id: 2,
					// 	title: "操场",
					// 	place: "江西省南昌市经开区广兰大道899号",
					// 	scope: "400米"
					// },
					// {
					// 	id: 3,
					// 	title: "操场",
					// 	place: "江西省南昌市经开区广兰大道899号",
					// 	scope: "500米"
					// }
				],
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#D43030'
					}
				}],
			}
		},
		onLoad() {

		},
		onShow() {
			let arr = uni.getStorageSync("chooseList")
			if (arr) {
				this.list = arr
			}
		},
		methods: {
			handleAdd() {
				// 获取用户位置权限
				uni.authorize({
					scope: 'scope.userLocation',
					success() {
						uni.navigateTo({
							url: "/pages/addLocation/addLocation"
						})
					},
					fail() {
						uni.showModal({
							title: '提示',
							content: '请先开启定位权限，否则将无法使用定位功能',
							cancelText: '不授权',
							confirmText: '授权',
							success: function(res) {
								if (res.confirm) {
									uni.openSetting({
										success(res) {}
									})
								} else if (res.cancel) {}
							}
						});
					}
				})
			},
			// 点击右侧删除按钮回调
			onClick(index) {
				console.log(index);
				uni.showModal({
					title: '提示',
					content: '确定删除该打卡位置吗？',
					success: (res) => {
						if (res.confirm) {
							this.list.splice(index, 1)
							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>
