<template>
	<view>
		<view class="downloadBox">
			<navigator url="/pages/promiseList/promiseList" class="downloadBoxLink">我的承诺书</navigator>
			<navigator url="/pages/downBook/downBook" class="downloadBoxLink">下载模板</navigator>
		</view>
		<view class="fixedTitleBox">
			<text class="fixedTitle">请拍摄本人签字的承诺书</text>
			<text class="fixedTitle2">确保所有信息真实有效，清晰完整</text>
		</view>
		<view class="promiseImageBox" >
			<image 
				v-if="images.length>0"
				:src="images[0]"
				mode="scaleToFill"
				class="promiseImage"
				@click="preview"
			>
			</image>
			<view @click="toChooseImage" class="add_icon_box" v-else >
				<view class="add_icon"></view>
			</view>
			<view 
				class="close" 
				v-if="images.length>0"
				@click="deleteImg"
			>
			</view>
		</view>
		<view class="preview_icon" v-if="images.length>0">点击图片预览</view>
		<view class="btnBox">
			<button class="btn" type="default" @click="submitPic">上传</button>
		</view>
	</view>
</template>

<script>
	
	import { downLoad } from "../../api/index.js"
	
	export default {
		data(){
			return {
				images:[],
				matchPoint: true
			}
		},
		methods:{
			//拍摄
			toChooseImage(){
				let that=this;
				
				if(!this.matchPoint){
					return false;
				}
				this.matchPoint=false
				
				uni.authorize({
					scope:"scope.camera",
					success() {
						uni.chooseImage({
							count: 1, 
							sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
							sourceType: ['album','camera'], //从相册选择
							success: function (res) {
								that.images=res.tempFilePaths
							}
						})
					},
					complete(){
						that.matchPoint=true
					}
				})
			},
			preview(){
				let urls=this.images
				uni.previewImage({
					urls:urls
				})
			},
			deleteImg(){
				this.images=[]
			},
			submitPic(){
				const that=this
				const token = this.$store.state.token
				if(!token){
					uni.showToast({
						title:"token丢失,请重新登录",
						icon:"none",
						duration:2500
					})
				}else{
					
					if(!that.images[0]){
						uni.showToast({
							title:"请先选择要上传的图片",
							icon:"none"
						})
						return false;
					}
					
					uni.showLoading({
					    title: '加载中'
					});
					uni.uploadFile({
						url:"https://helper.qcfun.ximengnaikang.cn/api/user/uploadcommitbook",
						// url:"http://192.168.0.15/api/user/uploadcommitbook",
						filePath:that.images[0],
						name:"file",
						header:{
							"authorization":"Bearer "+token
						},
						success:function(res){
							uni.hideLoading();
							const data=JSON.parse(res.data)
							const {error_code,msg} = data
							uni.showToast({
								title:msg ? msg : '上传失败,请稍后再试',
								icon:error_code===200?'success':'none',
								duration:2500
							})
							that.images=[];
						},
						fail:function(){
							uni.hideLoading();
							uni.showToast({
								title:'上传失败',
								icon:'none',
								duration:2500
							})
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	page{
		width: 100%;
		height: 100%;
		background: $uni-bg-color-normal;
	}
	.downloadBox{
		padding: 60rpx 30rpx 50rpx 30rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	.downloadBoxLink{
		font-size: 32rpx;
		color: $uni-btn-bg-color;
	}
	.fixedTitleBox{
		text-align: center;
	}
	.fixedTitle{
		display: block;
		font-size: 34rpx;
		color: #4D4D4D;
	}
	.fixedTitle2{
		display: block;
		color: #FE7444;
		font-size: 24rpx;
		margin-top: 28rpx;
		margin-bottom: 57rpx;
	}
	.promiseImageBox{
		width: calc(100% - 200rpx);
		height: 436rpx;
		border: 1px solid #c1c1c1;
		border-radius: 5px;
		margin:0 100rpx;
		position: relative;
		
		.add_icon_box{
			width: 100%;
			height: 100%;
			display: flex;
			justify-content: center;
			align-items: center;
		}
		
		.add_icon{
			content: "";
			display: block;
			width: 100rpx;
			height: 100rpx;
			background-image:url("../../static/add.png");
			background-size: 100% 100%;
			position: absolute;
			top: 50%;
			left: 50%;
			margin-top: -50rpx;
			margin-left: -50rpx;
		}
		
		.close{
			width: 50rpx;
			height: 50rpx;
			border-radius: 50%;
			background: #8d8d8d;
			position: absolute;
			top: -25rpx;
			right: -25rpx;
			transform: rotate(45deg);
			
			&:after,&:before{
				content: "";
				display: block;
				width: 30rpx;
				height: 4rpx;
				border-right: 4rpx;
				background: #FFFFFF;
				position: absolute;
				top: 50%;
				left: 50%;
				margin-left: -15rpx;
				margin-top: -2rpx;
			}
			
			&:before{
				transform: rotate(90deg);
			}
		}
	}
	.promiseImage{
		display: block;
		width: 100%;
		height: 100%;
	}
	.btn{
		margin: 60rpx 100rpx 26rpx 100rpx;
		font-size: 32rpx;
	}
	.btn:nth-of-type(1){
		background: $uni-btn-bg-color !important;
		color: #FFFFFF !important; 
	}
	.btn:nth-of-type(2){
		margin-top: 0;
	}
	
	.preview_icon{
		display: flex;
		align-items: center;
		font-size: 24rpx;
		justify-content: center;
		padding-top: 10px;
		
		&:before{
			content: "";
			display: block;
			width: 40rpx;
			height: 40rpx;
			background-image: url("../../static/preview.png");
			background-size: 100% 100%;
			transform: rotate(180deg);
		}
	}
</style>
