<template>
	<view class="bg">
		<view class="title_wrap"><text class="title">基本信息</text></view>
		<car-number-input @numberInputResult="carNumber" :defaultStr="state.form.vehiclePlate"></car-number-input>
		<u-form label-width="230" :model="state.form" ref="uForm" :label-style='labelStyle'>
			<u-form-item label="车牌颜色" :left-icon='`${$imgUrl}issuance/point-form.png`' :left-icon-style='leftIcon'
				borderBottom>
					<u-input v-model="state.form.vehiclePlateColorStr" type="select" @click="show1 = true" input-align='right'/>
			</u-form-item>
			<u-form-item label="姓名" :left-icon='`${$imgUrl}issuance/point-form.png`' :left-icon-style='leftIcon'
				borderBottom>
				<u-input placeholder='请输入姓名' type="idcard" v-model="state.form.customerName" maxlength="18"
					inputAlign="right" />
			</u-form-item>
			<u-form-item label="联系方式" :left-icon='`${$imgUrl}issuance/point-form.png`' :left-icon-style='leftIcon'
				borderBottom>
				<u-input placeholder='请输入手机号码' type="number" v-model="state.form.customerTel" maxlength="11"
					inputAlign="right" />
			</u-form-item>
		</u-form>
	</view>
	<view class="bg">
		<view class="title">工单信息</view>
        <u-form label-width="230" :model="state.form" ref="uForm" :label-style='labelStyle' borderBottom>
			<u-form-item label="工单类型" :left-icon='`${$imgUrl}issuance/point-form.png`' :left-icon-style='leftIcon'
				borderBottom>
					<u-input v-model="state.form.questionTypeStr" type="select" @click="show2 = true" input-align='right'/>
			</u-form-item>
			<u-form-item label="是否ETC用户" :left-icon='`${$imgUrl}issuance/point-form.png`' :left-icon-style='leftIcon'
				borderBottom>
					<u-input v-model="state.form.etcUserStr" type="select" @click="show3 = true" input-align='right'/>
			</u-form-item>
			<u-form-item label="事件发生日期" :left-icon='`${$imgUrl}issuance/point-form.png`' :left-icon-style='leftIcon'
				borderBottom>
				<picker mode="date" @change="bindDateChange">
					<view class="picker" v-if="!state.form.eventOccurrenceDate">
					  请选择日期
					</view>
					<view class="data">
						{{state.form.eventOccurrenceDate}}
					</view>
			  </picker>
			</u-form-item>
			<u-form-item label="客户投诉" :left-icon='`${$imgUrl}issuance/point-form.png`' :left-icon-style='leftIcon'
				borderBottom>
			</u-form-item>
			<textarea v-model="state.form.appeal" style="border: 1rpx dashed rgb(192, 196, 204);min-height: 200rpx; width: 100%;border-radius: 18rpx;padding: 20rpx;box-sizing: border-box;" placeholder="请输入述求" auto-height adjust-position></textarea>
		   <u-form-item label="其他佐证材料" :left-icon='`${$imgUrl}issuance/point-form.png`' :left-icon-style='leftIcon'
		   	borderBottom>
		   		<text class="common-word">(不限于图片、文件、不超过5个。)</text>
		   </u-form-item>
		   <button type="default" class="ui-btn-another" @click="addFile()">
		   	+添加附件
		   </button>
		</u-form>
	</view>
	<view class="action">
		<button type="default" class="ui-btn" @click="savaHandle()">
			提交
		</button>
	</view>
	<!-- 选择车牌颜色 -->
	<u-select v-model="show1" :list="state.colorRange" @confirm="changeColor"></u-select>
	<!-- 工单类型 -->
	<u-select v-model="show2" :list="state.questionTypeRange" @confirm="changeQuestionType"></u-select>
	<!--  -->
	<u-select v-model="show3" :list="state.etcUserRange" @confirm="changeEtcUser"></u-select>

</template>

<script setup lang="ts">
	import { navTo,msg,uploadFile} from "@/utils/utils";
	import { reactive,ref } from "vue";
	import carNumberInput from "@/components/car-number-input/car-number-input.vue";
	import { onLoad } from "@dcloudio/uni-app";
	import { getItem } from "@/utils/storage.ts"
    import {requestNew} from "@/utils/network/request.js";
	import {selfServiceUserAdd} from "@/utils/network/api.js";
	const labelStyle = {
		color: "#004576",
		fontSize: "28rpx",
	}
	const leftIcon = {
		height: '100%',
		width: '8rpx',
		display: 'flex',
		'align-items': 'center',
		'margin-right': '4rpx',
	}
	const show1 = ref(false)
	const show2 = ref(false)
	const show3 = ref(false)
	const state = reactive({
		form: {
			customerName: "",
			customerTel: "",
			vehiclePlate: "",
			vehiclePlateColor: "",
			vehiclePlateColorStr:"",//车牌颜色中文
			questionType:"",
			questionTypeStr:"",
			etcUser:"",
			etcUserStr:"",
			eventOccurrenceDate:"",//2016-09-01 00:00:00
			appeal:"",
			supportingMaterialsUrl:"",
			supportingMaterialsUrlShow:[]
		},
		colorRange: [],
		questionTypeRange:[
			{value: 1, label: "工单1"},
			{value: 2, label: "工单2"},
		],
		etcUserRange:[
			{value: 1, label: "是"},
			{value: 0, label: "否"},
		],
	})
	onLoad((option : any) => {
		let getColor = getItem('key')['VEHICLE_COLOR_TYPE'];
		for (var k = 0; k < getColor.length; k++) {
			let obj = {};
			obj['value'] = getColor[k]['code']
			obj['label'] = getColor[k]['name']
			state.colorRange.push(obj)
		}
		console.log("state.colorRange",state.colorRange)
	});

	//车牌号输入
	const carNumber = (val : any) => {
		state.form.vehiclePlate = val.trim();
	};
	const changeColor = (item) => {
			state.form.vehiclePlateColor = item[0].value
			state.form.vehiclePlateColorStr = item[0].label
			console.log(item)
		}
	const changeQuestionType = (item) => {
			state.form.questionType = item[0].value
			state.form.questionTypeStr = item[0].label
			console.log(item)
		}
    const changeEtcUser = (item) => {
    		state.form.etcUser = item[0].value
    		state.form.etcUserStr = item[0].label
    		console.log(item)
    	}
const bindDateChange=(e)=>{
    console.log('picker发送选择改变，携带值为', e.detail.value)
	state.form.eventOccurrenceDate= e.detail.value+" 00:00:00"
  }
	const savaHandle = () => {
		if(!state.form.vehiclePlate){
			msg('请输入车牌号');
			return;
		}
		if(!state.form.vehiclePlateColor){
			msg('请选择车牌颜色');
			return;
		}
		if(!state.form.customerName){
			msg('请输入姓名');
			return;
		}
		if(!state.form.customerTel){
			msg('请输入手机号码');
			return;
		}
		if(!state.form.questionType){
			msg('请选择工单类型');
			return;
		}
		if(!state.form.etcUser){
			msg('请选择是否ETC用户');
			return;
		}
		if(!state.form.eventOccurrenceDate){
			msg('请选择事件发生日期');
			return;
		}
		if(!state.form.appeal){
			msg('请输入述求');
			return;
		}
		if(!state.form.supportingMaterialsUrl){
			msg('请上传其他佐证材料');
			return;
		}
		state.form.supportingMaterialsUrl=state.form.supportingMaterialsUrlShow.toString()
		const options = {
			type: 2,
			data: {
				...state.form
			},
			method: "POST",
			showLoading: true,
		};
		requestNew(selfServiceUserAdd, options).then((res) => {
			console.log("添加成功",res)
		});
	}
	const addFile=()=>{
		uni.chooseMessageFile({
		  count: 5,
		  type: 'all',
		  success (res) {
		    // tempFilePath可以作为img标签的src属性显示图片
		    console.log("res",res)
			var files=res.tempFiles
			for(var i=0;i<files.length;i++){
				uploadFile(files[i]['path'], "", "").then((data) => {
					state.form.supportingMaterialsUrlShow.push(data);
					console.log("state.form.supportingMaterialsUrlShow",state.form.supportingMaterialsUrlShow)
				})
			}
			
		  }
		})
	}
</script>

<style lang="scss" scoped>
	.bg {
		background-color: white;
		width: 88%;
		margin: 0 auto;
		margin-top: 20rpx;
		border-radius: 12px;
		border: 1px solid #FFFFFF;
		padding: 20rpx;
		overflow: hidden;

		.title_wrap {
			display: flex;
			font-weight: 400;
			margin-bottom: 40rpx;

			.title {
				font-size: 30rpx;
				color: #01243A;
			}
		}

		.des {
			font-weight: 400;
			font-size: 28rpx;
			color: #01243A;
			line-height: 56rpx;
			margin-top: 10rpx;
			text-indent: 1rem;
		}
	}

	.action {
		position: absolute;
		left: 0;
		height: 188rpx;
		background-color: #fff;
		border-radius: 30rpx 30rpx 0 0;
		width: 100vw;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		margin-top: 20rpx;
	}
	.picker{
		text-align: right;
		color: rgb(192, 196, 204);
	}
	.data{
		text-align: right;
	}
	.five{
		color: rgb(195,188,157);
	}
</style>