<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-descriptions>
					<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.handle_user_name
					}}</el-descriptions-item>
					<el-descriptions-item label="开始时间">{{
						mlogItem.work_start_time
					}}</el-descriptions-item>
					<el-descriptions-item label="创建时间">{{
						mlogItem.create_time
					}}</el-descriptions-item>
				</el-descriptions>
				<div style="padding: 5px 10px;display: flex;justify-content: end;">
					<el-button
						type="primary"
						v-if="mlogItem.submit_time == null"
						@click="mlogUpdate"
						style="margin-right: 10px;"
						v-auth="'mlog.update'"
					>
						编辑
					</el-button>
					<div v-if="mlogItem.submit_time == null">
						<!-- 退火-->
						<el-button
							type="primary"
							v-if="mlogItem.mgroup_name=='退火'"
							:loading="isSaveing"
							@click="createTicket"
						>放行审批</el-button>
						<el-button
							type="primary"
							v-else
							:loading="isSaveing"
							@click="mlogSubmit"
							>提交</el-button
						>
					</div>
				</div>
			</el-card>
			<!-- 物料消耗 -->
			<el-card
				style="width: 100%; margin: 1vh 0"
				header="物料消耗"
				shadow="never"
			>
				<div>
					<el-button
						type="primary"
						v-if="mlogItem.submit_time == null"
						icon="el-icon-plus"
						@click="table_add"
						>新增</el-button
					>
				</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">
					</el-table-column>
					<el-table-column label="领用数量" prop="count_use">
					</el-table-column>
					<el-table-column label="加工前不良" prop="count_n_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"
							>
								编辑
							</el-button>
							<el-button
								link
								:disabled="mlogItem.submit_time !== null"
								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 :span="23">
							<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 :span="23">
							<el-form-item label="加工前不良" prop="count_n_jgqbl">
								<el-input-number
									v-model="saveInForm.count_n_jgqbl"
									:min="0"
									style="width: 100%"
									controls-position="right"
								/>
							</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">
								<el-descriptions :column="4">
									<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_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="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"
								@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"
				:materialIn="materialIn"
				@success="handleSaveSuccess"
				@closed="dialog.save = false"
			>
			</save-dialog>
			<check-dialog
				v-if="dialog.check"
				ref="checkDialog"
				@success="handleCheckSuccess"
				@closed="dialog.check = false"
			>
			</check-dialog>
			<edit-dialog
				v-if="dialog.edit"
				ref="editDialog"
				:dept="deptId"
				@success="handleEditSuccess"
				@closed="dialog.edit = false"
			>
			</edit-dialog>
			<!-- 退火放行通知单 -->
			<el-dialog v-model="ticketDialog" title="退火放行通知单">
				<el-form
					:model="saveInForm"
					:rules="rules"
					label-width="100px"
					ref="saveInForm"
				>
					<el-row>
						<el-col :md="12" :sm="24">
							<el-form-item label="退火炉号">
								<el-input v-model="oinfo_json.退火炉号"/>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="退火时间">
								<el-date-picker
									type="datetime"
									v-model="oinfo_json.退火时间"
									style="width: 100%"
									value-format="YYYY-MM-DD HH:mm"
									format="YYYY-MM-DD HH:mm"
								></el-date-picker>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="退火温度">
								<el-input-number
									v-model="oinfo_json.退火温度"
									:min="1"
									style="width: 100%"
									controls-position="right"
								/>
								<span style="position: absolute;right: -17px;">°C</span>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="抽检结果">
								<el-input v-model="oinfo_json.抽检结果"/>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="抽检数">
								<el-input-number
									v-model="oinfo_json.抽检数"
									:precision="0"
									:min="1"
									style="width: 100%"
									controls-position="right"
								/>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="强度落球">
								<el-input-number
									v-model="oinfo_json.强度落球"
									:min="0"
									style="width: 100%"
									controls-position="right"
								/>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-form-item label="放行标准">：退火后直径23.5±0.2；黑圈内径：5.9±0.1；外径7.6-7.77</el-form-item>
					</el-row>
				</el-form>
				<template #footer>
					<el-button
						v-for="item in initForm.transitions"
						:key="item.id"
						type="primary"
						:loading="isSaveing"
						:disabled="isSaveing"
						@click="submitTicketCreate(item.id)"
						style="margin-right: 4px"
						>{{ item.name }}</el-button
					>
				</template>
			</el-dialog>
		</div>
	</el-drawer>
</template>
<script>
import editDialog from "./mlog_form.vue";
import saveDialog from "./mlogb_form.vue";
import checkDialog from "./mlogb_check.vue";
export default {
	props: {
		mlogId: {
			type: String,
			default: "",
		},
	},
	components: {
		editDialog,
		saveDialog,
		checkDialog,
	},
	emits: ["success", "closed"],
	data() {
		return {
			loading: false,
			//表单数据
			form: {},
			dialog: {
				save: false,
				edit: false,
				check: false,
			},
			apiObj: null,
			paramsIn: {
				page: 0,
				mlog: "",
				material_in__isnull: 0,
			},
			paramsOut: {
				page: 0,
				mlog: "",
				material_out__isnull: 0,
			},
			mgroup: "",
			mlogItem: {},
			saveInForm: {
				count_use: 0,
				count_n_jgqbl: 0,
			},
			oinfo_json:{
				退火炉号:'',
				退火时间:'',
				退火温度:'',
				抽检结果:'',
				抽检数:'',
				强度落球:'',
			},
			deptId: "",
			visible: false,
			isSaveing: false,
			options: [],
			saveInDialog: false,
			ticketDialog:false,
			setFiltersVisible: false,
			rules: {
				count_use: [
					{
						required: true,
						message: "请输入领取数量",
						trigger: "blur",
					},
				],
			},
		};
	},
	mounted() {
		this.getMlogItem();
		this.paramsIn.mlog = this.mlogId;
		this.paramsOut.mlog = this.mlogId;
		this.apiObj = this.$API.wpm.mlogb.list;
		this.getInit();
	},
	methods: {
		open() {
			this.visible = true;
		},
		
		getMlogItem() {
			let that = this;
			that.$API.wpm.mlog.item.req(that.mlogId).then((res) => {
				that.mlogItem = res;
				that.mgroup = res.mgroup;
				that.materialIn = res.material_in;
				that.deptId = res.belong_dept;
			});
		},
		//表单注入数据
		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);
			});
		},
		// mlogUpdate(data) {
		// 	this.$refs.editDialog.open().setData(data);
		// },
		table_add() {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open();
			});
		},
		//自检
		table_in_edit(row) {
			this.saveInForm = row;
			this.saveInDialog = true;
		},
		//提交自检
		saveInSubmit() {
			let that = this;
			that.$refs.saveInForm.validate(async (valid) => {
				if (valid) {
					that.isSaveing = true;
					let obj = {};
					obj.count_use = that.saveInForm.count_use;
					obj.count_n_jgqbl = that.saveInForm.count_n_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) {
			this.dialog.check = true;
			this.$nextTick(() => {
				this.$refs.checkDialog.open(row);
			});
		},

		//表单提交方法
		mlogSubmit() {
			let that = this;
			that.$API.wpm.mlog.submit.req(that.mlogItem.id).then((res) => {
				that.isSaveing = false;
				that.visible = false;
				that.$message.success("操作成功");
			});
		},
		//渲染工单提交按钮
		getInit() {
			let that = this;
			that.$API.wf.workflow.initkey.req("	backfire").then((res) => {
				that.initForm = res;
			});
		},
		//填写退火放行单信息
		createTicket(){
			this.ticketDialog = true;
			this.oinfo_json.退火炉号 = this.mlogItem.equipment_name.split("|")[0];
		},
		//提交退火放行单信息,创建工单
		submitTicketCreate(id) {
			let that = this;
			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.$message.success("提交成功");
			}).catch((e) => {
				that.isSaveing = false;
			});
		},
		handleSaveSuccess() {
			this.$refs.tableIn.refresh();
			this.$refs.tableOut.refresh();
		},
		handleCheckSuccess() {
			this.$refs.tableOut.refresh();
		},
		handleEditSuccess() {},
		//设置过滤项
		setFilters(filters) {
			this.selectionFilters = filters;
			this.setFiltersVisible = true;
		},
	},
};
</script>

<style></style>
