<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
				>
				<el-button
					type="primary"
					icon="el-icon-plus"
					@click="table_rework"
					v-auth="'mlog.create'"
					>返工</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>
			<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"
			>
				<el-table-column label="#" type="index" width="50"></el-table-column>
				<el-table-column label="工艺路线" prop="route_name" :min-width="100">
				</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="启用" width="80">
					<template #default="scope">
						<el-switch v-model="scope.row.enabled" @change="enabledChange(scope.row)"></el-switch>
					</template>
				</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>
		<!-- 关联任务选择 -->
		<el-dialog v-model="dialog.mtask" title="关联任务">
			<el-form
				label-width="100px"
				ref="saveInForm"
			>
				<el-form-item label="关联任务" prop="mtask">
					<el-select
						v-model="mtaskId"
						placeholder="关联任务"
						clearable
						style="width: 100%"
						@change="changeMtask"
					>
						<el-option
							v-for="item in mtaskoptions"
							:key="item.id"
							:label="item.number"
							:value="item.id"
						>
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<template #footer>
				<el-button
					type="primary"
					:loading="isSaveing"
					@click="mtaskSure()"
					>确定</el-button
				>
			</template>
		</el-dialog>
		<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"
			:mtask="mtaskId"
			:mgroup="mgroupId"
			:fmlogId="fmlogId"
			:processId="processId"
			@closed="detailClose"
		>
		</detail-drawer>
	</el-container>
</template>
<script>
import saveDialog from "./fmlog_form.vue";
import detailDrawer from "./fmlog_detail.vue";

export default {
	props: {
		deptId: {
			type: String,
			default: "",
		},
		mgroupId: {
			type: String,
			default: "",
		},
		mgroupName: {
			type: String,
			default: "",
		},
		processId: {
			type: String,
			default: "",
		},
		mgroupcode: {
			type: String,
			default: "",
		},
	},
	name: "mlog",
	components: {
		saveDialog,
		detailDrawer,
	},
	data() {
		return {
			apiObj: null,
			params: { mgroup: "" },
			query: {},
			dialog: {
				save: false,
				detail: false,
				mtask:false,
			},

			tableData: [],
			selection: [],
			mtaskoptions:[],
			mtask: "",
			mlogId: "",
			fmlogId: "",
			codeText: "",
			mtaskId:"",
		};
	},
	mounted() {
		let that = this;
		that.params.mgroup = that.mgroupId;
		that.apiObj = that.$API.wpm.fmlog.list;
	},
	methods: {
		enabledChange(row){
			this.$API.wpm.fmlog.toggleenabled.req(row.id).then(res=>{
				// this.$refs.table.refresh();
			})
		},
		detailClose() {
			this.dialog.detail = false;
			this.$refs.table.refresh();
		},
		//添加日志
		table_add() {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open("add");
			});
		},
		table_rework(){
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open("add","rework");
			});
		},
		//编辑日志
		table_edit(row) {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open("edit").setData(row);
			});
		},
		//日志详情
		table_detail(row) {
			this.mtaskId = row.mtask;
			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("删除成功");
					}
				});
			});
		},
		//日志页面扫描物料二维码信息mlog
		codeTextChange(codeText){
			let that = this;
			that.codeText = codeText;
			let id = codeText.split('#')[1];
			that.$API.cm.labelmat.item.req(id).then((res) => {
				let params = {}
				params.page = 0;
				params.state = 20;
				params.mgroup = that.mgroupId;
				params.route__material_in  = res.material;
				that.$API.pm.mtask.list.req(params).then(res1=>{
					if(res1.length==1){//只有一个任务，直接打开日志详情
						that.mtaskId =res1[0].id;
						that.changeMtask();
					}else{//多个任务时，弹出任务选择框
						that.mtaskoptions = res1;
						that.dialog.mtask = true;
					}
				})
			}).catch((err) => {})	
		},
		changeMtask(){
			let that = this;
			that.dialog.mtask = false;
			let params = {}
			params.page = 0;
			params.mtask =that.mtaskId;
			params.mgroup = that.mgroupId;
			//根据任务获取日志
			that.$API.wpm.fmlog.list.req(params).then(res=>{
				that.fmlogId = res[0].id;
				that.dialog.detail = true;
				that.$nextTick(() => {
					that.$refs.detailDialog.open(that.codeText);
				});
			})
		},
		//表格选择后回调事件
		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>
