<template>
	<s-layout title="设置" class="auth-manage-page">
		<view class="card">
			<view class="auth-item" v-for="item in items" :key="item.key" @click="openDialog(item.key)">
				<text class="label">{{ item.label }}</text>
				<up-icon name="arrow-right" color="#999" size="20"></up-icon>
			</view>
		</view>

		<up-popup :show="showPopup" mode="bottom" @close="showPopup = false" :round="12" safeAreaInsetBottom>
			<view class="popup-body">
				<view class="title">{{ current.title }}</view>
				<view class="desc">{{ current.desc }}</view>
				<view class="btn-row">
					<up-button plain @click="showPopup = false">再想想</up-button>
					<up-button type="primary" @click="onGoSetting">去设置</up-button>
				</view>
			</view>
		</up-popup>
	</s-layout>
</template>

<script setup>
import { ref, reactive } from 'vue';
import sheep from '@/sheep';

const showPopup = ref(false);
const currentKey = ref('');

const items = [
	{ key: 'album', label: '相册权限', title: '相册权限', desc: '关闭后，将无法上传相册中的照片或视频，也无法下载作品至你的相册' },
	{ key: 'camera', label: '相机权限', title: '相机权限', desc: '关闭后，将无法拍摄照片或视频上传' },
	{ key: 'location', label: '位置权限', title: '位置权限', desc: '关闭后，将无法获取位置信息，影响部分定位功能' },
	{ key: 'microphone', label: '麦克风权限', title: '麦克风权限', desc: '关闭后，将无法录制语音或视频的声音' },
	{ key: 'other', label: '其它权限', title: '其它权限', desc: '关闭后，可能影响部分功能的正常使用' },
];

const current = reactive({ title: '', desc: '' });

function openDialog(key) {
	currentKey.value = key;
	const item = items.find((i) => i.key === key) || items[0];
	current.title = item.title;
	current.desc = item.desc;
	showPopup.value = true;
}

function onGoSetting() {
	// 根据平台尝试申请或打开设置页
	const key = currentKey.value;
	// #ifdef MP-WEIXIN || MP-ALIPAY || MP-BAIDU
	let mpScope = '';
	if (key === 'album') mpScope = 'scope.writePhotosAlbum';
	else if (key === 'camera') mpScope = 'scope.camera';
	else if (key === 'location') mpScope = 'scope.userLocation';
	else if (key === 'microphone') mpScope = 'scope.record';
	if (mpScope) {
		uni.authorize({
			scope: mpScope,
			success() {
				uni.showToast({ title: '授权成功', icon: 'none' });
			},
			fail() {
				// 打开小程序设置页
				if (uni.openSetting) {
					uni.openSetting({
						success() {
							//
						},
					});
				} else {
					uni.showToast({ title: '请在系统设置中开启权限', icon: 'none' });
				}
			},
		});
	}
	// #endif

	// #ifdef H5
	// H5 无统一设置页，提示用户手动调整
	uni.showModal({
		title: current.title,
		content: '请在浏览器或系统设置中为本应用开启该权限',
		showCancel: true,
		confirmText: '知道了',
	});
	// #endif

	// #ifdef APP-PLUS
	// 在 APP 中打开系统设置或应用设置页
	openAppSetting();
	// #endif

	showPopup.value = false;
}

// APP 打开应用设置（参考 uview-plus 实现）
function openAppSetting() {
	try {
		const isIOS = (plus.os.name && plus.os.name.toLowerCase().indexOf('ios') !== -1);
		if (isIOS) {
			var UIApplication = plus.ios.import('UIApplication');
			var application2 = UIApplication.sharedApplication();
			var NSURL2 = plus.ios.import('NSURL');
			var setting2 = NSURL2.URLWithString('app-settings:');
			application2.openURL(setting2);
			plus.ios.deleteObject(setting2);
			plus.ios.deleteObject(NSURL2);
			plus.ios.deleteObject(application2);
		} else {
			var Intent = plus.android.importClass('android.content.Intent');
			var Settings = plus.android.importClass('android.provider.Settings');
			var Uri = plus.android.importClass('android.net.Uri');
			var mainActivity = plus.android.runtimeMainActivity();
			var intent = new Intent();
			intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
			var uri = Uri.fromParts('package', mainActivity.getPackageName(), null);
			intent.setData(uri);
			mainActivity.startActivity(intent);
		}
	} catch (e) {
		console.warn('openAppSetting fail', e);
		uni.showToast({ title: '打开设置失败，请手动前往系统设置', icon: 'none' });
	}
}
</script>

<style scoped>
.card {
	background: #fff;
	border-radius: 12rpx;
	padding: 6rpx 0;
	margin: 20rpx;
}
.auth-item {
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding: 28rpx 30rpx;
	border-bottom: 1rpx solid #f0f0f0;
}
.label {
	font-size: 28rpx;
	color: #333;
}
.popup-body {
	padding: 30rpx;
	text-align: center;
}
.title {
	font-size: 30rpx;
	font-weight: 600;
	margin-bottom: 18rpx;
}
.desc {
	color: #999;
	font-size: 26rpx;
	line-height: 36rpx;
	margin-bottom: 24rpx;
}
.btn-row {
	display: flex;
	gap: 18rpx;
	justify-content: center;
}
.btn-row up-button {
	width: 260rpx;
}
</style>
