<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" @click="tomio('do_out')" v-auth="'mio.do'"
				 v-if="mgroupName=='清洗'"
					>领料</el-button
				>
				<!-- <el-button type="primary" @click="tomio('do_in')" v-auth="'mio.do'"
					>入库</el-button
				> -->
				<el-button type="primary" v-auth="'handover.create'" @click="table_add(40)">
					报废</el-button>
			</div>
			<div class="right-panel">
				<el-select
					v-model="query.state"
					placeholder="物料状态"
					clearable
					@change="searchStateChange"
					style="width: 250px"
				>
					<el-option
						v-for="item in stateOptions"
						:key="item.value"
						:label="item.name"
						:value="item.value"
					/>
				</el-select>
				<el-input
					style="margin-right: 5px"
					v-model="query.search"
					placeholder="名称"
					clearable
				></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"
				:params="params"
				:query="query"
			>
			<el-table-column label="状态" prop="state" width="100" >
				<template #default="scope">
					<el-tag :type="wmState[scope.row.state]?.type">
						{{wmState[scope.row.state]?.text}}
						</el-tag>
					</template>
				</el-table-column>
				<el-table-column
					label="物料名称"
					prop="material_name"
					min-width="150"
				>
					<template #default="scope">
						{{ scope.row.material_name }}
						<span v-if="scope.row.material_origin != null"
							>（{{ scope.row.material_origin_name }}）</span
						>
					</template>
				</el-table-column>
				<el-table-column
					label="批次号"
					prop="batch"
					min-width="120"
				></el-table-column>
				<el-table-column
					label="部门/工段"
					prop="belong_dept_name"
				>
				<template #default="scope">
					{{scope.row.belong_dept_name}}/{{scope.row.mgroup_name}}
				</template>
				</el-table-column>
				<el-table-column
					label="数量"
					prop="count"
					min-width="80"
				>
				<template #default="scope">
					<span v-if="scope.row.material_&&scope.row.material_.tracking==10">{{scope.row.count}}</span>
					<el-link v-else :underline="false" type="primary" @click="showWpr(scope.row)">{{scope.row.count}}</el-link>
				</template>
				</el-table-column>
				<el-table-column
					label="生产中"
					prop="count_working"
					min-width="80"
				></el-table-column>
				<el-table-column
					label="不合格标记"
					prop="notok_sign_name"
				></el-table-column>
				<el-table-column
					label="创建时间"
					prop="create_time"
					width="150"
				></el-table-column>
				<el-table-column
					label="操作"
					fixed="right"
					align="center"
					width="120"
				>
					<template #default="scope">
						<el-link :underline="false"  @click="printMaterial(scope.row,'wm')" type="primary">物料标签</el-link>
						<el-link :underline="false" v-if="scope.row.material_&&scope.row.material_.tracking==10&&scope.row.state!==20"  @click="tableCheckList(scope.row)" type="primary">检验记录</el-link>
						<el-link 
							v-if="scope.row.material_&&scope.row.material_.tracking==10&&scope.row.state!==20"
							:underline="false"  
							@click="inmCheck(scope.row)" 
							type="primary" 
							style="margin-left: 10px;"
						>检验</el-link>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
		<el-dialog title="选择物料" v-model="materialsVisible" width="90%">
			<materials
				style="height: 500px"
				:materialType="materialType"
				ref="materialsChose"
				@choseChange="choseChange"
			></materials>
		</el-dialog>
		<check-dialog
			v-if="dialog.check"
			ref="checkDialog"
			:itemObj = "checkItem"
			@success="handleSaveSuccess"
			@closed="dialog.check = false"
		>
		</check-dialog>
		<scrap-dialog
			v-if="dialog.scrap"
			ref="scrapDialog"
			:type="type"
			:mgroupName="mgroup_name"
			:mgroupId="mgroupId"
			@success="handleScrapSuccess"
			@closed="dialog.scrap = false"
		>
		</scrap-dialog>
		<showDrawer
			ref="showDrawer"
			v-if="visibleDrawer"
			:wm="wm"
			:material="wmMaterial"
			@closed="visibleDrawer = false"
		>
		</showDrawer>
		<save-dialog
			v-if="dialog.inmRecord"
			ref="inmRecordDialog"
			:cate = "cate_type"
			:process = "process"
			:mgroupId = "params.mgroupx"
			:deptId = " params.belong_dept"
			@success="handleinmSuccess"
		>
		</save-dialog>
		<el-drawer v-model="wprVisibleDrawer" :size="'50%'">
			<template #header>
				<h4>关联产品</h4>
			</template>
			<template #default>
				<scTable ref="wprTable" hidePagination :data="wprList">
					<el-table-column label="产品编号" prop="number"></el-table-column>
					<el-table-column label="记录数据" prop="oinfo">
						<template #default="scope">
							<div v-for="(item,index) in scope.row.oinfo" :key="item.id">
								{{ item.name }}:{{ item.val}}
								<span v-if="index<(scope.row.oinfo.length-1)">,</span>
							</div>
						</template>
					</el-table-column>
					<el-table-column label="缺陷项">
						<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-dialog title="打印机设置" v-model="setNameVisible" width="600px">
			<el-form label-width="100px">
				<el-form-item label="打印机名称">
					<el-input v-model="printer_name"></el-input>
				</el-form-item>
			</el-form>
			<el-footer>
				<el-button type="primary" @click="savePrinter">保存</el-button>
			</el-footer>
		</el-dialog>
	</el-container>
</template>
<script>
import { wmState } from "@/utils/enum.js";
import materials from "./../mtm/materials.vue";
import checkDialog from "./check_form.vue";
import showDrawer from "./check_drawer.vue";
import scrapDialog from "./handover_form.vue";
import saveDialog from "./inm_record.vue";
export default {
	props: {
		mgroupName: {
			type: String,
			default: "",
		},
	},
	components: {
		saveDialog,
		materials,
		checkDialog,
		showDrawer,
		scrapDialog,
	},
	name: "wmaterial",
	data() {
		return {
			wmState,
			apiObj: null,
			params: {
				mgroupx: "",
				belong_dept:''
			},
			wmtype:0,
			query: {
				search: "",
				state: null,
			},
			dialog: {
				save: false,
				check: false,
				scrap: false,
				permission: false,
				inmRecord:false,
			},
			wprList:[],
			tableData: [],
			selection: [],
			stateOptions:[
				{ value: 10, name: "合格"},
				{ value: 20, name: "不合格"},
				{ value: 30, name: "返修"},
				// { value: 40, name: "检验"},
				// { value: 50, name: "报废"},
			],
			queryWm: {
				search: "",
				material: "",
			},
			wprParams:{ 
				page:0,
				wm: "" 
			},
			wmMaterial:'',
			cate_type:'',
			process:'',
			materialType: "wm",
			materialsVisible:false,
			visibleDrawer: false,
			wprVisibleDrawer:false,
			setNameVisible:false,
			wprApiObj:this.$API.wpm.wpr.list,
			apiObjPrint:this.$API.cm.labelmat.fromWm,
			printer_name:localStorage.getItem("printer_name"),
			mgroup_name:'',
			checkItem:{},
		};
	},
	mounted() {
		this.getMgroupInfo();
	},
	methods: {
		printSetting(){
			this.setNameVisible = true;
		},
		savePrinter() {
			let that = this;
			localStorage.setItem("printer_name", that.printer_name);
			that.setNameVisible = false;
			that.$message.success("打印机设置成功，请重新进行打印操作。");
		},
		getMgroupInfo(){
			let that = this;
			console.log('mgroupName',that.mgroupName);
			that.$API.mtm.mgroup.list
				.req({ page: 0, name: that.mgroupName })
				.then((res) => {
					if (res.length < 1) {
						that.$message.error("获取工段错误");
						return;
					}
					that.mgroupId = res[0].id;
					that.$TOOL.data.set('bx_deptID',res[0].belong_dept);
					that.process = res[0].process;
					that.params.mgroupx = res[0].id;
					that.params.belong_dept = res[0].belong_dept;
					that.apiObj = that.$API.wpm.wmaterial.list;
					that.$refs.table.refresh();
				});
		},
		add() {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open("add");
			});
		},
		materialsChoses(str) {
			this.materialType = str;
			this.materialsVisible = true;
		},
		choseChange(data) {
			this.queryWm.material = data;
			this.$refs.table_wm.queryData(this.queryWm);
			this.materialsVisible = false;
		},
		tomio(type) {
			let that = this;
			that.cate_type=type;
			that.dialog.inmRecord = true;
			that.$nextTick(() => {
				that.$refs.inmRecordDialog.open("add");
			});
		},
		//获取当前批次车间物料的检验记录
		tableCheckList(row){
			this.wm = row.id;
			this.wmMaterial = row.material;
			this.visibleDrawer = true;
			this.$nextTick(() => {
				this.$refs.showDrawer.open();
			});
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//添加报废
		table_add(type) {
			this.dialog.scrap = true;
			this.type = type;
			this.$nextTick(() => {
				this.$refs.scrapDialog.open("add");
			});
		},
		handleScrapSuccess(){
			this.dialog.scrap = false;
			this.$refs.table.refresh();
		},
		//出入库后刷新页面
		handleinmSuccess(){
			this.$refs.table.refresh();
		},
		//搜索
		handleQuery() {
			this.$refs.table.queryData(this.query);
		},
		showWpr(row){
			let that = this;
			that.wprParams.wm = row.id;
			that.wprVisibleDrawer = true;
			this.$API.wpm.wpr.list.req(that.wprParams).then((res) => {
			    that.wprList = res;
			})
		},
		//本地更新数据
		handleSaveSuccess(data, mode) {
			this.dialog.save = false;
			this.$refs.table.refresh();
		},
		//打印物料标签
		printMaterial(row,type){
			let that = this;
			if(that.printer_name!==''&&that.printer_name!==null&&that.printer_name!==undefined){
				if(type=='wm'){
					that.apiObjPrint.req({tid:row.id}).then((res) => {
						let 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,
							"PRINT 1",
						]; 
						let obj = {};
						obj.printer_commands = str;
						obj.printer_name = that.printer_name;
						that.$API.wpm.prints.req(obj).then((response) => {
							that.$message.success("打印成功");
						});
					})
				}else{
					// let code = 'wpr#'+row.id;
					let code = row.number;
						let str = [
							"SIZE 70 mm,100 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," + row.material_name,
							"WINTEXT 240,550,28,90,0,0,Simhei," + row.number,
							"PRINT 1",
						]; 
						let obj = {};
						obj.printer_commands = str;
						obj.printer_name = that.printer_name;
						that.$API.wpm.prints.req(obj).then((response) => {
							that.$message.success("打印成功");
						});
				}
			}else{
			    that.printSetting();
			}
		},
		inmCheck(row){
			let that = this;
			that.checkItem = row;
			that.dialog.check = true;
			that.$nextTick(() => {
				that.$refs.checkDialog.open("add");
			
			})
		},
	},
};
</script>
<style scoped></style>
