<template>
	<view class="content">
		<view class="flex" style="overflow: hidden;flex-wrap: wrap;padding-bottom: 10rpx;">
			<view class="margin-top flex margin-right-sm flex-wrap" v-if="infantImg.length" style="width:100%;">
				<view class="flex" style="width: 200rpx;height: 200rpx;margin-right: 5rpx;position: relative;"
					v-for="(image,index) in  infantImg" :key="index">
					<image :src="image" style="width: 100%;height: 100%;"></image>
						<view 
							style="z-index: 9;position: absolute;top: -15rpx;right: -15rpx;" @click="removeImg(index)">
							<u-icon name="close-circle-fill" color="#d81e06" size="50rpx"></u-icon>
						</view>
				</view>
			</view>
			<view class="margin-top" @click="addImages(3)" v-if="infantImg.length<6" >
				<view style="width: 200rpx;height: 200rpx;background: #f7f8fa; margin-bottom: 20rpx;"
					class="flex justify-center align-center">
					<view>
						<view class="text-center">
							<image src="../../static/images/my/add.png" style="width: 80rpx;height: 80rpx;">
							</image>
						</view>
						<view class="text-center add-word">添加图片</view>
					</view>
				</view>
			</view>
			<view class="tabbar" style="background: linear-gradient(to right, #019c88, #2dbe93); border-radius: 50rpx;"
				@click="submit()" >提交</view>
		</view>
	</view>
</template>

<script>
	import empty from '@/components/empty.vue'
	import tRefresh from "@/components/t-refresh/t-refresh.vue"
	import * as websocketUtils from 'utils/websocketUtils.js';
	export default {
		components:{tRefresh,empty},
		data(){
			return{
				infantImg: [],
				form:{},
			}
		},
		onLoad(e) {
		},
		methods:{
			removeImg(index) {
				this.infantImg.splice(index, 1)
			},
			addImages(e) {
				let that = this
				let urlName = websocketUtils.getBaseUrl() + '/alioss/upload';
				if (e == 2) {
					urlName = websocketUtils.getBaseUrl() + '/alioss/uploadWatermark';
				}
				uni.chooseImage({
					count: 6,
					sourceType: ['album', 'camera'],
					success: res => {
						for (let i = 0; i < res.tempFilePaths.length; i++) {
							that.$queue.showLoading("上传中...");
							uni.uploadFile({ // 上传接口
								//url: websocketUtils.uploadFileUrl(), //真实的接口地址
								url: urlName,
								filePath: res.tempFilePaths[i],
								name: 'file',
								success: (uploadFileRes) => {
									if (e == 3) {
										if (that.infantImg.length <= 5) {
											that.infantImg.push(JSON.parse(uploadFileRes.data).data)
										}
									} else if (e == 2) {
										if (that.certificateImg.length <= 5) {
											that.certificateImg.push(JSON.parse(uploadFileRes.data).data)
										}
									}
									uni.hideLoading();
								}
							});
						}
					}
				})
			},
			submit() {
				this.form.infantImg = this.infantImg
				this.form.infantImg = this.form.infantImg.toString();
				if (!this.form.infantImg) {
					uni.showToast({
						title: '请上传生活照',
						icon: 'none',
						duration: 1000
					})
					return;
				}
				let data = {
					artificerId: uni.getStorageSync("artificerId"),
					lifePhoto: this.form.infantImg,
				}
				
				this.$Request.postJson("/app/artificer/updateShz", data).then(res => {
					if (res.code == 0) {
						uni.showToast({
							title: '提交成功！',
							icon: 'none'
						})
						setTimeout(function() {
							uni.switchTab({
								url: '/pages/my/index'
							})
						}, 1000)
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				});
			
			
			},
		}
	}
</script>

<style scoped>
	.tabbar {
		width: 690upx;
		height: 86upx;
		background: #019c88;
		border-radius: 8upx;
		margin: 0 auto;
		text-align: center;
		line-height: 86upx;
		color: #FFFFFF;
		margin-top: 20rpx;
	}
</style>