<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button
					type="primary"
					icon="el-icon-plus"
					@click="table_add"
					v-auth="'mgroup.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="material_out_name"
					min-width="100"
				>
				</el-table-column>
				<el-table-column
					label="预计工时"
					prop="hour_work"
				></el-table-column>
				<el-table-column
					label="工段名称"
					prop="mgroup_name"
					min-width="100"
				></el-table-column>
				<el-table-column
					label="生产设备"
					prop="equipment_name"
					min-width="150"
				></el-table-column>
				<el-table-column
					label="所属部门"
					prop="belong_dept_name"
				></el-table-column>
				<el-table-column
					label="处理人"
					prop="handle_user_name"
				></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="create_time"
				></el-table-column>
				<el-table-column
					label="操作"
					fixed="right"
					align="center"
					width="180"
				>
					<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-divider
							direction="vertical"
							v-if="scope.row.submit_time == null"
						></el-divider>
						<el-button
							link
							size="small"
							@click="table_detail(scope.row)"
							type="primary"
							>详情</el-button
						>
						<el-divider direction="vertical"></el-divider>

						<el-button
							link
							size="small"
							v-auth="'mgroup.delete'"
							type="danger"
							@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"
			@success="handleSaveSuccess"
			@closed="dialog.save = false"
		>
		</save-dialog>
		<detail-drawer
			v-if="dialog.detail"
			ref="detailDialog"
			:mlogId="mlogId"
			:mtask="mtask"
			@closed="dialog.detail = false"
		>
		</detail-drawer>
	</el-container>
</template>
<script>
import saveDialog from "./mlog_form.vue";
import detailDrawer from "./mlog_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) => {
				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.mlog.list;
			});
	},
	methods: {
		//添加
		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.mlogId = 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.mlog.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>
