<template>
	<view>
		<image src="/static/icon/background.png" mode="" class="background"></image>
		<view style="padding-top: 100rpx;">
			<view class="align-items">
				<back></back>
			</view>
			<view class="project-content">
				<view class="font-forty-eight blue">
					项目名称：{{name}}
				</view>
				<view class="font-forty-eight blue">
					项目地址：{{address}}
				</view>
			</view>
			<view v-if="photo != null && photo.length >= 1">
				<view class="flex" v-for="(item,index) in photo" :key="index">
					<image :src="item" mode="" class="project-img" @click="showPhotos(index)" v-if="item.substring(item.length - 3) == 'jpg'"></image>
					<image :src="item" mode="" class="project-img" @click="showPhotos(index)" v-else-if="item.substring(item.length - 3) == 'JPG'"></image>
					<image :src="item" mode="" class="project-img" @click="showPhotos(index)" v-else-if="item.substring(item.length - 3) == 'png'"></image>
					<image :src="item" mode="" class="project-img" @click="showPhotos(index)" v-else-if="item.substring(item.length - 3) == 'PNG'"></image>
					<image :src="item" mode="" class="project-img" @click="showPhotos(index)" v-else-if="item.substring(item.length - 4) == 'jpeg'"></image>
					<image :src="item" mode="" class="project-img" @click="showPhotos(index)" v-else-if="item.substring(item.length - 4) == 'JPEG'"></image>
					<video :src="item" controls v-else class="project-img"></video>
				</view>
			</view>
			<view class="" style="display: flex; flex-wrap: wrap; margin: 0 55rpx;">
				<image src="/static/icon/chooseimg.png" mode="" style="width: 190rpx; height: 190rpx; margin: 0 12rpx; "
					@click="choose()"></image>
				<view v-for="(item,index) in imgymxs" :key="index" style="position: relative;">
					<view v-if="item.type == 'image'">
						<image :src="item.url" mode=""
							style="width: 190rpx; height: 190rpx; margin: 0 20rpx;" @click="showPhoto(index)">
						</image>
					</view>
					<view v-else>
						<video :src="item"
							style="width: 190rpx; height: 190rpx; margin: 0 20rpx;"></video>
					</view>
					<view @click="remove(index)" style="position: absolute; top: 0; right: 14rpx; border-radius: 50%; background-color: #FF0000;">
						<u-icon name="close" color="#FFFFFF" size="35" ></u-icon> 
					</view>
				</view>
			</view>
			<view class="project background-color1 font-sixty-four white" @click="updatePhoto()">
				保存数据
			</view>
		</view>

	</view>
</template>

<script>
	import service from '@/api/index.js'
	export default {
		data() {
			return {
				action: this.$HTTP.webUrl + `/obs`,
				headers: {
					MAuthorization: "wxBearer " + uni.getStorageSync('token')
				},
				url: [],
				id: 0,
				name: '',
				address: '',
				photo: [],

				uploading: false,
				imgArr: [],
				imgymxs: [],
				progress: 0 ,//图片或视频上传百分比
			}
		},
		onLoad(e) {
			console.log(e)
			this.id = e.id;
			this.name = e.name;
			this.address = e.address
			this.getEngineDetail();
		},
		methods: {
			updatePhoto() {
				if (this.imgArr.length > 0) {
					
					service.updateEnginePhoto({
						photo: this.imgArr,
						id: this.id
					}).then(res => {
						this.$UTILS.showPrompt('提交成功!')
						setTimeout(function() {
							uni.navigateTo({
								url: '/pages/noLogin/typeList'
							})
						}, 1000)
					}).catch(e => {
						this.$UTILS.showPrompt('提交失败！')
					})
				} else {
					this.$UTILS.showPrompt('请先选择照片!')
				}
			},
			showPhoto(index){
				uni.previewImage({
					current:index,
					urls:this.imgArr,
				})
			},
			showPhotos(index){
				uni.previewImage({
					current:index,
					urls:this.photo,
				})
			},
			getEngineDetail() {
				service.getEngineDetail({
					id: this.id
				}).then(res => {
					this.photo = res.photo
				})
			},
			choose() {
				let _this = this;
				uni.showActionSheet({
					title: '上传',
					itemList: ['图片', '视频'],
					success: (res) => {
						// console.log(res)
						if (res.tapIndex == 0) {
							this.chooseimage()
						} else {
							this.choosevideo()
						}
					}
				})
			},
			chooseimage() {
				console.log('图片')
				let _this = this;
				uni.chooseImage({
					sizeType: ['album', 'camera'],
					success(resp) {
						console.log('res--uni.chooseMedia', resp);
						resp.tempFiles.forEach((item, index) => {
							const task = uni.uploadFile({
								url: _this.$HTTP.webUrl + `/obs`,
								filePath: item.path,
								name: 'file',
								formData: {},
								header: _this.headers,
								success: res => {
									// 判断是否json字符串，将其转为json格式
									let data = _this.$u.test.jsonString(res
										.data) ? JSON.parse(res.data) : res.data;
									if (![200, 201, 204].includes(res.statusCode)) {
										// this.uploadError(index, data);
										_this.$UTILS.showPrompt('选取失败！')
									} else {
										// 上传成功
										// this.lists[index].response = data;
										// this.lists[index].progress = 100;
										// this.lists[index].error = false;
										// this.$emit('on-success', data, index, this.lists, this
										// 	.index);
										if (_this.progress === 100) {
											// console.log('_this.progress', _this.progress)
											// console.log('data----', data)
											// console.log('res--', res)
											_this.imgymxs.push({url:data.data.url,type:'image'})
											_this.imgArr.push(data.data.url)
											// console.log('imgArr', _this.imgArr)
											_this.$UTILS.showPrompt('选取成功！')
										}
									}
								},
								fail: e => {
									_this.$UTILS.showPrompt('选取失败！')
									this.uploadError(index, e);
								},
								complete: res => {
									_this.uploading = false;
									// _this.uploadFile(index + 1);
									// this.$emit('on-change', res, index, this.lists, this
									// .index);
								}
							});
							task.onProgressUpdate(res => {
								// if (res.progress > 0) {
								// 	this.lists[index].progress = res.progress;
								// 	this.$emit('on-progress', res, index, this.lists, this.index);
								// }
								_this.progress = res.progress;
								console.log('onProgressUpdate', res)
								uni.showLoading({
									title: '选取中'
								})
							});
						})
					},
				})
			},
			choosevideo() {
				let _this = this;
				console.log('视频')
				uni.chooseVideo({
					sourceType: ['album','camera'],
					maxDuration: 30,
					success(resp) {
						const task = uni.uploadFile({
							url: _this.$HTTP.webUrl + `/obs`,
							filePath: resp.tempFilePath,
							name: 'file',
							formData: {},
							header: _this.headers,
							success: res => {
								// 判断是否json字符串，将其转为json格式
								let data = _this.$u.test.jsonString(res
									.data) ? JSON.parse(res.data) : res.data;
								if (![200, 201, 204].includes(res.statusCode)) {
									this.uploadError(index, data);
								} else {
									// 上传成功
									// this.lists[index].response = data;
									// this.lists[index].progress = 100;
									// this.lists[index].error = false;
									// this.$emit('on-success', data, index, this.lists, this
									// 	.index);	
									if (_this.progress === 100) {
										console.log('_this.progress', _this.progress)
										console.log('data----', data)
										console.log('res--', res)
										// _this.imgArr.push(data.data.url)
										_this.imgymxs.push({url:data.data.url,type:'video'})
										_this.imgArr.push(data.data.url)
										console.log('imgArr', _this.imgArr)
										_this.$UTILS.showPrompt('选取成功！')
									} 
								}
							},
							fail: e => {
								_this.$UTILS.showPrompt('选取失败！')
								this.uploadError(index, e);
							},
							complete: res => {
								uni.hideLoading();
								_this.uploading = false;
								// _this.uploadFile(index + 1);
								// this.$emit('on-change', res, index, this.lists, this
								// .index);
							}
						});
						task.onProgressUpdate(res => {
							// if (res.progress > 0) {
							// 	this.lists[index].progress = res.progress;
							// 	this.$emit('on-progress', res, index, this.lists, this.index);
							// }
							_this.progress = res.progress;
							console.log('onProgressUpdate', res)
							uni.showLoading({
								title: '选取中'
							})
						});

					},
				})
			},
			remove(index) {
				uni.showModal({
					title: '提示',
					content: '是否删除该图片或视频？',
					success: (res) => {
						if (res.confirm) {
							this.imgArr.splice(index, 1);
							this.imgymxs.splice(index, 1);
							console.log('this.imgarr',this.imgArr)
						}
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.background {
		z-index: -1;
		position: fixed;
		width: 100%;
		height: 100%;
		background-size: 100% 100%;
	}

	.project-content {
		border: 4rpx solid #3857F3;
		margin: 80rpx 55rpx 30rpx;
		padding: 30rpx 20rpx;
		border-radius: 48rpx;
	}

	.project {
		border-radius: 72rpx;
		padding: 25rpx 0;
		text-align: center;
		margin: 60rpx 55rpx 0;
	}

	.project-img {
		width: 300rpx;
		height: 168rpx;
		margin: 20rpx auto;
	}
</style>
