<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>
						<el-text style="margin-left: 1vw;cursor: pointer;" @click="handleWatch">仓库总数：{{ totalCount }}</el-text>
					</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"
						:hExportCols = "hExportCols"
						:hExportName="'仓库成品库存'"
					>
						<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="80">
							<template #default="scope">
								<el-button @click="printMaterial(scope.row)" type="text">打签</el-button>
							</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>
					</div>
				</el-header>
				<el-main>
					<scTable
						ref="table_wm"
						:apiObj="apiObjWm"
						row-key="id"
						:params="paramsWm"
						stripe
						:hExportCols = "hExportCols2"
						:hExportName="'车间成品库存'"
					>
						<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">
			<el-container>
				<el-header style="height: 40%;">
					<scTable  :data="tableData" hideDo hidePagination row-key="id" style="width: 100%;">
						<el-table-column label="物料名" prop="material_name"></el-table-column>
						<el-table-column label="数量" prop="count"></el-table-column>
					</scTable>
				</el-header>
				<el-main>
					<scEcharts :option="echartsOptions" height="100%"></scEcharts>
				</el-main>
			</el-container>
		</el-drawer>
	</el-container>
</template>
<script>
import printDialog from "./../template/printmaterial.vue";
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:[],
			tableData:[],
			queryWm: {},
			wmtype:0,
			totalCount:0,
			print_m:false,
			limitedWatch:false,
			materialsVisible:false,
			apiObjPrint:this.$API.cm.labelmat.fromMb,
			wmId:'',
			showBatch: "",
			hExportCols:[{
				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"
			}
			],
			hExportCols2:[{
				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"
			}
			]
		};
	},
	mounted() {
		this.getWarehouse();
		this.getProductCount();
	},
	methods: {
		getProductCount(){
			let that = this;
			let query ={
				material_types: "10",
				select_material: "material.name as material_name",
				groupby_material: "material.name"
			};
			that.$API.bi.dataset.exec.req("materialCount2", {query:query }).then((res) => {
				let data = that.tableData = res.data2.ds0;
				let totalCount = 0;
				data.forEach(item => {
					totalCount += item.count;
				});
				that.totalCount = totalCount;
				if (res.echart_options) {
					that.echartsOptions = JSON.parse(res.echart_options);
				}
			})
		},
		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() {
			this.limitedWatch = true;
		},
		//打签
		printMaterial(row,type){
			let that = this;
			let printer_name=localStorage.getItem("printer_name");
			let printer_ip=localStorage.getItem("printer_ip")||'127.0.0.1';
			if(printer_name!==''&&printer_name!==null&&printer_name!==undefined){
				if(that.project_code!=='gz'){
					if(type=='mb'){
						let params = {};
						params.tid = row.id;
						params.label_template_name = '库存标签模板';
						params.extra_data={count:row.count};
						that.$API.cm.labelmat.fromMb.req(params).then((res) => {
							let obj = {};
							obj.printer_commands = res.commands;
							obj.printer_name = printer_name;
that.$API.wpm.prints.req(printer_ip, obj).then((response) => {
								that.$message.success("打印成功");
							});
						})
					}else{
						let params = {};
						let name = row.material_name.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 = printer_name;
							let printer_ip=localStorage.getItem("printer_ip")||'127.0.0.1';
that.$API.wpm.prints.req(printer_ip, obj).then((response) => {
								that.$message.success("打印成功");
							});
						});
					}
				}else{
					that.wmId = row.id;
					that.wmtype = row.material_.type;
					that.print_m = true;
					this.$nextTick(() => {
						this.$refs.printmaterial.open();
					})
				}
			}else{
				that.$message.error("请先设置打印机");
			}
		},
	},
};
</script>
