<template>
	<view class="page">
		<u-navbar title="故障上报" :border-bottom="false" :background="bgc" title-color='#000' title-size='36'
			height='45'></u-navbar>
		<view class="cardbox">
			<view class="tip">
				<view class="ipnt">*</view>故障车编号
			</view>
			<view class="iptbox">
				<input type="text" class="ips" v-model="sn" placeholder="请输入车辆二维码下方编号"
					placeholder-style='font-weight: 400;font-size: 36rpx;color: #808080;' />
				<view class="iptbtn" @click="scanQRCode()">
					<image src="https://lxnapi.ccttiot.com/bike/img/static/u7thMxZGRQrS5yWh2mCD" mode=""></image>
				</view>
			</view>
		</view>
		<view class="cardbox">
			<view class="tip">
				<view class="ipnt">*</view>请输入车辆故障部位
			</view>

			<view class="input-container">
				<view class="placeholder" v-if="!textValue">请输入故障问题</view>
				<textarea class="custom-textarea" v-model="textValue" @focus="hidePlaceholder" style="border: none;"
					@input="updateWordCount" @blur="showPlaceholder"></textarea>
				<text class="word-count">{{ currentCount }}/500</text>
			</view>
		</view>
		<view class="cardbox">
			<view class="tip">
			请对故障部位拍照
			</view>
			<view class="icon">
			
				<view class="imgbox" v-for="(item,index) in imglist " :key="index">
					<image :src="item" mode=""></image>
				</view>
				<view class="imgbox" @click="btn">
					<image src="https://api.ccttiot.com/smartmeter/img/static/uY8CPw9YE6JxPzcHUaqf" mode="aspectFill"></image>
				</view>
			
			</view>
		</view>
		<view class="btn" @click="sub()">
			提交
		</view>
		<view class="sbjl" @click="btnzgjl">
			历史上报记录 <image src="https://api.ccttiot.com/smartmeter/img/static/uVbMMNz0STQy3PMImgRJ" mode=""></image>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bgc: {
					backgroundColor: "#fff",
				},
				cbgz: ['车头','车灯'],
				csgz: ['车座','车把','轮胎'],
				tsgz: ['其他'],
				textValue: '',
				currentCount: 0,
				imglist: [],
				token: '',
				sn:'',
				upurl:''
			}
		},
		onShow() {
			this.getQiniuToken()
		},
		onLoad(e) {

		},
		computed: {
			appointmentServiceFee() {
				return this.$store.getters.appointmentServiceFee;
			},
			dispatchFee() {
				return this.$store.getters.dispatchFee;
			},
			vehicleManagementFee() {
				return this.$store.getters.vehicleManagementFee;
			},
			startingPrice() {
				return this.$store.getters.startingPrice;
			},
			timeFee() {
				return this.$store.getters.timeFee;
			},
			timeMinutes() {
				return this.$store.getters.timeMinutes;
			},
			deposit() {
				return this.$store.getters.deposit;
			},
			userId() {
				return this.$store.getters.userId;
			},
		},
		methods: {
			// 点击跳转到故障记录
			btnzgjl(){
				uni.navigateTo({
					url:'/page_user/guzhang/guzhanglv'
				})
			},
			sub(){
				 let data = {
						faultDetail:this.textValue,
						vehicleCode:this.sn,
				        faultSite:'',
						picture:this.imglist.join(',')
				    }
					console.log(data,'dadadad');
					this.$u.post("/app/fault",data).then((res) => {
						if (res.code == 200) {
							uni.showToast({
								title: '提交成功',
								icon: 'success',
								duration: 1000
							})
							setTimeout(()=>{
								 uni.navigateBack({
								        delta: 1,
								    })
							},1100)
						}else if(res.code == 401){
							uni.showModal({
								title: '提示',
								content: '您当前未登录，是否前去登录？',
								showCancel: true,
								success: function (res) {
									if (res.confirm) {
										uni.reLaunch({
											url:'/pages/login/login'
										})
									} else if (res.cancel) {
										
									}
								}
							})
						} else{
							uni.showToast({
								title: res.msg,
								icon: 'none',
								duration: 1000
							})
						}
					})
			},
			getQiniuToken() {
				this.$u.get("/common/qiniuToken").then((res) => {
					if (res.code == 200) {
					this.token=res.data
					console.log(res,'resres')
					}
				})
			},
			getlist(){
				// checked
				this.$u.get("/appVerify/fault/allList?dictType=et_fault_type").then((res) => {
					if (res.code == 200) {
						 this.cbgz = res.data.headstock.map(item => ({...item, checked: false}))
					    this.csgz = res.data.body.map(item => ({...item, checked: false}))
					    this.tsgz = res.data.special.map(item => ({...item, checked: false}))
					} else {
						uni.showToast({
							title: '未登录，请登录后尝试',
							icon: 'none',
							duration: 2000
						})
					}
				})
			},
			toggleCheckbox(index) {
				this.cbgz[index].checked = !this.cbgz[index].checked
				this.$forceUpdate()
			},
			csCheckbox(index){
				this.csgz[index].checked = !this.csgz[index].checked
			},
			tsCheckbox(index){
				this.tsgz[index].checked = !this.tsgz[index].checked
			},
			hidePlaceholder() {
				this.placeholderVisible = false
			},
			showPlaceholder() {
				if (!this.textValue) {
					this.placeholderVisible = true
				}
			},
			scanQRCode() {
				uni.scanCode({
					onlyFromCamera: true,
					scanType: ['qrCode'],
					success: res => {
						let sn = null;
						let queryParams = res.result.split('?')[1]
						if (queryParams) {
						    let params = queryParams.split('&')
						    params.forEach(param => {
						        let [key, value] = param.split('=')
						        if (key === 's') {
						            sn = value
						        }
						    })
						}
						this.sn = sn
					},
					fail: err => {
						console.error('扫描失败：', err);
						uni.showToast({
							title: '扫描失败',
							icon: 'none'
						})
					}
				})
			},
			btn() {
				let _this = this
				let math = 'static/' + _this.$u.guid(20)
				uni.chooseImage({
					count: 9,
					type: 'all',
					success(res) {
						const tempFilePaths = res.tempFiles
						wx.uploadFile({
							url: 'https://up-z2.qiniup.com',
							name: 'file',
							filePath: tempFilePaths[0].path,
							formData: {
								token: _this.token, //后端返回的token
								key: 'smartmeter/img/' + math
							},
							success: function(res) {
								console.log(res,'resres');
								let str = JSON.parse(res.data)
								console.log(str.key)
								_this.userImgs = 'https://api.ccttiot.com/'+ str.key
								console.log(_this.userImgs)
								_this.imglist.push(_this.userImgs)
							}
						})
					}
				})
			},
			updateWordCount() {
				this.currentCount = this.textValue.trim().replace(/\s+/g, '').length
				// if (this.currentCount > 500) {
				// 	this.textValue = this.textValue.slice(0, this.textValue.lastIndexOf(' ', 500)).trim()
				// 	// uni.showToast({
				// 	// 	title: '字数已超过500字限制',
				// 	// 	icon: 'none'
				// 	// })
				// }
			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #fff;
	}

	.page {
		width: 750rpx;
		padding-bottom: 200rpx;
		.sbjl{
			width: 750rpx;
			margin-top: 20rpx;
			justify-content: center;
			display: flex;
			align-items: center;
			font-size: 28rpx;
			color: #3D3D3D;
			image{
				width: 40rpx;
				height: 40rpx;
			}
		}
		.btn{
			position: fixed;
			left: 34rpx;
			bottom: 80rpx;
			// margin: 0 auto;
			display: flex;
			align-items: center;
			justify-content: center;
			width: 680rpx;
			height: 90rpx;
			background: #4C97E7;
			border-radius: 54rpx 54rpx 54rpx 54rpx;
			font-weight: 500;
			font-size: 40rpx;
			color: #FFFFFF;
		}
		.cardbox {
			margin: 30rpx auto;
			width: 680rpx;
			padding: 20rpx 45rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 10rpx 64rpx 0rpx rgba(0, 0, 0, 0.08);
			border-radius: 20rpx 20rpx 20rpx 20rpx;
			.tip {
				display: flex;
				flex-wrap: nowrap;
				font-weight: 700;
				font-size: 32rpx;
				color: #3D3D3D;
				line-height: 44rpx;
				.ipnt {
					margin-top: 10rpx;
					margin-right: 12rpx;
					color: #FF4444;
					font-size: 48rpx;
				}
			}
			.iptbox {
				display: flex;
				align-items: center;
				margin-top: 30rpx;
				width: 592rpx;
				height: 80rpx;
				border-radius: 20rpx 20rpx 20rpx 20rpx;
				border: 2rpx solid #979797;
				.ips {
					margin-left: 30rpx;
					width: 80%;
				}
				.iptbtn {
					display: flex;
					align-items: center;
					justify-content: center;
					width: 112rpx;
					height: 80rpx;
					background: #4C97E7;
					border-radius: 20rpx 20rpx 20rpx 20rpx;
					image {
						width: 37rpx;
						height: 37rpx;
					}
				}
			}
			.txt {
				margin-top: 18rpx;
				font-weight: 400;
				font-size: 28rpx;
				color: #3D3D3D;
			}
			.icon {
				display: flex;
				flex-wrap: wrap;
				align-items: center;
				margin-top: 40rpx;
				.imgbox {
					width: 33%;
					image {
						width: 142rpx;
						height: 142rpx;
						border-radius: 10rpx;
					}
				}
			}
			.checkbox {
				display: flex;
				flex-wrap: wrap;

				.check_li {
					position: relative;
					 margin-right: 10rpx;
					margin-top: 18rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					width: 142rpx;
					height: 65rpx;
					border-radius: 20rpx 20rpx 20rpx 20rpx;
					border: 2rpx solid #C7C7C7;
					image{
						position: absolute;
						right: 0;
						bottom: 0;
						width: 40rpx;
						height: 20rpx;
					}
				}
				.check_li:nth-child(4n) {
				  margin-right: 0;
				}
				.act1 {
					width: 142rpx;
					height: 65rpx;
					 border: 2rpx solid #1E807A;
				}
			}
			.input-container {
				position: relative;
				width: 612rpx;
				height: 248rpx;
				background: #FFFFFF;
				box-shadow: 0rpx 16rpx 40rpx 0rpx rgba(42, 130, 228, 0.1);
				border-radius: 20rpx;
				margin-top: 40rpx;
				overflow: hidden;
				padding-right: 38rpx;
				box-sizing: border-box;
				border: 2rpx solid #C7C7C7;
			}
			
			.placeholder {
				position: absolute;
				top: 18rpx;
				left: 38rpx;
				color: #999;
				/* placeholder颜色 */
				pointer-events: none;
				/* 确保点击事件可以穿透到textarea上 */
			}
			.custom-textarea {
				width: 100%;
				height: 100%;
				/* 设置一个合适高度 */
				padding-top: 18rpx;
				/* 为placeholder留出空间 */
				padding-left: 38rpx;
				box-sizing: border-box;
				border: 1px solid #ccc;
			}
			
			.word-count {
				position: absolute;
				right: 10px;
				bottom: 10px;
				font-size: 12px;
				color: #999;
			}
		}
	}
</style>