<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="工艺路线">{{
						fmlogItem.routepack_name
					}}</el-descriptions-item>
					<el-descriptions-item label="任务编号">{{
						fmlogItem.mtask_number
					}}</el-descriptions-item>
					<el-descriptions-item label="部门/车间">{{
						fmlogItem.belong_dept_name
					}}</el-descriptions-item>
				</el-descriptions>
			</el-card>
			<el-card style="width: 100%; margin: 1vh 0" shadow="never" header="日志列表">
				<el-button
						type="primary"
						v-if="fmlogItem.submit_time == null"
						icon="el-icon-plus"
						@click="table_add"
						>新增</el-button
					>
					<el-input 
						ref="codeInput"
						v-model="codeText"
						clearable
						placeholder="正常交接"
						style="width:150px"
						@keyup.enter="codeTextChange"
					></el-input>
				<scTable
					stripe
					ref="table"
					:apiObj="apiObj"
					row-key="id"
					:params="params"
					:query="params"
					@row-click="table_detail"
				>
					<el-table-column  type="index" width="50"></el-table-column>
					<el-table-column type="expand">
						<template #default="props">
							<div style="padding-left: 50px">
								<el-descriptions :column="2">
									<template v-for="item in props.row.mlogb_full" :key="item.id">
										<el-descriptions-item :label="item.material_out!==null?'输出物料':'输入物料'">
											<span v-if="item.material_out!==null">{{item.batch}}</span>
											<span v-else>{{item.batch}}</span>
										</el-descriptions-item>
									</template>
								</el-descriptions>
								<el-divider></el-divider>
								<el-descriptions :column="4">
									<el-descriptions-item label="划伤" v-if="props.row.count_n_hs>0">
										{{props.row.count_n_hs}}
									</el-descriptions-item>
									<el-descriptions-item label="气泡" v-if="props.row.count_n_qp>0">
										{{props.row.count_n_qp}}
									</el-descriptions-item>
									<el-descriptions-item label="水纹" v-if="props.row.count_n_swen>0">
										{{props.row.count_n_swen}}
									</el-descriptions-item>
									<el-descriptions-item label="崩边" v-if="props.row.count_n_bb>0">
										{{props.row.count_n_bb}}
									</el-descriptions-item>
									<el-descriptions-item label="小崩边" v-if="props.row.count_n_xbb>0">
										{{props.row.count_n_xbb}}
									</el-descriptions-item>
									<el-descriptions-item label="雾面" v-if="props.row.count_n_wm>0">
										{{props.row.count_n_wm}}
									</el-descriptions-item>
									<el-descriptions-item label="麻点" v-if="props.row.count_n_md>0">
										{{props.row.count_n_md}}
									</el-descriptions-item>
									<el-descriptions-item label="线痕" v-if="props.row.count_n_xh>0">
										{{props.row.count_n_xh}}
									</el-descriptions-item>
									<el-descriptions-item label="破损" v-if="props.row.count_n_ps>0">
										{{props.row.count_n_ps}}
									</el-descriptions-item>
									<el-descriptions-item label="产品外径" v-if="props.row.count_n_wj>0">
										{{props.row.count_n_wj}}
									</el-descriptions-item>
									<el-descriptions-item label="产品圆度" v-if="props.row.count_n_xh>0">
										{{props.row.count_n_xh}}
									</el-descriptions-item>
									<el-descriptions-item label="产品同心度" v-if="props.row.count_n_yd>0">
										{{props.row.count_n_yd}}
									</el-descriptions-item>
									<el-descriptions-item label="其它" v-if="props.row.count_n_qt>0">
										{{props.row.count_n_ps}}
									</el-descriptions-item>
								</el-descriptions>
							</div>
						</template>
					</el-table-column>
					<el-table-column label="操作员" prop="handle_user_name" min-width="80"></el-table-column>
					<el-table-column label="设备" prop="equipment_name" min-width="150"></el-table-column>
					<el-table-column label="领料数" prop="count_use"></el-table-column>
					<el-table-column label="加工数" prop="count_real"></el-table-column>
					<el-table-column label="加工前不良" prop="count_pn_jgqbl"></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="work_start_time"></el-table-column>
					<el-table-column label="结束时间" prop="work_end_time"></el-table-column>
					<el-table-column label="班次" prop="shift_name"></el-table-column>
					<el-table-column
						label="操作"
						fixed="right"
						align="center"
						width="100"
					>
						<template #default="scope">
							<el-button link size="small" v-auth="'mlog.update'"
								v-if="scope.row.submit_time == null" type="primary"
								@click.stop="table_edit(scope.row)"
								>编辑
							</el-button>
							<el-button link size="small" v-auth="'mlog.submit'"
								v-if="scope.row.submit_time == null" type="primary"
								@click.stop="mlogSubmit(scope.row)"
								>提交
							</el-button>
							<el-button link size="small" v-auth="'mlog.delete'" type="danger"
								v-if="scope.row.submit_time == null"
								@click.stop="table_del(scope.row)"
								>删除
							</el-button>
							<el-button link v-else size="small" type="danger"
								@click.stop="mlogRevert(scope.row)"
								>撤回
							</el-button>
						</template>
					</el-table-column>
				</scTable>
			</el-card>
			<!-- 工段物料库存 -->
			<el-card style="width: 100%; margin: 1vh 0" header="工段物料列表" shadow="never">
				<scTable
					ref="tableWm"
					:apiObj="apiObjWm"
					:params="paramsWm"
					stripe
					hidePagination
				>
					<el-table-column label="物料名称" prop="material_name" show-overflow-tooltip>
					</el-table-column>
					<el-table-column label="批次号" prop="batch">
					</el-table-column>
					<el-table-column label="数量" prop="count">
					</el-table-column>
					<el-table-column label="不合格标记" prop="notok_sign_name">
					</el-table-column>
					<el-table-column width="100">
						<template #default="scope">
							<el-button @click="printMaterial(scope.row)" type="text">物料标签</el-button>
						</template>
					</el-table-column>
				</scTable>
			</el-card>
			<save-dialog
				v-if="dialog.save"
				ref="saveDialog"
				:fmlog="params.fmlog"
				:mgroup="mgroup"
				:mtask="mtask"
				:process="processId"
				@success="handleSaveSuccess"
				@closed="dialog.save = false"
			>
			</save-dialog>
			<check-dialog
				v-if="dialog.check"
				ref="checkDialog"
				@success="handleCheckSuccess"
				@closed="dialog.check = false"
			>
			</check-dialog>
			<el-dialog v-model="printVisible" width="1200px">
				<print :baseData="fmlogItem" :tableData="tableData" type="101"  @closePrint="printVisible=false"/>
			</el-dialog>
		</div>
	</el-drawer>
</template>
<script>
import saveDialog from "./f_mlogs_form.vue";
import checkDialog from "./mlogb_check.vue";
import print from "./../setting/print/A4.vue";
export default {
	props: {
		fmlogId: {
			type: String,
			default: "",
		},
		processId:{
			type: String,
			default: "",
		},
	},
	components: {
		saveDialog,
		checkDialog,
		print
	},
	emits: ["success", "closed"],
	data() {
		return {
			loading: false,
			//表单数据
			form: {},
			dialog: {
				save: false,
				edit: false,
				check: false,
			},
			apiObj: null,
			apiObjWm:null,
			paramsWm: {
				page: 0,
				search:'',
				mgroup:''
			},
			params: {
				page: 0,
				fmlog: "",
			},
			mgroup: "",
			fmlogItem: {},
			saveInForm: {
				count_use: 0,
				count_pn_jgqbl: 0,
			},
			oinfo_json:{
				退火炉号:'',
				退火时间:'',
				退火温度:'',
				抽检结果:'',
				抽检数:'',
				强度落球:'',
			},
			codeText:"",
			fileList:[],
			tableData:[],
			batchContains:[],
			test_file:'',
			deptId: "",
			visible: false,
			isSaveing: false,
			printVisible:false,
			options: [],
			saveInDialog: false,
			ticketDialog:false,
			setFiltersVisible: false,
			rules: {
				count_use: [
					{
						required: true,
						message: "请输入领取数量",
						trigger: "blur",
					},
				],
			},
		};
	},
	mounted() {
		this.getfmlogItem();
		this.params.fmlog = this.fmlogId;
		this.apiObj = this.$API.wpm.mlog.list;
	},
	methods: {
		open(codeText="") {
			this.codeText = codeText;
			this.visible = true;
		},
		
		getfmlogItem() {
			let that = this;
			that.$API.wpm.fmlog.item.req(that.fmlogId).then((res) => {
				that.fmlogItem = res;
				that.mgroup = res.mgroup;
				that.mtask = res.mtask;
				that.paramsWm.mgroup = res.mgroup;
				that.$API.wpm.mlog.list.req(that.params).then((res) => {
					that.tableData = res;
					if(res.length>0){
						res.forEach((item) => {
							that.batchContains+= item.batch + " ";
						})
						that.paramsWm.search = that.batchContains;
						that.apiObjWm = that.$API.wpm.wmaterial.list;
						if(that.codeText!=""){
							this.codeTextChange();
						}
					}else{}
				});
			});
		},
		//表单注入数据
		setData(data) {
			Object.assign(this.form, data);
			this.getRoute(data.id);
		},
		table_add() {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add');
			});
		},
		codeTextChange(){
			let that = this;
			that.dialog.save = true;
			that.$nextTick(() => {
				that.$refs.saveDialog.open('add',that.codeText);
			});
		},
		table_edit(row){
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row);
			});
		},
		//表单提交方法
		mlogSubmit(row) {
			let that = this; 
			that.isSaveing = true;
			that.$API.wpm.mlog.submit.req(row.id).then((res) => {
				that.isSaveing = false;
				this.$refs.table.refresh();
				that.$message.success("操作成功");
			});
		},
		//日志撤回
		mlogRevert(row) {
			this.$confirm(`确定撤回该日志吗？`, "提示", {
				type: "warning",
			}).then(() => {
				var id = row.id;
				this.$API.wpm.mlog.revert.req(id).then((res) => {
					if (res.err_msg) {
						this.$message.error(res.err_msg);
					} else {
						this.$refs.table.refresh();
						this.$message.success("撤回成功");
					}
				});
			});
		},
		//日志删除
		table_del(row) {
			let that = this;
			this.$confirm(`确定删除该日志吗？`, "提示", {
				type: "warning",
			}).then(() => {
				var id = row.id;
				this.$API.wpm.mlog.delete.req(id).then((res) => {
					if (res.err_msg) {
						this.$message.error(res.err_msg);
					} else {
						this.$refs.table.refresh();
						this.$message.success("删除成功");
					}
				});
			});
		},
		handleSaveSuccess() {
			this.$refs.table.refresh();
		},
		handleCheckSuccess() {
			this.$refs.table.refresh();
		},
		fileUPSuccess(res) {
			console.log('res',res);
			this.test_file = res.id;
		},
		//设置过滤项
		setFilters(filters) {
			this.selectionFilters = filters;
			this.setFiltersVisible = true;
		},
		handlePrint(){
			let that = this;
			that.$nextTick(() => {
				that.printVisible = true;
			})
			// that.$API.wpm.mlog.list.req(that.params).then((res) => {
			// 	that.tableData = res;
			// });
		},
		//打印物料标签
		printMaterial(row){
			let that = this;
			that.$API.cm.labelmat.fromWm.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,
					]; 
				if(res.notok_sign!==null){
					str.push("WINTEXT 280,550,28,90,0,0,Simhei," +res.notok_sign_name,)
				}
				str.push("PRINT 1",)
				let obj = {};
				obj.printer_commands = str;
				obj.printer_name = that.printer_name;
				this.$API.wpm.prints.req(obj).then((response) => {});
			})
			
		},
	},
};
</script>

<style></style>
