<template>
	<view class="receive-images">
		<view class="title-view">
			<view class="title-left-view" >
				<text>{{ uploadForm.label }}</text>
			</view>
			<view class="info-tip-text" v-if="!disabled">{{ uploadForm.tips }}</view>
			<view class="image-view">
				<block v-for="(item, key) in uploadForm.imgs" :key="key">
					<view class="info-image-box" v-if="key < uploadForm.count">
						<image class="info-image-box-inner" mode="aspectFill" v-if="item.path" :src="item.path" @click="perview(key)"></image>
						<view class="info-image-box-close" v-if="!disabled" @click="removeImage(key)"><u-icon name="close"></u-icon></view>
					</view>
				</block>
				<view class="info-image-box" @click="handleClick" v-if="!disabled&&uploadForm.imgs.length < uploadForm.count">
					<view class="info-image-box-add"><u-icon name="plus"></u-icon></view>
				</view>
			</view>
			<view v-if="!disabled&&uploadForm.imgs.length == 0" class="bottom-tip-view">
				<image class="bottom-tip-image" mode="widthFix" :src="tishiImage"></image>
				<text>最少上传一张箱码照片</text>
			</view>
		</view>
	</view>
</template>

<script>
import base from '@/components/upload/index';
export default {
	name: 'ReceiveImages',
	mixins: [base],
	props: {
		disabled: {
		  type: Boolean,
		  default: false,
		},
	},
	data() {
		return {
			tishiImage: process.uniEnv.qn_base_url + 'tishi.png',
			uploadForm: {
				filedName: '',
				count: 3,
				label: '箱码照片',
				tips: '上传箱码照片',
				imgs: []
			}
		};
	},
	computed: {},
	created() {},
	mounted() {
		// this.initImages();
	},
	methods: {
		initImages(images) {
			this.uploadForm.imgs = images || [];
			console.log(this.uploadForm.imgs)
		},

		valueChange(e) {
			this.$emit('onScanCodeChange', e);
		},
		async handleClick() {
			if(this.disabled) return false;
			let { count, imgs } = this.uploadForm;
			// let imgCount=count-imgs.length;
			// 已满3张不能再上传
			if (imgs.length === count) {
				return false;
			}
			//  size: ["original",'compressed']
			let imgList = await this.chooseImg({ count: 1 });
			this.uploadForm.imgs.push(...imgList);
			this.updateImages();
		},
		// 预览
		perview(index) {
			let urls = this.uploadForm.imgs.map(item => {
				return item.path;
			});
			uni.previewImage({
				current: index,
				urls: urls
			});
		},
		removeImage(index) {
			this.uploadForm.imgs.splice(index, 1);
			this.updateImages();
		},
		updateImages() {
			this.$emit('updateImages', this.uploadForm.imgs);
		}
	}
};
</script>

<style lang="scss" scoped>
.receive-images {
	background-color: #ffffff;
	border-radius: 12rpx;
	padding: 20rpx 30rpx;
	margin-bottom: 20rpx;
	color: #333333;

	.title-view {
		font-size: 32rpx;
		.title-left-view {
			font-weight: bold;
		}
		.info-tip-text {
			font-size: 24rpx;
			color: #666666;
			margin-top: 10rpx;
		}
	}
	.bottom-tip-view {
		color: #999999;
		font-size: 20rpx;
		display: flex;
		align-items: flex-start;
		margin-top: 30rpx;
	}

	.bottom-tip-image {
		width: 22rpx;
		height: 22rpx;
		margin-right: 8rpx;
		margin-top: 4rpx;
		flex-shrink: 0;
	}

	.image-view {
		margin-top: 30rpx;
		display: flex;
		.info-image-box {
			width: 216rpx;
			height: 216rpx;
			background-color: #cccccc;
			position: relative;
			margin-right: 20rpx;
			border-radius: 12rpx;
			&:last-child {
				margin-right: 0;
			}
		}
		.info-image-box-inner {
			border-radius: 12rpx;
			overflow: hidden;
			width: 100%;
			height: 100%;
		}
		.info-image-box-close {
			width: 38rpx;
			height: 38rpx;
			border-radius: 50%;
			background-color: #666666;
			font-size: 22rpx;
			line-height: 38rpx;
			color: #ffffff;
			display: flex;
			justify-content: center;
			align-items: center;
			position: absolute;
			top: -8rpx;
			right: -8rpx;
			z-index: 1;
		}
		.info-image-box-add {
			width: 100%;
			height: 100%;
			font-size: 120rpx;
			color: #ffffff;
			display: flex;
			justify-content: center;
			align-items: center;
		}
	}
}
</style>
