<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button
					type="primary"
					icon="el-icon-plus"
					@click="table_add(10)"
					v-auth="'handover.create'"
					>新增</el-button
				>
				<el-button
					type="primary"
					icon="el-icon-plus"
					@click="table_add(20)"
					v-auth="'handover.create'"
					>返工</el-button
				>
				<el-button
					type="primary"
					@click="printSetting"
					>打印机</el-button
				>
				<el-button
					type="primary"
					icon="el-icon-plus"
					@click="table_add(30)"
					v-auth="'handover.create'"
					v-if="mgroupName=='一次超洗'||mgroupName=='二次超洗'"
					>检验</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 type="expand">
						<template #default="props">
							<div style="padding-left: 50px">
								<template v-for="item in props.row.handoverb" :key="item.id">
									<el-descriptions :column="3">
										<el-descriptions-item label="批次">
											{{item.batch}}
										</el-descriptions-item>
										<el-descriptions-item label="数量">
											{{item.count}}
										</el-descriptions-item>
										<el-descriptions-item label="不合格标记" v-if="item.notok_sign_name!==null">
											{{item.notok_sign_name}}
										</el-descriptions-item>
									</el-descriptions>
								</template>
							</div>
						</template>
					</el-table-column>
				<el-table-column
					label="物料"
					prop="material_name"
					min-width="140"
				></el-table-column>
				<el-table-column label="批次" prop="batch" min-width="140">
					<template #default="scope">
						<el-text v-if="scope.row.handoverb.length>0" type="primary">{{scope.row.handoverb.length}}批</el-text>
					</template>
				</el-table-column>
				<el-table-column label="数量" prop="count" width="80"></el-table-column>
				<el-table-column label="交接类型" prop="type" width="100">
					<template #default="scope" v-if="mgroupName=='size'||mgroupName=='facade'">
						<el-text v-if="scope.row.send_dept == deptId" type="primary">交送</el-text>
						<el-text v-if="scope.row.recive_dept == deptId" type="success">接收</el-text>
					</template>
					<template #default="scope" v-else>
						<el-text v-if="scope.row.send_mgroup == mgroupId" type="primary">交送</el-text>
						<el-text v-if="scope.row.recive_mgroup == mgroupId" type="success">接收</el-text>
					</template>
				</el-table-column>
				<el-table-column label="交接类别" prop="type" width="80">
					<template #default="scope">
						<el-tag v-if="scope.row.type == 10" type="success"
							>正常</el-tag
						>
						<el-tag v-if="scope.row.type == 20" type="warning"
							>返工</el-tag
						>
						<el-tag v-if="scope.row.type == 30" type="primary"
							>检验</el-tag
						>
						<el-tag v-if="scope.row.type == 40" type="danger"
							>报废</el-tag
						>
					</template>
				</el-table-column>
				<el-table-column
					label="交送人"
					prop="send_user_name"
					width="80"
				></el-table-column>
				<el-table-column
					label="接收人"
					prop="recive_user_name"
					width="80"
				></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.recive_dept == deptId)&&scope.row.submit_time == null"
						>接收</el-button>
						<el-button
							link
							size="small"
							@click="table_show(scope.row)"
							type="success"
						>查看</el-button>
						<el-button
							link
							size="small"
							@click="table_edit(scope.row)"
							v-auth="'handover.update'"
							type="primary"
							v-if="
								scope.row.send_mgroup == mgroupId &&
								scope.row.submit_time == null
							"
							>编辑</el-button
						>
						<el-popconfirm
							title="确定删除吗？"
							v-if="
								scope.row.send_mgroup == mgroupId &&
								scope.row.submit_time == null
							"
							@confirm="table_del(scope.row, scope.$index)"
						>
							<template #reference>
								<el-button
									link
									size="small"
									v-auth="'handover.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"
		:mgroupName="mgroupName"
		: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>
	<el-dialog v-model="printVisible" width="1200px">
		<print :baseData="handoverItem" :tableData="handoverItem.handoverb" :type="'handover'"  @closePrint="printVisible=false"/>
	</el-dialog>
</template>
<script>
import saveDialog from "./handover_form.vue";
import print from "./../setting/print/A4.vue";
export default {
	props: {
		mgroupName: {
			type: String,
			default: "",
		},
	},
	name: "handover",
	components: {
		saveDialog,print
	},
	data() {
		return {
			apiObj: null,

			dialog: {
				save: false,
				permission: false,
			},
			params: {
				// count_xtest__gte: 0,
			},
			query: {
				send_mgroup: "",
				recive_mgroup: "",
			},
			typeOptions: [
				{ name: "交送", value: "send" },
				{ name: "接收", value: "recive" },
			],
			searchType: "",
			options: ["交送", "接收"],
			tableData: [],
			selection: [],
			handoverItem:{},
			values: "交送",
			deptId:'',
			mtask: "",
			mlogId: "",
			mgroupId: "",
			processId: "",
			processCate: "",
			printer_name: "",
			printVisible:false,
			setNameVisible: false,
			testCode: "mat:3689278065261125632",
		};
	},
	mounted() {
		let that = this;
		console.log('that.mgroupName',that.mgroupName);
		if(that.mgroupName=="size"){//尺寸检验
			// that.params.material__process__name="一次超洗";
			that.$API.system.dept.list.req({name__contains:'尺寸',page:0}).then((res) => {
			    if(res.length>0){
					that.deptId = res[0].id;
					that.params.dept=res[0].id;
					console.log('that.deptId',that.deptId);
				}
				that.apiObj = that.$API.wpm.handover.list;
			})
		}else if(that.mgroupName=="facade"){//外观检验
			// that.params.material__process__name="二次超洗";
			that.$API.system.dept.list.req({name__contains:'外观',page:0}).then((res) => {
			    if(res.length>0){
					that.deptId = res[0].id;
					that.params.dept=res[0].id;
					console.log('that.deptId',that.deptId);
				}
				that.apiObj = that.$API.wpm.handover.list;
			})
		}else{//工段交接
			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.$TOOL.data.set('gx_deptID',res[0].belong_dept)
				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: {
		printSetting(){
			this.setNameVisible = true;
		},
		handoverPrint(row) {
			let that = this;
			let str = [
				"SIZE 60 mm,90 mm",
				"GAP 0 mm,0 mm",
				"CLS",
				'QRCODE 0,60,H,5,A,0,"' +that.testCode +'"',
				"WINTEXT 0,200,36,0,0,0,Simhei," +row.material_name,
				"WINTEXT 0,240,36,0,0,0,Simhei,批次：" +
					row.batch,
				]; 
			if(row.wm_notok_sign!==null){
				str.push("WINTEXT 0,280,36,0,0,0,Simhei,不合格原因：" +
						row.wm_notok_sign,)
			}
			//供应商----原料、辅料
			// if(row.type==30||row.type==40){
			// 	str.push("WINTEXT 50,460,36,0,0,0,Simhei,供应商：" +
			// 			row.supplier_name,)
			// }
			str.push("PRINT 1",)
			// let str = [
			// 	"SIZE 60 mm,90 mm",
			// 	"GAP 0 mm,0 mm",
			// 	"CLS",
			// 	"WINTEXT 0,10,36,0,0,0,Simhei," +row.material_name,
			// 	'QRCODE 0,60,H,5,A,0,"' +that.testCode +'"',
			// 	"WINTEXT 50,340,36,0,0,0,Simhei,批次：" +
			// 		row.batch,
			// 	"WINTEXT 50,400,36,0,0,0,Simhei,不合格原因：" +
			// 		row.wm_notok_sign,
			// 	"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.type=row.type;
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open("edit").setData(row);
			});
		},
		table_show(row){
			this.type=row.type;
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open("show").setData(row);
			});
		},
		//删除
		async table_del(row) {
			var id = row.id;
			var res = await this.$API.wpm.handover.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") {
				if(that.mgroupName=="size"||that.mgroupName=="facade"){
					that.query.send_dept = that.deptId;
					that.query.recive_dept = "";
				}else{
					that.query.send_mgroup = that.mgroupId;
					that.query.recive_mgroup = "";
				}
			} else if (that.searchType == "recive") {
				if(that.mgroupName=="size"||that.mgroupName=="facade"){
					that.query.send_dept = "";
					that.query.recive_dept =that.deptId;
				}else{
					that.query.recive_mgroup = that.mgroupId;
					that.query.send_mgroup = "";
				}
			} else {
				that.query.send_mgroup = "";
				that.query.recive_mgroup = "";
				that.query.recive_dept = "";
				that.query.send_dept = "";
			}
			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;
			that.handoverItem = row;
			that.printVisible = true;
			//打印
			// 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(row);
			// 	});
			// }
		},
		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>
