<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<!-- <el-button type="primary" v-auth="'mio.do'" @click="tomio('do_out')">领料</el-button> -->
				<!-- <el-button type="primary" v-auth="'handover.create'" @click="table_add(20)">返工</el-button> -->
				<el-button type="primary" v-auth="'wmaterial.create'" @click="table_create()">来料</el-button>
				<el-button type="primary" v-auth="'handover.create'" @click="table_add(40)">报废</el-button>
				<el-button type="primary" v-auth="'handover.create'" @click="batchConcat()">合批</el-button>
				<!-- <scScanner :labeltext="'返工扫码'" @scanResult="codeTextChange20"></scScanner> -->
				<scScanner :labeltext="'报废扫码'" @scanResult="codeTextChange40"></scScanner>
			</div>
			<div class="right-panel">
				<el-select
					v-model="queryState"
					placeholder="物料状态"
					clearable
					style="width: 250px"
					@change="searchStateChange"
				>
					<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
					@keyup.enter.native="handleQuery"
				></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"
				@selection-change="selectionChange"
			>
			<el-table-column type="selection"></el-table-column>
			<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"
				>
					<template #default="scope">
						<el-link @click="rowClick(scope.row)" type="primary">{{scope.row.batch}}</el-link>
					</template>
				</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"
				></el-table-column>
				<el-table-column
					label="生产中"
					prop="count_working"
					min-width="80"
				></el-table-column>
				<el-table-column
					label="不合格标记"
					prop="defect_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="130"
				>
					<template #default="scope">
						<!-- <el-button
							link size="small"
							@click="table_Check(scope.row)"
							v-auth="'ftestwork.create'"
							type="primary"
							v-if="scope.row.defect_name == null"
							>检验
						</el-button>
						<el-button
							link size="small"
							type="warning"
							@click="tableCheckList(scope.row)"
							v-if="scope.row.defect_name == null"
							>检验记录
						</el-button> -->
						<el-button @click="printMaterial(scope.row)" type="text">打签</el-button>
						<el-button link type="danger" v-auth="'wmaterial.delete'" v-if="scope.row.is_manual" @click="table_del(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
		<!-- 检验 -->
		<!-- <check-dialog
			v-if="dialog.save"
			ref="saveDialog"
			:mgroup="mgroupId"
			:mgroupName="mgroupName"
			:itemObj = "checkItem"
			@success="handleSaveSuccess"
			@closed="dialog.save = false"
		>
		</check-dialog> -->
		<!-- 返工/报废 -->
		<scrap-dialog
			v-if="dialog.scrap"
			ref="scrapDialog"
			:type="type"
			:changebatch="changebatch"
			:mgroupName="mgroupName"
			:mgroupId="mgroupId"
			@success="handleScrapSuccess"
			@closed="dialog.scrap = false"
		>
		</scrap-dialog>
		<showDrawer
			ref="showDrawer"
			v-if="visibleDrawer"
			:wm="wm"
			@closed="visibleDrawer = false"
		>
		</showDrawer>
		<!-- 领料 -->
		<save-dialog
			v-if="dialog.inmRecord"
			ref="inmRecordDialog"
			:cate = "cate_type"
			:process = "processId"
			:mgroupId = "mgroupId"
			:deptId = "deptId"
			@success="handleinmSuccess"
		>
		</save-dialog>
		<handover-dialog
			v-if="dialog.handover"
			ref="handoverDialog"
			:type="type"
			:mgroupName="mgroupName"
			:mgroupId="mgroupId"
			@success="handleScrapSuccess"
			@closed="dialog.handover = false"
		>
		</handover-dialog>
		<print-dialog
			v-if="dialog.print_m"
			ref="printmaterial"
			:mId="wmId"
			:mtype="wmtype"
			:apiObj="apiObjPrint"
		></print-dialog>
		<el-dialog title="手动来料" v-model="dialog.create" width="400px">
			<el-form :model="createForm" label-width="80px">
				<el-form-item label="物料">
					<xtSelect
						v-model="createForm.material"
						:label="createForm.material_name"
						:apiObj="$API.mtm.material.list"
						:params="{ process_todo: processId }"
						style="width:100%"
						@change="(row) => createForm.material_name = row && row.name"
					>
						<el-table-column label="名称" prop="name" />
						<el-table-column label="规格" prop="specification" />
					</xtSelect>
				</el-form-item>
				<el-form-item label="批次号">
					<el-input v-model="createForm.batch" />
				</el-form-item>
				<el-form-item label="数量">
					<el-input-number v-model="createForm.count" :min="0" />
				</el-form-item>
			</el-form>
			<template #footer>
				<el-button @click="dialog.create = false">取消</el-button>
				<el-button type="primary" @click="submitCreate">确定</el-button>
			</template>
		</el-dialog>
	</el-container>
</template>
<script>
import { wmState } from "@/utils/enum.js";
import printDialog from "./../template/printmaterial.vue";
import checkDialog from "./check_form.vue";
import showDrawer from "./check_drawer.vue";
import scrapDialog from "./handover_form.vue";
import handoverDialog from "./handover_form2.vue";
import saveDialog from "./inm_record.vue";
export default {
	props: {
		deptId: {
			type: String,
			default: "",
		},
		mgroupId: {
			type: String,
			default: "",
		},
		mgroupName: {
			type: String,
			default: "",
		},
		processId: {
			type: String,
			default: "",
		},
		mgroupcode: {
			type: String,
			default: "",
		},
	},
	components: {
		saveDialog,
		checkDialog,
		showDrawer,
		scrapDialog,
		printDialog,
		handoverDialog
	},
	name: "wmaterial",
	data() {
		return {
			wmState,
			apiObj: null,
			params: {
				mgroup: "",
				
			},
			wmtype:0,
			query: {
				search: "",
				state: null,
			},
			dialog: {
				save: false,
				scrap: false,
				permission: false,
				inmRecord:false,
				print_m:false,
				handover:false,
				create: false,
			},
			tableData: [],
			selection: [],
			stateOptions:[
				{ value: 100, name: "完全合格"},
				{ value: 101, name: "合格B类"},
				{ value: 20, name: "不合格"},
				{ value: 30, name: "返工"},
			],
			queryState: null,
			codeText2: "",
			codeText4: "",
			cate_type:'',
			changebatch:false,
			visibleDrawer: false,
			apiObjPrint:this.$API.cm.labelmat.fromWm,
			wmId:'',
			printer_name:localStorage.getItem("printer_name"),
			createForm: { material: '', material_name: '', batch: '', count: 0 },
		};
	},
	mounted() {
		let that = this;
		that.params.mgroup = that.mgroupId;
		that.params.tag="todo";
		that.$TOOL.data.set('gx_deptID',that.deptId)
		that.apiObj = that.$API.wpm.wmaterial.list;
	},
	methods: {
		selectionChange(selection) {
			let that = this;
			that.selection = selection;
			//合批
			if(selection.length>0){
				let material = selection[0].material;
				selection.forEach((item)=>{
					if(item.material!==material){
						that.$refs.table.toggleRowSelection(item, false);
						that.$message.warning("该批次物料与前面的不一样");
					}
				})
			}
		},
		//分批mtype:20
		rowClick(row){
			this.dialog.handover = true;
			this.type = 10;
			this.$nextTick(() => {
				this.$refs.handoverDialog.open("add",row,20);
			});
		},
		//合批mtype:30
		batchConcat(){
			this.changebatch = true;
			let handoverb = [];
			if(this.selection.length>0){
				this.selection.forEach(item=>{
					let obj = {};
					obj.wm = item.id;
					obj.batch = item.batch;
					obj.defect = item.defect;
					obj.material = item.material;
					obj.count_canhandover = item.count_canhandover;
					obj.count = item.count_canhandover;
					obj.label = item.defect !== null?item.batch+'('+item.material_name+')'+item.defect_name:item.batch+'('+item.material_name+')';
					handoverb.push(obj)
				})
			}
			this.type = 10;
			this.dialog.scrap = true;
			this.$nextTick(() => {
				this.$refs.scrapDialog.open("add",handoverb,30);
			});
		},
		searchStateChange(){
			let that = this;
			that.query.state = null;
			that.query.defect__isnull = null;
			if(that.queryState==100){
				that.query.state = 10;
				that.query.defect__isnull = true;
			}else if(that.queryState==101){
			    that.query.state = 10;
				that.query.defect__isnull = false;
			}else if(that.queryState==20){
			    that.query.state = 20;
				that.query.defect__isnull = null;
			}else if(that.queryState==30){
			    that.query.state = 30;
				that.query.defect__isnull = null;
			}
			that.handleQuery();
		},
		//领料
		tomio(type) {
			let that = this;
			that.cate_type=type;
			that.dialog.inmRecord = true;
			that.$nextTick(() => {
				that.$refs.inmRecordDialog.open("add");
			});
		},
		table_Check(row){
			this.checkItem = row;
			let mode = this.mgroupName;
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open(mode);
			});
		},
		//获取当前批次车间物料的检验记录
		tableCheckList(row){
			this.wm = row.id;
			this.visibleDrawer = true;
			this.$nextTick(() => {
				this.$refs.showDrawer.open();
			});
		},
		//添加返工/报废
		table_add(type) {
			this.dialog.scrap = true;
			this.type = type;
			let handoverb = [];
			if(this.selection.length>0){
				this.selection.forEach(item=>{
					let obj = {};
					obj.wm = item.id;
					obj.batch = item.batch;
					obj.count_cando = item.count_cando;
					obj.count = item.count_cando;
					handoverb.push(obj)
				})
			}
			this.$nextTick(() => {
				this.$refs.scrapDialog.open("add",handoverb,10);
			});
		},
		codeTextChange20(codeText2){
			let that = this;
			that.type = 20;
			that.codeText2 = codeText2;
			that.dialog.scrap = true;
			that.$nextTick(() => {
				that.$refs.scrapDialog.open("add",codeText2,10);
			});
		},
		codeTextChange40(codeText4){
			let that = this;
			that.type = 40;
			that.codeText4 = codeText4;
			that.dialog.scrap = true;
			that.$nextTick(() => {
				that.$refs.scrapDialog.open("add",codeText4,10);
			});
		},
		handleScrapSuccess(){
			this.dialog.scrap = false;
			this.$refs.table.refresh();
		},
		//出入库后刷新页面
		handleinmSuccess(){
			this.$refs.table.refresh();
		},
		//搜索
		handleQuery() {
			this.$refs.table.queryData(this.query);
		},
		//本地更新数据
		handleSaveSuccess(data, mode) {
			this.dialog.save = false;
			this.$refs.table.refresh();
		},
		//手动来料
		table_create() {
			this.createForm = { material: '', material_name: '', batch: '', count: 0, mgroup: this.mgroupId };
			this.dialog.create = true;
		},
		submitCreate() {
			let that = this;
			if (!that.createForm.material) { that.$message.warning('请选择物料'); return; }
			if (!that.createForm.batch) { that.$message.warning('请输入批次号'); return; }
			let data = { material: that.createForm.material, batch: that.createForm.batch, count: that.createForm.count, mgroup: that.createForm.mgroup };
			that.$API.wpm.wmaterial.create.req(data).then(() => {
				that.$message.success('来料成功');
				that.dialog.create = false;
				that.$refs.table.refresh();
			});
		},
		table_del(row) {
			this.$confirm('确定删除该来料记录吗？', '提示', { type: 'warning' }).then(() => {
				this.$API.wpm.wmaterial.delete.req(row.id).then(() => {
					this.$message.success('删除成功');
					this.$refs.table.refresh();
				});
			}).catch(() => {});
		},
		//打签
		printMaterial(row){
			let that = this;
			if(that.printer_name!==''&&that.printer_name!==null&&that.printer_name!==undefined){
				let params = {};
				params.tid = row.id;
				params.extra_data={count:row.count};
				params.label_template_name = '工段物料打印模板';
				that.apiObjPrint.req(params).then((res) => {
					let obj = {};
					obj.printer_commands = res.commands;
					obj.printer_name = that.printer_name;
					let printer_ip=localStorage.getItem("printer_ip")||'127.0.0.1';
that.$API.wpm.prints.req(printer_ip, obj).then((response) => {
						that.$message.success("打印成功");
					});
				})
			}else{
				that.$message.warning("请先设置打印机");
			}
		},
	},
};
</script>
<style scoped></style>
