<template>
	<view class="zd_container">
		<nav-bar title="组盘入库"></nav-bar>
		<view class="zd_content">
			<view class="zd_wrapper">
				<view class="zd-row border-bottom">
					<view class="zd-col-5">
						<span class="filter_label">载具类型</span>
					</view>
					<view class="zd-col-17 filter_picker">
						<picker @change="pickerChange" :value="index1" :range="options1" range-key="text">
							<view class="uni-input">{{index1 !== '' ? options1[index1].text : ''}}</view>
						</picker>
					</view>
					<view class="uni-icons icon_right">&#xe6b5;</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-5">
						<span class="filter_label">载具编码</span>
					</view>
					<view class="zd-col-19">
						<search-box
						v-model="val1"
						/>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-5">
						<span class="filter_label">物料</span>
					</view>
					<view class="zd-col-19 filter_select">
						<zxz-uni-data-select v-model="index2" filterable :localdata="options2"></zxz-uni-data-select>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-5">
						<span class="filter_label">物料数量</span>
					</view>
					<view class="zd-col-19">
						<input type="number" class="filter_input" v-model="val2">
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-5">
						<span class="filter_label">当前点位</span>
					</view>
					<view class="zd-col-19">
						<search-box
						v-model="val3"
						/>
					</view>
				</view>
				<view class="zd-row">
					<view class="zd-col-5">
						<span class="filter_label">是否配盘</span>
					</view>
					<view class="zd-col-19 zd-row jcflexend">
						<switch :checked="isSwitchOn" color="#4e6ef2" style="transform:scale(0.7)" @change="switch1Change"/>
					</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>
							</tr>
						</thead>
						<tbody>
							<tr v-for="(e, i) in dataList" :key="i" @click="toChose(e)" :class="{'checked': e.vehicle_code === pkId}">
								<td class="fontcol1">{{e.vehicle_code}}</td>
								<td><view class="fontbg">{{e.vehicle_type}}</view></td>
								<td class="fontcol2">{{e.material_code}}</td>
								<td class="fontcol2">{{e.material_name}}</td>
								<td>{{e.point_code}}</td>
								<td>{{e.material_qty}}</td>
								<td>{{e.pcsn}}</td>
							</tr>
						</tbody>
					</table>
				</view>
			</view>
		</view>
		<view class="zd-row submit-bar">
			<button class="zd-col-4 button-default" @tap="clearUp">清空</button>
			<button class="zd-col-4 button-primary" :class="{'button-info': index1 === '' || !val1 || !val3 || !index2 || !val2}" :disabled="disabled1" @tap="_groupManual">组盘</button>
			<button class="zd-col-4 button-primary" @tap="_queryVehicleGroup">配盘</button>
			<button class="zd-col-4 button-primary" :class="{'button-info': !pkId}" :disabled="disabled3" @tap="_pdaPrintf">打印</button>
			<button class="zd-col-4 button-primary" :class="{'button-info': !pkId}" :disabled="disabled4" @tap="_fullVehicleIn">入库</button>
		</view>
		<view class="zd_content msg_wrapper" :class="show ? 'popshow' : 'pophide'">
			<view class="msg_content">
				<view class="slide_new">
					<table>
						<thead>
							<tr>
								<th>载具编码</th>
								<th>载具类型</th>
								<th>物料编码</th>
								<th>物料名称</th>
								<th>点位</th>
								<th>物料数量</th>
							</tr>
						</thead>
						<tbody>
							<tr v-for="e in dataList1" :key="e.group_id" @click="toCheck(e)" :class="{'checked': e.checked}">
								<td class="fontcol1">{{e.vehicle_code}}</td>
								<td><view class="fontbg">{{e.vehicle_type}}</view></td>
								<td class="fontcol2">{{e.material_code}}</td>
								<td class="fontcol2">{{e.material_name}}</td>
								<td>{{e.point_code}}</td>
								<td>{{e.material_qty}}</td>
							</tr>
						</tbody>
					</table>
				</view>
			</view>
			<view class="zd-row submit-bar">
				<button class="zd-col-9 button-default" @tap.stop="show = false">取消</button>
				<button class="zd-col-13 button-primary" :class="{'button-info': checkArr.length === 0}" :disabled="disabled2" @tap="_groupLink">确认</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 {getVehicleType, queryMaterial, queryExistGroup, groupManual, queryVehicleGroup, groupLink, pdaPrintf, fullVehicleIn} from '@/utils/getData2.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				options1: [],
				index1: '',
				options2: [],
				index2: '',
				val1: '',
				val2: '',
				val3: '',
				isSwitchOn: false,
				dataList: [],
				pkId: '',
				pkObj: {},
				disabled1: false,
				show: false,
				dataList1: [],
				checkArr: [],
				disabled2: false,
				disabled3: false,
				disabled4: false
			};
		},
		created () {
			this._getVehicleType()
			this._queryMaterial()
			this._queryExistGroup()
		},
		methods: {
			/** 载具类型下拉框 */
			async _getVehicleType () {
				let res = await getVehicleType()
				this.options1 = [...res]
			},
			pickerChange (e) {
				this.index1 = e.detail.value
			},
			/** 物料下拉框*/
			async _queryMaterial (e) {
				let res = await queryMaterial(e)
				res.map(el => {
					this.$set(el, 'value', el.material_id)
					this.$set(el, 'text', el.material_name)
				})
				this.options2 = [...res]
			},
			/** 是否配盘 */
			switch1Change: function (e) {
				this.isSwitchOn = e.detail.value
			},
			/** 查询已配盘的组盘信息 */
			async _queryExistGroup () {
				let res = await queryExistGroup()
				this.dataList = [...res]
			},
			/** 组盘 */
			async _groupManual () {
				this.disabled1 = true
				if (this.index1 === '' || !this.val1 || !this.val3 || !this.index2 || !this.val2) {
					this.disabled1 = false
					return
				}
				try {
					let code = ''
					this.options2.map(el => {
						if (el.value === this.index2) {
							code = el.material_code
						}
					})
					let res = await groupManual(this.options1[this.index1].value, this.val1, this.val3, this.index2, code, this.val2, this.isSwitchOn)
					this.clearUp()
					this._queryExistGroup()
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				} catch (e) {
					this.disabled1 = false
				}
			},
			clearUp () {
				this.index1 = ''
				this.val1 = ''
				this.val3 = ''
				this.index2 = ''
				this.val2 = ''
				this.isSwitchOn = false
				this.disabled1 = false
			},
			/** 获取待配盘信息 */
			async _queryVehicleGroup () {
				this.dataList1 = []
				this.checkArr = []
				this.disabled2 = false
				let res = await queryVehicleGroup()
				res.map(el => {
					this.$set(el, 'checked', false)
				})
				this.dataList1 = [...res]
				this.show = true
			},
			toCheck (e) {
				e.checked = !e.checked
				this.checkArr = this.dataList1.filter(i => { return i.checked === true })
			},
			/** 配盘确定 */
			async _groupLink () {
				this.disabled2 = true
				if (this.checkArr.length === 0) {
					this.disabled2 = false
					return
				}
				try {
					let res = await groupLink(this.checkArr)
					this.disabled2 = false
					this.show = false
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				} catch (e) {
					this.disabled2 = false
				}
			},
			toChose (e) {
				this.pkId = this.pkId === e.point_code ? '' : e.point_code
				this.pkObj = this.pkId === e.point_code ? e : {}
			},
			/**  打印 */
			async _pdaPrintf () {
				this.disabled3 = true
				if (!this.pkId) {
					this.disabled3 = false
					return
				}
				try {
					let res = await pdaPrintf(this.pkObj.vehicle_code)
					this.disabled3 = false
					this.pkId = ''
					if (JSON.stringify(res) !== '{}') {
						setTimeout(this.toPrint(res), 800)
					}
				} catch (e) {
					this.disabled3 = 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
					})
				    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', '60mm', '');
				LODOP.ADD_PRINT_RECT('1mm', '3mm', '74mm', '54mm', 0, 1);
				LODOP.SET_PRINT_STYLE('FontSize', 12);
				LODOP.SET_PRINT_STYLE('Bold', 1);
				LODOP.ADD_PRINT_BARCODE('2mm', '4mm', '32mm', '32mm', 'QRCode', data.vehicle_code + '##' + data.material_code + '##' + data.material_qty + '##' + data.pcsn + '##' + data.print_time);
				LODOP.ADD_PRINT_TEXT('5mm', '35mm', '50mm', '15mm', '载具编码：' + data.vehicle_code);
				LODOP.ADD_PRINT_TEXT('15mm', '35mm', '50mm', '15mm', '物料编码：' + data.material_code);
				LODOP.ADD_PRINT_TEXT('25mm', '35mm', '50mm', '15mm', '物料名称：' + data.material_name);
				LODOP.ADD_PRINT_TEXT('34mm', '5mm', '80mm', '15mm', '物料数量：' + data.material_qty);
				LODOP.ADD_PRINT_TEXT('41mm', '5mm', '80mm', '15mm', '配盘批次：' + data.pcsn);
				LODOP.ADD_PRINT_TEXT('48mm', '5mm', '80mm', '15mm', '打印时间：' + data.print_time);
				LODOP.PRINT();  // 打印
				// LODOP.PREVIEW()
				uni.showToast({
					title: '打印成功',
					icon: 'none'
				})
			},
			/**  入库 */
			async _fullVehicleIn () {
				this.disabled4 = true
				if (!this.pkId) {
					this.disabled4 = false
					return
				}
				try {
					let res = await fullVehicleIn(this.pkId)
					this.disabled4 = false
					this.pkId = ''
					this._queryExistGroup()
				} catch (e) {
					this.disabled4 = false
				}
			}
		}
	}
</script>

<style lang="stylus">
@import '../../common/style/mixin.styl';
</style>
