<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(`/material/applet/upload`, 'file', tempFile[0], 'file');
						that.$emit('uplSuc', {
							data: {
								name: arr.name,
								uri: arr.uri,
								url: serverFile + arr.uri
							},
							name: that.name
						})
					}
				});
			},
			// 删除图片
			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-direction: row;
			flex-wrap: wrap;

			.list {
				position: relative;
				width: 30vw;
				height: 28vw;
				text-align: center;
				margin: 0 3vw 2vw 0;
				border-radius: 5px;
				box-shadow: 0 0 2px var(--f85Color);

				.image {
					width: 100%;
					height: 100%;
					border-radius: 5px;
				}

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

				.del {
					position: absolute;
					right: 0;
					top: 0;
				}
			}

			.list:nth-child(3n) {
				margin: 0 0 2vw 0;
			}
		}
	}
</style>