<template>
	<view class="center">
		<view>
			<u-navbar left-text="返回" :autoBack="true" placeholder="true">
			</u-navbar>
		</view>

		<view class="mar-20 br-16" style="background-color: white;">
			<view class="mar-20 fs-32 fw-600" style="text-align: left;">访客登记</view>
			<view class="list-container">
				<view class="list-item">
					<view class="flex  w-220" style="text-align: left;">
						<text class="t">*</text>
						<text class="name">访客姓名</text>
					</view>
					<view style="flex: 1;">
						<input class="" style="text-align: left;" placeholder="请输入姓名" placeholder-style="color:#999999"
							v-model="data.guestName" />
					</view>
				</view>
				<view class="list-item" @click="open">
					<view class="flex w-220" style="text-align: left;">
						<text class="t">*</text>
						<text class="name">到访小区</text>
					</view>
					<view class="flex-column-center" style="flex: 1;">
						<text style="flex: 1;text-align: left;"
							v-model="data.communityId">{{address==''?'请选择小区':address}}</text>
						<image class="h-20 w-20" src="@/static/icon_r.png"></image>
					</view>
				</view>
				<view class="list-item">
					<view class="flex  w-220" style="text-align: left;">
						<text class="t">*</text>
						<text class="name">到访房间号</text>
					</view>
					<view style="flex: 1;">
						<input class="" style="text-align: left;" placeholder="请输入房间号" placeholder-style="color:#999999"
							v-model="data.guestRoom" />
					</view>
				</view>
				<view class="list-item">
					<view class="flex  w-220" style="text-align: left;">
						<text class="t">*</text>
						<text class="name">手机号</text>
					</view>
					<view style="flex: 1;">
						<input class="" style="text-align: left;" placeholder="请输入手机号" placeholder-style="color:#999999"
							v-model="data.guestPhone" />
					</view>
				</view>
				<view class="list-item">
					<view class="flex w-220" style="text-align: left;">
						<text class="t">*</text>
						<text class="name">验证码</text>
					</view>
					<view style="flex: 1;">
						<input class="" style="text-align: left;" placeholder="请输入验证码" placeholder-style="color:#999999"
							v-model="data.phoneCode" />
					</view>
					<view>
						<u-toast ref="uToast"></u-toast>
						<u-code seconds="60" ref="uCode" @change="codeChange"></u-code>
						<u-button @tap="getCode">{{tips}}</u-button>
					</view>
				</view>
				<view class="list-item">
					<view class="flex" style="flex: 1;text-align: left;">
						<text class="t"> </text>
						<text class="name">车牌号</text>
					</view>
					<view style="flex: 2;">
						<input class="" style="text-align: left;" placeholder="请输入车牌号" placeholder-style="color:#999999"
							v-model="data.plateNumber" />
					</view>
				</view>
			</view>
		</view>

		<view class="mt-65">
			<text class="plr-100 ptb-25 br-12 fs-30" style="background:#1FCA7C;color: #fff;" @click="onAdd">提交</text>
		</view>
		<u-popup :show="show" mode="center" @close="close" @open="open" round="20">
			<view class="flex-column mlr-50" style="width:600rpx;">
				<text class="fs-32 fw-600 mtb-20">选择小区</text>
				<scroll-view scroll-y class="h-400">
					<view v-for="(item, index) in listXq" :key="index" @click="onSelect(index)">
						<view class="fs-24 pad-30" style="border-bottom: 1rpx solid #E9E9E9;">
							<text
								:style="selectIndex==index?'color: #1FCA7C;':'color: #000000;'">{{item.communityName}}</text>
						</view>
					</view>
				</scroll-view>
				<view class="mt-40" @click="onSubmit()">
					<text class="plr-80 ptb-12 br-12" style="background:#1FCA7C;color: #fff;">下一步</text>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tips: '',
				show: false,
				selectIndex: -1,
				address: '',
				data: {
					guestName: "", //访客姓名
					guestPhone: "", //访客手机
					communityId: 0, //小区ID
					guestRoom: "", //访客房间号
					plateNumber: "", //车牌号
					phoneCode: "" //手机验证码
				},
				listXq: [],
				list1: [
					"https://zhongqian.xnszz.com/1-images/home/20250213002.png",
				],
				listService: [{
						img: "https://zhongqian.xnszz.com/1-images/home/20250213002.png",
						title: "访客",
						describe: "请点击输入访客信息"
					},
					{
						img: "https://zhongqian.xnszz.com/1-images/home/20250213002.png",
						title: "报修报事",
						describe: "保修报事一键即发"
					},
					{
						img: "https://zhongqian.xnszz.com/1-images/home/20250213002.png",
						title: "生活缴费",
						describe: "水费电费在线缴纳"
					},
					{
						img: "https://zhongqian.xnszz.com/1-images/home/20250213002.png",
						title: "常用电话",
						describe: "随时查找常用电话"
					}
				],
			};
		},
		onLoad(options) {
			this.listAll()
		},
		methods: {
			//选择小区
			listAll() {
				const params = {
					url: '/api/community/listAll',
					loadingTip: '',
					method: 'GET',
					data: {}
				}
				this.$request(params).then(res => {
					console.log(res.data)
					this.listXq = res.data
				})
			},
			onSelect(index) {
				this.selectIndex = index
			},
			onSubmit() {
				this.show = false
				this.data.communityId = this.listXq[this.selectIndex].communityId;
				this.address = this.listXq[this.selectIndex].communityName;
				uni.setStorageSync('addressItem', this.listXq[this.selectIndex]);
			},
			//获取验证码
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				if (!this.data.guestPhone) {
					this.$wskj.toast('请输入手机号');
					return
				}
				if (this.$refs.uCode.canGetCode) {
					const params = {
						url: '/api/login/sendSmsCode',
						loadingTip: '',
						method: 'GET',
						data: {
							mobile: this.data.guestPhone
						}
					}
					this.$request(params).then(res => {
						this.$refs.uCode.start();
					})

				} else {
					this.$wskj.toast('倒计时结束后再发送');
				}
			},
			//新增访客
			onAdd() {
				if (!this.data.guestName) {
					this.$wskj.toast('请输入姓名');
					return
				}
				if (!this.data.communityId) {
					this.$wskj.toast('请选择小区');
					return
				}
				if (!this.data.guestRoom) {
					this.$wskj.toast('请输入房间号');
					return
				}
				if (!this.data.guestPhone) {
					this.$wskj.toast('请输入手机号');
					return
				}
				if (!this.data.phoneCode) {
					this.$wskj.toast('请输入验证码');
					return
				}
				const params = {
					url: '/api/guest/add',
					loadingTip: '加载中...',
					data: this.data
				}
				this.$request(params).then(res => {

				})
			},
			open() {
				this.show = true
			},
			close() {
				this.show = false
			}
		}
	};
</script>

<style lang="scss" scoped>
	.center {
		height: 100vh;
		display: flex;
		flex-direction: column;
		background: #f5f5f5;
	}

	.list-container {
		display: flex;
		flex-direction: column;
	}

	.list-item {
		display: flex;
		flex-direction: row;
		align-items: center;
		padding: 20rpx 20rpx;
		border-radius: 15rpx;
		border-bottom: 1rpx solid #f5f5f5;
		color: #999999;

		.t {
			width: 30rpx;
			color: #FF0000;
		}

		.name {}
	}
</style>