<template>
	<view>
		<uni-section :title="orderCode+'/'+shipmentName" type="line">
			<view class="example">
				<u-upload
					:fileList="imageList"
					:sizeType="imageSizeType"
					@delete="deletePic"
					@afterRead="readPic"
					name="1"
					previewImage
					:maxCount="3"
				>
				</u-upload>
				<button type="primary" @click="submitTrace()">提交</button>
			</view>
		</uni-section>
	</view>
</template>

<script>
	import request from '@/common/libs/request.js';
	export default {
		data() {
			return {
				orderCode:'',
				idOrder:'',
				imageSizeType:['original','compressed'],
				imageList: [],
				imageSrcList: []
			}
		},
		onLoad(option){
			if(option && option.idOrder){
				this.idOrder = option.idOrder
				this.orderCode = option.orderCode
        this.shipmentName = option.shipmentName
        this.index = option.index
				// this.getSrcTraceImage()
			}
		},
		methods: {
			// 删除图片
			deletePic(event) {
				this.imageList.splice(event.index, 1)
			},
      async readPic(event) {
				this.imageList.push(event.file)
			},
			submitTrace() {
        let _that = this
        if(_that.imageList.length<=0){
          uni.showToast({
            title: "请先拍照上传图片",
            icon: 'none'
          })
          return
        }
				// 请求前
				uni.showLoading({
					mask:true  //是否显示透明蒙层，防止触摸穿透，默认：false
				});


				let uploadFiles = []
				for (let i = 0; i < _that.imageList.length; i++) {
					uploadFiles.push({
						uri: _that.imageList[i].url,
						name: 'traceImage' + i
					})
				}
				// _that.imageList.forEach(x => {
				// 	uploadFiles.push({
				// 		uri: x.url
				// 	})
				// })
				let token = uni.getStorageSync('erpToken');
				let uploadUrl = "http://192.168.1.24:51106/driverSign/signItem";
				if (process.env.NODE_ENV === 'production') {
					uploadUrl = "http://192.168.1.24:51106/driverSign/signItem"
				}
        console.log("111",uploadFiles);
        uni.uploadFile({
					url: uploadUrl,
					files: uploadFiles,
					header: {
						'Access-Token': token
					},
					formData: {
            idItem: _that.idOrder
					},
					success: (res) => {
						console.log("222",res);
            uni.showToast({
              title: "签收成功",
            })
            uni.hideLoading();
            uni.navigateBack({})
            uni.$emit("eventName",_that.index)
					}
				})
			}
		}
	}
</script>

<style>
	.example {
		padding: 15px;
		background-color: #fff;
	}
</style>
