<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'"
					v-if="mgroup_name!=='切片'&&mgroup_name!=='拉单丝'&&mgroup_name!=='一次复丝'&&mgroup_name!=='二次复丝'&&mgroup_name!=='切丝排版'"
					>返工</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="mgroup_name=='一次超洗'||mgroup_name=='二次超洗'"
					>检验</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" width="100">
					<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="mgroup_name=='size'||mgroup_name=='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||deptId==scope.row.send_dept"
							@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||deptId==scope.row.send_dept)&&
								scope.row.submit_time == null
							"
							>编辑</el-button
						>
						<el-popconfirm
							title="确定删除吗？"
							v-if="
							(scope.row.send_mgroup == mgroupId||deptId==scope.row.send_dept)&&
								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="mgroup_name"
		: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,
			mgroup_name:'',
		};
	},
	watch: {
		mgroupName: {
			handler: function (newval,odlval) {
				let that = this;
				that.params.dept = "";
				that.params.mgroup = "";
				that.apiObj = null;
				that.mgroup_name  = newval;
				that.getMgroupInfo();
			},
		},
	},
	mounted() {
		this.getMgroupInfo();
	},
	methods: {
		getMgroupInfo(){
			let that = this;
			console.log('that.mgroupName',that.mgroupName);
			that.printer_name = localStorage.getItem("printer_name");
			that.$API.mtm.mgroup.list.req({ page: 0, name: 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;
				that.$refs.table.refresh();
			});
		},
		printSetting(){
			this.setNameVisible = true;
		},
		//添加
		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.mgroup_name=="size"||that.mgroup_name=="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.mgroup_name=="size"||that.mgroup_name=="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;
		},
		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>
