<template>
	<view class="zd_container">
		<nav-bar :title="title"></nav-bar>
		<view class="zd_content">
			<view class="zd_wrapper grid-wraper">
				<view class="slide_new">
					<table>
						<thead>
							<tr>
								<th>选择</th>
								<th>任务编码</th>
								<th>状态</th>
								<th>工单编码</th>
								<th>物料编码</th>
								<th>物料名称</th>
								<th>物料重量</th>
								<th>配料桶数</th>
								<th>剩余桶数</th>
								<th>终点编码</th>
							</tr>
						</thead>
						<tbody>
							<tr v-for="(e, i) in dataList" :key="i">
								<td @tap="toCheck(e)">
									<view class="checkbox">
										<uni-icons type="checkmarkempty" size="20" :color="e.work_code === pkId ? '#4e6ef2' : '#fff'"></uni-icons>
									</view>
								</td>
								<td>{{e.work_code}}</td>
								<td>{{ e.status }}</td>
								<td>{{ e.mfg_order_name }}</td>
								<td>{{ e.material_code }}</td>
								<td>{{ e.material_name }}</td>
								<td>{{ e.qty }}</td>
								<td>{{ e.require_num }}</td>
								<td>{{ e.remain_num }}</td>
								<td @tap="updateCode(e)">{{ e.next_device_code }}</td>	
							</tr>
						</tbody>
					</table>
				</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" :disabled="disabled" :class="{'button-info': !pkId}" @tap="_handConfirm">确认</button>
		</view>
		<view class="msg_wrapper" :class="show ? 'popshow' : 'pophide'">
			<view class="msg_tip">工单编码{{pkObj.mfg_order_name}}</view>
			<view class="msg_content">
				<view class="zd-row">
					<view class="zd-col-6 filter_label">选择终点</view>
					<view class="zd-col-18 filter_input_wraper">
						<uni-data-select v-model="index1" :localdata="options1" @change="selectChange1"></uni-data-select>
					</view>
				</view>
			</view>
			<view class="msg_btns">
				<button class="zd-col-24 msg_btn" :disabled="disabled" @tap="_confirmZZDevice">确定</button>
				<button class="zd-col-24 msg_btn msg_btn_cancle" @tap.stop="show = false">返回</button>
			</view>
		</view>
		<view v-if="show" class="msg_mask"></view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	// import {queryWorks, queryZDDevice, confirmZZDevice} from '@/utils/mork2.js'
	import {queryWorks, queryZDDevice, confirmZZDevice, handConfirm} from '@/utils/getData2.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				title: '',
				options1: [],
				index1: '',
				disabled: false,
				dataList: [],
				pkId: '',
				pkObj: {},
				show: false
			};
		},
		onLoad (options) {
			this.title = options.title
			this._queryWorks()
		},
		methods: {
			async _queryZDDevice (code) {
				let res = await queryZDDevice(code)
				this.options1 = [...res]
				this.options1.map(el => {
					this.$set(el, 'text', el.device_name)
					this.$set(el, 'value', el.device_code)
				})
			},
			selectChange1 (e) {
				this.index1 = e
			},
			async _queryWorks () {
				let res = await queryWorks()
				this.dataList = [...res]
			},
			clearUp () {
				this.dataList = []
				this.disabled = false
				this.pkId = ''
			},
			toCheck (e) {
				this.pkId = this.pkId === e.work_code ? '' : e.work_code
			},
			updateCode (e) {
				this.options1 = []
				this._queryZDDevice(e.mfg_order_name)
				this.show = true
				this.pkObj = e
				this.index1 = ''
			},
			async _confirmZZDevice () {
				this.disabled = true
				try {
					let res = await confirmZZDevice(this.pkObj.mfg_order_name, this.pkObj.work_code, this.index1)
					this.show = false
					this.disabled = false
					this._queryWorks()
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				} catch (e) {
					this.disabled = false
				}
			},
			async _handConfirm () {
				this.disabled = true
				if (!this.pkId) {
					this.disabled = false
					return
				}
				try {
					let res = await handConfirm(this.pkId)
					this.clearUp()
					this._queryWorks()
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				} catch (e) {
					this.disabled = false
				}
			}
		}
	}
</script>

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