<template>
	<view class="content">
		<view class="uni-btn-v uni-common-mt">
			<button type="primary" @click="startListerPhone">注册电话监听</button>
			<button type="primary" @click="stopListerPhone">注销电话监听</button>
			<button type="primary" @click="checkIsAutoRecord">是否开启通话自动录音功能</button>

			<button type="primary" @click="toCallAutoRecorderPage">跳转到开启通话自动录音界面</button>
			<button type="primary" @click="navigateToCallRecordingSettings">跳转到系统的通话录音界面</button>
			<button type="primary" @click="jumpToPermissionPage">请求所有文件访问权限(主要针对ANDROID 11以上获取录音文件需要的权限)</button>
			<button type="primary" @click="allRecorderFilesAction">获取通话自动录音文件</button>


			<!-- <button type="primary" @click="registerSmsReceiver">注册短信监听</button> -->
			<button type="primary" @click="makePhoneCall">拨打电话</button>
		</view>
		<view class="text-box" scroll-y="true">
			<text>{{phoneState}}</text>
		</view>
		<view v-if="change_pop_show">
			<view class="status text-center">蓝牙列表（直接点击连接打印机）</view>
			<view class="divider"></view>
			<view :class="{ 'popup-height': type === 'left' || type === 'right' }" class="device-list">
				<scroll-view scroll-y="true" class="p-content underline">
					<view class="underline p-item device-item" hover-class="list-active"
						v-for="(item, index) in Filelist" :key="index">
						<view class="desc">
							{{item}}
						</view>
					</view>
				</scroll-view>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		startPhoneListener,
		stopPhoneListener,
		checkIsAutoRecord,
		toCallAutoRecorderPage,
		navigateToCallRecordingSettings,
		jumpToPermissionPage,
		makePhoneCall,
		allRecorderFilesAction,
	} from '@/uni_modules/yao-lister';


	export default {
		data() {
			return {
				title: 'Hello',
				phoneState: '',
				stateText: '',
				isAndroid: true,
				change_pop_show: false,
				type: 'center',
				Filelist: [],
			}
		},
		mounted() {
			this.initPhoneStateListener()
		},
		beforeDestroy() {
			stopListerPhone()
		},
		methods: {
			toggle(type) {
				const _this = this;
				_this.type = type;
				_this.change_pop_show = true;
			},
			allRecorderFilesAction() {
				const _this = this;
				allRecorderFilesAction(res => {
					console.log("录音文件输入", JSON.stringify(res));
					// 录音文件输入,  ["/storage/emulated/0/Recordings/Record/Call/15099989786 2025-11-11 09-41-57.m4a","/storage/emulated/0/Recordings/Record/Call/18925012557 2025-11-11 11-10-33.m4a"]
					_this.toggle('bottom');
					_this.Filelist = res
					uni.showToast({
						title: JSON.stringify(res),
						duration: 2000
					})
				})
			},
			jumpToPermissionPage() {
				jumpToPermissionPage()
			},
			navigateToCallRecordingSettings() {
				navigateToCallRecordingSettings()
			},
			toCallAutoRecorderPage() {
				toCallAutoRecorderPage()
			},
			checkIsAutoRecord() {
				let checkRecord = checkIsAutoRecord()
				uni.showToast({
					title: checkRecord ? "已开启电话录音" : "未开启电话录音",
					duration: 2000
				})
			},
			makePhoneCall() {
				makePhoneCall('15099989786')
				// uni.makePhoneCall({
				// 	phoneNumber: '13771854499', // 电话号码
				// 	success: function() {
				// 		console.log('拨打电话成功！');
				// 	},
				// 	fail: function(err) {
				// 		console.error('拨打电话失败：', err);
				// 	}
				// });
			},
			startListerPhone() {
				startPhoneListener(res => {
					uni.showToast({
						icon: 'success',
						title: '电话监听开启',
						duration: 2000
					});
					this.phoneState = res + "012"
				})
			},
			stopListerPhone() {
				stopPhoneListener(res => {
					uni.showToast({
						icon: 'success',
						title: res,
						duration: 2000
					});
				})
			},

			async initPhoneStateListener() {
				// 动态申请权限
				const permissions = ["android.permission.READ_PHONE_STATE",
					"android.permission.ANSWER_PHONE_CALLS", "android.permission.CALL_PHONE",
					"android.permission.MANAGE_EXTERNAL_STORAGE",
					"android.permission.READ_EXTERNAL_STORAGE",
					"android.permission.READ_CALL_LOG",
					"android.permission.READ_PHONE_NUMBERS",
					"android.permission.FOREGROUND_SERVICE"
				]
				const result = await plus.android.requestPermissions(permissions, 1001)

				if (result.granted) {
					startListening()
					// 监听原生事件
					uni.$on('PHONE_STATE_CHANGE', (state) => {
						this.phoneState = state
					})
				}
			}
		},
	}
</script>

<style>
	.text-box {
		margin-bottom: 40rpx;
		padding: 40rpx 0;
		display: flex;
		min-height: 300rpx;
		background-color: #FFFFFF;
		justify-content: center;
		align-items: center;
		text-align: center;
		font-size: 30rpx;
		color: #353535;
		line-height: 1.8;
		border: 1px #007AFF;
		height: 200px;
		width: 200px;
	}

	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	button {
		margin: 10px 0;
		background-color: #007AFF;
		color: white;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}

	.uni-popup {
		z-index: 9999;
		/* 根据需要调整 */
	}

	.p-content {
		height: calc(70vh - 20px);
		width: 700rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}

	.text-center {
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.desc {
		padding-left: 10px;
		margin-top: 10px;
	}
</style>