<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-7">
						<!-- <span class="filter_label">起点区域</span> -->
						<span class="filter_label">{{$t('filter.start-area')}}</span>
					</view>
					<view class="zd-col-24 filter_select">
						<uni-data-select v-model="index" :localdata="options" placeholder="" @change="selectChange"></uni-data-select>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-7">
						<!-- <span class="filter_label">起点</span> -->
						<span class="filter_label">{{$t('filter.start-point')}}</span>
					</view>
					<view class="zd-col-24 filter_select">
						<uni-data-select v-model="index3" :localdata="options3" placeholder="" @change="selectChange3"></uni-data-select>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-7">
						<!-- <span class="filter_label">终点区域</span> -->
						<span class="filter_label">{{$t('filter.end-area')}}</span>
					</view>
					<view class="zd-col-24 filter_select">
						<uni-data-select v-model="index2" :localdata="options2" placeholder="" @change="selectChange2"></uni-data-select>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-7">
						<!-- <span class="filter_label">终点</span> -->
						<span class="filter_label">{{$t('filter.end-point')}}</span>
					</view>
					<view class="zd-col-24 filter_select">
						<uni-data-select v-model="index4" :localdata="options4" placeholder="" @change="selectChange4"></uni-data-select>
					</view>
				</view>
			</view>
		</view>
		<view class="zd-row submit-bar">
			<button class="zd-col-8 button-default" @tap="clearUp">{{$t('button.clear')}}</button>
			<button class="zd-col-15 button-primary" :class="{'button-info': !index3 || !index4}" :disabled="disabled" @tap="handleConfirm">{{$t('button.confirm')}}</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import { confirmAction } from '@/utils/utils.js'
	import {getStartRegions, getEndRegions, getPointnByRegion, sendPointTask} from '@/utils/getData4.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				title: '',
				options: [],
				index: '',
				options2: [],
				index2: '',
				options3: [],
				index3: '',
				options4: [],
				index4: '',
				disabled: false
			};
		},
		onLoad (options) {
			this.title = options.title
		},
		created () {
			this._getStartRegions()
			this._getEndRegions()
		},
		methods: {
			async _getStartRegions () {
				try {
					let res = await getStartRegions()
					if (res) {
						this.options = res.data
					} else {
						this.options =[]
					}
				} catch (e) {
					this.options = []
				}
			},
			selectChange (e) {
				this.index = e
				this._getPointnByRegion1()
			},
			async _getEndRegions () {
				try {
					let res = await getEndRegions()
					if (res) {
						this.options2 = res.data
					} else {
						this.options2 =[]
					}
				} catch (e) {
					this.options2 = []
				}
			},
			selectChange2 (e) {
				this.index2 = e
				this._getPointnByRegion2()
			},
			async _getPointnByRegion1 () {
				try {
					let res = await getPointnByRegion(this.index)
					if (res) {
						this.options3 = res.data
					} else {
						this.options3 =[]
					}
				} catch (e) {
					this.options3 = []
				}
			},
			async _getPointnByRegion2 () {
				try {
					let res = await getPointnByRegion(this.index2)
					if (res) {
						this.options4 = res.data
					} else {
						this.options4 =[]
					}
				} catch (e) {
					this.options4 = []
				}
			},
			clearUp () {
				this.index = ''
				this.index2 = ''
				this.index3 = ''
				this.index4 = ''
				this.disabled = false
			},
			async handleConfirm() {
				if (!this.index3 || !this.index4) {
					return
				}
				const isConfirmed = await confirmAction("确认操作", "确定要执行此操作吗？")
				if (isConfirmed) {
					this._sendPointTask()
				}
			},
			async _sendPointTask () {
				this.disabled = true
				try {
					let res = await sendPointTask(this.index3, this.index4)
					if (res.code === '200') {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
						this.clearUp()
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
						this.disabled = false
					}
					this.disabled = false
				} catch (e) {
					this.disabled = false
				}
			}
		}
	}
</script>

<style lang="stylus">

</style>
