<template>
	<view>
		<image src="/static/icon/background-from.png" mode="" class="background"></image>
		<view class="project-content">
			<u-form :model="form" :rules="rules" ref="form">
				<u-form-item borderBottom ref="item1">
					<view>备注</view>
					<u-input v-model="form.remarks" :disabled="type==1" type="textarea"
						placeholder="请输入警示柱信息"></u-input>
				</u-form-item>
				<u-form-item borderBottom ref="item1">
					<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()" v-if="type!=1">
						</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" v-if="type!=1"></u-icon>
							</view>
						</view>
					</view>
				</u-form-item>
			</u-form>
			<u-button style="margin: 500rpx 155rpx 0;" @click="save()" v-if="type!=1" type='primary'>确认上传</u-button>
		</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: [],
				uploading: false,
				imgymxs: [],
				progress: 0, //图片或视频上传百分比
				form: {},
				value: null,
				imgArr: [],
				type: 2,
				id: null,
				userId: null,
				createBy:null
			}
		},
		onLoad(e) {
			console.log(e)
			this.id = e.id
			//this.userId=e.userId
			if (e.type == 1) {
				this.type = e.type
				console.log(this.id)
				service.getpatrolRecord(this.id).then(res => {
					this.form = res
					//this.imgymxs=res.photoList
					if (null != res.photoList) {
						res.photoList.forEach(item => {
							let url = {}
							url.url = item
							url.type = 'image'
							this.imgymxs.push(url)
						})
					}
				})
			} else {
				//this.getwarningPileInfo(e.value)
			}
			uni.setNavigationBarTitle({
				title: '警示柱巡查记录',
			});
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: '#2d95f4',
			})
			this.getUsreName()
		},
		methods: {
			showPhoto(index) {
				uni.previewImage({
					current: index,
					urls: this.imgArr,
				})
			},
			showPhotos(index) {
				uni.previewImage({
					current: index,
					urls: this.photo,
				})
			},
			getwarningPileInfo(value) {
				service.getpatrolRecord(value).then(res => {
					this.form = res
					if (null != res.photoList) {
						//this.imgymxs=res.photoList
						if (null != res.photoList) {
							res.photoList.forEach(item => {
								let url = {}
								url.url = item
								url.type = 'image'
								this.imgymxs.push(url)
							})
						}
					}
					console.log(this.imgymxs)
				})
			},
			getUsreName() {
				service.getUserName().then(res => {
					this.userId = res.id;
					this.createBy=res.name
				})
			},
			save() {
				let _this = this
				if(this.imgArr.length==0)
				{
					this.$UTILS.showPrompt('请上传图片或视频！')
					return
				}
				this.form.photoList = this.imgArr
				this.form.warningPileId = this.id
				this.form.userId = this.userId
				this.form.createBy=this.userId
				service.patrolRecord(this.form).then(res => {
					console.log(res)
					_this.$UTILS.showPrompt('上报成功')
					// setTimeout(() => {
					// 	console.log('跳')
					// 	uni.switchTab({
					// 		url: `/pages/index/index`
					// 	})
					// }, 2000)
					this.form={}
					this.imgArr=[]
					this.imgymxs=[]
				})
			},
			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({
					sourceType: ['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: ['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>
	.project-content {
		padding: 20rpx;
		border-radius: 20rpx;
		background: #fff;
		width: 90%;
		margin: 0 auto;
	}

	.background {
		z-index: -1;
		position: fixed;
		width: 100%;
		height: 100%;
		background-size: 100% 100%;
	}
</style>