<template>
	<el-drawer
		title="日志详情"
		v-model="visible"
		:size="'90%'"
		destroy-on-close
		@closed="$emit('closed')"
	>
		<div>
			<el-card style="width: 100%" header="基本信息" shadow="never">
				<el-button type="primary" @click="handlePrint" style="position: absolute;right: 20px;">打印</el-button>
				<el-descriptions>
					<el-descriptions-item label="工艺步骤">
						<el-tag v-if="mlogItem.is_fix" type="warning">返工</el-tag>
						<span v-if="mlogItem.material_in_name!==null">{{mlogItem.material_in_name}}-></span>
						<span v-if="mlogItem.material_out_name!==null">{{mlogItem.material_out_name}}</span>
					</el-descriptions-item>
					<el-descriptions-item label="输入物料">{{
						mlogItem.material_in_name
					}}</el-descriptions-item>
					<el-descriptions-item label="输出物料">{{
						mlogItem.material_out_name
					}}</el-descriptions-item>
					<el-descriptions-item label="工段名称">{{
						mlogItem.mgroup_name
					}}</el-descriptions-item>
					<el-descriptions-item label="生产设备">{{
						mlogItem.equipment_name
					}}</el-descriptions-item>
					<el-descriptions-item label="预计工时">{{
						mlogItem.hour_work
					}}</el-descriptions-item>
					<el-descriptions-item label="部门/车间">{{
						mlogItem.belong_dept_name
					}}</el-descriptions-item>
					<el-descriptions-item label="所在班组">{{
						mlogItem.team_name
					}}</el-descriptions-item>
					<el-descriptions-item label="处理人">
						<div v-if="mlogItem.handle_users_&&mlogItem.handle_users_.length>0">
							<span v-for="item in mlogItem.handle_users_" :key="item.id">{{ item.name }}</span>
						</div>
						<span v-else>{{mlogItem.handle_user_name}}</span>
					</el-descriptions-item>
					<el-descriptions-item label="创建时间">{{
						mlogItem.create_time
					}}</el-descriptions-item>
					<el-descriptions-item label="开始时间">{{
						mlogItem.work_start_time
					}}</el-descriptions-item>
					<el-descriptions-item label="结束时间">{{
						mlogItem.work_end_time
					}}</el-descriptions-item>
					<el-descriptions-item label="投入库存缺陷" v-if="mlogItem.wm_in_defect_name">
						<el-tag type="warning">{{ mlogItem.wm_in_defect_name }}</el-tag>
					</el-descriptions-item>
					<el-descriptions-item label="投入库存状态" v-if="mlogItem.wm_in_state">
						<el-tag :type="wmState[mlogItem.wm_in_state]?.type">{{ wmState[mlogItem.wm_in_state]?.text }}</el-tag>
					</el-descriptions-item>
					<el-descriptions-item label="检验表单" v-if="mlogItem.mgroup_name=='黑化'">
						<el-link :href="mlogItem.test_file" target="_blank" type="primary" :underline="false">{{mlogItem.test_file}}</el-link>
					</el-descriptions-item>
				</el-descriptions>
				<div style="padding: 5px 10px;display: flex;justify-content: end;align-items: center;">
					<!--  hasOkBDefect -->
					<div v-if="mlogItem.submit_time == null" style="margin-right: 16px;display:flex;align-items:center;">
						<span style="margin-right: 6px;">合格B类不拆批</span>
						<el-switch v-model="mlogItem.clear_defect"></el-switch>
					</div>
					<el-button
						type="primary"
						v-if="mlogItem.submit_time == null"
						@click="mlogUpdate"
						style="margin-right: 10px;"
						v-auth="'mlog.update'"
					>
						编辑
					</el-button>
					<el-button
						type="primary"
						v-if="mlogItem.submit_time == null"
						:loading="isSaveing"
						@click="mlogSubmit"
						>提交</el-button
					>
				</div>
			</el-card>
			<!-- 输入物料 -->
			<el-card
				style="width: 100%; margin: 1vh 0"
				header="输入物料"
				shadow="never"
			>
				<div style="display: flex;">
					<el-button
						type="primary"
						v-if="mlogItem.submit_time == null&&(mlogItem.ticket==null||(mlogItem.ticket_&&mlogItem.ticket_.state_.type==1))"
						icon="el-icon-plus"
						@click="table_add"
					>新增</el-button>
					<scScanner @scanResult="codeTextChange" style="margin:0 10px;"></scScanner>
					<el-input 
						ref="codeInput"
						v-model="codeText"
						clearable
						placeholder="物料批次号"
						style="width:150px"
						@keyup.enter="codeTextChange(codeText)"
					></el-input>
				</div>
				<div style="height: 4px"></div>
				<scTable
					ref="tableIn"
					:apiObj="apiObj"
					row-key="id"
					stripe
					:params="paramsIn"
					hidePagination
				>
					<el-table-column type="index" width="50" />
					<el-table-column
						label="物料"
						prop="material_in_name"
						show-overflow-tooltip
					></el-table-column>
					<el-table-column label="批次号" prop="batch" min-width="200" show-overflow-tooltip>
						<template #default="scope">
							<span>{{ scope.row.batch }}</span>
							<el-tag v-if="scope.row.wm_in_defect_name" type="warning" size="small" style="margin-left: 6px;">{{ scope.row.wm_in_defect_name }}</el-tag>
							<el-tag v-if="scope.row.wm_in_state" :type="wmState[scope.row.wm_in_state]?.type" size="small" style="margin-left: 6px;">{{ wmState[scope.row.wm_in_state]?.text }}</el-tag>
						</template>
					</el-table-column>
					<el-table-column label="领用数量" prop="count_use">
					</el-table-column>
					<el-table-column label="加工前不良" prop="count_pn_jgqbl">
					</el-table-column>
					<el-table-column
						label="创建时间"
						prop="create_time"
						show-overflow-tooltip
					>
					</el-table-column>
					<el-table-column
						label="操作"
						fixed="right"
						align="center"
						width="100px"
					>
						<template #default="scope">
							<el-button
								link
								type="primary"
								@click="table_in_edit(scope.row)"
								:disabled="mlogItem.submit_time !== null||(mlogItem.ticket!==null&&mlogItem.ticket_&&mlogItem.ticket_.state_.type!==1)"
							>
								编辑
							</el-button>
							<el-button
								link
								:disabled="mlogItem.submit_time !== null||(mlogItem.ticket!==null&&mlogItem.ticket_&&mlogItem.ticket_.state_.type!==1)"
								type="danger"
								@click="table_in_del(scope.row)"
							>
								删除
							</el-button>
						</template>
					</el-table-column>
				</scTable>
			</el-card>
			<!-- 输入物料编辑 -->
			<el-dialog v-model="saveInDialog" title="编辑">
				<el-form
					:model="saveInForm"
					:rules="rules"
					label-width="100px"
					ref="saveInForm"
				>
					<el-row>
						<el-col :md="12" :sm="24">
							<el-form-item label="领取数量" prop="count_use">
								<el-input-number
									v-model="saveInForm.count_use"
									:min="1"
									style="width: 100%"
									controls-position="right"
								/>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="加工前不良" prop="count_pn_jgqbl">
								<el-input-number
									v-model="saveInForm.count_pn_jgqbl"
									:min="0"
									:disabled="true"
									style="width: 100%"
									controls-position="right"
								/>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24" v-for="item in qct_defects" :key="item.id">
							<el-form-item :label="item.defect_name">
								<el-input-number
									v-model="defectinform[item.defect_name]"
									:min="0"
									style="width: 100%"
									controls-position="right"
									@change="countChange"
								/>
							</el-form-item>
						</el-col>
					</el-row>
				</el-form>
				<template #footer>
					<el-button
						type="primary"
						:loading="isSaveing"
						@click="saveInSubmit()"
						>保 存</el-button
					>
				</template>
			</el-dialog>
			<!-- 输出物料 -->
			<el-card style="width: 100%" header="输出物料" shadow="never">
				<scTable
					ref="tableOut"
					:apiObj="apiObj"
					row-key="id"
					stripe
					:params="paramsOut"
					hidePagination
				>
					<el-table-column type="index" width="50" />
					<el-table-column type="expand">
						<template #default="props">
							<div style="padding-left: 50px" v-if="props.row.count_notok_json.length>0">
								<el-descriptions :column="4" v-for="item in props.row.count_notok_json" :key="item">
									<el-descriptions-item label="层数">
										{{item.floor}}
									</el-descriptions-item>
									<el-descriptions-item label="抽检数">
										{{item.count_sampling}}
									</el-descriptions-item>
									<el-descriptions-item label="总数">
										{{item.count}}
									</el-descriptions-item>
								</el-descriptions>
							</div>
							<div style="padding-left: 50px" v-else>
								<el-descriptions :column="4" v-if="props.row.mlogbdefect.length>0">
									<el-descriptions-item v-for="item in props.row.mlogbdefect" :label="item.defect_name" :key="item.id">
										{{item.count}}
									</el-descriptions-item>
								</el-descriptions>
								<el-descriptions :column="4" v-else>
									<el-descriptions-item label="划伤">{{
										props.row.count_n_hs
									}}</el-descriptions-item>
									<el-descriptions-item label="气泡">{{
										props.row.count_n_qp
									}}</el-descriptions-item>
									<el-descriptions-item label="水纹">{{
										props.row.count_n_swen
									}}</el-descriptions-item>
									<el-descriptions-item label="崩边">{{
										props.row.count_n_bb
									}}</el-descriptions-item>
									<el-descriptions-item label="小崩边">{{
										props.row.count_n_xbb
									}}</el-descriptions-item>
									<el-descriptions-item label="雾面">{{
										props.row.count_n_wm
									}}</el-descriptions-item>
									<el-descriptions-item label="麻点">{{
										props.row.count_n_md
									}}</el-descriptions-item>
									<el-descriptions-item label="线痕">{{
										props.row.count_n_xh
									}}</el-descriptions-item>
								</el-descriptions>
							</div>
						</template>
					</el-table-column>
					<el-table-column
						label="产出物料"
						prop="material_out_name"
						show-overflow-tooltip
					></el-table-column>
					<el-table-column label="批次号" prop="batch">
					</el-table-column>
					<el-table-column label="数量" prop="count_real">
					</el-table-column>
					<el-table-column label="合格数" prop="count_ok">
					</el-table-column>
					<el-table-column label="不合格数量" prop="count_notok">
					</el-table-column>
					<el-table-column label="抽检人" prop="test_user_name">
					</el-table-column>
					<el-table-column
						label="创建时间"
						prop="create_time"
						show-overflow-tooltip
					>
					</el-table-column>
					<el-table-column
						label="操作"
						fixed="right"
						align="center"
						width="100px"
					>
						<template #default="scope">
							<el-button
								link
								type="primary"
								:disabled="mlogItem.submit_time !== null||(mlogItem.ticket!==null&&mlogItem.ticket_&&mlogItem.ticket_.state_.type!==1)"
								@click="table_out_check(scope.row)"
							>
								检验
							</el-button>
						</template>
					</el-table-column>
				</scTable>
			</el-card>
			<save-dialog
				v-if="dialog.save"
				ref="saveDialog"
				:mlog="mlogId"
				:mgroup="mgroup"
				:is_fix = "is_fix"
				:mlogbdefect = "mlogbdefect"
				:mgroupMtype="mgroupMtype"
				:mgroupName = "mgroupName"
				:codeText = "codeText"
				:materialIn="materialIn"
				:process = "process"
				:hasRoute = "hasRoute"
				@success="handleSaveSuccess"
				@closed="dialog.save = false"
			>
			</save-dialog>
			<check-dialog
				v-if="dialog.check"
				ref="checkDialog"
				:deptId="deptId"
				:mgroupMtype="mgroupMtype"
				:mlogboutdefect = "mlogboutdefect"
				@success="handleCheckSuccess"
				@closed="dialog.check = false"
			>
			</check-dialog>
			<edit-dialog
				v-if="dialog.edit"
				ref="editDialog"
				:dept="deptId"
				:mgroup="mgroup"
				:process="process"
				:mgroupMtype="mgroupMtype"
				:mgroupName= "mgroupName"
				@success="handleEditSuccess"
				@closed="dialog.edit = false"
			>
			</edit-dialog>
			<el-dialog v-model="printVisible" width="1200px">
				<print :baseData="mlogItem" :tableData="tableData" :tableData2="tableData2" type="102"  @closePrint="printVisible=false"/>
			</el-dialog>
			<print-dialog
				v-if="dialog.print_m"
				ref="printmaterial"
				:mId="wmId"
				:mtype="wmtype"
				:apiObj="apiObjPrint"
			></print-dialog>
		</div>
	</el-drawer>
</template>
<script>
import editDialog from "./mlog_form.vue";
import saveDialog from "./mlogb_form.vue";
import checkDialog from "./mlogb_check.vue";
import print from "./../setting/print/A4.vue";
import printDialog from "./../template/printmaterial.vue";
import { wmState } from "@/utils/enum.js";
export default {
	props: {
		mlogId: {
			type: String,
			default: "",
		},
		mgroupMtype:{
			type: String,
			default: "",
		},
		process:{
			type: String,
			default: "",
		}
	},
	components: {
		editDialog,
		saveDialog,
		checkDialog,
		print,
		printDialog
	},
	emits: ["success", "closed"],
	computed: {
		hasOkBDefect() {
			const rows = this.tableData2 || [];
			return rows.some(row => Array.isArray(row.mlogbdefect)
				&& row.mlogbdefect.some(d => Number(d.defect_okcate) === 20 && Number(d.count) > 0));
		},
	},
	data() {
		return {
			wmState,
			loading: false,
			//表单数据
			form: {},
			dialog: {
				save: false,
				edit: false,
				check: false,
				print_m:false,
			},
			apiObj: null,
			apiObjWm:null,
			paramsWm: {
				page: 0,
				search:'',
				mgroup:''
			},
			paramsIn: {
				page: 0,
				mlog: "",
				material_in__isnull: 0,
			},
			paramsOut: {
				page: 0,
				mlog: "",
				material_out__isnull: 0,
			},
			cells:0,
			mgroup: "",
			mlogItem: {},
			saveInForm: {
				count_use: 0,
				count_pn_jgqbl: 0,
			},
			act_states: {
				0: "草稿中",
				1: "进行中",
				2: "被退回",
				3: "被撤回",
				4: "已完成",
				5: "已关闭",
			},
			defectinform:{},
			fileList:[],
			cellsList:[],
			qct_defects:[],
			tableDataWm:[],
			mgroupName:'',
			test_file:'',
			deptId: "",
			codeText: "",
			visible: false,
			isSaveing: false,
			options: [],
			tableData:[],
			tableData2:[],
			mlogbdefect:[],
			mlogboutdefect:[],
			saveInDialog: false,
			// ticketDialog:false,
			printVisible:false,
			setFiltersVisible: false,
			rules: {
				count_use: [
					{
						required: true,
						message: "请输入领取数量",
						trigger: "blur",
					},
				],
			},
			is_fix:false,
			wmId:'',
			wmtype:'',
			batchContains:'',
			apiObjPrint:this.$API.cm.labelmat.fromWm,
		};
	},
	mounted() {
		console.log('this.mlogId',this.mlogId);
		let that = this;
		this.paramsIn.mlog = this.mlogId;
		this.paramsOut.mlog = this.mlogId;
		this.apiObj = this.$API.wpm.mlogb.list;
		// this.getInit();
		that.$API.wpm.mlogb.list.req(that.paramsIn).then((res) => {
			that.tableData = res;
		})
		that.getMlogItem();
	},
	methods: {
		open() {
			this.visible = true;
		},
		getMlogItem() {
			let that = this;
			that.$API.wpm.mlog.item.req(that.mlogId).then((res) => {
				that.mlogItem = res;
				if(res.route!==null){
					this.hasRoute = true;
				}
				that.mgroupName = res.mgroup_name;
				if(res.test_file!==null){
					that.fileList = [{name:res.test_file,url:res.test_file}];
					that.form.test_file = res.test_file;
				}
				that.is_fix = res.is_fix;
				that.mgroup = res.mgroup;
				that.paramsWm.mgroup = res.mgroup;
				that.materialIn = res.material_in;
				that.deptId = res.belong_dept;
				that.$API.wpm.mlogb.list.req(that.paramsOut).then((res) => {
					that.tableData2 = res;
					if(res.length>0){
						res.forEach((item) => {
							that.batchContains+= item.batch + " ";
						})
						// that.paramsWm.search = that.batchContains;
						// that.apiObjWm = that.$API.wpm.wmaterial.list;
					}
				})
			});
		},
		getdefects(){
			let that = this;
			that.$API.qm.qct.list.req({qctmat__material:that.mlogItem.material_in,page:0,tags:'process'}).then((res) => {
				if(res.length>0){
					that.$API.qm.qct.item.req(res[0].id).then((res) => {
						res.qct_defects.forEach((item) => {
							that.defectinform[item.defect_name] = 0;
						})
						that.qct_defects=res.qct_defects;
						console.log('that.saveInForm.mlogbdefect',that.saveInForm.mlogbdefect);
						if(that.saveInForm.mlogbdefect&&that.saveInForm.mlogbdefect.length>0){
							that.saveInForm.mlogbdefect.forEach((item) => {
								that.defectinform[item.defect_name] = Number(item.count);
							})
						}
					})
				}
			})
		},
		countChange() {
			let that = this;
			that.saveInForm.count_pn_jgqbl = 0;
			that.qct_defects.forEach(item => {
				if(item.defect_okcate==30){
			    	that.saveInForm.count_pn_jgqbl += that.defectinform[item.defect_name];
				}
			})
		},
		//表单注入数据
		setData(data) {
			Object.assign(this.form, data);
			this.getRoute(data.id);
		},
		//编辑
		mlogUpdate() {
			this.dialog.edit = true;
			this.$nextTick(() => {
				this.$refs.editDialog.open("edit").setData(this.mlogItem);
			});
		},
		table_add() {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add');
			});
		},
		//自检
		table_in_edit(row) {
			let that = this;
			if(that.mlogItem.mgroup_name=='黑化'||that.mlogItem.mgroup_name=='退火'){
				that.dialog.save = true;
				that.$nextTick(() => {
					that.$refs.saveDialog.open('edit',row);
				});
			}else{
				that.saveInForm = row;
				that.getdefects();
				that.saveInDialog = true;
			}
		},
		//提交自检
		saveInSubmit() {
			let that = this;
			that.$refs.saveInForm.validate(async (valid) => {
				if (valid) {
					that.isSaveing = true;
					let obj = {};
					let count_pn_jgqbl = 0,mlogbdefect=[];
					that.qct_defects.forEach(item => {
						count_pn_jgqbl += that.defectinform[item.defect_name] ;
						let obj = {};
						obj.defect = item.defect;
						obj.count = that.defectinform[item.defect_name];
						obj.mlogb = that.saveInForm.id;
						mlogbdefect.push(obj);
					})
					obj.count_use = that.saveInForm.count_use;
					obj.mlogbdefect = mlogbdefect;
					obj.count_pn_jgqbl = count_pn_jgqbl;
					that.$API.wpm.mlogb.updateIn.req(that.saveInForm.id, obj).then((res) => {
						that.isSaveing = false;
						that.$message.success("操作成功");
						that.saveInDialog = false;
						that.$refs.tableIn.refresh();
						that.$refs.tableOut.refresh();
					}).catch(() => {
						that.isSaveing = false;
					});
				}
			});
		},
		table_in_del(row) {
			let that = this;
			that.$confirm("确定删除吗？").then(() => {
				that.$API.wpm.mlogb.delIn.req(row.id).then((res) => {
					that.$message.success("操作成功");
					that.$refs.tableIn.refresh();
					that.$refs.tableOut.refresh();
					//删除in记录后，out也要删除相应的记录
				});
			});
		},
		table_out_check(row) {
			let that = this;
			that.mlogboutdefect = row.mlogbdefect;
			let obj = {};
			Object.assign(obj, row);
			obj.mgroup_name = that.mlogItem.mgroup_name;
			that.$nextTick(() => {
				that.dialog.check = true;
				that.$refs.checkDialog.open(obj);
			});
		},
		//表单提交方法
		mlogSubmit() {
			let that = this;
			that.isSaveing = true;
			const doSubmit = () => {
				that.$API.wpm.mlog.submit.req(that.mlogItem.id).then((res) => {
					that.isSaveing = false;
					that.visible = false;
					that.$message.success("操作成功");
				}).catch(() => {
					that.isSaveing = false;
				});
			};
			if (that.hasOkBDefect) {
				that.$API.wpm.mlog.change.req(that.mlogItem.id, { clear_defect: !!that.mlogItem.clear_defect }).then(() => {
					doSubmit();
				}).catch(() => {
					that.isSaveing = false;
				});
			} else {
				doSubmit();
			}
		},
		// //渲染工单提交按钮
		// getInit() {
		// 	let that = this;
		// 	that.$API.wf.workflow.initkey.req("	backfire").then((res) => {
		// 		that.initForm = res;
		// 	});
		// },
		// //提交退火放行单信息,创建工单
		// submitTicketCreate(id) {
		// 	let that = this;
		// 	let obj = {};
		// 	obj.test_file = that.test_file;
		// 	that.$API.wpm.mlog.change.req(that.mlogItem.id, obj).then((res) => {
		// 		let ticket = {};
		// 		that.isSaveing = true;
		// 		ticket.title = '退火放行审批单';
		// 		ticket.workflow = that.initForm.workflow;
		// 		ticket.ticket_data = {t_id: that.mlogItem.id};
		// 		ticket.transition = id;
		// 		that.$API.wf.ticket.create.req(ticket).then((res) => {
		// 			that.isSaveing = false;
		// 			that.ticketDialog = false;
		// 			that.visible = false;
		// 			that.$message.success("提交成功");
		// 		}).catch((e) => {
		// 			that.isSaveing = false;
		// 		});
		// 	});
		// },
		codeTextChange(codeText){
			this.codeText = codeText;
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open();
				this.codeText = '';
			});
		},
		handleSaveSuccess() {
			this.$refs.tableIn.refresh();
			this.$refs.tableOut.refresh();
		},
		handleCheckSuccess() {
			this.$refs.tableOut.refresh();
		},
		fileUPSuccess(res) {
			this.test_file = res.path;
		},
		//编辑成功后的方法调用
		handleEditSuccess() {
			this.getMlogItem();
		},
		//设置过滤项
		setFilters(filters) {
			this.selectionFilters = filters;
			this.setFiltersVisible = true;
		},
		handlePrint(){
			let that = this;
			that.printVisible = true;
		},
		//打签
		printMaterial(row){
			let that = this;
			that.wmId = row.id;
			that.wmtype = row.material_.type;
			that.dialog.print_m = true;
			this.$nextTick(() => {
			    this.$refs.printmaterial.open();
			})
		},
	},
};
</script>

<style></style>
