<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<!-- <el-header>
					<el-segmented
						v-model="values"
						:options="options"
						size="default"
					></el-segmented>
				</el-header> -->
				<el-button
					type="primary"
					icon="el-icon-plus"
					@click="table_add(10)"
					v-auth="'mgroup.create'"
					>新增</el-button
				>
				<el-button
					type="primary"
					icon="el-icon-plus"
					@click="table_add(20)"
					v-auth="'mgroup.create'"
					>返工</el-button
				>
			</div>
			<div class="right-panel">
				<el-select
					v-model="searchType"
					placeholder="交接类型"
					clearable
					@change="searchTypeQuery"
					style="width: 150px"
				>
					<el-option
						v-for="item in typeOptions"
						:key="item.value"
						:label="item.name"
						:value="item.value"
					/>
				</el-select>
				<el-input
					style="width: 180px; margin-right: 5px"
					v-model="query.search"
					placeholder="批次号/关键字/id"
					clearable
					@keydown.enter="handleQuery"
				></el-input>
				<el-button
					type="primary"
					icon="el-icon-search"
					@click="handleQuery"
				></el-button>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable
				ref="table"
				:apiObj="apiObj"
				row-key="id"
				:params="params"
				:query="query"
			>
				<el-table-column
					label="#"
					type="index"
					width="50"
				></el-table-column>
				<el-table-column
					label="物料"
					prop="material_name"
					min-width="120"
				></el-table-column>
				<el-table-column label="批次" prop="batch" min-width="100">
				</el-table-column>
				<el-table-column label="数量" prop="count"></el-table-column>
				<el-table-column
					label="交送人"
					prop="send_user_name"
				></el-table-column>
				<el-table-column
					label="接收人"
					prop="recive_user_name"
				></el-table-column>
				<el-table-column
					label="交接日期"
					prop="send_date"
					width="120"
				></el-table-column>
				<el-table-column
					label="操作"
					fixed="right"
					align="center"
					width="140"
				>
					<template #default="scope">
						<el-button
							link
							size="small"
							v-if="scope.row.send_mgroup == mgroupId"
							@click="table_print(scope.row)"
							type="success"
							>打印</el-button
						>
						<el-button
							link
							size="small"
							@click="table_receive(scope.row)"
							type="success"
							v-if="
								scope.row.recive_mgroup == mgroupId &&
								scope.row.submit_time == null
							"
							>接收</el-button
						>
						<el-button
							link
							size="small"
							@click="table_edit(scope.row)"
							v-auth="'mgroup.update'"
							type="primary"
							v-if="
								scope.row.send_mgroup == mgroupId &&
								scope.row.submit_time == null
							"
							>编辑</el-button
						>
						<el-popconfirm
							title="确定删除吗？"
							@confirm="table_del(scope.row, scope.$index)"
						>
							<template #reference>
								<el-button
									link
									size="small"
									v-auth="'mgroup.delete'"
									type="danger"
									>删除</el-button
								>
							</template>
						</el-popconfirm>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>
	<save-dialog
		v-if="dialog.save"
		ref="saveDialog"
		:type="type"
		:mgroupId="mgroupId"
		@success="handleSaveSuccess"
		@closed="dialog.save = false"
	>
	</save-dialog>
	<el-dialog title="打印机设置" v-model="setNameVisible" width="600px">
		<el-form label-width="100px">
			<el-form-item label="打印机名称">
				<el-input v-model="printer_name"></el-input>
			</el-form-item>
		</el-form>
		<el-footer>
			<el-button type="primary" @click="savePrinter">保存</el-button>
		</el-footer>
	</el-dialog>
</template>
<script>
import saveDialog from "./handover_form.vue";
export default {
	props: {
		mgroupName: {
			type: String,
			default: "",
		},
	},
	name: "handover",
	components: {
		saveDialog,
	},
	data() {
		return {
			apiObj: null,

			dialog: {
				save: false,
				permission: false,
			},
			params: {
				mgroup: "",
			},
			query: {
				send_mgroup: "",
				recive_mgroup: "",
			},
			typeOptions: [
				{ name: "交送", value: "send" },
				{ name: "接收", value: "recive" },
			],
			searchType: "",
			options: ["交送", "接收"],
			tableData: [],
			selection: [],
			handoverItem: {},
			values: "交送",
			mtask: "",
			mlogId: "",
			processId: "",
			processCate: "",
			printer_name: "",
			setNameVisible: false,
		};
	},
	mounted() {
		let that = this;
		that.printer_name = localStorage.getItem("printer_name");
		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.processId = res[0].process;
				that.processCate = res[0].process_cate;
				that.params.mgroup = that.mgroupId;
				that.apiObj = that.$API.wpm.handover.list;
			});
	},
	methods: {
		handoverPrint() {
			let that = this;
			let str = [
				"SIZE 70 mm,90 mm",
				"GAP 2 mm,0 mm",
				"CLS",
				"WINTEXT 50,80,36,0,0,0,Simhei,光芯科技——交接单",
				'BARCODE 50,140,"128",108,0,0,3,5,"' +
					that.handoverItem.id +
					'"',
				"WINTEXT 50,280,36,0,0,0,Simhei," +
					that.handoverItem.material_name,
				"WINTEXT 50,340,36,0,0,0,Simhei,批次：" +
					that.handoverItem.batch,
				"WINTEXT 50,400,36,0,0,0,Simhei,数量：" +
					that.handoverItem.count,
				"WINTEXT 50,460,36,0,0,0,Simhei,日期：" +
					that.handoverItem.send_date,
				"WINTEXT 50,520,36,0,0,0,Simhei,送料：" +
					that.handoverItem.send_mgroup_name +
					"——" +
					that.handoverItem.send_user_name,
				"WINTEXT 50,580,36,0,0,0,Simhei,收料：" +
					that.handoverItem.recive_mgroup_name +
					"——" +
					that.handoverItem.recive_user_name,
				"PRINT 1",
			];
			// 送料：工段-姓名
			// 收料：工段-姓名
			let obj = {};
			obj.printer_commands = str;
			obj.printer_name = that.printer_name;
			this.$API.wpm.prints.req(obj).then((response) => {});
		},
		//添加
		table_add(type) {
			this.dialog.save = true;
			this.type = type;
			this.$nextTick(() => {
				this.$refs.saveDialog.open("add");
			});
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open("edit").setData(row);
			});
		},
		//删除
		async table_del(row) {
			var id = row.id;
			var res = await this.$API.mtm.mgroup.delete.req(id);
			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);
		},
		searchTypeQuery() {
			let that = this;
			if (that.searchType == "send") {
				that.query.send_mgroup = that.mgroupId;
				that.query.recive_mgroup = "";
			} else if (that.searchType == "recive") {
				that.query.recive_mgroup = that.mgroupId;
				that.query.send_mgroup = "";
			} else {
				that.query.send_mgroup = "";
				that.query.recive_mgroup = "";
			}
			console.log(that.query);
			this.$refs.table.queryData(that.query);
		},
		table_receive(row) {
			//接收
			let that = this;
			that.$API.wpm.handover.submit.req(row.id).then((res) => {
				that.$refs.table.queryData(that.query);
			});
		},
		table_print(row) {
			//打印
			let that = this;
			if (
				that.printer_name == null ||
				that.printer_name == "" ||
				that.printer_name == undefined
			) {
				this.setNameVisible = true;
			} else {
				that.$API.wpm.handover.item.req(row.id).then((res) => {
					that.handoverItem = res;
					that.handoverPrint();
				});
			}
		},
		savePrinter() {
			let that = this;
			localStorage.setItem("printer_name", that.printer_name);
			that.setNameVisible = false;
			that.$message.success("打印机设置成功，请重新进行打印操作。");
		},
		//本地更新数据
		//新增岗位后更新数据
		handleSaveSuccess(data, mode) {
			this.dialog.save = true;
			this.$refs.table.refresh();
		},
	},
};
</script>
<style scoped>
.treeMain {
	width: 100%;
	height: 280px;
	overflow: auto;
	border: 1px solid #dcdfe6;
	margin-bottom: 10px;
}
</style>
