<template>
	<el-drawer
		v-model="visible"
		title="出入库记录"
		:size="'95%'"
		destroy-on-close
		@closed="$emit('closed')"
	>
		<div style="padding: 8px">
			<div>
				<el-card style="width: 100%" header="基本信息" shadow="hover">
					<el-button type="primary" @click="handlePrint" style="position: absolute;right: 40px;">打印</el-button>
					<el-descriptions>
						<el-descriptions-item label="编号">{{
							mioObj.number
						}}</el-descriptions-item>
						<el-descriptions-item label="出入库类型">{{
							typeDict[mioObj.type]
						}}</el-descriptions-item>
						<el-descriptions-item label="状态">{{
							stateDict[mioObj.state]
						}}</el-descriptions-item>
						<el-descriptions-item label="部门/车间">{{
							mioObj.belong_dept_name
						}}-{{ mioObj.mgroup_name }}</el-descriptions-item>
						<el-descriptions-item label="执行人">
							<span v-if="mioObj.do_user_name!==null">{{mioObj.do_user_name}}</span>
							<span v-if="mioObj.mio_user_name!==null">{{mioObj.mio_user_name}}</span>
						</el-descriptions-item>
						<el-descriptions-item label="创建时间">{{
							mioObj.create_time
						}}</el-descriptions-item>
						<el-descriptions-item
							label="采购订单"
							v-if="mioObj.type == 'pur_in'"
							>{{ mioObj.pu_order_number }}</el-descriptions-item
						>
						<el-descriptions-item
							label="供应商"
							v-if="mioObj.type == 'pur_in'"
							>{{ mioObj.supplier_name }}</el-descriptions-item
						>
						<el-descriptions-item
							label="销售订单"
							v-if="mioObj.type == 'sale_out'"
							>{{ mioObj.order_number }}</el-descriptions-item
						>
						<el-descriptions-item
							label="客户"
							v-if="mioObj.type == 'sale_out'"
							>{{ mioObj.customer_name }}</el-descriptions-item
						>
					</el-descriptions>
				</el-card>
			</div>
			<div style="height: 8px"></div>
			<div>
				<el-card style="width: 100%" header="物料明细" shadow="hover">
					<div>
						<el-button
							type="primary"
							icon="el-icon-plus"
							@click="table_add"
							v-if="mioObj.state == 10"
							>新增</el-button
						>
					</div>
					<div style="height: 8px"></div>
					<scTable
						ref="table"
						:apiObj="apiObj"
						row-key="id"
						stripe
						:params="params"
						hidePagination
						hideDo
					>
						<el-table-column type="index" width="50" />
						<el-table-column
							label="物料"
							prop="material"
							show-overflow-tooltip
						>
							<template #default="scope"
								>{{ scope.row.material_name }}
							</template>
						</el-table-column>
						<el-table-column label="批次号" prop="batch">
						</el-table-column>
						<el-table-column label="仓库" prop="warehouse_name">
						</el-table-column>
						<el-table-column label="数量">
							<template #default="scope">
								<span v-if="scope.row.material_&&scope.row.material_.tracking==10">{{scope.row.count}}</span>
								<el-link v-else :underline="false" type="primary" @click="showWpr(scope.row)">{{scope.row.count}}</el-link>
							</template>
						</el-table-column>
						<el-table-column label="检验" prop="count_tested" v-if="cate=='mainso'">
						</el-table-column>
						<el-table-column
							label="组合件信息"
							v-if="cate == 'good'"
							width="300"
						>
							<template #default="scope">
								<div v-if="scope.row.assemb.length > 0">
									<div
										v-for="item in scope.row.assemb"
										:key="item.id"
									>
										<div>
											{{ item.material_name }}
											<span
												style="
													color: gray;
													font-size: 12px;
												"
												>批:</span
											>{{ item.batch }}
											<span
												style="
													color: gray;
													font-size: 12px;
												"
												>比:</span
											>{{ item.rate }}
										</div>
									</div>
								</div>
							</template>
						</el-table-column>
						<!-- <el-table-column label="合格数量" prop="count_ok">
						</el-table-column> -->
						<el-table-column label="不合格数量" prop="count_notok">
						</el-table-column>
						<el-table-column
							label="创建时间"
							prop="create_time"
							show-overflow-tooltip
						>
						</el-table-column>
						<el-table-column
							label="操作"
							fixed="right"
							align="center"
							width="100px"
						>
							<template #default="scope">
								
								<el-button
									link
									type="primary"
									@click="table_check(scope.row)"
									v-if="project_code=='bxerp'&&mioObj.state == 10 &&
										(type == 'pur_in' ||type == 'do_in' ||type == 'other_in')"
									v-auth="'mioitem.test'"
								>
									检验
								</el-button>
								<el-button
									link
									type="primary"
									@click="table_check(scope.row)"
									v-if="
										scope.row.test_date == null &&
										mioObj.state == 20 &&
										(type == 'pur_in' ||
											type == 'do_in' ||
											type == 'other_in')
									"
									v-auth="'mioitem.test'"
								>
									检验
								</el-button>
								<el-button
									link
									type="primary"
									@click="check_Show(scope.row)"
									v-if="scope.row.test_date !== null"
								>
									查看
								</el-button>
								<el-button
									link
									type="danger"
									@click="check_reSet(scope.row)"
									v-if="scope.row.test_date !== null"
								>
									撤回
								</el-button>
								<el-button
									link
									type="danger"
									@click="table_del(scope.row)"
									v-if="mioObj.state == 10"
								>
									删除
								</el-button>
								<el-link
									type="primary"
									v-if="mioObj.type == 'do_out'"
									@click="printMaterial(scope.row,'mioitem')"
								>
									物料标签
								</el-link>

							</template>
						</el-table-column>
					</scTable>
				</el-card>
			</div>
		</div>
	</el-drawer>
	<save-dialog
		v-if="dialog.save"
		ref="saveDialog"
		:mioId="mioId"
		:mgroup="mioObj.mgroup"
		:belongDeptId="belongDeptId"
		:belongDeptName="mioObj.belong_dept_name"
		:mioObj="mioObj"
		:cate="cate"
		@success="handleSaveSuccess"
		@closed="dialog.save = false"
	>
	</save-dialog>
	<check-dialog
		v-if="dialog.check"
		ref="checkDialog"
		:mioitemId="mioitemId"
		:cate="cate"
		:type="type"
		:objitem="objitem"
		@success="handleCheckSuccess"
		@closed="checkDialogClose"
	>
	</check-dialog>
	<el-dialog v-model="printVisible" width="1200px">
		<print :baseData="mioObj" :tableData="tableData" :type="mioObj.type" @closePrint="printVisible=false"/>
	</el-dialog>
	<el-drawer v-model="wprVisibleDrawer" :size="'70%'">
		<template #header>
			<h4>关联产品</h4>
		</template>
		<template #default>
			<scTable ref="wprTable" hidePagination :data="wprList">
				<el-table-column label="产品编号" prop="number" min-width="100px"></el-table-column>
				<el-table-column
					label="操作"
					fixed="right"
					align="center"
					width="120"
				>
					<template #default="scope">
						<el-link :underline="false" v-if="mioObj.type == 'do_out'" @click="printMaterial(scope.row,'wpr')" type="primary">打印标签</el-link>
					</template>
				</el-table-column>
			</scTable>
		</template>
		<template #footer>
		<div style="flex: auto">
			<el-button @click="wprVisibleDrawer=false">关闭</el-button>
		</div>
		</template>
	</el-drawer>
	<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 "./mioitem_form.vue";
import checkDialog from "./mioitem_check.vue";
import print from "./../setting/print/A4.vue";
export default {
	name: "mioitem",
	components: {
		saveDialog,
		checkDialog,
		print
	},
	props: {
		type: {
			type: String,
			default: "",
		},
		cate: {
			type: String,
			default: "",
		},
		mioId: {
			type: String,
			default: "",
		},
	},
	data() {
		return {
			dialog: {
				check: false,
				save: false,
			},
			apiObj: null,
			params: {},
			// mioId: "",
			mioObj: {},
			wprList:[],
			selection: [],
			tableData:[],
			stateDict: {
				10: "创建中",
				20: "已提交",
			},
			typeDict: {
				do_out: "生产领料",
				sale_out: "销售发货",
				pur_in: "采购入库",
				do_in: "生产入库",
				other_in: "其他入库",
				other_out: "其他出库",
			},
			visible: false,
			printVisible:false,
			mioitemId: "",
			// type: "",
			// cate: "",
			objitem: {},
			mtype:10,
			mioType:'',
			project_code:'',
			setNameVisible:false,
			wprVisibleDrawer:false,
			printer_name:localStorage.getItem("printer_name")
		};
	},
	mounted() {
		// this.type = this.$route.query.type;
		// this.cate = this.$route.query.cate;
		// this.mioId = this.$route.query.mio;
		// this.params.mio = this.$route.query.mio;
		this.project_code = this.$TOOL.data.get("BASE_INFO").base.base_code;
		this.params.mio = this.mioId;
		this.apiObj = this.$API.inm.mioitem.list;
		this.getMio();
	},
	methods: {
		printSetting(){
			this.setNameVisible = true;
		},
		savePrinter() {
			let that = this;
			localStorage.setItem("printer_name", that.printer_name);
			that.setNameVisible = false;
			that.$message.success("打印机设置成功，请重新进行打印操作。");
		},
		open() {
			this.visible = true;
		},
		getMio() {
			let that = this;
			that.$API.inm.mio.item.req(that.mioId).then((res) => {
				that.mioObj = res;
				that.mioType = res.type;
				that.belongDeptId = res.belong_dept;
			});
		},
		//添加
		table_add() {
			let that = this;
			console.log(this.mioType);
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open("add", that.mioType);
			});
		},

		//删除
		table_del(row) {
			this.$confirm(`确定删除吗？`, "提示", {
				type: "warning",
			}).then(() => {
				this.$API.inm.mioitem.delete.req(row.id).then((res) => {
					this.$message.success("删除成功");
					this.$refs.table.refresh();
					return res;
				}).catch((err) => {
					return err;
				});
			}).catch(() => {});
		},
		//检验
		table_check(row) {
			this.mioitemId = row.id;
			this.objitem = row;
			this.dialog.check = true;
			this.$nextTick(() => {
				this.$refs.checkDialog.open("add").setData(row);
			});
		},
		//查看
		check_Show(row) {
			this.mioitemId = row.id;
			this.objitem = row;
			this.dialog.check = true;
			this.$nextTick(() => {
				this.$refs.checkDialog.open("show").setData(row);
			});
		},
		//编辑
		check_edit(row) {
			this.mioitemId = row.id;
			this.objitem = row;
			this.dialog.check = true;
			this.$nextTick(() => {
				this.$refs.checkDialog.open("edit").setData(row);
			});
		},
		//撤回
		check_reSet(row) {
			this.$confirm(`确定撤回吗？`, "提示", {
				type: "warning",
			}).then(() => {
				this.$API.inm.mioitem.test_revert.req(row.id).then((res) => {
					this.$message.success("撤回成功");
					this.$refs.table.refresh();
				});
			});
		},
		//本地更新数据
		handleSaveSuccess(data, mode) {
			if (mode == "add") {
				this.$refs.table.refresh();
			} else if (mode == "edit") {
				this.$refs.table.refresh();
			}
		},
		handleCheckSuccess() {
			this.$refs.table.refresh();
			this.dialog.check = false;
		},
		handleQuery() {
			this.$refs.table.queryData(this.query);
		},
		resetQuery() {
			this.query = {};
		},
		mioSubmit() {
			this.$API.inm.mio.submit.req(this.mioObj.id).then((res) => {
				this.$message.success("提交成功");
				this.mioObj = res;
			});
		},
		checkDialogClose(){
			let that = this;
			that.dialog.check = false;
			that.$refs.table.refresh();
		},
		handlePrint(){
			let that = this;
			let params = {
				mio: that.mioId,
				page:0
			}
			that.$API.inm.mioitem.list.req(params).then((res) => {
				that.tableData = res;
				that.printVisible = true;
			})
		},//打印
		showWpr(row){
			let that = this;
			that.printMaterialName = row.material_name;
			that.wprList = row.mioitemw;
			that.wprVisibleDrawer = true;
		},
		//打印物料标签
		printMaterial(row,type){
			let that = this;
			if(that.printer_name!==''&&that.printer_name!==null&&that.printer_name!==undefined){
				if(type=='mioitem'){
					let params = {};
					params.tid = row.id;
					params.extra_data={count:row.count};
					params.label_template_name = '库存标签模板';
					that.$API.cm.labelmat.fromMioitem.req(params).then((res) => {
						let obj = {};
						obj.printer_commands = res.commands;
						obj.printer_name = that.printer_name;
						that.$API.wpm.prints.req(obj).then((response) => {
							that.$message.success("打印成功");
						});
					})
				}else{
					let params = {};
					let name = that.printMaterialName.split('|')[0];
					params.label_template_name = '单件打印模板';
					params.data = {number:row.number,name:name};
					that.$API.cm.labeltemplate.commands.req(params).then((res) => {
						let obj = {};
						obj.printer_commands = res.commands;
						obj.printer_name = that.printer_name;
						that.$API.wpm.prints.req(obj).then((response) => {
							that.$message.success("打印成功");
						});
					});
				}
			}else{
				that.printSetting();
			}
		},
	},
};
</script>
