<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-select
					v-model="query.warehouse"
					clearable
					placeholder="所在仓库"
					@change="handleQuery"
					style="width: 150px"
				>
					<el-option
						v-for="item in warehouseOptions"
						:key="item.id"
						:label="item.name"
						:value="item.id"
					></el-option>
				</el-select>
			</div>
			<div class="right-panel">
				<el-input
					v-model="query.search"
					placeholder="物料名"
					clearable
					style="margin-right: 5px"
				></el-input>
				<el-button
					type="primary"
					icon="el-icon-search"
					@click="handleQuery"
				></el-button>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable
				ref="table"
				:apiObj="apiObj"
				row-key="id"
				stripe
				:params="params"
			>
				<el-table-column type="index" width="50" />
				<el-table-column label="物料批次" prop="batch">
				</el-table-column>
				<el-table-column label="物料名称" prop="material_name">
				</el-table-column>
				<!-- <el-table-column label="规格型号">
					<template #default="scope">
						{{ scope.row.specification }} {{ scope.row.model }}
					</template>
				</el-table-column> -->
				<el-table-column label="仓库" prop="warehouse_name">
				</el-table-column>
				<el-table-column label="物料存量" prop="count">
				</el-table-column>
				<el-table-column label="有效期" prop="expiration_date">
				</el-table-column>
				<el-table-column label="更新时间" prop="update_time">
				</el-table-column>
				<el-table-column width="90">
					<template #default="scope">
						<el-button @click="printMaterial(scope.row)" type="text">物料标签</el-button>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>
</template>
<script>
export default {
	name: "materials",
	data() {
		return {
			apiObj: this.$API.inm.warehouse.batch,
			params: { count__gte: 1, material__type__in: "30" },
			selection: [],
			query: {},
			warehouseOptions: [],
			printer_name:localStorage.getItem("printer_name")
		};
	},
	mounted() {
		this.getWarehouse();
	},
	methods: {
		getWarehouse() {
			this.$API.inm.warehouse.list.req({ page: 0 }).then((res) => {
				this.warehouseOptions = res;
			});
		},
		handleQuery() {
			this.$refs.table.queryData(this.query);
		},
		resetQuery() {
			this.query = {};
		},
		//打印物料标签
		printMaterial(row){
			let that = this;
			let code = "";
			that.$API.cm.labelmat.fromMb.req({tid:row.id}).then((res) => {
			    code = res.code_label;
				let str = [
					"SIZE 40 mm,70 mm",
					"GAP 7 mm,7 mm",
					"CLS",
					"REFERENCE 0,0",
					'QRCODE 30,400,H,5,A,0,"' +code +'"',
					"WINTEXT 200,550,28,90,0,0,Simhei," + res.material_name,
					"WINTEXT 240,550,28,90,0,0,Simhei," + res.batch,
					]; 
				if(res.notok_sign!==null){
					str.push("WINTEXT 280,550,28,90,0,0,Simhei," +res.notok_sign_name,)
				}
				str.push("PRINT 1",)
				let obj = {};
				obj.printer_commands = str;
				obj.printer_name = that.printer_name;
				this.$API.wpm.prints.req(obj).then((response) => {});
			})
			
		},
	},
};
</script>
