<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-6">
						<span class="filter_label">查询类型</span>
					</view>
					<view class="zd-col-18 filter_select">
						<uni-data-select v-model="index1" :localdata="options1" :clear="false"></uni-data-select>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-6">
						<span class="filter_label">点位</span>
					</view>
					<view class="zd-col-18">
						<search-box v-model="val1"/>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-6">
						<span class="filter_label">物料编码</span>
					</view>
					<view class="zd-col-13">
						<!-- <search-box v-model="materialData.material_code"/> -->
						<input type="text" class="filter_input" v-model="materialData.material_code">
					</view>
					<button class="mini-btn" type="primary" @tap="toJump('mater-save?title=物料维护')">查询</button>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-6">
						<span class="filter_label">批号</span>
					</view>
					<view class="zd-col-13">
						<input type="text" class="filter_input" v-model="val2">
					</view>
					<button class="mini-btn" type="primary" @tap="_queryIvt">查询</button>
				</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>
								<th>载具</th>
							</tr>
						</thead>
						<tbody>
							<tr v-for="(e, i) in dataList" :key="i">
								<td>{{i+1}}</td>
								<td>{{e.struct_code}}</td>
								<td>{{e.material_code}}</td>
								<td>{{e.material_name}}</td>
								<td>{{e.pcsn}}</td>
								<td>{{e.canuse_qty}}</td>
								<td>{{e.qty_unit_name}}</td>
								<td>{{e.class_name}}</td>
								<td>{{e.insert_time}}</td>
								<td>{{e.storagevehicle_code}}</td>
							</tr>
						</tbody>
					</table>
				</view>
			</view>
		</view>
		<view class="zd-row submit-bar" v-if="index1 === 'ZZC01'">
			<!-- <button class="zd-col-5 button-default" @tap="toEmpty">清空</button> -->
			<button class="zd-col-22 button-primary" :class="{'button-info': !dataList.length}" :disabled="disabled1" @tap="labelPrint">打印</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {queryIvt} from '@/utils/getData3.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				title: '',
				// options1: [],
				materialData: {},
				options1: [{text:'原料', value: 'WYL01'}, {text:'辅料', value: 'WFL01'}, {text:'批料室', value: 'PZC01'}, {text:'中间站', value: 'ZZC01'}, {text:'内包材', value: 'NBC01'}, {text:'外包材', value: 'WBC01'}],
				index1: 'WYL01',
				val1: '',
				val2: '',
				dataList: [],
				disabled1: false
			};
		},
		onLoad (options) {
			this.title = options.title
		},
		onShow () {
			if (this.$store.getters.publicObj !== '') {
				this.materialData = this.$store.getters.publicObj
				this.$store.dispatch('setPublicObj', '')
			}
		},
		methods: {
			toEmpty () {
				this.val1 = ''
				this.val2 = ''
				this.dataList = []
				this.materialData = {}
				this.disabled1 = false
			},
			toJump (name) {
				uni.navigateTo({
					url: `/pages/hdyy/tygn/${name}`
				})
			},
			async _queryIvt () {
				try {
					let res = await queryIvt(this.index1, this.val1, this.materialData.material_code, this.val2)
					if (res && res.data) {
						this.dataList = [...res.data]
					} else {
						this.dataList = []
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
					}
				} catch (e) {
					this.dataList = []
				}
			},
			labelPrint () {
				this.disabled1 = true
				if (!this.dataList.length) {
					this.disabled1 = false
					return
				}
				this.toPrint()
			},
			toPrint () {
				let iparr = this.$store.getters.baseUrl.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.disabled1 = false
				    return
				}
				// 1. 定义静态数据（模拟固定数据）:后续需要换实际数据
				// const tableData = [
				// 	{ struct_code: 'L01-01-01', material_code: '000301000216', material_name: '二甲双胍恩格列净片（Ⅰ）', pcsn: 'AAA', canuse_qty: '67.1', class_name: '制粒', storagevehicle_code: 'LT0004', insert_time: '2026-03-19' },
				// 	{ struct_code: 'L01-01-01', material_code: '000301000216', material_name: '二甲双胍恩格列净片（Ⅰ）中间体素片（规格：500mg/5mg/片）', pcsn: 'AAA', canuse_qty: '100.06', class_name: '制粒', storagevehicle_code: 'LT0004', insert_time: '2026-03-19 14:43:40' }
				// ]
				const tableData = this.dataList
				// 2. 拼接tbody内容：循环数据生成<tr>标签
				let tbodyHtml = ''
				tableData.forEach(item => {
					// 把每个数据项拼接到<tr>中，对应列的位置
					tbodyHtml += `
								<tr>
									<td>${item.struct_code}</td>
									<td>${item.material_code}</td>
									<td>${item.material_name}</td>
									<td>${item.pcsn}</td>
									<td>${item.canuse_qty}</td>
									<td>${item.class_name}</td>
									<td>${item.storagevehicle_code}</td>
									<td>${item.insert_time}</td>
								</tr>
							`
				})
				const tableHtml = `
							<table border="1" cellpadding="8" cellspacing="0" style="width:100%; text-align:center; font-size:14px;">
								<thead>
									<tr style="background:#f0f0f0; font-weight:bold;">
										<th>点位</th>
										<th>物料编码</th>
										<th>物料名称</th>
										<th>批号</th>
										<th>数量</th>
										<th>类别</th>
										<th>载具</th>
										<th>入库时间</th>
									</tr>
								</thead>
								<tbody>
								${tbodyHtml} <!-- 嵌入动态生成的行 -->
								</tbody>
							</table>
						`
				// const LODOP = getLodop()
				LODOP.SET_SHOW_MODE('HIDE_DISBUTTIN_SETUP', 1)// 隐藏那些无效按钮
				// 纸张大小：A4纸
				LODOP.SET_PRINT_PAGESIZE(1, '210mm', '297mm', '')
				// 抬头
				LODOP.ADD_PRINT_TEXT('3.15mm', '49.74mm', '112.95mm', '10.58mm', '中 间 站 （2） 桶 库 存 信 息')
				LODOP.SET_PRINT_STYLEA(0, 'FontSize', 20)
				// 表格渲染
				LODOP.ADD_PRINT_TABLE('15mm', '5mm', '95%', '95%', tableHtml)

				// LODOP.PRINT()// 打印
				LODOP.PREVIEW()// 预览
				// LODOP.PRINT_DESIGN()
				uni.showToast({
					title: '操作成功',
					icon: 'none'
				})
				this.toEmpty()
				this.disabled1 = false
			}
		}
	}
</script>

<style scoped>
.filter_picker {
	text-align: center
}
</style>