<template>
	<view class="container">
		<view class="list-group">
			<view class="list-item" @tap="bindUpImg" @longpress="bindDelImage" :data-idx="index"
				v-for="(item, index) in car_info" :key="item.unique">
				<image v-if="item.img_url == ''" class="img-item-demo" :src="item.url"></image>
				<image v-if="item.img_url != ''" class="img-item" :src="item.img_url"></image>
				<view class="img_text">{{ item.title? item.title : '车辆照片'}}</view>
				<view v-if="item.img_url == ''" class="empity-item">＋</view>
			</view>
		</view>

	</view>
</template>

<script>
	var common = require('../../common/common.js');
	export default {
		props: {
			car_info: {
				type: Array,
				required: true //必填项
			},
		},
		data() {
			return {
			}
		}
		/**
		 * 生命周期函数--监听页面加载
		 */
		,
		onLoad: function(options) {
			this.car_info.forEach(item => {
			    item.img_url = '' // 
			})
		},
		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function() {

		},
		methods: {
			// 上传
			bindUpImg: function(e) {
				const me = this
				const index = e.currentTarget.dataset.idx;
				common.upLoadImgQiNiu(
					function(imgUrl) {
						const car_info = me.car_info
						car_info[index].img_url = imgUrl
						me.setData({
							car_info: car_info
						})
						me.updateImages(me.car_info)
						me.$forceUpdate()
						
					},
					['camera']
				)
				
			},	
			
            // 删除图片
			bindDelImage: function(e) {
				const me = this;
				const index = e.currentTarget.dataset.idx
				// 样例图不删除
				if (me.car_info[index].img_url == '') return false
				uni.showModal({
					title: '提示',
					content: '你确定要删除吗？',
					showCancel: true,
					success: function(res) {
						if (res.confirm) {
							const car_info = me.car_info
							car_info[index].img_url = ''
							me.setData({
								car_info: car_info
							});
							// 注意异步加载car_info更新到父组件
							me.updateImages(car_info);
						}
					}
				})
			},
			updateImages(car_info) {
				this.$emit('update-car-images', car_info);
			}

		}
	};
</script>

<style>
	@import './uploader.css';
</style>