<template>
	<view>
		<view class="content ">
			<custom_navbar :title="navbar_title" @getNavHeight="getNavHeight" :theme="theme" />
			<view class="photo_main_face" :style="'margin-top:-'+margin_top+'px'">
				<view class="photo_main_face_head"></view>
				<view class="photo_main_face_1">
					<image src="/static/img/photo/face_5.png" v-if="uploadFiles.length >=5"/>
					<image src="/static/img/photo/face_1.png" v-if="uploadFiles.length < 5"/>
				</view>
				<view class="photo_main_face_2">
					<view class="photo_main_face_2_1">上传5-10张，有效照片越多，生成效果越好</view>
					<view class="photo_main_face_2_2">
						<image src="/static/img/index/tips.png" />
					</view>
				</view>

				<view class="photo_main_face_3">
					<view class="photo_main_face_3_one" v-for="(item,key) in successFiles" :key="key" :class="{
						 this_class_0:key==0,
						 this_class_4:key==4,
						 this_class_5:key==5,
						 this_class_9:key==9
						 }">
						<image :src="item" v-if="item" />
						<view class="photo_main_face_3_one_icon" v-if="item" @tap="delPhoto(item)">
							<image src="/static/img/common/common_22.png" />
						</view>
					</view>
				</view>


				<u-upload :fileList="photoList" @afterRead="afterRead" @delete="deletePic" name="up1" :maxCount="1"
					v-if="uploadFiles.length < 10 && uploadFiles.length >= 1">
					<view class="photo_main_face_4 " style="height: 88rpx;margin-left: 24rpx;margin-bottom: 0;">
						<image src="/static/img/photo/face_7.png" />
					</view>
				</u-upload>

				<!-- :fileList="photoList" @afterRead="afterRead" @delete="deletePic" name="1" :maxCount="5" -->
				<u-upload :fileList="photoList" @afterRead="afterRead" @delete="deletePic" name="up2" :maxCount="1"
					v-if="uploadFiles.length < 1">
					<view class="photo_main_face_4 photo_full_blue_btn" style="margin-left: 24rpx;">
						上传照片
					</view>
				</u-upload>


				<view class="photo_main_face_4 photo_full_blue_btn" v-if="uploadFiles.length >=5" @tap="subFace()">
					提交制作
				</view>

				<view class="photo_main_face_5" v-if="errorFiles.length > 0">以下照片不合格，请重新上传</view>
				<view class="photo_main_face_3" v-if="errorFiles.length > 0">
					<view class="photo_main_face_3_one" v-for="(item,key) in errorFiles" :key="key">
						<image :src="item" />
					</view>
				</view>

				<view class="photo_main_face_6">
					<image src="/static/img/photo/face_6.png" />
				</view>

			</view>

		</view>


		<u-popup :show="show_1" mode="center" bgColor="transparent" :round="12">
			<view class="show_readme_div">
				<view class="show_readme_div_1">
					服务说明
				</view>
				<view class="show_readme_div_2">
					<view>1.面部档案实际效果与您上传的照片有关，请认真上传。</view>
					<view>2.生成高峰期，可能会有排队情况，请耐心等候。</view>
					<view>3.稍后请前往<text>【我的】</text>增加人物的面部档案。</view>
				</view>
				<view class="show_readme_div_3" @tap="subPhoto">
					确认
				</view>
			</view>
		</u-popup>

		
	</view>
</template>

<script>
	import custom_navbar from "../photo/components/photo_header.vue";
	import unit from "@/config/unit.js";
	export default {
		components: {
			custom_navbar

		},
		data() {
			return {
				navbar_title: '上传图片',
				theme: "blue",
				margin_top: 0,
				show_1: false,
				show_2: false,
				show_3: false,
				photoList: [],
				uploadFiles: [],
				successFiles: [],
				errorFiles: []

			}
		},

		mounted() {

		},
		onPageScroll(e) {
			if (e.scrollTop > 50) {
				this.theme = 'white';

			} else {

				this.theme = 'blue';
			}
		},

		onShow() {
			var _this = this;
			_this.checkAuth();
			this.setSuccessFiles();
		
		},

		methods: {
			setSuccessFiles() {
				this.uploadFiles = uni.getStorageSync('ai_face_success');
				
				// console.log('&&&&&&&&&&&&&&&&&&&&&&&&&&', this.uploadFiles);
				if (!this.uploadFiles) {
					this.uploadFiles = [];
				}
				this.successFiles = [];
				this.uploadFiles.forEach((item, index) => {
					this.successFiles.push(item);
				})
				// console.log('&&&&&&&&&&&&&&&&&&&&&&&&&&', this.successFiles);
				let slen = this.successFiles.length;

				for (let n = slen; n < 10; n++) {
					this.successFiles.push('');
				}

			},

			delPhoto(url) {
				let tmp = uni.getStorageSync('ai_face_success');
				if (tmp) {
					const index = tmp.indexOf(url);
					if (index !== -1) {
						tmp.splice(index, 1);
					}
					
					uni.setStorageSync('ai_face_success', tmp);
					this.setSuccessFiles();
				}


			},

			getNavHeight(e) {
				this.margin_top += e;
			},
			
			subPhoto(){
				
				//提交申请
				uni.redirectTo({
					url:'/pagesA/photo/cartoon_4?from=idcard'
				})
				
				
			},
			subFace(){
				
				let data = {};
				data.images = [];
				let imgs =  uni.getStorageSync('ai_face_success');
				if(imgs.length < 5){
					this.$com.showError('请上传至少5张照片!')
					return false;
				}
				let root = this.$api.getOssurl();
				if(typeof(root) == 'undefined' || !root){
					this.$com.showError('数据获取错误!');
					return false;
				}
				
				imgs.forEach((item,key)=>{					
					data.images.push(item.replace(root,'')) 
				})				
			
				let _this = this;
				uni.showLoading({
					title: '面部档案生成中',
					mask:true					
				});
				this.$api.post('/api/portrait/append',data,function(rs){
					uni.hideLoading();
					uni.redirectTo({
						url:'/pagesA/merge/success?from=face'
					})
					
				})
				
			},		
			
			//判断
			checkAuth() {
				var _this = this;
				console.log('已授权---');
				uni.authorize({
					scope: 'scope.camera',
					success(res) {
						console.log('已授权', res);

					},
					fail(err) {

						uni.showModal({
							title: '授权失败',
							content: '请在设置界面打开摄像头相关权限',
							success: (res) => {
								if (res.confirm) {
									uni.openSetting()
								}
							}
						})
					},
				})
			},

			async afterRead(event) {
				var _this = this;
				let lists = [].concat(event.file);


				uni.showLoading({					
					title: '图片上传中',
					mask:true					
				});
			
				for (let i = 0; i < lists.length; i++) {
					var edetail = lists[i].url;
					let filePath = edetail;
					let _date = new Date();
					let ext_name = filePath.substr(filePath.lastIndexOf('.'));
					console.log(ext_name);
					if (ext_name !== '.png' && ext_name !== '.jpg' && ext_name !== '.jpeg') {
						 _this.$com.showError('请上传 png / jpg / jpeg 格式的图片');
						 return false;
					}
					
					let address = _date.getFullYear() + '' + (_date.getMonth() + 1) + '' + _date.getDate();
					let fileName = address + _date.getTime() + ext_name;
					const img_info = await unit.getImgInfo(filePath);
					
					if(typeof(img_info) == 'undefined' || !img_info){
						this.$com.showError('图片格式错误!请重试!');
						return false;
					}
					let {
						width,
						height
					} = img_info;
					let maxWidth = 2480;
					if (width > maxWidth) {
						let scale = Math.ceil(width / maxWidth);
						height = Math.ceil(height / scale);
						width = Math.ceil(maxWidth);
					}
					let compress_path = '';
					compress_path = await unit.compressImg(filePath, {
						compressedWidth: width
					}).catch((res) => {
						console.log('上传压缩失败', err);
						return false;
					});
					compress_path = compress_path ? compress_path : filePath;

					//_this.upload_img(compress_path, fileName);
					//上传
					var upData = {
						filePath: compress_path,
						fileType: 'image',
						scene: 'user_max'
					};
					
					_this.$api.ossUpload(upData, function(res) {

						if (res.code == 200) {
							let url = res.data.file_url;
							
							url = _this.$api.ossurl(url);
							//开始验证图片
							let check_image = {
								'check_image': res.data.file_url
							}

							
							uni.showLoading({
								mask: true,
								title: '图片验证中',
							});
							_this.$api.post('/api/portrait/check',check_image,function(rs){
								uni.hideLoading();
								console.log('-------------',rs);
								if(rs.status == 1){
									//验证成功
									_this.uploadFiles = uni.getStorageSync('ai_face_success');
									if (!_this.uploadFiles) {
										_this.uploadFiles = [];
									}
									if (_this.uploadFiles.length < 10) {
									
										_this.uploadFiles.push(url);
									
										uni.setStorageSync('ai_face_success', _this.uploadFiles);
										_this.setSuccessFiles();
									}
								}else{
									//验证失败
									_this.errorFiles.push(url);
									console.log(_this.errorFiles);
									
								}
							
								
							},function(err){
								uni.hideLoading();
								this.$com.showError('图片验证失败!');
								return false;
								
							});

							



							//验证失败
							//_this.errorFiles.push(url);


						}

					}, function(err) {
						console.log('ossUpload fail', err);
						_this.$com.showError('上传图片错误!!')

					});



				}
				uni.hideLoading();

			}

		}
	}
</script>

<style lang="scss" scoped>
	@import './this.scss';

	.this_class_0 {
		border-top-left-radius: 20rpx;

		image {
			border-top-left-radius: 20rpx;
		}
	}

	.this_class_4 {
		border-top-right-radius: 20rpx;

		image {
			border-top-right-radius: 20rpx;
		}
	}

	.this_class_5 {
		border-bottom-left-radius: 20rpx;

		image {
			border-bottom-left-radius: 20rpx;
		}
	}

	.this_class_9 {
		border-bottom-right-radius: 20rpx;

		image {
			border-bottom-right-radius: 20rpx;
		}
	}
</style>