<template>
	<!-- 登记 -->
	<view class="content main">
		<app-header @back="back">扫码登记</app-header>
		<view>
			<u-cell title="选择粮库" :value="form.orgName" :is-link="true" @click="storehouseSelectShow = true"></u-cell>
			<user-info ref="userInfo"></user-info>
			<u-button @click="submit" type="primary">提交</u-button>
			<u-action-sheet2 :actions="orgList" :closeOnClickOverlay="true" :closeOnClickAction="true" title="选择粮库"
				:show="storehouseSelectShow" @select="onOrgSelect" @close="storehouseSelectShow = false">
			</u-action-sheet2>
			<tabbar></tabbar>
		</view>
	</view>
</template>

<script>
	import { HOME_INDEX, CAR_INDEX, MONITOR_INDEX, LIVE_INDEX } from '@/config/constants'
	import userInfo from '@/pages/components/userInfo.vue'
	import {
		showErr
	} from '@/utils/common'
	import {
		getOrgTypes,
		gsqReservationRegisterAdd
	} from '@/api/basic'
	import appHeader from '@/pages/components/header/header.vue'
	import tabbar from '@/pages/components/tabbar.vue'
	import uActionSheet2 from '@/components/u-action-sheet/my-action-sheet.vue'
	export default {
		components: {
			userInfo,
			appHeader,
			tabbar,
			uActionSheet2
		},
		data() {
			return {
				storehouseSelectShow: false,
				orgList: [],
				form: {
					orgName: null,
					orgId: null,
				}
			}
		},
		mounted() {
			
			getOrgTypes().then(resp => {
				const list = resp.data || list
				list.forEach(d => {
					d.name = d.orgName
				})
				this.orgList = resp.data || []
			})
		},
		methods: {
			back() {
				getApp().globalData.tabIndex = CAR_INDEX
				uni.redirectTo({
					url: '/pages/carManager/carManager'
				})
			},
			onOrgSelect(data) {
				console.log('choose', data);
				this.form.orgName = data.orgName
				this.form.orgId = data.orgId
			},
			submit() {
				this.$refs.userInfo.getUserInfo().then(info => {
					gsqReservationRegisterAdd({
						type: 2,
						orgId: this.form.orgId,
						driverName: info.driverName,
						identificationNumber: info.identificationNumber,
						mobileNumber: info.mobileNumber,
						vehicleType: info.vehicleType,
						licensePlateNumber: info.licensePlateNumber,
						driverAddress: info.driverAddress,
					}).then(resp => {
						if (resp.code === 200) {
							uni.redirectTo({
								url: '/pages/registration/success'
							})
						}
					})
				}).catch(err => {
					if (err) {
						showErr(err)
					}
				})
			}
		}
	}
</script>

<style>
.main {
	background-color: #f0f1f2;
	height: 100vh;
}
</style>