<template>
	<div class="main-container order-mc-bg">
		<PrintModel
			:printData="{
				tableData: multipleSelection,
				orderName: '采购入库单',
			}"
			:theadDate="theadDate"
			v-show="false"
		></PrintModel>
		<div
			:style="{ height: containerHeight + 'px!important' }"
			class="sale-container"
		>
			<toolBtn :btnType="btnType" @actionhandel="actionhandel"></toolBtn>
			<div class="TopInfo" v-if="OrderNoNameslist.length > 0">
				关联查询：
				<strong>{{ OrderNoNameStr }}</strong>
				<strong>{{ OrderNoNameslist.length > 5 ? '等' : '' }}</strong>
				{{ $route.query.actionName ? $route.query.actionName : '' }}所选
				<strong>{{ OrderNoNameslist.length }}</strong
				>条明细行的 <strong>{{ CXaction }}</strong
				>结果
			</div>
			<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
												:oninput="
													(billNo = billNo.replace(
														/(^\s*)|(\s*$)/g,
														''
													))
												"
												@input="
													searchCom(
														billNo,
														'billNo',
														'ipt'
													)
												"
												@keyup.enter="
													searchComS(
														billNo,
														'billNo',
														'ipt'
													)
												"
												class="f_12"
												placeholder="请输入采购入库单号"
												v-model.trim="billNo"
											></el-input>
											<div
												:data-clipboard-text="billNo"
												@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="true"
												@change="
													clearCom(
														documentStatus,
														'documentStatus'
													)
												"
												filterable
												placeholder="选择状态"
												v-model="documentStatus"
											>
												<el-option
													:key="item.id"
													:label="item.name"
													:value="item.id"
													@click="
														searchCom(
															documentStatus,
															'documentStatus'
														)
													"
													class="f_12"
													v-for="item in statusForm.documentStatus"
												></el-option>
											</el-select>
										</el-form-item>
									</el-col>
									<el-col :span="4">
										<el-form-item label="供应商">
											<el-select
												:clearable="true"
												@change="
													clearCom(
														supplierId,
														'supplierId'
													)
												"
												filterable
												placeholder="请输入供应商名称"
												v-model="supplierId"
											>
												<el-option
													:key="item.id"
													:label="item.name"
													:value="item.id"
													@click="
														searchCom(
															supplierId,
															'supplierId'
														)
													"
													class="f_12"
													v-for="item in statusForm.supplier"
												></el-option>
											</el-select>
										</el-form-item>
									</el-col>
									<el-col :span="4">
										<el-form-item
											class="Iisshow"
											label="物料"
										>
											<el-input
												:oninput="
													(productKey =
														productKey.replace(
															/(^\s*)|(\s*$)/g,
															''
														))
												"
												@input="
													searchCom(
														productKey,
														'productKey',
														'ipt'
													)
												"
												@keyup.enter="
													searchComS(
														productKey,
														'productKey',
														'ipt'
													)
												"
												class="f_12"
												placeholder="请输入物料名称/编码/SKU"
												v-model.trim="productKey"
											></el-input>
											<div
												:data-clipboard-text="
													productKey
												"
												@click="saleOrderCopy"
												class="copyBtn"
											>
												<i
													class="el-icon-document-copy"
												/>
											</div>
										</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">
											<div
												@click="openRow"
												class="alias-open"
											>
												<span
													:class="[
														isDown
															? 'icon-shuangjiantouzhankai-xia'
															: 'icon-shuangjiantoushouqi-shang',
													]"
													class="iconfont"
												></span>
											</div>
											<el-button
												@click="resetSearch"
												size="small"
												>重置</el-button
											>
											<el-button
												@click="searchList"
												size="small"
												type="primary"
												>搜索</el-button
											>
										</el-form-item>
									</el-col>
								</el-row>
							</el-col>
						</el-row>
						<el-row class="form-row2" v-show="rowShow">
							<el-col :span="21">
								<el-row :gutter="2" class="form-row2-row2">
									<el-col :span="4">
										<el-form-item
											class="cascader-dis"
											label="采购部门"
										>
											<el-cascader
												:options="
													statusForm.purchaseDept
												"
												:props="optionPropsPur"
												:show-all-levels="false"
												@change="
													searchCom(
														purchaseDeptId,
														'purchaseDeptId',
														'purType'
													)
												"
												clearable
												placeholder="请选择部门"
												ref="cascader"
												style="font-size: 12px"
												v-model="purchaseDeptId"
											></el-cascader>
										</el-form-item>
									</el-col>
									<el-col :span="4">
										<el-form-item label="收料组织">
											<el-select
												:clearable="true"
												@change="
													clearCom(
														stockOrgId,
														'stockOrgId'
													)
												"
												filterable
												placeholder="请选择组织"
												v-model="stockOrgId"
											>
												<el-option
													:key="item.id"
													:label="item.name"
													:value="item.id"
													@click="
														searchCom(
															stockOrgId,
															'stockOrgId'
														)
													"
													class="f_12"
													v-for="item in statusForm.receivingOrg"
												></el-option>
											</el-select>
										</el-form-item>
									</el-col>
									<el-col :span="4">
										<el-form-item
											class="cascader-dis"
											label="收料部门"
										>
											<el-cascader
												:options="
													statusForm.receivingDept
												"
												:props="optionProps"
												:show-all-levels="false"
												@change="
													searchCom(
														stockDeptId,
														'stockDeptId',
														'type'
													)
												"
												clearable
												placeholder="请选择部门"
												ref="cascaderType"
												style="font-size: 12px"
												v-model="stockDeptId"
											></el-cascader>
										</el-form-item>
									</el-col>
									<el-col :span="8">
										<el-form-item label="入库日期">
											<el-date-picker
												@change="dateCom(date)"
												class="f_12"
												end-placeholder="结束时间"
												format="YYYY-MM-DD"
												start-placeholder="开始时间"
												type="daterange"
												v-model="date"
												value-format="YYYY-MM-DD"
											></el-date-picker>
										</el-form-item>
									</el-col>
								</el-row>
							</el-col>
							<el-col :span="21">
								<el-row :gutter="2" class="form-row2-row2">
									<el-col :span="8" class="form-r2-r2-col">
										<el-form-item label="自定义条件1">
											<CustomCondition
												:getCustomerItem="statusForm"
												:key="compKey1"
												:reset="reset"
												@result="
													resultCom(
														$event,
														'customerSel1',
														'customerObj1',
														'tempObj1'
													)
												"
											></CustomCondition>
										</el-form-item>
									</el-col>
									<el-col :span="8">
										<el-form-item label="自定义条件2">
											<CustomCondition
												:getCustomerItem="statusForm"
												:key="compKey2"
												:reset="reset"
												@result="
													resultCom(
														$event,
														'customerSel2',
														'customerObj2',
														'tempObj2'
													)
												"
											></CustomCondition>
										</el-form-item>
									</el-col>
									<el-col :span="8">
										<el-form-item label="自定义条件3">
											<CustomCondition
												:getCustomerItem="statusForm"
												:key="compKey3"
												:reset="reset"
												@result="
													resultCom(
														$event,
														'customerSel3',
														'customerObj3',
														'tempObj3'
													)
												"
											></CustomCondition>
										</el-form-item>
									</el-col>
								</el-row>
							</el-col>
						</el-row>
					</el-form>
				</div>
				<div
					:style="{ height: heightNum + 'px' }"
					class="sale-page-table-body"
				>
					<el-table
						:data="tableData"
						:header-cell-style="headerBg"
						:max-height="heightNum"
						@cell-click="openDetails"
						@row-dblclick="openDetailsDB"
						@selection-change="handleSelectionChange"
						border
						class="table-box"
						ref="tableInfoRef"
						style="width: 100%; font-size: 12px"
						v-loading="tableLoading"
					>
						<el-table-column
							align="center"
							fixed
							type="selection"
							width="40"
						/>
						<el-table-column
							align="left"
							fixed
							label="销售订单号"
							prop="saleOrderNo"
							width="187"
						/>
						<el-table-column
							align="left"
							fixed
							label="采购订单号"
							prop="parentOrderBillNo"
							width="187"
						/>
						<el-table-column
							align="left"
							fixed
							label="采购入库单号"
							prop="billNo"
							width="187"
						>
							<template #default="scope">
								<span style="color: #4178d5">{{
									scope.row.billNo
								}}</span>
							</template>
						</el-table-column>
						<el-table-column
							align="left"
							label="入库日期"
							prop="date"
							width="83"
						/>
						<el-table-column
							align="center"
							label="单据状态"
							prop="documentStatus"
							width="72"
						/>
						<el-table-column
							align="left"
							label="供应商"
							prop="supplier"
							width="172"
						/>
						<el-table-column
							align="left"
							label="物料编码"
							prop="materialNumber"
							width="172"
						/>
						<el-table-column
							align="left"
							label="规格型号(SKU)"
							prop="specification"
							width="280"
						/>
						<el-table-column
							align="left"
							label="物料名称"
							prop="materialName"
							width="150"
						/>
						<el-table-column
							align="center"
							label="单位"
							prop="remaininStockUnit"
							width="72"
						/>
						<el-table-column
							align="right"
							label="实收数量"
							prop="realQty"
							width="100"
						/>
						<el-table-column
							align="right"
							label="含税单价"
							prop="taxPrice"
							width="100"
						/>
						<el-table-column
							align="right"
							label="税率%"
							prop="taxRate"
							width="100"
						/>
						<el-table-column
							align="right"
							label="单价"
							prop="price"
							width="100"
						/>
						<el-table-column
							align="right"
							label="金额"
							prop="amount"
							width="100"
						/>
						<el-table-column
							align="right"
							label="价税合计"
							prop="allAmount"
							width="100"
						/>
						<el-table-column
							align="left"
							label="仓库"
							prop="soStock"
							width="120"
						/>
						<el-table-column
							align="center"
							label="是否211"
							prop="is211"
							width="72"
						/>
						<el-table-column
							align="center"
							label="供应链"
							prop="supplyChain"
							width="72"
						/>
						<el-table-column
							align="left"
							label="备注"
							prop="note"
							width="280"
						/>
					</el-table>
				</div>
			</div>
			<!-- 操作结果 -->
			<el-dialog title="操作结果" v-model="dialogVisible" width="40%">
				<div class="sale-page-table-body">
					<el-table
						:data="tableInfo"
						:header-cell-class-name="headerRigthMethod"
						border
						class="table-box"
						height="300"
						id="tableInfo-table"
					>
						<el-table-column label="序号" prop="date" width="50">
							<template #default="scope">
								<span>{{ scope.$index + 1 }}</span>
							</template>
						</el-table-column>
						<el-table-column
							label="采购入库单"
							prop="orderNo"
							width="200"
						></el-table-column>
						<el-table-column label="指令" prop="a" width="110">
							<template #default>
								<span>{{
									actionType === 'submit'
										? '提交'
										: actionType === 'revoke'
										? '撤销'
										: actionType === 'audit'
										? '审核'
										: actionType === 'de-approval'
										? '反审'
										: actionType === 'delet'
										? '删除'
										: actionType === 'finish'
										? '完工'
										: actionType === 'close-case'
										? '结案'
										: actionType === 'de-excute-start'
										? '反执行至开工'
										: actionType === 'de-approval'
										? '反审'
										: actionType === 'delet'
										? '删除'
										: actionType === 'finish'
										? '完工'
										: actionType === 'close-case'
										? '结案'
										: actionType === 'de-excute-finish'
										? '反执行至完工'
										: ''
								}}</span>
							</template>
						</el-table-column>
						<el-table-column label="结果" width="80">
							<template #default="scope">
								<span
									:style="{ 'txt-Red': scope.row.isSuccess }"
									>{{
										scope.row.isSuccess ? '成功' : '失败'
									}}</span
								>
							</template>
						</el-table-column>
						<el-table-column label="信息" prop="msg">
							<template #default="scope">
								<span class="txt-Red">{{ scope.row.msg }}</span>
							</template>
						</el-table-column>
					</el-table>
				</div>
				<template #footer>
					<span class="dialog-footer">
						<span
							class="f-12 ext-black-light"
							style="margin-right: 25px"
							>共{{
								tableInfo && tableInfo.length > 0
									? tableInfo.length
									: 0
							}}条</span
						>
						<el-button
							@click="
								handelhtmlToExcel(
									'tableInfo-table',
									'操作结果'
								);
								dialogVisible = false;
							"
							size="small"
							>导 出</el-button
						>
						<el-button
							@click="dialogVisible = false"
							size="small"
							type="primary"
							>关 闭</el-button
						>
					</span>
				</template>
			</el-dialog>
			<!-- 选择上下查单据 -->
			<el-dialog
				:title="CXttile"
				top="15%"
				v-model="dialogCX"
				width="20%"
			>
				<div class="sale-page-table-body">
					<el-radio-group
						v-if="CXttile === '上查结果'"
						v-model="CXcheckVal"
					>
						<el-radio
							:key="index"
							:label="it.orderType"
							v-for="(it, index) in upOrDownList"
							>{{ it.orderName }}({{ it.orderCount }})</el-radio
						>
					</el-radio-group>
					<el-radio-group
						v-if="CXttile === '下查结果'"
						v-model="CXcheckVal"
					>
						<el-radio
							:key="index"
							:label="it.orderType"
							v-for="(it, index) in upOrDownList"
							>{{ it.orderName }}({{ it.orderCount }})</el-radio
						>
					</el-radio-group>
				</div>
				<template #footer>
					<span class="dialog-footer">
						<el-button @click="dialogCX = false" size="small"
							>取 消</el-button
						>
						<el-button @click="submitCX" size="small" type="primary"
							>确 定</el-button
						>
					</span>
				</template>
			</el-dialog>
			<div class="sale-page-footer">
				<div class="footer-left">
					<div>
						<span>实收数量:</span>
						<span class="sale-number-color">{{
							realQtyTotal === '' ? 0 : realQtyTotal
						}}</span>
					</div>
					<div>
						<span>价税合计:</span>
						<span class="sale-number-color">{{
							allAmountTotal === '' ? 0 : allAmountTotal
						}}</span>
					</div>
				</div>
				<div class="footer-right">
					<el-pagination
						:page-size="pageSize"
						:page-sizes="pagingList"
						:pager-count="5"
						:total="totalItemNumber"
						@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;
								"
								>{{ selectItem }}</text
							>行
						</span>
					</el-pagination>
				</div>
				<div class="footer-pagination-total-page">
					<span>共{{ totalPageNumber }}页</span>
				</div>
			</div>
		</div>
	</div>
</template>
<script>
import { mapState, mapActions } from 'vuex';
import { orderCopy } from '@/utils/takeMethods';
import { toRaw } from '@vue/reactivity';
import {
	exporthandel,
	htmlToExcel,
	removeChildren,
	resultComMethods,
	customChangeMethods,
	dateData,
	printPreview,
} from '@/utils/pubulic.js';
import { getStaffListApiUser } from '@/utils/api.js';
import CustomCondition from '@/components/public/CustomCondition.vue';
export default {
	name: 'WarehousePurchaseWarehouse',
	components: {
		CustomCondition,
	},
	data() {
		return {
			theadDate: [
				{
					thName: 'No.',
					field: '',
				},
				{
					thName: '采购订单号',
					field: 'parentOrderBillNo',
				},
				{
					thName: '物料编码',
					field: 'materialNumber',
				},
				{
					thName: '物料名称',
					field: 'materialName',
				},
				{
					thName: '规格型号',
					field: 'specification',
				},
				{
					thName: '仓库',
					field: 'soStock',
				},
				{
					thName: '采购员',
					field: 'purchaserName',
				},
				{
					thName: '实收数量',
					field: 'realQty',
				},
				{
					thName: '单位',
					field: 'remaininStockUnit',
				},
				{
					thName: '备注',
					field: 'note',
				},
			],
			optionProps: {
				expandTrigger: 'hover',
				checkStrictly: true,
				value: 'id',
				label: 'name',
				children: 'children',
				emitPath: false,
			},
			optionPropsPur: {
				expandTrigger: 'hover',
				checkStrictly: true,
				value: 'id',
				label: 'name',
				children: 'children',
				emitPath: false,
			},
			containerHeight: '', // body容器高度
			tableLoading: false, // 加载
			realQtyTotal: '', // 实收数量
			allAmountTotal: 0,
			pagingList: [50, 100, 150], // 底部分页
			pageSize: 50,
			pageNo: 1,
			totalItemNumber: 0,
			totalPageNumber: 0,
			selectItem: 0,
			statusForm: {
				// 自定义条件
				customCondition: [], // 自定义
				supplier: [],
				documentStatus: [], // 单据状态
				billType: [], // 单据类型
				receivingOrg: [], // 收料组织
				receivingDept: [], // 收料部门
				purchaseOrg: [], // 采购组织
				purchaseDept: [], // 采购部门
				purchasePerson: [], // 采购员
				warehouse: [], // 仓库
				childWarehouse: [], // 子仓库
				isFree: [], // 是否赠品
				is211: [], // 是否是211型号
				supplyChainType: [], // 供应链
				followPersons: [], // 仓管员
			},
			billNo: '', // 单据编号（采购入库单号）
			documentStatus: '', // 单据状态
			date: '', // 下单日期
			supplierId: '', // 供应商
			productKey: '', // 物料
			purchaseDeptId: '', // 采购部门
			stockOrgId: '', // 收料组织
			stockDeptId: '', // 收料部门
			customerSel1: '', // 条件
			customerSel2: '',
			customerSel3: '',
			customerObj1: null,
			customerObj2: null,
			customerObj3: null,
			tempObj1: null,
			tempObj2: null,
			tempObj3: null,
			compKey1: 0,
			compKey2: 0,
			compKey3: 0,
			search: {
				// 列表请求参数
				pageNo: 1,
				pageSize: 50,
				orgId:
					this.$store.state &&
					this.$store.state.org &&
					this.$store.state.org.orgId === ''
						? this.orgIdArr[0].id
						: this.$store.state.org.orgId,
				extention: {},
			},
			selectIndex: [], // 选择的条目
			tableData: [], // 表格数据
			rowShow: false, // 展开行
			heightNum: 0, // 搜索高度
			windowHeight: 0, // 屏幕高度
			isDown: true, // 按钮切换
			multipleSelection: [],
			btnType: [
				'exDetails',
				'add',
				'copy',
				'save',
				'split-order',
				'print-aterial-list',
				'finish',
				'close-case',
				'de-excute-start',
				'de-excute-finish',
				'de-close',
				'close',
				'push-down',
				'out-export',
				's-p-t-g8',
				'down-check',
			], // 禁用按钮
			dialogVisible: false, // 操作弹框显示
			tableInfo: [], // 操作结果
			actionType: '', // 操作类型
			OrdedetailIds: [], // 勾选的明细id集合
			dialogCX: false, // 上下查弹框控制
			upOrDownList: [], // 上查下查弹窗可选数据
			OrderNoNameslist: [], // 上查下查的订单号
			OrderNoNameStr: '', // 上查下查的订单号str
			CXaction: '', // 判断是上查还是下查
			CXttile: '', // 查询弹框的名称
			CXcheckVal: '', // 选择的查询radio
			OrderNoNames: [], // 选择的单据订单号集合
			reset: false,
		};
	},
	methods: {
		Init() {
			let vuexKey = ['warehouseAll', 'supplierSelect', 'dicPurInstock'];
			let setVuexData = [
				'setWarehouseAll',
				'setSupplierSelect',
				'setDicPurInstock',
			];
			let requestQueryUrl = [
				'SysConfig/GetWarehouse',
				'SysConfig/supplier/select',
				'Dictionary/purchaseinstock',
			];
			let resDataQuery = ['warehouse', 'supplier', 'customCondition'];

			vuexKey.forEach((ele, ind) => {
				if (this[vuexKey[ind]] && this[vuexKey[ind]].length > 0) {
					if (typeof resDataQuery[ind] == 'string') {
						this.statusForm[resDataQuery[ind]] = JSON.parse(
							JSON.stringify(this[vuexKey[ind]])
						);
					} else {
						resDataQuery[ind].forEach((ele, index) => {
							this.statusForm[resDataQuery[ind][index]] =
								JSON.parse(JSON.stringify(this[vuexKey[ind]]));
						});
					}
				} else {
					this.$http.get(requestQueryUrl[ind]).then((r) => {
						if (r.status === 200) {
							if (typeof resDataQuery[ind] == 'string') {
								this.statusForm[resDataQuery[ind]] = r.data;
							} else {
								resDataQuery[ind].forEach((ele, index) => {
									this.statusForm[resDataQuery[ind][index]] =
										r.data;
								});
							}
							this[setVuexData[ind]](
								this.statusForm[resDataQuery[ind]]
							);
						}
					});
				}
			});

			// 获取人员接口
			if (this.getAllUserList && this.getAllUserList.length > 0) {
				this.statusForm.followPersons = this.getAllUserList;
				this.statusForm.purchasePerson = this.getAllUserList;
			} else {
				getStaffListApiUser().then((r) => {
					r.status == 200 ? this.setGetAllUserList(r.data) : false;
					r.status == 200
						? (this.statusForm.followPersons = r.data)
						: (this.statusForm.followPersons = []);
					r.status == 200
						? (this.statusForm.purchasePerson = r.data)
						: (this.statusForm.purchasePerson = []);
				});
			}

			if (
				this.listGetStatus &&
				JSON.stringify(this.listGetStatus) !== '{}'
			) {
				for (const key in this.listGetStatus.documentStatus) {
					this.statusForm.documentStatus.push({
						id: key,
						name: this.listGetStatus.documentStatus[key],
					});
				}
				for (const key in this.listGetStatus.is211) {
					this.statusForm.is211.push({
						id: key,
						name: this.listGetStatus.is211[key],
					});
				}
				for (const key in this.listGetStatus.isFree) {
					this.statusForm.isFree.push({
						id: key,
						name: this.listGetStatus.isFree[key],
					});
				}
				for (const key in this.listGetStatus.supplyChainType) {
					this.statusForm.supplyChainType.push({
						id: key,
						name: this.listGetStatus.supplyChainType[key],
					});
				}
			} else {
				this.$http.get('SysConfig/GetStatus').then((req) => {
					// 获取需要的状态
					if (req.status == 200) {
						this.setListGetStatus(req.data);
						for (const key in req.data.documentStatus) {
							this.statusForm.documentStatus.push({
								id: key,
								name: req.data.documentStatus[key],
							});
						}
						for (const key in req.data.is211) {
							this.statusForm.is211.push({
								id: key,
								name: req.data.is211[key],
							});
						}
						for (const key in req.data.isFree) {
							this.statusForm.isFree.push({
								id: key,
								name: req.data.isFree[key],
							});
						}
						for (const key in req.data.supplyChainType) {
							this.statusForm.supplyChainType.push({
								id: key,
								name: req.data.supplyChainType[key],
							});
						}
					}
				});
			}

			// 枚举 - 单据状态、单据类型
			if (this.inStockType && JSON.stringify(this.inStockType) !== '{}') {
				for (const key in this.inStockType.purchaseInstockBillType) {
					this.statusForm.billType.push({
						id: key,
						name: this.inStockType.purchaseInstockBillType[key],
					});
				}
			} else {
				this.$http
					.showLoading(false)
					.get('PurchaseInstock/status/get')
					.then((req) => {
						if (req.status == 200) {
							this.setInStockType(req.data);
							for (const key in req.data
								.purchaseInstockBillType) {
								this.statusForm.billType.push({
									id: key,
									name: req.data.purchaseInstockBillType[key],
								});
							}
						}
					});
			}

			this.windowHeight = document.documentElement.clientHeight; // 计算表格高度和底部位置
			if (this.$route.query.detailsIds) {
				this.heightNum = this.windowHeight - 301; // 计算表格高度
			} else {
				this.heightNum = this.windowHeight - 258; // 计算表格高度
			}
			this.containerHeight = this.windowHeight - 102; // 计算容器的高度
			this.getList(this.search); // 获取列表
		},
		// 操作弹框导出
		handelhtmlToExcel(domid, title) {
			htmlToExcel(domid, title);
		},
		// 订单号 复制按钮
		saleOrderCopy() {
			orderCopy();
		},
		// 自定义条件赋值
		resultCom(result, fieldSale, fieldObj, tempObj) {
			let returnResult = resultComMethods(
				result,
				this[fieldSale],
				toRaw(result.outResult),
				this[tempObj],
				this.search.extention
			);
			this[fieldSale] = returnResult.fieldSale;
			this[fieldObj] = returnResult.fieldObj;
			this[tempObj] = returnResult.tempObj;
			this.search.extention = returnResult.search;
		},
		actionhandel(type) {
			// 顶部公共按钮操作
			this.actionType = type;
			if (this.multipleSelection && this.multipleSelection.length > 0) {
				let nweArysize = 0;
				let newArry = [
					...new Set(
						this.multipleSelection.map((item) => {
							return item.id;
						})
					),
				];
				let detailsIds = [
					...new Set(
						this.multipleSelection.map((item) => {
							console.log('detailsIds', item);
							return item.detailsId;
						})
					),
				];
				this.OrdedetailIds = [];
				this.OrdedetailIds = detailsIds;
				// 取单据订单号
				let orderNoArry = [
					...new Set(
						this.multipleSelection.map((item) => {
							return item.billNo;
						})
					),
				];
				this.OrderNoNames = [];
				this.OrderNoNames = orderNoArry;
				nweArysize = new Set(
					this.multipleSelection.map((item) => {
						return item.id;
					})
				).size;
				if (type === 'submit') {
					// 点击提交
					this.toolarFn('PurchaseInstock/submit/status', newArry);
				}
				if (type === 'revoke') {
					// 点击撤销
					this.toolarFn('PurchaseInstock/revoke', newArry);
				}
				if (type === 'audit') {
					// 点击审核
					this.toolarFn('PurchaseInstock/check', newArry);
				}
				if (type === 'de-approval') {
					// 点击反审核
					this.toolarFn('PurchaseInstock/recheck', newArry);
				}
				if (type === 'delet') {
					// 点击删除
					this.toolarFn('PurchaseInstock/delete', newArry);
				}
				if (type === 'up-check') {
					// 点击查询--上查
					this.$http
						.post('PurchaseInstock/upper', { ids: detailsIds })
						.then((res) => {
							if (res.status === 200) {
								if (res.data.length === 0 || res.data === []) {
									this.$message.warning({
										message: '没有可上查的单据!',
										showClose: true,
									});
								} else {
									this.upOrDownList = res.data;
									this.dialogCX =
										res.data && res.data.length > 0
											? true
											: false;
									this.CXttile = '上查结果';
									this.CXcheckVal =
										res.data && res.data.length === 1
											? res.data[0].orderType
											: null; //只有一个，就个默认值
								}
							}
						});
				}
				if (type === 'print-brower') {
					if (
						this.multipleSelection.filter((it) => {
							return it.documentStatus !== '已审核';
						}).length > 0
					) {
						this.$message.error({
							message: '仅可打印已审核的单据',
							showClose: true,
						});
					} else {
						this.multipleSelection = this.multipleSelection.sort(
							dateData('date', false)
						);
						setTimeout(() => {
							printPreview(1, '采购入库单');
						}, 200);
					}
				}
			} else {
				//按列表字段导出,点击全部导出
				if (
					type !== 'add' &&
					type !== 'refresh' &&
					type !== 'list-export' &&
					type !== 'all-export'
				) {
					this.$message.error({
						message: '请先勾选',
						showClose: true,
					});
				}
			}
			if (type === 'list-export' || type === 'all-export') {
				if (type === 'all-export') {
					exporthandel('PurchaseInstock/ExportAll', this.search);
				} else {
					exporthandel('PurchaseInstock/Export', this.search);
				}
			}
			if (type === 'refresh') {
				// 点击刷新
				this.getList(this.search);
			}
		},
		getcheckInfo() {
			// 上下查数据
			if (this.$route.query) {
				this.search.detailsIds = this.$route.query.detailsIds
					? JSON.parse(
							decodeURIComponent(this.$route.query.detailsIds)
					  )
					: [];
				this.CXaction =
					this.$route.query.actionType === 'up' ? '上查' : '下查';
				let ordNostr = '';
				this.OrderNoNameslist = this.$route.query.OrderNoNames
					? JSON.parse(
							decodeURIComponent(this.$route.query.OrderNoNames)
					  )
					: [];
				if (this.OrderNoNameslist) {
					this.OrderNoNameslist.forEach((it, index) => {
						if (index < 5) {
							ordNostr = ordNostr + it + ',';
						}
					});
				}
				this.OrderNoNameStr = ordNostr;
			}
		},
		submitCX() {
			// 确定上下查
			if (this.CXcheckVal) {
				let pathName = '';
				if (this.CXcheckVal === 2) {
					// "采购订单"
					pathName = 'DemandOrder';
				}
				let ary = [];
				ary = this.upOrDownList.filter((it) => {
					return it.orderType === this.CXcheckVal;
				});
				if (pathName) {
					this.dialogCX = false;
					this.$router.push({
						name: pathName,
						query: {
							detailsIds: JSON.stringify(
								ary.length > 0 ? ary[0].detailsIds : []
							),
							actionType:
								this.CXttile === '上查结果' ? 'up' : 'down',
							actionName: '采购入库单',
							OrderNoNames: JSON.stringify(
								ary.length > 0 ? ary[0].billNos : []
							),
						},
					});
					this.dialogCX = false;
				}
			} else {
				this.$message.error({
					message: '请先选择在确认',
					showClose: true,
				});
			}
		},
		toolarFn(path, newArry, detailsIds, type) {
			// 顶部操作方法
			let formdata = detailsIds ? { ids: detailsIds } : { ids: newArry };
			this.$http.post(path, formdata).then((res) => {
				if (res.status === 200) {
					this.dialogVisible = true;
					this.tableInfo = res.data;
					this.getList(this.search);
				}
			});
		},
		headerBg({ columnIndex }) {
			// 表头居右
			if (columnIndex === 17 || columnIndex === 18) {
				return `text-align:right!important;`;
			}
		},
		headerRigthMethod({ row, column, rowIndex, columnIndex }) {
			return 'headerRight';
		},
		handleSelectionChange(val) {
			// 表格选中条目发生改变
			this.selectItem = val.length;
			this.multipleSelection = val;
		},
		handleSizeChange(val) {
			// 每页条数
			this.pageSize = val;
			this.search.pageSize = val;
			this.totalPageNumber = Math.ceil(
				this.totalItemNumber / this.pageSize
			);
			this.getList(this.search);
		},
		handleCurrentChange(val) {
			// 分页当前页切换
			this.pageNo = val;
			this.search.pageNo = val;
			this.getList(this.search);
		},
		openRow() {
			// 搜索切换高度
			this.isDown = !this.isDown;
			this.rowShow = !this.rowShow;
			if (this.rowShow) {
				this.heightNum = this.heightNum - 80;
			} else {
				this.heightNum = this.heightNum + 80;
			}
		},
		openDetails(row, column) {
			// 打开详情页
			if (column.property == 'billNo') {
				this.$router.push({
					path: '/warehouse/purchase/activeWareHouse',
					query: {
						type: 'details',
						id: row.id,
						status: row.documentStatus,
					},
				});
			}
		},
		openDetailsDB(row, column) {
			this.$router.push({
				path: '/warehouse/purchase/activeWareHouse',
				query: {
					type: 'details',
					id: row.id,
					status: row.documentStatus,
				},
			});
		},
		getList(query) {
			// 列表请求
			this.tableLoading = true;
			this.$http.post('PurchaseInstock/list', query).then((req) => {
				if (req.status == 200) {
					this.tableLoading = false;
					this.tableData = req.data.list;
					this.totalItemNumber = req.data.totalCount;
					this.totalPageNumber = Math.ceil(
						this.totalItemNumber / this.pageSize
					);
					this.realQtyTotal = req.data.realQtyTotal;
					this.allAmountTotal = req.data.allAmountTotal;
					if (
						this.multipleSelection.length &&
						this.multipleSelection.length > 0
					) {
						let detailsIds = [
							...new Set(
								this.multipleSelection.map((item) => {
									return item.detailsId;
								})
							),
						];
						this.tableData.forEach((el, ind) => {
							if (detailsIds.indexOf(el.detailsId) !== -1) {
								this.$refs.tableInfoRef.toggleRowSelection(
									this.tableData[ind],
									true
								);
							}
						});
					}
				}
			});
		},
		searchList() {
			// 搜索
			this.getList(this.search);
		},
		searchCom(val, key, type) {
			// 搜索公共方法
			if (type) {
				if (val && val != ' ') {
					this.search[key] = val;
					// this.searchList()
				} else {
					delete this.search[key];
					// this.searchList()
				}
			} else {
				this.search[key] = val;
				// this.searchList()
			}
			if (key === 'stockOrgId') {
				if (val) {
					this.stockDeptId = '';
					this.$http
						.post('SysConfig/GetDept/' + this.stockOrgId)
						.then((res) => {
							// 根据组织获取部门
							if (res.status === 200) {
								this.statusForm.receivingDept = removeChildren(
									res.data
								);
							}
						});
				}
			}
			if (type) {
				if (type === 'purType') {
					this.$refs.cascader.togglePopperVisible(); // 选择之后将下拉框界面收起
				}
				if (type === 'type') {
					this.$refs.cascaderType.togglePopperVisible(); // 选择之后将下拉框界面收起
				}
			}
		},
		searchComS(val, key, type) {
			// 搜索公共方法
			if (type) {
				if (val && val != ' ') {
					this.search[key] = val;
					this.searchList();
				} else {
					delete this.search[key];
					this.searchList();
				}
			} else {
				this.search[key] = val;
				this.searchList();
			}
			if (key === 'stockOrgId') {
				if (val) {
					this.stockDeptId = '';
					this.$http
						.post('SysConfig/GetDept/' + this.stockOrgId)
						.then((res) => {
							// 根据组织获取部门
							if (res.status === 200) {
								this.statusForm.receivingDept = removeChildren(
									res.data
								);
							}
						});
				}
			}
			if (type) {
				if (type === 'purType') {
					this.$refs.cascader.togglePopperVisible(); // 选择之后将下拉框界面收起
				}
				if (type === 'type') {
					this.$refs.cascaderType.togglePopperVisible(); // 选择之后将下拉框界面收起
				}
			}
		},
		clearCom(val, key) {
			// 删除的公共方法
			if (!val) {
				delete this.search[key];
				this.searchList();
			}
		},
		dateCom(val) {
			// 采购日期
			this.date = val;
			if (this.date == null) {
				delete this.search.beginDate;
				delete this.search.endDate;
				this.searchList();
			} else {
				this.search.beginDate = this.date[0];
				this.search.endDate = this.date[1];
				this.searchList();
			}
		},
		resetSearch() {
			// 重置
			let arr = [
				'billNo',
				'documentStatus',
				'supplierId',
				'productKey',
				'purchaseDeptId',
				'stockOrgId',
				'stockDeptId',
				'date',
				'beginDate',
				'endDate',
			];
			for (let index = 0; index < arr.length; index++) {
				this[arr[index]] = '';
			}
			this.reset = true;
			this.search = {
				pageNo: this.pageNo,
				pageSize: this.pageSize,
				orgId:
					this.$store.state && this.$store.state.org
						? this.$store.state.org.orgId
						: null,
			};
			this.search.detailsIds = [];
			this.search.extention = {};
			this.customerSel1 = '';
			this.customerSel2 = '';
			this.customerSel3 = '';
			if (this.$route.query.detailsIds) {
				this.search.detailsIds = JSON.parse(
					decodeURIComponent(this.$route.query.detailsIds)
				);
			}
			setTimeout(() => {
				this.reset = false;
			}, 200);
			this.searchList();
		},
		getStatusType() {
			// 获取状态和类型
			this.statusForm.receivingOrg = this.orgIdArr;
			this.statusForm.purchaseOrg = this.orgIdArr;
			if (
				this.$store.state &&
				this.$store.state.org &&
				this.$store.state.org.orgId
			) {
				this.$http
					.post('SysConfig/GetDept/' + this.$store.state.org.orgId)
					.then((res) => {
						// 根据组织获取部门
						if (res.status === 200) {
							this.statusForm.purchaseDept = removeChildren(
								res.data
							);
						}
					});
			}
		},
		...mapActions({
			setSupplierSelect: 'storeData/setSupplierSelect',
			setWarehouseAll: 'storeData/setWarehouseAll',
			setListGetStatus: 'storeData/setListGetStatus',
			setInStockType: 'storeData/setInStockType',
			setDicPurInstock: 'storeData/setDicPurInstock',
			setGetAllUserList: 'storeData/setGetAllUserList',
		}),
		addListWareHouse(event) {
			if (event.keyCode === 13) {
				this.searchList();
			}
		},
	},
	computed: {
		...mapState({
			orgIdArr: (state) => (state ? state.user.orgIdArr : null),
			getDeptId: (state) => (state ? state.org.orgId : null),
			supplierSelect: (state) => state.storeData.supplierSelect,
			warehouseAll: (state) => state.storeData.warehouseAll,
			listGetStatus: (state) => state.storeData.listGetStatus,
			inStockType: (state) => state.storeData.inStockType,
			dicPurInstock: (state) => state.storeData.dicPurInstock,
			getAllUserList: (state) => state.storeData.getAllUserList,
		}),
		orgIdChange() {
			// 监听ID
			return this.$store.state && this.$store.state.org
				? this.$store.state.org.orgId
				: null;
		},
	},
	created: function () {
		this.getcheckInfo();
		this.Init();
		this.getStatusType();
	},
	activated() {
		document.addEventListener('keyup', this.addListWareHouse, false);
		this.$refs.tableInfoRef.clearSelection();
	},
	deactivated() {
		document.removeEventListener('keyup', this.addListWareHouse, false);
	},
	watch: {
		// 监听组织
		orgIdChange: function () {
			this.search.orgId =
				this.$store.state && this.$store.state.org
					? this.$store.state.org.orgId
					: null;
			// if (!this.search.orgId) {
			// 	return;
			// }
			this.getList(this.search);
			this.resetSearch();
		},
		customerSel1: function () {
			let returnR = customChangeMethods(
				this.customerSel1,
				this.customerSel2,
				this.customerSel3,
				'x',
				this.compKey1
			);
			this.customerSel1 = returnR.selfVal;
			this.customerSel2 = returnR.otherO;
			this.customerSel3 = returnR.otherT;
			this.compKey1 = returnR.keyRsf;
		},
		customerSel2: function () {
			let returnR = customChangeMethods(
				this.customerSel2,
				this.customerSel1,
				this.customerSel3,
				'y',
				this.compKey2
			);
			this.customerSel2 = returnR.selfVal;
			this.customerSel1 = returnR.otherO;
			this.customerSel3 = returnR.otherT;
			this.compKey2 = returnR.keyRsf;
		},
		customerSel3: function () {
			let returnR = customChangeMethods(
				this.customerSel3,
				this.customerSel1,
				this.customerSel2,
				'z',
				this.compKey3
			);
			this.customerSel3 = returnR.selfVal;
			this.customerSel1 = returnR.otherO;
			this.customerSel2 = returnR.otherT;
			this.compKey3 = returnR.keyRsf;
		},
	},
};
</script>
<style lang="scss" scoped>
@import '@/assets/style/saleOrder/saleOrderCommon.scss';
@import '@/assets/style/saleOrder/orderList.scss';
@import '@/assets/style/saleOrder/common.scss';
.sale-container ::v-deep(.s-p-t-g1::after) {
	background: #fff !important;
}
.sale-container ::v-deep(.page-top .s-p-t-g1) {
	padding-right: 0 !important;
}
.sale-container ::v-deep(.page-top .s-p-t-g2) {
	padding: 0 !important;
	padding-right: 24px !important;
}
.sale-container ::v-deep(.page-top .s-p-t-g2 .el-button) {
	padding-left: 0 !important;
}
::v-deep(.el-date-editor) {
	width: 100% !important;
}
::v-deep(.el-date-editor .el-range-separator) {
	display: flex !important;
	justify-content: center !important;
	align-items: center !important;
	color: #9b9fa5 !important;
}
.cascader-dis {
	::v-deep(.el-form-item__content) {
		display: flex;
		justify-content: center !important;
		align-items: center !important;
	}
}
</style>
