<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.route_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="日志列表">
				<div style="display: flex;">
					<el-button
						type="primary"
						v-if="fmlogItem.submit_time == null"
						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>
				<scTable
					stripe
					ref="table"
					:apiObj="apiObj"
					:params="params"
					row-key="id"
				>
					<el-table-column  type="index" width="50"></el-table-column>
					<el-table-column label="批次号" prop="batch" min-width="100"></el-table-column>
					<el-table-column label="操作员"  min-width="80">
						<template #default="scope">
							<div v-if="scope.row.handle_users_&&scope.row.handle_users_.length>0">
								<span v-for="item in scope.row.handle_users_" :key="item">{{ item.name }}、</span>
							</div>
							<span v-else>{{ scope.row.handle_user_name }}</span>
						</template>
					</el-table-column>
					<el-table-column label="班组" prop="team_name" min-width="80"></el-table-column>
					<el-table-column label="设备" prop="equipment_name" min-width="140"></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="handle_date"></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>
			<save-dialog
				v-if="dialog.save"
				ref="saveDialogs"
				:mlogId="mlogId"
				:fmlog="fmlogId"
				:mgroup="mgroup"
				:route="route"
				:is_fix="is_fix"
				: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: "",
		},
		mgroup: {
		    type: String,
			default: "",
		},
		mtask: {
			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: {
				fmlog: "",
			},
			fmlogItem: {},
			saveInForm: {
				count_use: 0,
				count_pn_jgqbl: 0,
			},
			oinfo_json:{
				退火炉号:'',
				退火时间:'',
				退火温度:'',
				抽检结果:'',
				抽检数:'',
				强度落球:'',
			},
			codeText:"",
			fileList:[],
			tableData:[],
			batchContains:[],
			test_file:'',
			deptId: "",
			route_code: "",
			is_fix:false,
			visible: false,
			isSaveing: false,
			printVisible:false,
			options: [],
			saveInDialog: false,
			ticketDialog:false,
			setFiltersVisible: false,
			rules: {
				count_use: [
					{
						required: true,
						message: "请输入领取数量",
						trigger: "blur",
					},
				],
			},
		};
	},
	mounted() {
		let that = this;
		that.route_code = that.$route.path.split("/")[2];
		this.getfmlogItem();
		this.params.fmlog = this.fmlogId;
		this.paramsWm.mgroup = this.mgroup;
		this.apiObj = this.$API.wpm.mlog.list;
	},
	methods: {
		open(codeText="") {
			this.codeText = codeText;
			if(this.codeText!=""){
				this.$nextTick(() => {
				    this.codeTextChange(codeText);
				})
			}
			this.visible = true;
		},
		
		getfmlogItem() {
			let that = this;
			that.$API.wpm.fmlog.item.req(that.fmlogId).then((res) => {
				that.fmlogItem = res;
				that.route = res.route;
				that.is_fix = res.is_fix;
				that.material_in = that.material_in;
				that.material_out = that.material_out;
				that.paramsWm.mgroup = res.mgroup;
				that.apiObjWm = that.$API.wpm.wmaterial.list;
			});
		},
		getMlogs(){
			let that = this;
			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;
				}else{}
			});
		},
		//表单注入数据
		setData(data) {
			Object.assign(this.form, data);
			this.getRoute(data.id);
		},
		table_add() {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialogs.open('add');
			});
		},
		codeTextChange(codeText){
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialogs.open('add',codeText);
			});
		},
		table_edit(row){
			this.mlogId = row.id;
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialogs.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;
				that.$refs.table.refresh();
				that.$message.success("操作成功");
			});
		},
		//日志撤回
		mlogRevert(row) {
			let that = this;
			that.$confirm(`确定撤回该日志吗？`, "提示", {
				type: "warning",
			}).then(() => {
				var id = row.id;
				that.$API.wpm.mlog.revert.req(id).then((res) => {
					if (res.err_msg) {
						that.$message.error(res.err_msg);
					} else {
						that.$refs.table.refresh();
						that.$message.success("撤回成功");
					}
				});
			});
		},
		//日志删除
		table_del(row) {
			let that = this;
			that.$confirm(`确定删除该日志吗？`, "提示", {
				type: "warning",
			}).then(() => {
				var id = row.id;
				that.$API.wpm.mlog.delete.req(id).then((res) => {
					if (res.err_msg) {
						that.$message.error(res.err_msg);
					} else {
						that.$refs.table.refresh();
						that.$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;
			})
		},
		//打签
		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;
				let printer_ip=localStorage.getItem("printer_ip")||'127.0.0.1';
				this.$API.wpm.prints.req(printer_ip, obj).then((response) => {});
			})
			
		},
	},
};
</script>

<style></style>
