<template>
	<el-container>
		<el-header style="height: 50%; padding: 0">
			<el-container>
				<el-header>
					<div class="left-panel">
						<div style="font-size: 14px">仓库</div>
						<el-select
							v-model="query.warehouse"
							clearable
							placeholder="所在仓库"
							@change="handleQuery"
							style="margin-left: 10px; width: 150px"
						>
							<el-option
								v-for="item in warehouseOptions"
								:key="item.id"
								:label="item.name"
								:value="item.id"
							></el-option>
						</el-select>
					</div>
					<div class="right-panel">
						<el-input
							v-model="query.search"
							placeholder="物料名/批次号"
							clearable
							style="margin-right: 5px"
						></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"
						stripe
						:params="params"
						:hExportExcel="hExportExcel"
					>
						<el-table-column type="index" width="50" />
						<el-table-column label="批次" prop="batch">
						</el-table-column>
						<el-table-column label="物料名称" prop="material_name">
						</el-table-column>
						<el-table-column label="规格型号">
							<template #default="scope">
								<span v-if="scope.row.material_">
									{{ scope.row.material_.specification }}
									{{ scope.row.material_.model }}
								</span>
							</template>
						</el-table-column>
						<el-table-column label="已完成工序">
							<template #default="scope">
								<span v-if="scope.row.material_">
									{{ scope.row.material_.process_name }}
								</span>
							</template>
						</el-table-column>
						<el-table-column label="仓库" prop="warehouse_name">
						</el-table-column>
						<el-table-column label="物料存量" prop="count">
						</el-table-column>
						<el-table-column label="有效期" prop="expiration_date">
						</el-table-column>
						<el-table-column label="更新时间" prop="update_time">
						</el-table-column>
						<el-table-column width="90">
							<template #default="scope">
								<!-- <el-button @click="printMaterial(scope.row)" type="text">物料标签</el-button> -->
								<el-link :underline="false"  type="primary" 
									@click="handleWatch(scope.row)"
								>流程图</el-link>
							</template>
						</el-table-column>
					</scTable>
				</el-main>
			</el-container>
		</el-header>
		<el-main class="nopadding">
			<el-container>
				<el-header>
					<div class="left-panel">
						<span style="font-size: 14px">车间库存</span>
						<!-- <el-select v-model="queryWm.material__process" clearable placeholder="已到工序" @change="handleQueryWm"
                            style="margin-left: 4px">
                            <el-option v-for="item in processOptions" :key="item.id" :label="item.name"
                                :value="item.id"></el-option>
                        </el-select> -->
					</div>
				</el-header>
				<el-main>
					<scTable
						ref="table_wm"
						:apiObj="apiObjWm"
						row-key="id"
						:params="paramsWm"
						stripe
						:hExportExcel="hExportExcel2"
					>
						<el-table-column label="批次" prop="batch">
						</el-table-column>
						<el-table-column label="物料名" prop="material">
							<template #default="scope">{{
								scope.row.material_.name
							}}</template>
						</el-table-column>
						<el-table-column label="规格" prop="material">
							<template #default="scope">{{
								scope.row.material_.specification
							}}</template>
						</el-table-column>
						<el-table-column label="型号" prop="material">
							<template #default="scope">{{
								scope.row.material_.model
							}}</template>
						</el-table-column>
						<el-table-column label="已到工序" prop="material">
							<template #default="scope">{{
								scope.row.material_.process_name
							}}</template>
						</el-table-column>
						<el-table-column
							label="所在车间"
							prop="belong_dept_name"
						>
						</el-table-column>
						<el-table-column label="数量" prop="count" width="80">
						</el-table-column>
					</scTable>
				</el-main>
			</el-container>
		</el-main>
		<print-dialog
			v-if="print_m"
			ref="printmaterial"
			:mId="wmId"
			:mtype="wmtype"
			:apiObj="apiObjPrint"
		></print-dialog>
		<el-drawer v-model="limitedWatch" title="工艺路线流程图" size="80%" @closeDialog="limitedWatch = false">
			<scDegra
				v-if="limitedWatch" 
				ref="degraDialogs" 
				:nodes="nodes"
				:edges="edges"
				:rankdir="'LR'"
				>
			</scDegra>
		</el-drawer>
	</el-container>
</template>
<script>
import printDialog from "./../template/printmaterial.vue";
import { dataToExcel } from "@/utils/exportExcel";
export default {
	name: "halfgood",
	components: {
		printDialog,
	},
	data() {
		return {
			apiObj: this.$API.inm.warehouse.batch,
			params: { count__gte: 1, material__type: 10 },
			selection: [],
			query: {},
			warehouseOptions: [],
			apiObjWm: this.$API.wpm.wmaterial.list,
			paramsWm: { count__gte: 1, material__type: 10 },
			nodes:[],
			edges:[],
			queryWm: {},
			wmtype:0,
			print_m:false,
			limitedWatch:false,
			materialsVisible:false,
			apiObjPrint:this.$API.cm.labelmat.fromMb,
			wmId:'',
			showBatch: "",
		};
	},
	mounted() {
		this.getWarehouse();
		// this.getProcessOptions()
	},
	methods: {
		getWarehouse() {
			this.$API.inm.warehouse.list.req({ page: 0 }).then((res) => {
				this.warehouseOptions = res;
			});
		},
		getProcessOptions() {
			this.$API.mtm.process.list.req({ page: 0 }).then((res) => {
				this.processOptions = res;
			});
		},
		handleQueryWm() {
			this.$refs.table_wm.queryData(this.queryWm);
		},
		handleQuery() {
			this.$refs.table.queryData(this.query);
		},
		resetQuery() {
			this.query = {};
		},
		handleWatch(row) {
			let that = this;
			that.$API.wpm.batchlog.dag.req({batch:row.batch}).then((res) => {
			    that.nodes = res.nodes;
				that.edges = res.edges;
				that.limitedWatch = true;
				that.$nextTick(() => {
					that.$refs.degraDialogs.open();
				});
			})
		},
		//打印物料标签
		printMaterial(row){
			let that = this;
			that.wmId = row.id;
			that.wmtype = row.material_.type;
			that.print_m = true;
			this.$nextTick(() => {
			    this.$refs.printmaterial.open();
			})
		},
		hExportExcel(tdata) {
		    let cols = [{
				header: "批次",
				key: "batch"
			}, {
			    header: "物料名",
				key: "material_.name"
			}, {
				header: "规格",
				key: "material_.specification"
			}, {
				header: "型号",
				key: "material_.model"
			}, {
				header: "已完成工序",
				key: "material_.process_name"
			}, {
				header: "所在仓库",
				key: "warehouse_name"
			}, {
				header: "存量",
				key: "count"
			}, {
				header: "更新时间",
				key: "update_time"
			}
			]
			dataToExcel(cols, tdata, "成品库库存")
		},
		hExportExcel2(tdata) {
		    let cols = [{
				header: "批次",
				key: "batch"
			}, {
			    header: "物料名",
				key: "material_.name"
			}, {
				header: "规格",
				key: "material_.specification"
			}, {
				header: "型号",
				key: "material_.model"
			}, {
				header: "已完成工序",
				key: "material_.process_name"
			}, {
				header: "所在车间",
				key: "belong_dept_name"
			}, {
				header: "存量",
				key: "count"
			}, {
				header: "更新时间",
				key: "update_time"
			}
			]
			dataToExcel(cols, tdata, "车间成品库存")
		}
	},
};
</script>
