<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="类型" min-width="100"  show-overflow-tooltip>
					<template #default="scope">
						<el-tag :type="getType(scope.row.state)">{{ state_[scope.row.state] }}</el-tag>
						<el-tag type="info" v-if="scope.row.defect_name">{{ scope.row.defect_name }}</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="仓库" width="80" prop="warehouse_name">
				</el-table-column>
				<el-table-column label="物料存量">
					<template #default="scope">
						<el-link @click="handleWpr(scope.row)" type="primary">{{ scope.row.count }}</el-link>
					</template>
				</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,'mb')" type="text">物料标签</el-button>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
		<print-dialog
			v-if="print_m"
			ref="printmaterial"
			:mId="wmId"
			:mtype="wmtype"
			:apiObj="apiObjPrint"
		></print-dialog>
		<el-drawer v-model="wprVisibleDrawer" :size="'70%'">
			<template #header>
				<h4>关联产品</h4>
			</template>
			<template #default>
				<scTable ref="wprTable" hidePagination hideDo :data="wprList">
					<el-table-column type="index" width="50"></el-table-column>
					<el-table-column label="产品编号" prop="number" min-width="100px"></el-table-column>
					<el-table-column label="缺陷项"  min-width="200px">
						<template #default="scope">
							<span v-for="item in scope.row.wprdefect" :key="item.id">
								<el-tag type="warning">{{ item.defect_name }}</el-tag>
							</span>
						</template>
					</el-table-column>
					<el-table-column
						label="操作"
						fixed="right"
						align="center"
						width="120"
					>
						<template #default="scope">
							<el-link :underline="false"  @click="printMaterial(scope.row,'wpr')" type="primary">打印标签</el-link>
						</template>
					</el-table-column>
				</scTable>
			</template>
			<template #footer>
			<div style="flex: auto">
				<el-button @click="wprVisibleDrawer=false">关闭</el-button>
			</div>
			</template>
		</el-drawer>
	</el-container>
</template>
<script>
import printDialog from "./../template/printmaterial.vue";
export default {
	name: "materials",
	components: {
		printDialog
	},
	data() {
		return {
			apiObj: this.$API.inm.warehouse.batch,
			params: { count__gte: 1, material__type__in: "30" },
			selection: [],
			wprList:[],
			query: {},
			warehouseOptions: [],
			wmtype:0,
			print_m:false,
			wprVisibleDrawer:false,
			materialsVisible:false,
			apiObjPrint:this.$API.cm.labelmat.fromMb,
			wmId:'',
			state_:{
				10:'合格',
				20:'不合格',
				30:'返工',
			},
			wprParams:{
				page:0
			},
			project_code:''
		};
	},
	mounted() {
		this.getWarehouse();
		this.project_code = this.$TOOL.data.get("BASE_INFO").base.base_code;
	},
	methods: {
		getType(type){
			if(type==10){
				return 'success'
			}else if(type==20){
				return 'danger'
			}else if(type==30){
				return 'warning'
			}
		},
		getWarehouse() {
			this.$API.inm.warehouse.list.req({ page: 0 }).then((res) => {
				this.warehouseOptions = res;
			});
		},
		handleQuery() {
			this.$refs.table.queryData(this.query);
		},
		resetQuery() {
			this.query = {};
		},
		handleWpr(row){
			let that = this;
			that.wprParams.mb = row.id;
			that.wprVisibleDrawer = true;
			that.$API.wpm.wpr.list.req(that.wprParams).then((res) => {
			    that.wprList = res;
			})
		},
		//打印物料标签
		printMaterial(row,type){
			let that = this;
			let printer_name=localStorage.getItem("printer_name");
			if(printer_name!==''&&printer_name!==null&&printer_name!==undefined){
				if(that.project_code!=='gz'){
					if(type=='mb'){
						let params = {};
						params.tid = row.id;
						params.label_template_name = '库存标签模板';
						params.extra_data={count:row.count};
						that.$API.cm.labelmat.fromMb.req(params).then((res) => {
							let obj = {};
							obj.printer_commands = res.commands;
							obj.printer_name = printer_name;
							that.$API.wpm.prints.req(obj).then((response) => {
								that.$message.success("打印成功");
							});
						})
					}else{
						let params = {};
						let name = row.material_name.split('|')[0];
						params.label_template_name = '单件打印模板';
						params.data = {number:row.number,name:name};
						that.$API.cm.labeltemplate.commands.req(params).then((res) => {
							let obj = {};
							obj.printer_commands = res.commands;
							obj.printer_name = printer_name;
							that.$API.wpm.prints.req(obj).then((response) => {
								that.$message.success("打印成功");
							});
						});
					}
				}else{
					that.wmId = row.id;
					that.wmtype = row.material_.type;
					that.print_m = true;
					this.$nextTick(() => {
						this.$refs.printmaterial.open();
					})
				}
			}else{
				that.$message.error("请先设置打印机");
			}
		},
	},
};
</script>
