<!-- 物料收发明细 -->
<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="4">
										<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-col :span="5">
										<el-form-item label="日期">
											<el-date-picker
												@change="time1Chang"
												class="f-12"
												end-placeholder="结束时间"
												start-placeholder="开始时间"
												type="daterange"
												v-model="Time1"
												value-format="YYYY-MM-DD"
											></el-date-picker>
										</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"
							width="150"
						/>
						<el-table-column
							align="left"
							label="规格型号(SKU)"
							prop="specification"
							width="280"
						/>
						<el-table-column
							align="left"
							label="物料名称"
							prop="materialName"
							width="120"
						/>
						<el-table-column
							align="left"
							label="单号"
							prop="orderBillNo"
							width="172"
						/>
						<el-table-column
							align="center"
							label="单据类型"
							prop="orderType"
							width="72"
						/>
						<el-table-column
							align="left"
							label="日期"
							prop="createTime"
							width="110"
						/>
						<el-table-column
							align="center"
							label="是否出入库"
							prop="isInOrOut"
							width="96"
						>
							<template #default="scope">
								<span>{{
									scope.row.isInOrOut ? '入库' : '出库'
								}}</span>
							</template>
						</el-table-column>
						<!-- <el-table-column prop="waitQty" label="待出入数量" align="left" /> -->
						<!-- <el-table-column align="right" label="待入量" prop="waitInQty" width="100" />
            <el-table-column align="right" label="待出量" prop="waitOutQty" width="100" /> -->
						<el-table-column align="right" label="数量" width="100">
							<template #default="scope">
								<span>{{
									scope.row.isInOrOut || scope.row.qty === 0
										? scope.row.qty
										: '-' + scope.row.qty
								}}</span>
							</template>
						</el-table-column>
						<el-table-column
							align="right"
							label="结存"
							prop="surplusQty"
							width="100"
						/>
						<el-table-column
							align="left"
							label="库存组织"
							prop="stockOrgName"
							width="172"
						/>
						<el-table-column
							align="left"
							label="仓库"
							prop="stockName"
							width="120"
						/>
						<el-table-column
							align="left"
							label="客户"
							prop="customerName"
							width="172"
						/>
						<el-table-column
							align="left"
							label="客户仓库"
							prop="customerStockName"
							width="120"
						/>
					</el-table>
				</div>
				<!-- 底部 -->
				<div class="sale-page-footer">
					<div class="footer-left">
						<div>
							<span>入库数量总数:</span>
							<span class="sale-number-color">{{
								inQtyTotal
							}}</span>
						</div>
						<div>
							<span>出库总数:</span>
							<span class="sale-number-color">{{
								outQtyTotal
							}}</span>
						</div>
						<div>
							<span>当前结存:</span>
							<span class="sale-number-color">{{
								surplusQtyTotal
							}}</span>
						</div>
					</div>
					<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>
	</div>
</template>
<script>
import { mapState } from 'vuex';
import { exporthandel } from '@/utils/pubulic.js';
import { orderCopy } from '@/utils/takeMethods';
export default {
	name: 'WarehouseQueryMaterial',
	data() {
		return {
			btnType: [
				's-p-t-g1',
				's-p-t-g2',
				's-p-t-g3',
				's-p-t-g4',
				's-p-t-g12',
				'push-down',
				'query',
				'finish',
				'close-case',
				'de-excute-start',
				'de-excute-finish',
				'close',
				'de-close',
				'all-export',
				'print-brower',
				'print-aterial-list',
				'operation',
				'push-down',
				'dayin',
			], // 不需要显示的顶部按钮类型
			// 搜索条件
			search: {
				orgId: '', // 组织id
				pageNo: 1, // 页码
				pageSize: 50, // 页数
				productKey: '', // 产品（关键字）：物料表中的->>编码、名称、规格型号
				stockId: '', // 仓库id
				beginTime: '', //开始日期
				endTime: '', // 结束日期
			},
			inQtyTotal: 0,
			outQtyTotal: 0,
			surplusQtyTotal: 0,
			multipleSelection: [],
			Time1: [], // 日期
			warehouseList: [], // 仓库数据
			supplierlist: [], // 供应商下拉数据
			tableData: [],
			selectItem: 0,
			rowShow: false,
			heightNum: 0,
			windowHeight: 0,
			isDown: true,
		};
	},
	computed: {
		...mapState({
			orgId: (state) =>
				state && state.org && state.org.orgId ? state.org.orgId : null,
			orgIdArr: (state) => (state ? 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: '', // 组织id
					pageNo: 1, // 页码
					pageSize: 50, // 页数
					productKey: '', // 产品（关键字）：物料表中的->>编码、名称、规格型号
					stockId: '', // 仓库id
					beginTime: '', //开始日期
					endTime: '', // 结束日期
				};
				this.search.orgId = value;
				this.search.pageNo = 1;
				this.getwarehouse();
				this.getIssueDetails();
			}
		},
	},
	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.getIssueDetails();
			this.getwarehouse();
			this.getsupplierList();
		},
		resetSearch() {
			this.search = {
				orgId: this.orgId, // 组织id
				pageNo: 1, // 页码
				pageSize: 50, // 页数
				productKey: '', // 产品（关键字）：物料表中的->>编码、名称、规格型号
				stockId: '', // 仓库id
				beginTime: '', //开始日期
				endTime: '', // 结束日期
			};
			this.getIssueDetails();
		},
		// 下单日期改变
		time1Chang(value) {
			if (value && value.length > 0) {
				this.search.beginTime = value[0];
				this.search.endTime = value[1];
			} else {
				this.search.beginTime = '';
				this.search.endTime = '';
			}
		},
		// 订单号 复制按钮
		saleOrderCopy() {
			orderCopy();
		},
		// 获取仓库
		getwarehouse() {
			// 获取仓库
			if (this.orgId) {
				this.$http
					.get('SysConfig/GetWarehouseByOrg/' + this.orgId)
					.then((req) => {
						if (req.status === 200) {
							this.warehouseList = req.data;
						}
					});
			}
		},
		// 获取供应商-下拉列表
		getsupplierList() {
			this.$http.get('SysConfig/supplier/select').then((r) => {
				if (r.status === 200) {
					this.supplierlist = r.data;
				}
			});
		},
		// 搜索切换高度
		openRow() {
			this.isDown = !this.isDown;
			this.rowShow = !this.rowShow;
			if (this.rowShow) {
				this.heightNum = this.heightNum - 80;
			} else {
				this.heightNum = this.heightNum + 80;
			}
		},
		// 获取物料收发明细
		getIssueDetails() {
			this.isLoading = true;
			if (this.search.orgId) {
				this.$http
					.post('Inventory/list/inout', this.search)
					.then((res) => {
						if (res.status === 200) {
							this.isLoading = false;
							this.tableData = res.data.list;
							this.orderTotal = res.data.totalCount;
							this.inQtyTotal = res.data.inQtyTotal;
							this.outQtyTotal = res.data.outQtyTotal;
							this.surplusQtyTotal = res.data.surplusQtyTotal;
						}
					});
			}
		},
		// 顶部公共按钮操作
		actionhandel(type) {
			if (type === 'list-export') {
				exporthandel('Inventory/Export/InOrOut', this.search);
			}
			if (type === 'daochu') {
				exporthandel('Inventory/Export/InOrOut', 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.getIssueDetails();
			}
		},
		// 搜索事件
		searchData() {
			this.search.pageNo = 1;
			this.getIssueDetails();
		},
		//控制表头颜色
		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>
