<template>
	<view class="zd_container">
		<!-- 装配线点到点 -->
		<nav-bar :title="title"></nav-bar>
		<view class="zd_content">
			<view class="zd_wrapper xzbox">
				<view class="ozxbox"><span>起点：</span><span>{{start.device_name}}</span></view>
			</view>
			<view class="zd_wrapper ocon">
				<view class="obox" v-for="(e, i) in arrList" :key="i" :class="{'checked': e.device_code === pkId1}" @tap="toCheck1(e)">
					{{ e.device_name }}
				</view>
			</view>
		</view>
		<view class="zd_content">
			<view class="zd_wrapper xzbox">
				<view class="ozxbox"><span>终点：</span><span>{{end.device_name}}</span></view>
			</view>
			<view class="zd_wrapper ocon">
				<view class="obox" v-for="(e, i) in arrList" :key="i" :class="{'checked': e.device_code === pkId2}" @tap="toCheck2(e)">
					{{ e.device_name }}
				</view>
			</view>
		</view>
		<view class="zd-row submit-bar">
			<button class="zd-col-6 button-default" @tap="clearUp">清空</button>
			<button class="zd-col-16 button-primary" :class="{'button-info': !pkId1 || !pkId2}" :disabled="disabled" @tap="_createTask1">确认</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import { createTask3} from '@/utils/getData01.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				title: '',
				pkId: '',
				pkId1: '',
				pkId2: '',
				start: '',
				end: '',
				arrList: [
					{
						device_code:'ZPXA',
						device_name: '装配线A'
					},
					{
						device_code:'ZPXB',
						device_name: '装配线B'
					},
					{
						device_code:'ZPXC',
						device_name: '装配线C'
					},
					{
						device_code:'ZPXD',
						device_name: '装配线D'
					},
					{
						device_code:'ZPXE',
						device_name: '装配线E'
					},
					{
						device_code:'ZPXF',
						device_name: '装配线F'
					}
				],
				disabled: false
			};
		},
		onLoad (options) {
			this.title = options.title
		},
		methods: {
			toCheck1 (e) {
				if (e.device_code === this.end.device_code) {
					this.pkId1 = ''
					this.start = ''
					return
				}
				this.pkId1 = e.device_code
				this.start = e
			},
			toCheck2 (e) {
				if (e.device_code === this.start.device_code) {
					this.pkId2 = ''
					this.end = ''
					return
				}
				this.pkId2 = e.device_code
				this.end = e
			},
			clearUp () {
				this.start = ''
				this.end = ''
				this.pkId1 = ''
				this.pkId2 = ''
			},
			async _createTask1 () {
				this.disabled = true
				if (!this.pkId1 || !this.pkId2) {
					this.disabled = false
					return
				}
				try {
					let res = await createTask3(this.start.device_code, this.end.device_code)
					if (res.code === '200') {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
						this.clearUp()
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
						this.disabled = false
					}
				} catch (e) {
					this.disabled = false
				}
			}
		}
	}
</script>

<style lang="stylus">
.ocon
	display flex
	flex-wrap wrap
.obox
	width 140px
	height 60px
	line-height 60px
	text-align center
	background #ff0
	// border 1px solid #666
	border-radius 3px
	background #eee
	margin-right 5px
	margin-bottom 5px
.checked
	background #fffbe5
.zd_content
	padding 12px 8px 8px 8px
</style>
