<template>
	<view class="upload">
		<view class="upload_1">
			<view class="list" v-for="(item,index) in list" :key="index">
				<image class="image" :src="item.url" @click="toView(index,item)"></image>
				<uni-icons class="del" type="close" size="30" color="#007AFF" @click="uplDel(index,item)"></uni-icons>
			</view>
			<view class="list" v-if="list&&list.length<count" @tap="uplSuc()">
				<uni-icons class="add" type="plusempty" size="55" color="#007AFF"></uni-icons>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			list: {
				type: Array,
			},
			name: {
				type: String,
			},
			count: {
				type: Number,
				default: 1
			}
		},
		data() {
			return {

			};
		},
		methods: {
			uplSuc() {
				const that = this;
				let serverFile = that.$config.serverFile;
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'],
					sourceType: ['album', 'camera'],
					success: async function(res) {
						let tempFile = JSON.parse(JSON.stringify(res.tempFilePaths));
						const arr = await that.$apifile(`/applet/upload`, 'file', tempFile[0],
							'file');
						if (arr.errcode == '0') {
							that.$emit('uplSuc', {
								data: {
									id: arr.data.name,
									name: arr.data.name,
									uri: arr.data.uri,
									url: serverFile + arr.data.uri
								},
								name: that.name
							})
						} else {
							uni.showToast({
								title: arr.errmsg,
								icon: 'none'
							});
						}
					}
				});
			},
			// 删除图片
			uplDel(index, e) {
				const that = this;
				that.$emit('uplDel', {
					data: {
						file: e,
						index: index
					},
					name: that.name
				})
			},
			// 图片预览
			toView(index, e) {
				const that = this;
				uni.previewImage({
					current: index,
					urls: [e.url]
				})
			}
		}
	}
</script>

<style lang="scss">
	.upload {
		padding: 0 2vw;

		.upload_1 {
			display: flex;
			flex-wrap: wrap;

			.list {
				position: relative;
				width: 25vw;
				height: 25vw;
				text-align: center;
				margin: 1vw;
				border: 1px solid var(--f99Color);

				.image {
					width: 100%;
					height: 100%;
				}

				.add {
					position: relative;
					top: 20px;
				}

				.del {
					position: absolute;
					right: 0;
					top: 0;
				}
			}
		}
	}
</style>