<template>
	<div style="display: inline-block;">
		<view class="bank-license" v-for="item,index in Fimages" :key='index'>
			<block v-if="item">
				<img class="license-pic" :src="item" alt="" />
				<u-icon v-if="!flag" name="close" class='del-icon' size='30' @click.stop="handleRemove(item, index)">
				</u-icon>
			</block>
		</view>
		<view class="bank-license" @click="uploadAva">
			<text>+</text>
		</view>
		<!-- <slot v-show="false" name="cont"><button>上传</button></slot> -->
	</div>
</template>

<script>
	import webUrl from '@/config.js';
	import qiniuUploader from './qiniuUploader.js';
	import {
		getEnToken
	} from '@/access/common.js';
	export default {
		data() {
			return {
				Fimages: []
			};
		},
		props: {
			images: {
				// type: Array,
				default: () => {
					return [];
				}
			},
			flag: {
				type: Boolean,
				default: false
			},
		},
		watch: {
			images(val) {
				this.Fimages = val;
			}
		},
		created() {
			this.Fimages = this.images;
		},
		methods: {
			// 上传头像
			uploadAva() {
				uni.chooseImage({
					success: async res => {
						const imgPath = res.tempFilePaths[0]; //选择图片的路径
						const imgName = imgPath
							.split('.')
							.slice(-2)
							.join('.');
						const key = `${getEnToken()}/${imgName}`; //图片和企业token拼接 为自定义文件名
						this.$u.api.uploadToken({
							bucket: webUrl.QINIU_KEY, //企业token
							key: key //图片的路径
						}).then(data => {
							this.uploadtoken = data.data; //获取上传图片的token
							// url: 'https://upload-z2.qiniup.com',

							let domain = webUrl.QINIU_UP; //文件上传地址
							let token = data.data; //token为七牛云的token一般由后台接口提供
							let filePath = imgPath; //为需要上传的文件
							uni.showLoading();
							qiniuUploader.upload(
								filePath,
								res => {
									uni.hideLoading();
									//图片上传完成后返回值
									//因为目前只有头像上传,所以压缩图片,限制尺寸为 300 px
									const imgData = {
										...res,
										key: res.key
									};
									const uploadPicUrl = `${webUrl.QINIU_URL}/${res.key}`;
									this.$emit('uploadSuccess', imgData, uploadPicUrl);
								},
								error => {
									uni.hideLoading();
									// resolve(error)
								}, {
									region: 'SCN', // (必须填写正确)ECN, SCN, NCN, NA, ASG,分别对应七牛的:华东,华南,华北,北美,新加坡 5 个区域
									domain: domain, // // bucket 域名,下载资源时用到。如果设置,会在 success callback 的 res 参数加上可以直接使用的 ImageURL                                         字  段。否则需要自己拼接
									key: key, // [非必须]自定义文件 key。如果不设置,默认为使用微信小程序 API 的临时文件名
									// 以下方法三选一即可,优先级为:uptoken > uptokenURL > uptokenFunc
									uptoken: token // 由其他程序生成七牛 uptoken
								},
								res => {
									//上传进度
									uni.hideLoading();
									if (res.progress === 100) {
										// resolve(keys);
									}
								}
							);
						});
					}
				});
			},
			//移除图片
			handleRemove(url, index) {
				this.Fimages.splice(index, 1);
				this.$emit('handleRemove', this.Fimages);
			}
		}
	};
</script>

<style scoped lang="scss">
	.bank-license {
		width: 124upx;
		height: 124upx;
		border-radius: 8upx;
		border: 2upx #999999 dashed;
		margin: 32upx 10rpx;
		background: #eee;
		text-align: center;
		line-height: 100upx;
		color: #666;
		font-size: 100rpx;
		font-weight: 300;
		display: inline-block;
		vertical-align: middle;
		position: relative;

		.license-pic {
			width: 120upx;
			height: 120upx;
			display: block;
		}

		.del-icon {
			box-sizing: border-box;
			padding-left: 7rpx;
			position: absolute;
			color: #fff;
			right: -12rpx;
			top: -20upx;
			background-color: #FD463E;
			width: 40rpx;
			height: 40rpx;
			border-radius: 100%;
			line-height: 40rpx;
		}
	}
</style>