<template>
	<view class="zd_container">
		<nav-bar :title="title"></nav-bar>
		<view class="zd_content">
			<view class="zd_wrapper">
				<view class="zd-row border-bottom">
					<view class="zd-col-5">
						<span class="filter_label">当前点位</span>
					</view>
					<view class="zd-col-19">
						<search-box
						v-model="val1"
						@handleChange="handleChange"
						/>
					</view>
				</view>
				<view class="zd-row">
					<view class="zd-col-5">
						<span class="filter_label">下一点位</span>
					</view>
					<view class="zd-col-19 filter_select">
						<zxz-uni-data-select v-model="index1" filterable :localdata="options1"></zxz-uni-data-select>
					</view>
				</view>
			</view>
		</view>
		<view class="zd-row submit-bar">
			<button class="zd-col-11 button-default" @tap="clearUp">清空</button>
			<button class="zd-col-11 button-primary" :class="{'button-info': index1 === '' || !val1}" :disabled="disabled" @tap="_createP2pTask">确认</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {queryEndPointsByStart, createP2pTask} from '@/utils/getData2.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				title: '',
				val1: '',
				options1: [],
				index1: '',
				disabled: false
			};
		},
		onLoad (options) {
			this.title = options.title
		},
		methods: {
			handleChange (e) {
				this._queryEndPointsByStart(e)
			},
			/** 下一点位下拉框 */
			async _queryEndPointsByStart (e) {
				let res = await queryEndPointsByStart(e)
				if (res.length > 0) {
					res.map(el => {
						this.$set(el, 'value', el.point_code2)
						this.$set(el, 'text', el.point_name)
					})
					this.options1 = [...res]
					this.index1 = this.options1[0].value
				}
			},
			async _createP2pTask () {
				this.disabled = true
				if (!this.val1 || this.index1 === '') {
					this.disabled = false
					return
				}
				try {
					let res = await createP2pTask(this.val1, this.index1)
					this.clearUp()
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				} catch (e) {
					this.disabled = false
				}
			},
			clearUp () {
				this.index1 = ''
				this.options1 = []
				this.val1 = ''
				this.disabled = false
			}
		}
	}
</script>

<style lang="stylus">
</style>
