<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-10">
						<span class="filter_label">料箱编码</span>
					</view>
					<view class="zd-col-14">
						<search-box
						v-model="val1"
						@handleChange="handleChange"
						/>
					</view>
				</view>
				<view class="zd-row border-bottom filter_input_disabled">
					<view class="zd-col-10">
						<span class="filter_label">拣选单据</span>
					</view>
					<view class="zd-col-14">
						<input type="text" class="filter_input" v-model="data.code" disabled>
					</view>
				</view>
				<view class="zd-row border-bottom filter_input_disabled">
					<view class="zd-col-10">
						<span class="filter_label">车间</span>
					</view>
					<view class="zd-col-14">
						<input type="text" class="filter_input" v-model="data.product_area" disabled>
					</view>
				</view>
				<view class="zd-row border-bottom filter_input_disabled">
					<view class="zd-col-10">
						<span class="filter_label">拣选仓库</span>
					</view>
					<view class="zd-col-14">
						<input type="text" class="filter_input" v-model="data.stor_code" disabled>
					</view>
				</view>
				<view class="zd-row border-bottom filter_input_disabled">
					<view class="zd-col-10">
						<span class="filter_label">拣选站台</span>
					</view>
					<view class="zd-col-14">
						<input type="text" class="filter_input" v-model="data.point_code" disabled>
					</view>
				</view>
				<view class="zd-row border-bottom filter_input_disabled">
					<view class="zd-col-10">
						<span class="filter_label">拣选数量</span>
					</view>
					<view class="zd-col-14">
						<input type="number" class="filter_input" v-model="data.assign_qty" disabled>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-10">
						<span class="filter_label">出库至二楼料箱</span>
					</view>
					<view class="zd-col-14">
						<search-box
						v-model="val2"
						@handleChange="handleChange2"
						/>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-10">
						<span class="filter_label">是否机械搬运</span>
					</view>
					<view class="relative zd-col-14">
						<switch :checked="isChecked" color="#4e6ef2" style="transform:scale(0.8); transform-origin: left;"/>
						<text @tap="setWStatus" style="position: absolute;display: inline-block;width: 52px; height: 32px;left: 0;"></text>
					</view>
				</view>
				<!-- <view class="zd-row border-bottom">
					<view class="zd-col-10">
						<span class="filter_label">是否机打印</span>
					</view>
					<view class="relative zd-col-14">
						<switch :checked="isChecked1" color="#4e6ef2" style="transform:scale(0.8); transform-origin: left;"/>
						<text @tap="setWStatus1" style="position: absolute;display: inline-block;width: 52px; height: 32px;left: 0;"></text>
					</view>
				</view> -->
				<view class="zd-row border-bottom">
					<view class="zd-col-10">
						<span class="filter_label">返回至立库料箱</span>
					</view>
					<view class="zd-col-14">
						<search-box
						v-model="val3"
						@handleChange="handleChange3"
						/>
					</view>
				</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': !val1|| !val2 || !val3 || (val2 === val3)}" :disabled="disabled" @tap="toSure">拣选确认</button>
		</view>
		<view class="zd_content msg_wrapper" :class="show ? 'popshow' : 'pophide'">
			<view class="pop-line"></view>
			<view class="msg_content">
				<view class="zd-row border-bottom">
					<view class="zd-col-10 filter_label">出库二楼料箱号为：</view>
					<view class="zd-col-14 font-size-1">{{val2}}</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-10 filter_label">返回立库料箱号为：</view>
					<view class="zd-col-14 font-size-1">{{val3}}</view>
				</view>
				<view class="filter_label">是否确认？</view>
			</view>
			<view class="zd-row submit-bar">
				<button class="zd-col-11 button-default" @tap.stop="show = false;disabled = false">关闭</button>
				<button class="zd-col-11 button-primary" :disabled="disabled" @tap="toConfirm">确定</button>
			</view>
		</view>
		<view v-if="show" class="msg_mask"></view>
	</view>
</template>

<script>
	// import {getCLodop} from "@/utils/CLodopfuncs.js"
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {queryPick, savePickTask} from '@/utils/getData2.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				val1: '',
				val2: '',
				val3: '',
				isChecked: true,
				isChecked1: false,
				data: {},
				disabled: false,
				show: false
			};
		},
		onLoad (options) {
			this.title = options.title
		},
		onShow() {
			if (this.$store.getters.publicObj !== '') {
				this.data = this.$store.getters.publicObj
			}
		},
		methods: {
			handleChange (e) {
				this._queryPick(e)
			},
			handleChange2 (e) {
				if (this.val3 && e === this.val3) {
					uni.showToast({
						title: '出库和入库料箱不能相同',
						icon: 'none'
					})
					this.val3 = ''
				}
			},
			handleChange3 (e) {
				if (this.val2 && e === this.val2) {
					uni.showToast({
						title: '出库和入库料箱不能相同',
						icon: 'none'
					})
					this.val2 = ''
				}
			},
			async _queryPick (e) {
				let res = await queryPick('Picking', e)
				this.data = res
			},
			setWStatus () {
				this.isChecked = !this.isChecked
			},
			setWStatus1 () {
				this.isChecked1 = !this.isChecked1
			},
			clearUp () {
				this.data = {}
				this.val1 = ''
				this.val2 = ''
				this.val3 = ''
				this.isChecked = true
				this.isChecked1 = false
				this.disabled = false
				this.show = false
			},
			toSure () {
				if (!this.val1|| !this.val2 || !this.val3) {
					return
				}
				if (this.val2 === this.val3) {
					uni.showToast({
						title: '出库和入库料箱不能相同',
						icon: 'none'
					})
					return
				}
				this.show = true
			},
			toConfirm () {
				// if (this.isChecked1) {
				// 	this.disabled = true
				// 	let data = Object.assign({}, this.data, {lxCode: this.val2})
				// 	this.toPrint(data)
				// } else {
				// 	this._savePickTask()
				// }
				this._savePickTask()
			},
			async _savePickTask () {
				this.disabled = true
				try {
					let obj = Object.assign({}, this.data, {is_move: this.isChecked, pick_vehicle_23: this.val2, pick_vehicle_13: this.val3})
					let res = await savePickTask(obj)
					if (res.code === '200') {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
						this.clearUp()
					} else {
						this.disabled = false
					}
				} catch (e) {
					this.disabled = false
				}
			},
			toPrint (data) {
				let iparr = this.$store.getters.printUrl.split(":")
				let printUrl = iparr[1].slice(2)
				let LODOP = getCLodop();
				if (!(LODOP.webskt && LODOP.webskt.readyState === 1)) {
				    uni.showToast({
						title: '当前配置ip：' + printUrl + '网络不通，请检查',
						icon: 'none',
						duration: 5000
					})
					this.disabled = false
				    return
				}
				LODOP.SET_SHOW_MODE('HIDE_DISBUTTIN_SETUP', 1)// 隐藏那些无效按钮
				LODOP.SET_LICENSES('浙江省烟草专卖局（公司）', 'C0C4A46A3A0D1F526D426018D9F11921', '', '')
				// 更换为打印服务器ip 不需要加前缀
				LODOP.PRINT_INIT(null, printUrl);
				// 打印机序号 规则为打印服务器打印机列表倒数从0开始 -1为默认打印机 
				LODOP.SET_PRINTER_INDEX(-1);
				// 设置打印纸大小
				LODOP.SET_PRINT_PAGESIZE(1, '80mm', '50mm', '');
				// LODOP.SET_PRINT_MODE("POS_BASEON_PAPER",true);
				LODOP.ADD_PRINT_RECT('1mm', '3mm', '74mm', '44mm', 0, 1);
				LODOP.SET_PRINT_STYLE('FontSize', 10);
				LODOP.SET_PRINT_STYLE('Bold', 1);
				LODOP.ADD_PRINT_TEXT('5mm', '30mm', '32mm', '15mm', '物料信息卡');
				let tableHtml = `
								<div id="div1">
									<style>table{border:none} td{border: 1px solid #000;height:18px;font-size: 10px;text-align: center}</style>
									
									<table border=0 cellSpacing=0 cellPadding=0  width="100%" bordercolor="#000000" style="border-collapse:collapse">
										<tbody>
											<tr>
											    <td width="40%">用料单号</td>
											    <td width="60%">${data.prd_ppbom_no}</td>
											</tr>
											<tr>
											    <td width="40%">物料编码</td>
											    <td width="60%">${data.material_code}</td>
											</tr>
											<tr>
											    <td width="40%">物料名称</td>
											    <td width="60%">${data.material_name}</td>
											</tr>
											<tr>
											    <td width="40%">物料规格</td>
											    <td width="60%">${data.material_spec}</td>
											</tr>
											<tr>
											    <td width="40%">物料数量</td>
											    <td width="60%">${data.assign_qty}</td>
											</tr>
											<tr>
											    <td width="40%">料箱号</td>
											    <td width="60%">${data.lxCode}</td>
											</tr>
										</tbody>
									</table>
								</div>`
				LODOP.ADD_PRINT_TABLE("12mm",'5mm',"70mm","30mm",tableHtml);
				LODOP.PRINT();
				// LODOP.PREVIEW()
				uni.showToast({
					title: '打印成功',
					icon: 'none'
				})
				if (this.isChecked1) {
					this._savePickTask()
				}
			}
		}
	}
</script>