<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button
					type="primary"
					icon="el-icon-plus"
					@click="table_add"
					v-auth="'mlog.create'"
					>新增</el-button
				>
			</div>
			<div class="right-panel">
				<el-input
					style="margin-right: 5px"
					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>
			<scTable
				ref="table"
				:apiObj="apiObj"
				row-key="id"
				:params="params"
				:query="params"
				@row-click="table_detail"
			>
				<el-table-column label="#" type="index" width="50"></el-table-column>
				<el-table-column label="工艺路线" prop="routepack_name">
				</el-table-column>
				<el-table-column label="任务编号" prop="mtask_number">
				</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="create_time"></el-table-column>
				<el-table-column label="备注" prop="note"></el-table-column>
				<el-table-column
					label="操作"
					fixed="right"
					align="center"
					width="150"
				>
					<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"
							@click="table_detail(scope.row)"
							type="primary"
							>详情</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, scope.$index)"
							>删除</el-button
						>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
		<save-dialog
			v-if="dialog.save"
			ref="saveDialog"
			:process="processId"
			:mgroup="mgroupId"
			:dept="deptId"
			:mgroupName = "mgroupName"
			@success="handleSaveSuccess"
			@closed="dialog.save = false"
		>
		</save-dialog>
		<detail-drawer
			v-if="dialog.detail"
			ref="detailDialog"
			:fmlogId="fmlogId"
			@closed="detailClose"
		>
		</detail-drawer>
	</el-container>
</template>
<script>
import saveDialog from "./fmlog_form.vue";
import detailDrawer from "./fmlog_detail.vue";

export default {
	props: {
		mgroupName: {
			type: String,
			default: "",
		},
	},
	name: "mlog",
	components: {
		saveDialog,
		detailDrawer,
	},
	data() {
		return {
			apiObj: null,
			params: { mgroup: "" },
			query: {},
			dialog: {
				save: false,
				detail: false,
			},
			tableData: [],
			selection: [],
			mtask: "",
			mlogId: "",
			deptId: null,
			processId: "",
			processCate: "",
		};
	},
	mounted() {
		let that = this;
		that.$API.mtm.mgroup.list
			.req({ page: 0, search: that.mgroupName })
			.then((res) => {
				if (res.length < 1) {
					that.$message.error("获取工段错误");
					return;
				}
				that.mgroupId = res[0].id;
				that.deptId = res[0].belong_dept;
				that.processId = res[0].process;
				that.processCate = res[0].process_cate;
				that.params.mgroup = res[0].id;
				that.apiObj = that.$API.wpm.fmlog.list;
			});
	},
	methods: {
		detailClose() {
			this.dialog.detail = false;
			this.$refs.table.refresh();
		},
		//添加日志
		table_add() {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open("add");
			});
		},
		//编辑日志
		table_edit(row) {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open("edit").setData(row);
			});
		},
		//日志详情
		table_detail(row) {
			this.fmlogId = row.id;
			this.dialog.detail = true;
			this.$nextTick(() => {
				this.$refs.detailDialog.open();
			});
		},
		//日志删除
		table_del(row) {
			this.$confirm(`确定删除吗？`, "提示", {
				type: "warning",
			}).then(() => {
				var id = row.id;
				this.$API.wpm.fmlog.delete.req(id).then((res) => {
					if (res.err_msg) {
						this.$message.error(res.err_msg);
					} else {
						this.$refs.table.refresh();
						this.$message.success("删除成功");
					}
				});
			});
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		handleQuery() {
			this.$refs.table.queryData(this.query);
		},
		//本地更新数据
		//新增岗位后更新数据
		handleSaveSuccess(data, mode) {
			this.dialog.save = true;
			this.$refs.table.refresh();
		},
	},
};
</script>
<style scoped></style>
