<template>
	<view class="content">
		<view class="info">
			<scroll-view scroll-y="true" class="scroll-view">
				<view class="list-scroll-view">
					<view class="one">
						<view class="text">
							免费估值 精准报价
						</view>
					</view>
					<view class="two">
						<view class="text">为爱车估值</view>
						<view class="two_1" @tap="toSelect" v-if="!form.brand&&!form.brand?.title">
							<u-icon name="plus-circle-fill" size="20" color="#2979ff"></u-icon>
							<text>请选择车辆品牌</text>
						</view>
						<view class="two_2" @tap="toSelect" v-else>
							<image class="image"
								:src="form.brand?.url&&form.brand?.url.length>0?form.brand?.url[0].url:'/static/logo.png'">
							</image>
							<view class="title">{{form.brand}}</view>
							<u-icon name="arrow-right"></u-icon>
						</view>
						<u--form class="form" :model="form" ref="uForm" :rules="rules" labelWidth="65"
							:border-bottom="true">
							<u-form-item label="车辆车系" prop="bank">
								<input class="input" v-model="form.bank" placeholder="填写车辆车系" />
								<template #right>
									<u-icon name="arrow-right"></u-icon>
								</template>
							</u-form-item>
							<u-form-item label="车辆车型" prop="type">
								<input class="input" v-model="form.type" placeholder="填写车辆车型" />
								<template #right>
									<u-icon name="arrow-right"></u-icon>
								</template>
							</u-form-item>
							<u-form-item label="上牌日期" prop="start">
								<picker name="date" @change="dateChange" mode="date">
									<view class="picker">{{form.start||'请选择'}}</view>
								</picker>
								<template #right>
									<u-icon name="arrow-right"></u-icon>
								</template>
							</u-form-item>
							<u-form-item label="行驶里程" prop="course">
								<picker name="course" @change="courseChange" :value="index" :range="courseList"
									range-key="label">
									<view class="picker">{{form.course||'请选择'}}</view>
								</picker>
								<template #right>
									<u-icon name="arrow-right"></u-icon>
								</template>
							</u-form-item>
							<u-form-item label="上牌城市" prop="city">
								<text class="picker" @tap="toCity">{{form.city||'请选择'}}</text>
								<template #right>
									<u-icon name="arrow-right"></u-icon>
								</template>
							</u-form-item>
							<u-form-item label="估值地区" prop="place">
								<text class="picker" @tap="toPlace">{{form.place||'请选择'}}</text>
								<template #right>
									<u-icon name="arrow-right"></u-icon>
								</template>
							</u-form-item>
							<u-form-item label="联系人" prop="contacts">
								<input class="input" v-model="form.contacts" placeholder="填写联系人" />
							</u-form-item>
							<u-form-item label="联系电话" prop="tel">
								<input class="input" v-model="form.tel" placeholder="填写常用手机号" />
							</u-form-item>
						</u--form>
					</view>
				</view>
			</scroll-view>
		</view>
		<view class="foot">
			<view class="foot_1">
				<button class="btn" @click="toEvaluate">免费估价</button>
			</view>
			<view class="foot_2">
				<checkbox-group @change="changeAgree">
					<label>
						<checkbox color="#2979ff" style="transform:scale(0.7)" :checked="agree" />
						<text @tap.stop="toAgree">已阅读并同意 《个人信息保护声明》，提交后将会有1-3家服务商为您提供报价服务</text>
					</label>
				</checkbox-group>
			</view>
		</view>
	</view>
</template>

<script setup lang="ts">
	import { getCurrentInstance, computed, ref } from 'vue';
	//该依赖已内置不需要单独安装
	import { onLoad, onShow } from "@dcloudio/uni-app";
	// 请求接口
	const $api = getCurrentInstance()?.appContext.config.globalProperties.$api;
	const $config = getCurrentInstance()?.appContext.config.globalProperties.$config;
	// 基本信息
	const config = ref({});
	// 用户协议
	const agree = ref(false);
	// 用户信息
	const user = ref({});
	const form = ref({ brand: '', bank: '', type: '', start: '', course: '', place: '', city: '', tel: '' });
	const rules = {
		'bank': {
			type: 'string',
			required: true,
			message: '请输入车辆车系',
			trigger: ['blur', 'change'],
		},
		'type': {
			type: 'string',
			required: true,
			message: '请输入车辆车型',
			trigger: ['blur', 'change'],
		},
		'start': {
			type: 'string',
			required: true,
			message: '请选择上牌日期',
			trigger: ['blur', 'change'],
		},
		'course': {
			type: 'string',
			required: true,
			message: '请选择行驶里程',
			trigger: ['blur', 'change'],
		},
		'tel': {
			type: 'string',
			required: true,
			message: '请输入手机号',
			trigger: ['blur', 'change'],
		}
	}
	const uForm = ref(null);
	// openid
	const openid = computed(() => {
		return uni.getStorageSync('openid');
	})
	// 字典表
	const courseList = ref([{ value: '1', label: '1万公里' }, { value: '2', label: '2万公里' }, { value: '3', label: '3万公里' }, { value: '4', label: '4万公里' }, { value: '5', label: '5万公里' }]);
	onLoad(async () => {
		await searchConfig();
	})
	onShow(() => {
		uni.$on('toRoute', function (data) {
			form.value.brand = data.name
		})
		uni.$on('toCity', function (data) {
			form.value[data.field] = data.name
		})
	})
	// config信息
	const searchConfig = async () => {
		config.value = uni.getStorageSync('config');
	};
	// 上牌日期
	const dateChange = (e) => {
		form.value.start = e.target.value;
	};
	// 行驶里程
	const courseChange = (e) => {
		const data = courseList.value[e.detail.value]
		if (data) form.value.course = data.label
	};
	// 选择上牌城市
	const toCity = () => {
		uni.navigateTo({
			url: `/pagesHome/city/index?field=${'city'}`,
		})
	};
	// 选择估值城市
	const toPlace = () => {
		uni.navigateTo({
			url: `/pagesHome/city/index?field=${'place'}`,
		})
	};
	// 选择品牌
	const toSelect = () => {
		uni.navigateTo({
			url: `/pagesHome/brand/index`,
		})
	};
	// 估价
	const toEvaluate = async () => {
		if (agree.value) {
			if (openid.value) {
				uForm.value.validate().then(async res => {
					if (form.value.place || form.value.city) {
						form.value.openid = openid.value
						const res = await $api(`estimate`, 'POST', form.value);
						if (res.errcode == 0) {
							uni.showToast({
								title: '免费估价申请成功',
								icon: 'none'
							})
							uni.navigateBack({
								delta: 1
							})
						} else {
							uni.showToast({
								title: res.errmsg,
								icon: 'none'
							})
						}
					} else {
						uni.showToast({
							title: '缺少城市信息',
							icon: 'none'
						})
					}
				}).catch(err => {
					console.log(err, '校验失败');
				})
			} else {
				uni.showToast({
					title: '系统更新中，请稍后再试!',
					icon: 'none'
				})
			}
		} else {
			uni.showToast({
				title: '请阅读并同意用户协议和隐私政策',
				icon: 'none'
			})
		}
	};
	// 查看隐私协议
	const toAgree = () => {
		uni.navigateTo({
			url: `/pagesHome/agree/index`
		})
	};
	// 同意隐私协议
	const changeAgree = () => {
		agree.value = !agree.value;
	};
</script>
<style lang="scss" scoped>
	.content {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;

		.info {
			position: relative;
			flex-grow: 1;
			background-color: var(--f1Color);

			.one {
				display: flex;
				align-items: center;
				justify-content: center;
				height: 25vh;
				background: linear-gradient(to bottom, #2979ff, #f1f1f1);

				.text {
					background-image: -webkit-linear-gradient(left, #000, #2979ff 10%, #2979ff 20%, #2979ff 30%, #2979ff 40%, #2979ff 50%, #2979ff 60%, #2979ff 70%, #2979ff 80%, #2979ff 90%, #000 100%);
					-webkit-text-fill-color: transparent;
					-webkit-background-clip: text;
					-webkit-background-size: 200% 100%;
					-webkit-animation: masked-animation 4s linear infinite;
					font-size: 30px;
					font-weight: bold;
				}

				@keyframes masked-animation {
					0% {
						background-position: 0 0;
					}

					100% {
						background-position: -100% 0;
					}
				}
			}

			.two {
				background-color: var(--mainColor);
				margin: 0 3vw 2vw 3vw;
				padding: 2vw;
				border-radius: 10px;

				.text {
					font-size: var(--font16Size);
					font-weight: bold;
					padding: 2vw;
				}

				.two_1 {
					display: flex;
					align-items: center;
					justify-content: center;
					background-color: var(--f1Color);
					padding: 6vw 2vw;

					text {
						font-size: var(--font16Size);
						margin: 0 0 0 1vw;
					}
				}

				.two_2 {
					display: flex;
					align-items: center;
					color: #323233;
					padding: 2vw 0;
					font-size: var(--font16Size);
					border-bottom: 1px solid var(--f5Color);

					.image {
						width: 60px;
						height: 60px;
						border-radius: 60px;
					}

					.title {
						width: 75%;
						margin: 0 0 0 2vw;
					}
				}

				.form {
					.picker {
						padding: 2vw;
					}

					.input {
						padding: 2vw;
					}
				}
			}
		}

		.foot {
			padding: 2vw;

			.foot_1 {
				.btn {
					background-color: var(--fFFColor);
					font-size: var(--font16Size);
				}
			}

			.foot_2 {
				font-size: var(--font12Size);
				color: var(--f85Color);
				margin: 1vw;
			}
		}
	}

	.scroll-view {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;

		.list-scroll-view {
			display: flex;
			flex-direction: column;
		}
	}
</style>