<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<!-- <el-button type="primary" v-auth="'mio.do'" @click="tomio('do_in')">入库</el-button> -->
				<el-button type="primary" v-auth="'handover.create'" @click="table_add(20)">返工</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-date-picker
					v-if="route_code=='chicuncheck'||route_code=='waiguancheck'"
					v-model="query.mlog_date_start"
					type="date"
					placeholder="产出开始日期"
					value-format="YYYY-MM-DD"
					clearable
					style="width: 130px; margin: 5px"
				></el-date-picker>
				<el-date-picker
					v-if="route_code=='chicuncheck'||route_code=='waiguancheck'"
					v-model="query.mlog_date_end"
					type="date"
					placeholder="产出结束日期"
					value-format="YYYY-MM-DD"
					clearable
					style="width: 130px; margin:0"
				></el-date-picker>
				<el-select
					v-model="queryState"
					placeholder="物料状态"
					clearable
					@change="searchStateChange"
					style="width: 110px"
				>
					<el-option
						v-for="item in stateOptions"
						:key="item.value"
						:label="item.name"
						:value="item.value"
					/>
				</el-select>
				<el-input
					style="margin-right: 5px;width: 200px"
					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"
				@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="100"
				>
					<template #default="scope">
						<el-button
							link size="small"
							@click="table_Check(scope.row)"
							v-auth="'ftestwork.create'"
							type="primary"
							v-if="scope.row.state == 10||scope.row.state == 34"
						>检验</el-button>
						<!-- v-if="(scope.row.mgroup_name == '黑化'||scope.row.mgroup_name == '退火'||scope.row.mgroup_name == '外观检验')&&(scope.row.state == 10||scope.row.state == 20||scope.row.state == 34)" -->
						<el-button
							link size="small"
							type="warning"
							@click="tableCheckList(scope.row)"
							v-if="scope.row.state == 10||scope.row.state == 34"
						>检验记录</el-button>
						<el-button @click="printMaterial(scope.row)" type="text">打签</el-button>
					</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.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"
			:mgroupName="mgroupName"
			:mgroupId="mgroupId"
			@success="handleScrapSuccess"
			@closed="dialog.scrap = false"
		>
		</scrap-dialog>
		<handover-dialog
			v-if="dialog.handover"
			ref="handoverDialog"
			:type="type"
			:mgroupName="mgroupName"
			:mgroupId="mgroupId"
			@success="handleScrapSuccess"
			@closed="dialog.handover = false"
		>
		</handover-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>
		<print-dialog
			v-if="dialog.print_m"
			ref="printmaterial"
			:mId="wmId"
			:mtype="wmtype"
			:apiObj="apiObjPrint"
		></print-dialog>
	</el-container>
</template>
<script>
import { wmState } from "@/utils/enum.js";
import materials from "./../mtm/materials.vue";
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,
		materials,
		checkDialog,
		showDrawer,
		scrapDialog,
		printDialog,
		handoverDialog
	},
	name: "wmaterial",
	data() {
		return {
			wmState,
			apiObj: null,
			params: {
				mgroup: "",
				belong_dept:''
			},
			wmtype:0,
			query: {
				search: "",
				state: null,
			},
			dialog: {
				save: false,
				scrap: false,
				permission: false,
				inmRecord:false,
				print_m:false,
				handover:false,
			},
			tableData: [],
			selection: [],
			stateOptions:[
				{ value: 100, name: "完全合格"},
				{ value: 101, name: "合格B类"},
				{ value: 20, name: "不合格"},
				{ value: 30, name: "返工"},
			],
			queryWm: {
				search: "",
				material: "",
			},
			queryState:null,
			route_code:'',
			cate_type:'',
			materialType: "wm",
			changebatch:false,
			materialsVisible:false,
			visibleDrawer: false,
			apiObjPrint:this.$API.cm.labelmat.fromWm,
			wmId:'',
			printer_name:localStorage.getItem("printer_name"),
		};
	},
	mounted() {
		let that = this;
		that.route_code = that.$route.path.split("/")[2];
		that.params.mgroup = that.mgroupId;
		that.params.tag = 'done';
		that.$TOOL.data.set('gx_deptID',that.deptId);
		that.apiObj = that.$API.wpm.wmaterial.list;
		// that.$refs.table.refresh();
	},
	methods: {
		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;
		},
		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;
			}
		},
		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);
			});
		},
		rowClick(row){
			this.dialog.handover = true;
			this.type = 10;
			this.$nextTick(() => {
				this.$refs.handoverDialog.open("add",row,20);
			});
		},
		//获取当前批次车间物料的检验记录
		tableCheckList(row){
			this.wm = row.id;
			this.visibleDrawer = true;
			this.$nextTick(() => {
				this.$refs.showDrawer.open();
			});
		},
		//表格选择后回调事件
		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("该批次物料与前面的不一样");
					}
				})
			}
		},
		//添加返工/报废
		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_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.$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);
			});
		},
		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);
			});
		},
		//报废后刷新页面
		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();
		},
		//打签
		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;
					that.$API.wpm.prints.req(obj).then((response) => {
						that.$message.success("打印成功");
					});
				})
			}else{
				that.$message.warning("请先设置打印机");
			}
		},
	},
};
</script>
<style scoped></style>
