<template>
	<view class="index">
		<u-upload :fileList="fileList6" @afterRead="afterRead" @delete="deletePic" name="6" multiple
			:maxCount="maxCount" :width="width" :height="height" :disabled="disabled" :previewFullImage="true">
			<view v-if="imgType === 'takePictures'" class="index-upload baiyin-flex baiyin-flex-c-t baiyin-flex-c-b" style="text-align: center;line-height: 170rpx;">
				<image  class="index-upload-tlo-lp" src="@/static/icon/zp4.png" mode="widthFix"></image>
			</view>
			<view v-else class="index-upload baiyin-flex baiyin-flex-c-t baiyin-flex-c-b" style="text-align: center;">
				<!-- <image v-if="imgType === 'camera'" class="index-upload-tlo-lp" src="@/static/icon/zp4.png" mode="widthFix"></image> -->
				<view class="index-upload-tlo baiyin-flex baiyin-flex-wrap baiyin-flex-c-t baiyin-flex-c-b"
					 style="text-align: center;">
					<image :class="index-upload-tlo-lp" src="@/static/icon/zp4.png" mode="">
					</image>
					<view class="" v-if="crossTextShow">上传</view>
				</view>
			</view>
		</u-upload>
	</view>
</template>

<script>
	import apiBaseConfig from '@/config/index.js';
	import {
		orderStatisticsPOST,
		ordersjUploadPOST
	} from '@/api/upload.js';
	export default {
		props: {
			disabled: {
				type: Boolean,
				default: false
			},
			imgType: {
				type: String,
				default: 'camera' //camera cross
			},
			crossTextShow: {
				type: Boolean,
				default: true
			},
			maxCount: {
				type: Number,
				default: 10
			},
			width: {
				type: Number,
				default: 170
			},
			height: {
				type: Number,
				default: 170
			},
			//是否注册页面上传图片
			register:{
				type: Boolean,
				default: false
			}
			
		},
		data() {
			return {
				imgBgUrl: apiBaseConfig.imgBgUrl,
				globalData: getApp().globalData,
				fileList6: []
			};
		},
		watch: {
			fileList6: {
				handler(val,old) {
					let self = this,
						are = [];
					if (self.fileList6 && self.fileList6.length) {
						// self.fileList6.forEach(item => {
						// 	are.push({url:item.url});
						// });
						self.$emit('changeList', val);
					}
				},
				immediate: true,
				deep: true
			}
		},
		onLoad() {},
		methods: {
			initle(list, index) {
				let self = this;
				// console.log(list, index, '=====initle');
				let arr = [];
				if (list && list.length) {
					list.forEach((fk, fl) => {
						arr.push({
							url: fk,
							status: 'success'
						});
					});
					self.fileList6 = arr;
				}
			},
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1);
			},
			// 新增图片
			async afterRead(event) {
				let self = this;
				// 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
				let lists = [].concat(event.file);
				lists.forEach((item, index) => {
					console.log(item.thumb.split('.')[item.thumb.split('.').length - 1]);
					if (!['jpg', 'jpeg', 'png', 'JPG', 'JPEG', 'PNG'].includes(item.thumb.split('.')[item.thumb
							.split('.').length - 1])) {
						self.$toast('只支持jpg/jpeg/png格式');
						self.$delete(lists, index);
					}
				});
				let fileListLen = this[`fileList${event.name}`].length;
				lists.map(item => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					});
				});
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url);
					let item = this[`fileList${event.name}`][fileListLen];
					this[`fileList${event.name}`].splice(
						fileListLen,
						1,
						Object.assign(item, {
							status: 'success',
							message: '',
							url: result
						})
					);
					fileListLen++;
					self.$forceUpdate();
				}
			},
			uploadFilePromise(url) {
				let token = uni.getStorageSync("token")
				console.log(this.register,"ordersjUploadPOST=================")
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: this.register==true?ordersjUploadPOST:orderStatisticsPOST, // 仅为示例,非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						header: {
							'Content-Type': 'application/json;charset=UTF-8',
							'Authorization': `Bearer ${token}`
						},
						// success: res => {
						// 	setTimeout(() => {
						// 		resolve(res.data.data);
						// 	}, 1000);
						// }
						success: ({
							data
						}) => {
							const dat = JSON.parse(data)
							const {
								url
							} = dat.data;
							setTimeout(() => {
								resolve(url);
							}, 1000);
						}
						// success(res) {
						// 	console.log('上传文件成功', res)
						// 	//do something
						// },
						// fail(error) {
						// 	console.log('上传文件失败', error)
						// }
					});
				});
			}
		}
	};
</script>

<style lang="scss" scoped>
	.index {
		&-upload {
			width: 170rpx;
			height: 170rpx;
			background-color: #f3f3f3;

			>image {
				width: 48rpx;
				height: 48rpx;
			}

			&-tlo {
				width: 100%;
				height: 100%;

				image {
					width: 40rpx;
					height: 40rpx;
				}

				view {
					width: 100%;
					text-align: center;
					font-size: 24rpx;
					font-weight: 400;
					color: #000000;
					// margin-top: -45rpx;
				}

				&-lp {
					margin-top: 43rpx;
				}
			}
		}
	}

	::v-deep .u-upload__wrap {
		display: flex;
		justify-content: center;
	}
</style>