<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="mgroupName!=='切片'"
				>返工</el-button>
				<scScanner :labeltext="'交接扫码'" @scanResult="codeTextChange10"></scScanner>
				<scScanner :labeltext="'返工扫码'" @scanResult="codeTextChange20"></scScanner>
				<el-input 
					ref="codeInput"
					v-model="codeText"
					clearable
					style="width: 150px;margin: 0 4px;"
					placeholder="正常交接"
					@keyup.enter="codeTextChange10(codeText)"
				></el-input>
				<el-input 
					ref="codeInput"
					v-model="codeText2"
					clearable
					placeholder="返工交接"
					style="width: 150px;"
					@keyup.enter="codeTextChange20(codeText2)"
				></el-input>
			</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.defect_name!==null&&item.defect_name!==undefind">
											{{item.defect_name}}
										</el-descriptions-item>
									</el-descriptions>
								</template>
							</div>
						</template>
					</el-table-column>
				<el-table-column
					label="物料"
					prop="material_name"
					min-width="120"
				></el-table-column>
				<el-table-column label="数量" prop="count" width="80"></el-table-column>
				<el-table-column label="批次" prop="batch" width="60">
					<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="new_batch" min-width="80">
				</el-table-column>
				
				<el-table-column label="交接类型" prop="type" width="100">
					<template #default="scope">
						<el-text v-if="scope.row.mtype==20" type="warning">拆批</el-text>
						<el-text v-else-if="scope.row.mtype==30" type="warning">合批</el-text>
						<template 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>
					</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
						>
						<el-tag v-if="scope.row.type == 60" type="warning"
							>退料</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="note"
					width="80"
				></el-table-column>
				<el-table-column
					label="交接日期"
					prop="send_date"
					width="100"
				></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="primary"
							v-if="scope.row.recive_mgroup == mgroupId&&scope.row.submit_time == null"
						>接收</el-button>
						<el-button
							link
							size="small"
							@click="table_reBack(scope.row)"
							type="danger"
							v-if="scope.row.recive_mgroup == mgroupId&&scope.row.submit_time!== null&&(scope.row.type == 10||scope.row.type == 20)&&scope.row.mtype == 10"
						>退回</el-button>
						<el-button
							link
							size="small"
							@click="table_show(scope.row)"
							type="success"
						>查看</el-button>
						<el-popconfirm
							title="确定撤消吗？"
							v-if="scope.row.submit_time!== null&&(scope.row.type == 10||scope.row.type == 20)"
							@confirm="table_reBack2(scope.row)"
						>
							<template #reference>
								<el-button type="danger" link size="small">撤消</el-button>
							</template>
						</el-popconfirm>
						<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&&scope.row.mtype == 10
							"
							>编辑</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"
		:process = "processId"
		:processtype = "processtype"
		:mgroupName="mgroupName"
		:mgroupId="mgroupId"
		@success="handleSaveSuccess"
		@closed="dialog.save = false"
	>
	</save-dialog>
	<el-dialog title="退料操作" v-model="backDialogVisible" style="width: 70%;">
		<el-row v-for="item in handoverbList" :key="item.id" style="padding: 10px 10px 0 20px;">
			<el-col style="display: flex;border-bottom: 1px dashed #eee;">
				<el-checkbox v-model="item.checked" :label="item.batch"/>
				<el-form-item label="退料数量" label-width="150px" style="margin-bottom: 10px;">
					<el-input-number v-model="item.count" :min="0" controls-position="right"/>
				</el-form-item>
			</el-col>
		</el-row>
		<el-form ref="backForm" label-width="100px" :rules="rules" :model="form" style="margin-top: 15px;">
			<el-form-item label="接收工段" prop="recive_mgroup">
				<el-select
					v-model="form.recive_mgroup"
					placeholder="接收工段"
					clearable
					filterable
					style="width: 100%"
				>
					<el-option
						v-for="item in mgroupOptions"
						:key="item.id"
						:label="item.name"
						:value="item.id"
					></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="退料原因">
				<el-input v-model="form.note"></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: {
		deptId: {
			type: String,
			default: "",
		},
		mgroupId: {
			type: String,
			default: "",
		},
		mgroupName: {
			type: String,
			default: "",
		},
		processId: {
			type: String,
			default: "",
		},
		mgroupcode: {
			type: String,
			default: "",
		},
		processtype:{
			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: [],
			mgroupOptions:[],
			handoverbList:[],
			handoverItem:{},
			values: "交送",
			mtask: "",
			mlogId: "",
			codeText:"",
			codeText2:"",
			printer_name: "",
			printVisible:false,
			backDialogVisible:false,
			form: { },
			rules: {
				recive_mgroup: [
					{ required: true, message: "请选择接收工段", trigger: "blur" },
				],
			},
			// setNameVisible: false,
		};
	},
	mounted() {
		let that = this;
		that.printer_name = localStorage.getItem("printer_name");
		that.$TOOL.data.set('gx_deptID',that.deptId)
		that.params.mgroup = that.mgroupId;
		that.apiObj = that.$API.wpm.handover.list;
		that.$refs.table.refresh();
		that.getMgroupOptions();
	},
	methods: {
		//获取工段列表
		getMgroupOptions() {
			let that = this;
			that.$API.mtm.mgroup.list.req({ page: 0}).then((res) => {
				that.mgroupOptions = res;
				if(that.type==40){
					res.forEach(item=>{
						if(item.name=="废品库"){
							that.form.recive_mgroup = item.id;
						}
					})
					that.getCkUserList();//废品库接收人
				}
			});
		},
		//添加
		table_add(type) {
			this.dialog.save = true;
			this.type = type;
			this.$nextTick(() => {
				this.$refs.saveDialog.open("add",'',10);
			});
		},
		codeTextChange10(codeText){
			let that = this;
			that.type = 10;
			that.codeText = codeText;
			that.dialog.save = true;
			that.$nextTick(() => {
				that.$refs.saveDialog.open("add",codeText,10);
			});
		},
		codeTextChange20(codeText2){
			let that = this;
			this.type = 20;
			that.codeText2 = codeText2;
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open("add",codeText2,10);
			});
		},
		//编辑
		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_reBack2(row){
			let that = this;
			that.$API.wpm.handover.revert.req(row.id).then((res) => {
				that.$refs.table.refresh();
			});
		},
		//退回
		table_reBack(row){
			let that = this;
			let form = {};
			form.send_mgroup = row.recive_mgroup;
			form.recive_mgroup = row.send_mgroup;
			form.handoverb = [];
			that.handoverbList = [];
			row.handoverb.forEach((item) => {
			    let obj = {};
				obj.wm = item.wm_to;
				obj.checked = true;
				obj.batch = item.batch;
				obj.count = item.count;
				form.handoverb.push(obj);
				that.handoverbList.push(obj);
			})
			form.send_user = that.$TOOL.data.get('USER_INFO').id;
			form.send_date = that.$TOOL.dateFormat2(new Date());
			form.type = 60;//退料
			that.form = form;
			that.backDialogVisible = true;
		},
		table_print(row) {
			let that = this;
			that.handoverItem = row;
			that.printVisible = true;
		},
		savePrinter() {
			let that = this;
			that.form.handoverb = [];
			that.handoverbList.forEach(item=>{
				if(item.checked){
					let obj = {};
					obj.wm = item.wm;
					obj.batch = item.batch;
					obj.count = item.count;
					that.form.handoverb.push(obj);
				}
			})
			// console.log(that.form)
			that.$API.wpm.handover.create.req(that.form).then((res) => {
				that.$message.success("操作成功");
				that.backDialogVisible = false;
				that.$refs.table.refresh();
				that.form = {};
			}).catch((err) => {
				return err;
			});
		},
		//本地更新数据
		//新增岗位后更新数据
		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>
