<template>
	<view class="content">
		<headerTopsTips></headerTopsTips>
		<uni-nav-bar left-icon="left" title="故障上报" backgroundColor="#e6eafa" @clickLeft="handlegoback" />
		<view class="tips">
			<!-- <u-icon name="list"></u-icon> -->
			<text class="icon iconfont icon-guzhang"></text>
			<text style="margin-left: 10rpx;">请选择故障类型</text>
		</view>
		<view class="main_form">
			<view class="choose_list">
				<view v-for="(item,index) in choose_list" :key="index" @click="handle_choose(index)"
					:class="item.check? 'ischeck_choose': '' ">
					{{item.label}}
				</view>
			</view>
			<!--  -->
		</view>
		<view class="tips">
			<!-- <u-icon name="list"></u-icon> -->
			<text class="icon iconfont icon-paizhao"></text>
			<text style="margin-left: 10rpx;">拍摄单车周围环境,便于维修师傅找车</text>
		</view>
		<view class="upload_imgs">
			<!-- <u-upload :action="action" :file-list="fileList"></u-upload> -->
			<!-- <uni-file-picker limit="1" title="最多选择1张图片" @select="select_image"></uni-file-picker> -->
			<view class="item_up">
				<uni-icons type="plusempty" size="30" @click="select_image"></uni-icons>
			</view>
			<view class="imgs_item" v-for="(item,index) in upload_imgs_list" :key="index">
				<image :src="item" mode="aspectFill"></image>
				<uni-icons type="closeempty" size="20" class="close_imgs" @click="remove_imgs(index)"></uni-icons>
			</view>
		</view>
		<view class="scan_code">
			<input type="text" placeholder="车牌号(车牌损坏不用填)" style="font-size: 12px; letter-spacing: 3rpx; height: 70rpx;"
				v-model="car_no" />
			<view style=" height: 1px; background: #d9d9d9;"></view>
			<input type="text" placeholder="备注" style="font-size: 12px; letter-spacing: 3rpx; height: 60rpx;"
				v-model="remark" />
		</view>
		<view style="margin-top: 20rpx; display: flex;
		justify-content: center;">
			<button class="submit_clas" @click="submit_rush">提交</button>
		</view>
	</view>
</template>

<script setup>
	import headerTopsTips from '@/pages/components/header_tops_tips.vue'
	import {
		baseUrl,
		apiPath
	} from '@/sheep/config';

	import roastApi from '@/sheep/api/emergent/index'
	import {
		ref
	} from 'vue'
	const choose_list = ref([{
			label: '私锁私用',
			check: false,
			id: 0
		},
		{
			label: '车牌缺损',
			check: false,
			id: 1
		},
		{
			label: '轮胎坏了',
			check: false,
			id: 2
		},
		{
			label: '车锁坏了',
			check: false,
			id: 3
		},
		{
			label: '违规乱停',
			check: false,
			id: 4
		},
		{
			label: '密码不对',
			check: false,
			id: 5
		},
		{
			label: '刹车坏了',
			check: false,
			id: 6
		},
		{
			label: '其他',
			check: false,
			id: 7
		},
	])
	const car_no = ref('')
	const remark = ref('')
	const fault_choose = ref(0)
	const upload_imgs_list = ref([])

	function handle_choose(index) {
		for (var i = 0; i < choose_list.value.length; i++) {
			if (index == i) {
				choose_list.value[i].check = true
			} else {
				choose_list.value[i].check = false
			}
		}
		fault_choose.value = index
	}

	function handlegoback() {
		uni.navigateBack()
	}

	async function submit_rush() {
		let data = {
			type: fault_choose.value,
			picture: upload_img.value.toString(),
			no: car_no.value,
			remark: remark.value
		}
		// console.log(data,'上传参数')
		const response = await roastApi.addCart(data)
		if (response.code == 0) {
			uni.showModal({
				title: '提交成功',
				content: '您的故障上报已成功提交',
				success: (res) => {
					uni.navigateBack()
				}
			});
		} else {
			uni.showModal({
				title: '提交失败',
				content: '您的故障上报失败,请稍后重试',
			
				success: (res) => {
					uni.navigateBack()
				}
			});
		}
	}

	function select_image() {
		// console.log(data.tempFilePaths, 'data')
		uni.chooseImage({
			count: 4, //默认9
			sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album'], //从相册选择
			success: function(img) {
				// console.log(img.tempFilePaths, 'img')
				upload_img.value = []
				upload_imgs_list.value = img.tempFilePaths
				for(var i = 0; i < img.tempFilePaths.length; i++) {
					handle_upload_images(img.tempFilePaths[i])
				}
				// if (upload_imgs_list.value.length == 0) {
				// 	upload_imgs_list.value = img.tempFilePaths
				// } else {
				// 	for (var i = 0; i < img.tempFilePaths.length; i++) {
				// 		if (upload_imgs_list.value.length >= 3) {
				// 			return
				// 		} else {
				// 			upload_imgs_list.value.push(img.tempFilePaths[i])
				// 		}
				// 	}
				// }
			}
		})
		
	}
	const upload_img = ref([])

	function handle_upload_images(data) {
		// console.log(data[0], '上传的文件')
		uni.uploadFile({
			url: `${baseUrl + apiPath}/infra/file/upload`,
			filePath: data,
			name: 'file',
			method: 'post',
			header: {
				'tenant-id': 1,
				'Authorization': uni.getStorageSync('token')
			},
			success: (res) => {
				const data_inter = JSON.parse(res.data)
				// console.log(data_inter, '上传成功')
				upload_img.value.push(data_inter.data)
			}
		})
	}
	
	// 删除上传的照片
	function remove_imgs(index) {
		upload_imgs_list.value.splice(index,1)
	}
</script>

<style lang="scss" scoped>
	.content {
		height: 100vh;
		background: #f8f8f8;
		// padding: 20rpx;
		// box-sizing: border-box;
	}

	.submit_clas {
		width: 500rpx;
		color: #fff;
		background: #000;
	}

	.main_form {
		// height: 700rpx;
		background: #ffffff;
		width: 100%;
		border-radius: 10rpx;
		padding: 40rpx 20rpx;
		box-sizing: border-box;
		font-size: 14px;
		font-weight: 600;
		color: #202020;
	}

	.phone {
		color: #44c159;
		font-size: 12px;
		text-decoration: underline;
	}

	.tips {
		// margin-top: 30rpx;
		// color: #b6b6b6;
		padding: 20rpx 0 20rpx 40rpx;
		font-size: 12px;
	}

	.scan_code {
		margin-top: 20rpx;
		background: #fff;
		padding: 20rpx 40rpx;
	}

	.choose_list {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
		justify-content: space-evenly;

		>view {
			width: 45%;
			line-height: 60rpx;
			border-radius: 8rpx;
			font-size: 12px;
			border: 1px solid #d8d8d8;
			text-align: center;
			margin-bottom: 20rpx;
		}

		>view:nth-child(7),
		>view:nth-child(8) {
			margin-bottom: unset;
		}
	}

	.ischeck_choose {
		background: #000;
		color: #fff;
		opacity: 0.8;
	}

	.submit_btn {
		line-height: 80rpx;
		width: 80%;
		margin: 20rpx auto;
		background: #44c159;
		color: #fff;
		font-size: 14px;
		text-align: center;
		border-radius: 40rpx;
	}

	.upload_imgs {
		width: 100%;
		height: 150rpx;
		padding: 20rpx 40rpx;
		background: #fff;
		// padding: 10rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;

		.item_up {
			width: 100rpx;
			height: 100rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			border: 1px solid #ccc;
		}

		.imgs_item {
			width: 100rpx;
			height: 100rpx;
			margin: 0 10rpx;
			position: relative;

			image {
				width: 100%;
				height: 100%;
			}

			.close_imgs {
				position: absolute;
				right: -5rpx;
				top: -5rpx;
			}
		}
	}
</style>