<template>
	<view class="zd_container">
		<nav-bar title="人工包装"></nav-bar>
		<view class="zd_content">
			<view class="zd_wrapper">
				<view class="filter_item">
					<view class="filter_label_wraper">
						<span class="filter_label">木托盘编码</span>
					</view>
					<view class="filter_input_wraper">
						<search-box v-model="val1" />
					</view>
				</view>
				<view class="filter_item">
					<view class="filter_label">砖块数量</view>
					<view class="filter_input_wraper">
						<input type="number" class="filter_input" v-model="val2">
					</view>
				</view>
				<view class="filter_item">
					<view class="filter_label">是否满垛</view>
					<view class="relative filter_input_wraper">
						<switch :checked="isChecked" color="#6798ef" style="transform:scale(0.8)"/>
						<text @tap="setWStatus" style="position: absolute;display: inline-block;width: 52px; height: 32px;left: 0;"></text>
					</view>
				</view>
			</view>
			<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>
							</tr>
						</thead>
						<tbody>
							<tr v-for="(e, i) in dataList" :key="i" @tap="toCheck(e)" :class="{'checked': e.workorder_id === pkId}">
								<td>{{e.order_no}}</td>
								<td>{{filter(e.custer_no)}}</td>
								<td>{{e.workorder_code}}</td>
								<td>{{e.real_qty}}</td>
								<td>{{e.material_code}}</td>
								<td>{{e.half_material_code}}</td>
								<td>{{e.raw_material_code}}</td>
								<td>{{e.material_name}}</td>
								<td>{{e.material_spec}}</td>
							</tr>
						</tbody>
					</table>
				</view>
			</view>
		</view>
		<view class="zd-row submit-bar">
			<button class="zd-col-5 submit-button" @tap="clearUp">清空</button>
			<button class="zd-col-8 submit-button" :class="{'btn-disabled': !val1 || !val2 || !pkId}" :disabled="disabled" @tap="_manualPackageBinding">绑定</button>
			<button class="zd-col-8 submit-button" :class="{'btn-disabled': !pkId}" :disabled="disabled" @tap="_manualPackageOrderOk">完工</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {manualPackageOrder, pdmBdWorkorderGetCuster, manualPackageBinding, manualPackageOrderOk} from '@/utils/getData2.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				val1: '',
				val2: '',
				isChecked: false,
				custer: [],
				dataList: [],
				pkId: '',
				pkObj: {},
				disabled: false
			};
		},
		created () {
			this._pdmBdWorkorderGetCuster()
		},
		methods: {
			filter (e) {
				let t = ''
				this.custer.forEach(el => {
					if (el.custer_NO === e) {
						t = el.custer_NAME
					}
				})
				return t
			},
			async _pdmBdWorkorderGetCuster () {
				let res = await pdmBdWorkorderGetCuster()
				this.custer = [...res]
				this._manualPackageOrder()
			},
			/** grid查询 */
			async _manualPackageOrder () {
				let res = await manualPackageOrder()
				this.dataList = [...res]
			},
			clearUp () {
				this.val1 = ''
				this.val2 = ''
				this.isChecked = false
				this.pkId = ''
				this.pkObj = {}
				this.disabled = false
			},
			/** 绑定 */
			async _manualPackageBinding () {
				this.disabled = true
				if (!this.val1 || !this.val2 || !this.pkId) {
					this.disabled = false
					return
				}
				try {
					console.log(this.isChecked)
					let checked = this.isChecked ? '1' : '0'
					let res = await manualPackageBinding(this.val1, this.val2, checked, this.pkId)
					this.clearUp()
					this._manualPackageOrder()
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				} catch (e) {
					this.disabled = false
				}
			},
			/** 完工 */
			async _manualPackageOrderOk () {
				this.disabled = true
				if (!this.pkId) {
					this.disabled = false
					return
				}
				try {
					let res = await manualPackageOrderOk(this.pkId)
					this.clearUp()
					this._manualPackageOrder()
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				} catch (e) {
					this.disabled = false
				}
			},
			setWStatus () {
				this.isChecked = !this.isChecked
			},
			toCheck (e) {
				this.pkId = this.pkId === e.workorder_id ? '' : e.workorder_id
				this.pkObj = this.pkId === e.workorder_id ? e : {}
			}
		}
	}
</script>

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