<template>
	<Layout ref="Layout" @instruct="handleInstruct" @switchCamera="switchCamera()" @takePhoto="takePhoto" >
		<camera class="camera"  mode="normal" :device-position="device" @error="error"
			style="width: 100%; height: 100%;">
			<cover-view class="cover-view">
				<cover-view class="uni-nav-bar">
					<cover-view :style="{height: statusBarHeight,width: '100%'}"></cover-view>
					<cover-view class="uni-nav-barbox"  :style="{width: menuLeft}">
						<cover-view class="uni-nav-back" @click="back">返回
							<!-- <uni-icons color="#222" type="left" size="20" /> -->
						</cover-view>
						<cover-view class="uni-nav-title" @click="navTo()">
							<cover-view>使用教程</cover-view>
							<cover-image class="ques" src="https://cqtyt-2025.oss-cn-beijing.aliyuncs.com/huyi/imagesNew/4b44f64918f44a0a8a2fba3dd8db1d9f.png" mode="aspectFill"></cover-image>
						</cover-view>
					</cover-view>
				</cover-view>
				<cover-view class="box">
					<cover-view style="background-color: #fff;width: 100%">
						<cover-view class="title">请拍摄舌面</cover-view>
						<cover-view class="tips">舌尖放松，舌面平展，舌尖略向下，口张大不要太用力</cover-view>
					</cover-view>
					<cover-view class="imagebox">
						<cover-image class="tongue" src="https://cqtyt-2025.oss-cn-beijing.aliyuncs.com/huyi/imagesNew/41881987f277469ea84b68fc57146256.png" mode="aspectFill"></cover-image>
					</cover-view>
					<cover-view style="flex: 1;background-color: #fff;width: 100%;"></cover-view>
				</cover-view>
			</cover-view>
			<!-- <cover-image v-if="coverImage" :src="coverImage" style="width: 100%;height: 100%;"></cover-image> -->
		</camera>
	</Layout>
</template>

<script>
	import Layout from './Layout.vue'

	export default {
		components: {
			Layout
		},
		data() {
			return {
				baseUrl:uni.getStorageSync('requestPath'),
				statusBarHeight: uni.getSystemInfoSync().statusBarHeight + 'px',
				// 前置或后置摄像头，值为front, back
				device: 'front',
				cameraContext: null,
				shutterShow: false,
				coverImage: null,
				menuLeft: '100%'
			}
		},
		mounted() {
			uni.getSystemInfo({
				success: (result) => {
					// 获取右侧胶囊的信息 单位px
					//#ifndef H5 || APP-PLUS
					const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
					//left:   胶囊左侧距离屏幕左侧的距离
					this.menuLeft = menuButtonInfo.left + 'px'
					//#endif
				},
				fail: (error) => {
					console.log(error)
				}
			})
			this.cameraContext = uni.createCameraContext();
			// this.coverImage = 'https://cqtyt-2025.oss-cn-beijing.aliyuncs.com/huyi/images/portrait.jpg'
		},
		methods: {
			initShow() {
				this.$refs.Layout.initShow()
			},
			navTo() {
				uni.navigateTo({
					url: "/pages_user/tongue/ques"
				})
			},
			switchCamera(){
				this.device = this.device === 'back' ? 'front' : 'back';
			},
			back(){
				uni.navigateBack()
			},
			error(err) {
				console.log(err)
			},
			takePhoto(){
				this.$emit('takePhoto')
				
			},
			handleInstruct(instruct) {
				switch (instruct) {
					// 返回
					case 'back':
						this.$emit('back')
						break;
						// 快门
					case 'shutter':
						this.cameraContext.takePhoto({
							quality: 'high',
							success: (res) => {
								// console.log(res)
								this.$emit('getImage', res.tempImagePath)
							}
						})
						break;
						// 反转
					case 'reversal':
						this.device = this.device === 'back' ? 'front' : 'back'
						break;
						// 相册
					case 'album':
						uni.chooseImage({
							count: 1, //默认9
							sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
							sourceType: ['album'], //从相册选择
							success: (res) => {
								 uni.setStorageSync("tongueUrl",res.tempFilePaths[0])
								this.$emit('getImage')
							}
						})
						break;
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	@mixin u-flex($flexD, $alignI, $justifyC) {
		display: flex;
		flex-direction: $flexD;
		align-items: $alignI;
		justify-content: $justifyC;
	}
	.imagebox {
		width: 100%;
		// margin-top: 30rpx;
		@include u-flex(column, center, center);
	}
	.tongue {
		width: 100%;
		height: 608rpx;
		margin: 0 auto;
		display: block;
		// margin-top: 30rpx;
	}
	.cover-view {
		width: 100%;
		height: 100%;
		position: absolute;
		z-index: 99;
		top: 0;
		left: 0;
		display: flex;
		flex-direction: column;
	}
	.box {
		flex: 1;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	.title {
		margin-top: calc(var(--status-bar-height) + 160rpx);;
		font-family: SourceHanSansCN-Medium;
		font-weight: 500;
		font-size: 43rpx;
		color: #222;
		text-align: center;
	}
	.tips {
		padding: 38rpx 24rpx;
		font-family: SourceHanSansSC-Regular;
		font-weight: 400;
		font-size: 28rpx;
		color: #222;
		text-align: center;
	}
	.uni-nav-bar {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		z-index: 999;
		overflow: hidden;
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		color: #222;
		background-color: #fff;
		.uni-nav-barbox {
			width: 100%;
			height: 88rpx;
			padding: 0 24rpx;
			box-sizing: border-box;
			@include u-flex(row, center, space-between);
			position: relative;
			font-size: 32rpx;
		}
		.uni-nav-title {
			flex-shrink: 0;
			font-family: SourceHanSansSC;
			font-weight: 400;
			// font-size: 32rpx;
			color: #222;
			/* #ifdef APP-PLUS */
			font-size: 32rpx;
			/* #endif */
			/* #ifndef APP-PLUS */
			font-size: 32rpx;
			/* #endif */
			@include u-flex(row, center, flex-start);
			.ques {
				flex-shrink: 0;
				width: 33rpx;
				height: 33rpx;
				margin-left: 9rpx;
			}
		}
		.uni-nav-back {
			width: 100%;
			height: 88rpx;
			line-height: 88rpx;
		}
	}

	.camera {
		position: relative;
	}
</style>
