<template>
	<view class="content">
		<view>
			<view class="form-data">
				<u-form :model="form" ref="uForm" :label-width="140" :error-type="['toast']">
						<u-form-item prop="gatherNum_other" label="其他垃圾:" >
							<u-input :disabled="isView" @input="numberToFixed('gatherNum_other',0,999999)" type="number" placeholder="请输入" v-model="form.gatherNum_other" />
							<text slot="right" style="color: #000000;">桶(约{{form.gatherNum_other*240}}kg)</text>
						</u-form-item>
						<u-form-item prop="gatherNum_chuyu" label="厨余垃圾:" >
							<u-input :disabled="isView" @input="numberToFixed('gatherNum_chuyu',0,999999)" placeholder="请输入" type="number" v-model="form.gatherNum_chuyu" />
							<text slot="right" style="color: #000000;">桶(约{{form.gatherNum_chuyu*240}}kg)</text>
						</u-form-item>
						<u-form-item prop="gatherNum_jianzhu" label="建筑垃圾:" >
							<u-input :disabled="isView" @input="numberToFixed('gatherNum_jianzhu',2,999999)" placeholder="请输入" type="digit" v-model="form.gatherNum_jianzhu" />
							<text slot="right" style="color: #000000;">吨(约{{form.gatherNum_jianzhu*1000}}kg)</text>
						</u-form-item>
				</u-form>
			</view>
			<view class="form-data remark">
				<text>信息备注</text>
				<textarea :disabled="isView" :auto-height="true" :maxlength="30" v-model="form.remarks" class="receiveAddress"/>
			</view>
			<view class="form-data qyImg">
				<text>清运照片</text>
				<view class="info-main">
					<view v-if="photograph.length<3 && !isView" class="camera-btn">
						<view @click="goPhotograph" class="photograph-camera">
							<u-icon name="camera" color="#bfbfbf" size="60" ></u-icon>
						</view>
						<view >
							(最多3张)
						</view>
						<view >
							(每张最大不超过10M)
						</view>
					</view>
					<view v-show="photograph.length" v-for="(item,index) in photograph" :key="index" class="photograph-con">
						<u-icon v-show="!isView" name="close-circle-fill" color="#fa3534" size="50" class="close-circle-icon" @click="cancelPhotograph(index)"></u-icon>
						<u-image width="100%" height="100%" :src="item" @click="previewPictures(item)"></u-image>
					</view>
				</view>
			</view>
			<u-button v-if="isView" @click="isView = false" class="submitBtn" type="waring" shape="circle">修改</u-button>
			<u-button v-else @click="submit" :loading="loading" class="submitBtn" type="success" shape="circle">提交</u-button>
			<uni-popup ref="openModal" type="center">
				<uni-popup-dialog content="确认提交吗？" @confirm="onOk" :title="title"></uni-popup-dialog>
			</uni-popup>
		</view>
	</view>
</template>

<script>
	import uniPopup from '@/components/uni-popup/uni-popup.vue'
	import uniPopupDialog from '@/components/uni-popup/uni-popup-dialog.vue'
	import {gatherSave,gatherDetail} from '@/api/index.js'
	import {numberToFixed,saveImgToAliOss} from '@/libs/tools.js'
	export default {
		components: {
			uniPopup,uniPopupDialog
		},
		data() {
			return {
				title:'',
				loading:false, // 提交按钮加载圈
				photograph: [], //  图片路径
				form: {
					gatherNum_other: '', // 其它垃圾
					gatherNum_chuyu: '', // 厨余垃圾
					gatherNum_jianzhu: '', // 建筑垃圾
					remarks: '' // 备注
				},
				isView: false ,// 是否是查看详情
				itemId:''
			};
		},
		onLoad(option) {
			console.log(option,option.id,'ppppppppp')
			// 查看
			if(option && option.id){
				this.isView = true
				this.itemId=option.id
				this.getDetailData()
			}else {
				console.log('--------新增')
				this.photograph = []
				this.form.remarks = ''
				this.$refs.uForm.resetFields()
			}
		},
		onUnload() {
		},
		methods: {
			// 保留几位小数
			numberToFixed: function (key, num, max) {
			  let val = this.form[key]
			  let ret = numberToFixed(val, num, max)
			  this.$nextTick(() => {
			  	this.form[key] = ret
			  })
			},
			// 获取详情数据
			getDetailData(){
				gatherDetail({id:this.itemId}).then(res=>{
					if(res.status==200){
						console.log(res)
						const a = res.data.gatherRubbishList.find(item=>item.rubbishType=='GATHER_BUILDING')
						const b = res.data.gatherRubbishList.find(item=>item.rubbishType=='GATHER_OTHER')
						const c = res.data.gatherRubbishList.find(item=>item.rubbishType=='GATHER_KITCHEN')
						this.form.gatherNum_jianzhu= a ? a.gatherNum : '0'
						this.form.gatherNum_chuyu= c ? c.gatherNum : '0'
						this.form.gatherNum_other= b ? b.gatherNum : '0'
						this.form.remarks=res.data.remarks
						this.photograph=res.data.imageUrlList || []
					}
				})
			},
			// 拍照或从相册选图片
			goPhotograph() {
				uni.chooseImage({
				    count: 1, //默认9
				    sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
				    sourceType: ['album','camera'], //从相册选择
				    success:  (res) =>{
						console.log(res,'rrrrrrrrrrr')
				        console.log(JSON.stringify(res.tempFilePaths));
						console.log(this.photograph.length,'this.photograph')
						if(res.tempFiles[0].size>10485760){
							uni.showToast({
								title: '图片过大无法上传！',
								icon: 'none'
							})
							return
						}
						uni.showLoading({
							title:'正在保存图片...',
							mask: true
						})
						saveImgToAliOss(res.tempFilePaths[0],(ret)=>{
							// console.log(ret,'----------ret')
							this.photograph.push(ret.data)
							// _this.photograph = ret.data
							uni.hideLoading()
						})
				    }
				});
			},
			//  预览图片
			previewPictures(item) {
				const photograph = [item];
				uni.previewImage({
					urls: photograph,
				});
			},
			//  删除图片
			cancelPhotograph(index) {
				setTimeout(() => {
					this.photograph.splice(index,1)
				}, 500);
			},
			// 提交
			submit(){
				if((!this.form.gatherNum_other ||this.form.gatherNum_other==0)  && (!this.form.gatherNum_chuyu || this.form.gatherNum_chuyu==0) && (!this.form.gatherNum_jianzhu || this.form.gatherNum_jianzhu==0)) {
					uni.showToast({
						title: '请至少输入一项垃圾分类的数量！',
						icon: 'none'
					})
					return false
				} else {
					this.$refs.openModal.open()
				}
			},
			// 确认提交
			onOk() {
				this.$refs.openModal.close()
				let params = {
					rubbishEntityList: [],
					imageUrlList: this.photograph,
					remarks: this.form.remarks
				}
				if(this.itemId){
					params.id=this.itemId
				}
				if(this.form.gatherNum_other) {
					params.rubbishEntityList.push({
						"rubbishType":"GATHER_OTHER",
						"rubbishWeight": this.form.gatherNum_other*240,
						"gatherNum": this.form.gatherNum_other,
						"unit":"BUCKET"
					})
				}
				if(this.form.gatherNum_chuyu) {
					params.rubbishEntityList.push({
						"rubbishType":"GATHER_KITCHEN",
						"rubbishWeight": this.form.gatherNum_chuyu*240,
						"gatherNum": this.form.gatherNum_chuyu,
						"unit":"BUCKET"
					})
				}
				if(this.form.gatherNum_jianzhu) {
					params.rubbishEntityList.push({
						"rubbishType":"GATHER_BUILDING",
						"rubbishWeight": this.form.gatherNum_jianzhu*1000,
						"gatherNum": this.form.gatherNum_jianzhu,
						"unit":"TON"
					})
				}
				this.loading=true
				gatherSave(params).then(res=>{
					if(res.status == 200) {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
						setTimeout(()=>{
							uni.navigateBack()
						},300)
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
					}
					this.loading= false
				})
			},
			
		},
		// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		}
	}
</script>

<style lang="scss">
	.content{
		width: 100%;
		height: 100vh;
		padding: 0;
		background: #F5F5F5;
		// .buttons{
		// 	font-size: 32upx;
		// 	display: block;
		// 	height: 80upx;
		// 	line-height: 80upx;
		// 	margin: 0 auto;
		// 	background-color: #0DC55F;
		// 	color: #fff;
		// 	border-radius: 50upx;
		// }
		.receiveAddress{
			width: 100%;
			line-height: 1.5em;
			box-sizing: border-box;
			font-style: normal;
		}
		.form-data{
			padding: 0 40upx;
			margin-bottom: 20upx;
			background-color: #fff;
		}
		.remark{
			padding: 40upx ;
		}
		.qyImg{
			padding: 40upx ;
		}
		.info-main {
			margin-top: 14upx;
			border-radius: 12upx;
			background-color: #fff;
			display: flex;
			.location-icon {
				padding-left: 10upx;
				vertical-align: sub;
			}
			.photograph-camera {
				border: 2upx dashed #bfbfbf;
				border-radius: 12upx;
				width: 140upx;
				height: 140upx;
				display: flex;
				align-items: center;
				justify-content: center;
			}
			.camera-btn{
				display: flex;
				flex-direction: column;
				align-items: center;
				color: #999999;
				font-size: 24upx;
			}
			.photograph-con {
				margin: 0 20upx;
				width: 140upx;
				height: 140upx;
				text-align: center;
				position: relative;
				.photograph-icon {
					display: inline-block;
					padding-top: 48upx;
				}
				.close-circle-icon {
					background-color: #fff;
					border-radius: 50%;
					position: absolute;
					right: -23upx;
					top: -23upx;
					z-index: 9;
				}
			}
		}
		.submitBtn{
			font-size: 32upx;
			display: block;
			width: 670upx;
			height: 80upx;
			line-height: 80upx;
			margin: 0 auto;
			color: #fff;
			border-radius: 50upx;
		}
	}
</style>
