<!-- 及时库存汇总 -->
<template>
	<div class="main-container order-mc-bg">
		<div class="sale-container">
			<!-- 顶部按钮组 -->
			<toolBtn :btnType="btnType" @actionhandel="actionhandel"></toolBtn>
			<!-- 列表 -->
			<div class="sale-page-body">
				<!-- 表头 -->
				<div class="sale-page-table-header" ref="getSaleTableHeader">
					<el-form :inline="true">
						<el-row class="form-row1">
							<el-col :span="20">
								<el-row :gutter="2">
									<el-col :span="4">
										<el-form-item
											class="Iisshow"
											label="物料"
										>
											<el-input
												class="f-12"
												placeholder="请输入物料名称/编码/SKU"
												v-model.trim="search.productKey"
											></el-input>
											<div
												:data-clipboard-text="
													search.productKey
												"
												@click="saleOrderCopy"
												class="copyBtn"
											>
												<i
													class="el-icon-document-copy"
												/>
											</div>
										</el-form-item>
									</el-col>
									<el-col :span="5">
										<el-form-item label="库存量">
											<div
												style="
													display: flex;
													flex-direction: row;
													justify-content: space-between;
													wdith: 250px;
												"
											>
												<el-input
													class="f-12"
													@input="
														comInput(
															search.beginRealityQty,
															'beginRealityQty'
														)
													"
													placeholder="请输入下限值"
													v-model="
														search.beginRealityQty
													"
												></el-input>
												<span style="margin: 0 10px"
													>-</span
												>
												<el-input
													class="f-12"
													@input="
														comInput(
															search.endRealityQty,
															'endRealityQty'
														)
													"
													placeholder="请输入上限值"
													v-model="
														search.endRealityQty
													"
												></el-input>
											</div>
										</el-form-item>
									</el-col>
									<el-col :span="7">
										<el-form-item label="仓库">
											<el-select
												clearable
												filterable
												placeholder="请选择"
												v-model="search.stockId"
											>
												<el-option
													:key="item.id"
													:label="item.name"
													:value="item.id"
													v-for="item in warehouseList"
												>
												</el-option>
											</el-select>
										</el-form-item>
									</el-col>
								</el-row>
							</el-col>
							<el-col :span="4" class="form-row-lastcol">
								<el-row :gutter="2">
									<el-col :span="24">
										<el-form-item class="form-group1">
											<el-button
												@click="resetSearch"
												size="small"
												>重置</el-button
											>
											<el-button
												@click="searchData"
												size="small"
												type="primary"
												>搜索</el-button
											>
										</el-form-item>
									</el-col>
								</el-row>
							</el-col>
						</el-row>
					</el-form>
				</div>
				<!-- 列表 -->
				<div class="sale-page-table-body">
					<el-table
						:data="tableData"
						:header-cell-class-name="headerRigthMethod"
						:header-cell-style="headerBg"
						:max-height="heightNum"
						@selection-change="handleSelectionChange"
						border
						class="table-box"
						element-loading-spinner="el-icon-loading"
						element-loading-text="加载中，请稍后..."
						ref="tableInfoRef"
						style="width: 100%; font-size: 12px"
						v-loading="isLoading"
					>
						<el-table-column
							align="center"
							fixed
							type="selection"
							width="40"
						/>
						<el-table-column
							align="left"
							label="物料编码"
							prop="materialNumber"
						/>
						<el-table-column
							align="left"
							label="规格型号(SKU)"
							prop="specification"
						/>
						<el-table-column
							align="left"
							label="物料名称"
							prop="materialName"
						/>
						<el-table-column
							align="center"
							label="单位"
							prop="materialUnitName"
							width="120"
						/>
						<el-table-column
							align="left"
							label="仓库"
							prop="stockNumber"
						/>
						<el-table-column
							align="right"
							label="库存量"
							prop="realityQtyTotal"
							width="120"
						/>
						<el-table-column
							align="right"
							label="待入量"
							prop="waitOutQtyTotal"
							width="120"
						/>
						<el-table-column
							align="right"
							label="待出量"
							prop="waitInQtyTotal"
							width="120"
						/>
						<el-table-column
							align="right"
							label="可用量"
							prop="availableQtyTotal"
							width="120"
						/>
					</el-table>
				</div>
			</div>
			<!-- 底部 -->
			<div class="sale-page-footer">
				<div class="footer-right">
					<el-pagination
						:currentPage="search.pageNo"
						:page-size="search.pageSize"
						:page-sizes="[50, 100, 150]"
						:total="orderTotal"
						@current-change="handleCurrentChange"
						@size-change="handleSizeChange"
						layout="slot,sizes, total,prev,pager, next,jumper"
						popper-class="每页显示"
					>
						<span class="total-select-item">
							共选中
							<text
								style="
									text-align: center;
									color: #4178d5;
									padding: 0 1px;
								"
								>{{
									multipleSelection
										? multipleSelection.length
										: 0
								}}</text
							>行
						</span>
					</el-pagination>
				</div>
				<div class="footer-pagination-total-page">
					<span
						>共{{ Math.ceil(orderTotal / search.pageSize) }}页</span
					>
				</div>
			</div>
		</div>
	</div>
</template>
<script>
import { mapState } from 'vuex';
import { exporthandel } from '@/utils/pubulic.js';
import { orderCopy, numberRexgu } from '@/utils/takeMethods';
export default {
	name: 'TimelyInventorySummary',
	data() {
		return {
			btnType: [
				's-p-t-g1',
				's-p-t-g2',
				's-p-t-g3',
				's-p-t-g4',
				's-p-t-g12',
				'operation',
				'push-down',
				'query',
				'finish',
				'close-case',
				'de-excute-start',
				'de-excute-finish',
				'close',
				'de-close',
				'all-export',
				'dayin',
				'print-aterial-list',
				'print-brower',
			], // 不需要显示的顶部按钮类型
			// 搜索条件
			search: {
				orgId: '',
				pageNo: 1, // 页码
				pageSize: 50, // 页数
				productKey: '', // 产品（关键字）：物料表中的->>编码、名称、规格型号
				stockId: '', // 仓库id
				beginRealityQty: null, //库存量下线
				endRealityQty: null, //库存量上限
				stockDays: '', // 库存天数
				supplierId: '', // 供应商
			},
			multipleSelection: [],
			warehouseList: [], // 仓库数据
			tableData: [],
			selectItem: 0,
			rowShow: false,
			heightNum: 0,
			windowHeight: 0,
			isDown: true,
		};
	},
	computed: {
		...mapState({
			orgId: (state) => {
				return state && state.org ? state.org.orgId : null;
			},
			orgIdArr: (state) =>
				state && state.user ? state.user.orgIdArr : null,
		}),
	},
	activated() {
		// 键盘监听事件
		document.addEventListener('keyup', this.keyupFn, false);
	},
	deactivated() {
		document.removeEventListener('keyup', this.keyupFn, false);
	},
	watch: {
		orgId(value) {
			if (value) {
				this.search = {
					orgId: '',
					pageNo: 1, // 页码
					pageSize: 50, // 页数
					productKey: '', // 产品（关键字）：物料表中的->>编码、名称、规格型号
					stockId: '', // 仓库id
					beginRealityQty: null, //库存量下线
					endRealityQty: null, //库存量上限
					stockDays: '', // 库存天数
					supplierId: '', // 供应商
				};
				this.search.orgId = value;
				this.getwarehouse();
				this.getTimelyInventorysummary();
			}
		},
	},
	created() {
		this.customerSel1 = 1;
		this.windowHeight = document.documentElement.clientHeight;
		this.heightNum =
			this.windowHeight - (40 + 30 + 54 + 34 + 34 + 34 + 16 + 16);
		this.search.orgId = this.orgId;
		this.Init();
	},
	methods: {
		keyupFn(e) {
			if (e.keyCode === 13) {
				// enter见搜索
				this.searchData();
			}
		},
		// 初始化
		Init() {
			this.getTimelyInventorysummary();
			this.getwarehouse();
		},
		resetSearch() {
			this.search = {
				orgId: this.orgId, // 组织id
				pageNo: 1, // 页码
				pageSize: 50, // 页数
				productKey: '', // 产品（关键字）：物料表中的->>编码、名称、规格型号
				stockId: '', // 仓库id
				beginTime: '', //开始日期
				endTime: '', // 结束日期
			};
			this.getTimelyInventorysummary();
		},
		// 订单号 复制按钮
		saleOrderCopy() {
			orderCopy();
		},
		// 获取仓库
		getwarehouse() {
			// 获取仓库
			if (this.orgId) {
				this.$http
					.get('SysConfig/GetWarehouseByOrg/' + this.orgId)
					.then((req) => {
						if (req.status === 200) {
							this.warehouseList = req.data;
						}
					});
			}
		},
		// 输入 验证
		comInput(val, key) {
			if (val) {
				this.search[key] = numberRexgu(val);
			}
		},
		// 搜索切换高度
		openRow() {
			this.isDown = !this.isDown;
			this.rowShow = !this.rowShow;
			if (this.rowShow) {
				this.heightNum = this.heightNum - 80;
			} else {
				this.heightNum = this.heightNum + 80;
			}
		},
		// 获取及时库存明细
		getTimelyInventorysummary() {
			this.isLoading = true;
			let newdata = this.search;
			// if (newdata.beginRealityQty) {
			//   newdata.beginRealityQty = parseInt(newdata.beginRealityQty)
			// }
			// if (newdata.endRealityQty) {
			//   newdata.endRealityQty = parseInt(newdata.endRealityQty)
			// }
			this.$http.post('Inventory/list/summary', newdata).then((res) => {
				if (res.status === 200) {
					this.isLoading = false;
					this.tableData = res.data;
					this.orderTotal = res.totalCount;
					console.log('获取委外领料订单数据', res);
				}
			});
		},
		// 顶部公共按钮操作
		actionhandel(type) {
			if (type === 'list-export') {
				exporthandel('Inventory/Export/Summary', this.search);
			}
			if (type === 'daochu') {
				exporthandel('Inventory/Export/Summary', this.search);
			}
			if (type === 'dayin') {
				exporthandel('Inventory/Export/Details', this.search);
			}
			if (type === 'refresh') {
				this.search.orgId = this.orgId;
				this.search.pageNo = 1;
				this.getwarehouse();
				this.getTimelyInventorysummary();
			}
		},
		// 搜索事件
		searchData() {
			this.search.pageNo = 1;
			this.getTimelyInventorysummary();
		},
		//控制表头颜色
		headerBg({ row, column, rowIndex, columnIndex }) {
			if (columnIndex === 17 || columnIndex === 18) {
				return `text-align:right!important;`;
			}
		},
		headerRigthMethod({ row, column, rowIndex, columnIndex }) {
			return 'headerRight';
		},
		handleSelectionChange(val) {
			this.multipleSelection = val;
		},
		handleSizeChange(val) {
			this.search.pageNo = 1;
			this.search.pageSize = val;
			this.Init();
		},
		handleCurrentChange(val) {
			this.search.pageNo = val;
			this.Init();
		},
	},
};
</script>
<style lang="scss" scoped>
@import '@/assets/style/saleOrder/saleOrderCommon.scss';
@import '@/assets/style/saleOrder/orderList.scss';
@import '@/assets/style/saleOrder/common.scss';
</style>
