<template>
	<div class="main-container order-mc-bg">
		<!-- 打印摸版组件-->
		<PrintModel
			:printData="{
				tableData: multipleSelection,
				orderName: '销售退货订单',
			}"
			:theadDate="theadDate"
			v-show="false"
		></PrintModel>
		<div class="return-order-container">
			<!-- 顶部按钮组 -->
			<div class="return-order-page-top" ref="getSaleTopHeight">
				<toolBtn
					:btnType="btnType"
					@actionhandel="actionhandel"
				></toolBtn>
			</div>
			<!-- 上下查 -->
			<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="return-order-page-body">
				<!-- 表头 -->
				<div
					class="return-order-table-header"
					ref="getSaleTableHeader"
					style="margin-top: 10px"
				>
					<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
												@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'
													)
												"
												placeholder="选择状态"
												v-model="documentStatus"
											>
												<el-option
													:key="item.id"
													:label="item.name"
													:value="item.id"
													@click="
														searchCom(
															documentStatus,
															'documentStatus',
															'opt'
														)
													"
													class="f_12"
													v-for="item in statusForm.documentStatus"
												></el-option>
											</el-select>
										</el-form-item>
									</el-col>
									<el-col :span="7">
										<el-form-item label="退货日期">
											<el-date-picker
												@change="
													dateCom(
														returnData,
														'beginDate',
														'endDate'
													)
												"
												class="f_12"
												end-placeholder="结束时间"
												format="YYYY-MM-DD"
												start-placeholder="开始时间"
												type="daterange"
												v-model="returnData"
												value-format="YYYY-MM-DD"
											></el-date-picker>
										</el-form-item>
									</el-col>
									<el-col :span="4">
										<el-form-item
											class="Iisshow"
											label="退货客户"
										>
											<el-input
												class="f-12"
												placeholder="请输入客户"
												@keyup.enter="
													searchComS(
														search.retcust,
														'retcust',
														'ipt'
													)
												"
												v-model="search.retcust"
											></el-input>
											<div
												:data-clipboard-text="
													search.retcust
												"
												@click="saleOrderCopy"
												class="copyBtn"
											>
												<i
													class="el-icon-document-copy"
												/>
											</div>
											<!-- <el-select
                        :clearable="true"
                        @change="clearCom(retcust,'retcust')"
                        class="f_12"
                        filterable
                        placeholder="请输入退货客户名称"
                        remote
                        reserve-keyword
                        v-model="retcust"
                      >
                        <el-option
                          :key="item.id"
                          :label="item.name"
                          :value="item.id"
                          @click="searchCom(item.id,'retcust','opt')"
                          v-for="item in getCustomerNumList"
                        ></el-option>
                      </el-select>-->
										</el-form-item>
									</el-col>
									<el-col :span="4">
										<el-form-item
											class="Iisshow"
											label="产品"
										>
											<el-input
												@input="
													searchCom(
														product,
														'product',
														'ipt'
													)
												"
												@keyup.enter="
													searchComS(
														product,
														'product',
														'ipt'
													)
												"
												class="f_12"
												placeholder="请输入产品物料名称/编码/SKU"
												v-model.trim="product"
											></el-input>
											<div
												:data-clipboard-text="product"
												@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="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="return-order-table-body"
				>
					<el-table
						:data="tableData"
						:max-height="heightNum"
						@row-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="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="returnDate"
							width="83"
						/>
						<el-table-column
							align="left"
							label="单据状态"
							prop="documentStatus"
							width="72"
						/>
						<el-table-column
							align="left"
							label="退货客户"
							prop="retcust"
							width="172"
						/>
						<el-table-column
							align="left"
							label="销售组织"
							prop="saleOrg"
							width="172"
						/>
						<el-table-column
							align="left"
							label="物料编码"
							prop="materialNumber"
							width="150"
						/>
						<el-table-column
							align="left"
							label="规格型号(SKU)"
							prop="sku"
							width="280"
						/>
						<el-table-column
							align="left"
							label="物料名称"
							prop="partName"
							width="100"
						/>
						<el-table-column
							align="left"
							label="仓库"
							prop="stock"
							width="120"
						/>
						<el-table-column
							align="center"
							label="库存单位"
							prop="unit"
							width="72"
						/>
						<el-table-column
							align="right"
							label="实退数量"
							prop="realQty"
							width="100"
						/>
						<el-table-column
							align="center"
							label="退货类型"
							prop="returnType"
							width="72"
						/>
						<el-table-column
							align="right"
							label="含税单价"
							prop="taxPrice"
							width="100"
						/>
						<el-table-column
							align="right"
							label="价税合计"
							prop="allAmount"
							width="100"
						/>
						<el-table-column
							align="right"
							label="价税合计(本位币)"
							prop="allAmountLc"
							width="120"
						/>
					</el-table>
				</div>
			</div>
			<!-- 操作结果 -->
			<el-dialog
				:destroy-on-close="true"
				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
				:destroy-on-close="true"
				title="选择下推单据"
				top="15%"
				v-model="dialogxt"
				width="20%"
			>
				<el-radio-group v-model="pushdownVal">
					<el-radio
						:key="item.id"
						:label="item.id"
						:value="item.id"
						v-for="item in pushDownList"
						>{{ item.value }}</el-radio
					>
				</el-radio-group>
				<template #footer>
					<span class="dialog-footer">
						<el-button @click="dialogxt = false" size="small"
							>取 消</el-button
						>
						<el-button
							@click="sumitPushdown(pushdownVal)"
							size="small"
							type="primary"
							>确 定</el-button
						>
					</span>
				</template>
			</el-dialog>
			<!-- 选择上下查单据 -->
			<el-dialog
				:destroy-on-close="true"
				: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="return-order-page-footer">
				<div class="footer-left">
					<div>
						<span>实退数量:</span>
						<span class="sale-number-color">{{ realQtySm }}</span>
					</div>
					<div>
						<span>价税合计(本位币):</span>
						<span class="sale-number-color">{{
							allAmountLcSm
						}}</span>
					</div>
				</div>
				<div class="footer-right">
					<el-pagination
						:page-size="pageSize"
						:page-sizes="pageSizes"
						:pager-count="5"
						:total="total"
						@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>共{{ total }}页</span>
				</div>
			</div>
		</div>
	</div>
</template>
<script>
import { mapState, mapActions } from 'vuex';
import { toRaw } from '@vue/reactivity';
import { orderCopy } from '@/utils/takeMethods';
import { getStaffListApiUser } from '@/utils/api.js';
import {
	getStatusType,
	exporthandel,
	htmlToExcel,
	resultComMethods,
	customChangeMethods,
	printPreview,
	dateData,
} from '@/utils/pubulic.js';
import { returnOrderPrint } from '@/utils/printPagekey.js';
import CustomCondition from '@/components/public/CustomCondition.vue';

export default {
	name: 'SaleReturnOrder',
	components: {
		CustomCondition,
	},
	data() {
		return {
			theadDate: JSON.parse(JSON.stringify(returnOrderPrint)), // 打印要的字段表
			tableLoading: false, // 列表加载
			billNo: '', // 单据编号
			documentStatus: '', // 单据状态
			returnData: '', // 退货日期
			dateBegin: '', // 退货日期 S
			dateEnd: '', // 退货日期 E
			retcust: '', // 退货客户
			product: '', // 产品
			heightNum: 0, // 表格高度
			windowHeight: 0, // 屏幕高度
			pageNo: 1, // 当前页
			pageSize: 50, // 页码
			totalItemNumber: 0, // 总计
			realQtySm: '', // 底部销售数量
			allAmountLcSm: '', // 价税合计
			total: 0, // 共计页数
			customerSel1: '', // 自定义条件
			customerSel2: '',
			customerSel3: '',
			customerObj1: null,
			customerObj2: null,
			customerObj3: null,
			tempObj1: null,
			tempObj2: null,
			tempObj3: null,
			compKey1: 0,
			compKey2: 0,
			compKey3: 0,
			rowShow: false, // 搜索是否 隐藏
			isDown: true, // 搜索是否 展开
			selectItem: 0, // 分页 选中行
			multipleSelection: [], // 选中的行
			pageSizes: [50, 100, 150], // 页码数组
			tableData: [], // 表格
			btnType: [
				'save',
				'split-order',
				'print-aterial-list',
				'finish',
				'close',
				'de-close',
				'close-case',
				'de-excute-start',
				'de-excute-finish',
				'exDetails',
			], // 隐藏按钮
			search: {
				// 搜索
				retcust: '',
				pageNo: 1, // 页码
				pageSize: 50, // N/页
				orgId:
					this.$store.state &&
					this.$store.state.org &&
					this.$store.state.org.orgId === ''
						? this.orgIdArr[0].id
						: this.$store.state.org.orgId,
			},
			statusForm: {
				customCondition: [], // 自定义条件
				warehouse: [], // 仓库
				childWarehouse: [], // 仓位
				is211: [], // 是否211
				isFree: [], // 是否赠品
				returnTypes: [], // 退货类型
				dcurrency: [], // 结算币别
				salePersons: [], // 销售员
				saleDept: [], // 销售部门
				documentStatus: [], // 单据状态
				saleOrgs: [], // 销售组织
				reccondition: [], // 收款条件
				settleType: [], // 结算方式
			},
			dialogVisible: false, // 操作弹框显示
			tableInfo: [], // 操作结果
			actionType: '', // 操作类型
			pushdownVal: null, // 下推选择
			dialogxt: false, // 选择下推单据弹框显示
			OrdedetailIds: [], // 勾选的明细id集合
			pushDownList: [], // 下推集合
			dialogCX: false, // 上下查弹框控制
			upOrDownList: [], // 上查下查弹窗可选数据
			OrderNoNameslist: [], // 上查下查的订单号
			OrderNoNameStr: '', // 上查下查的订单号str
			CXaction: '', // 判断是上查还是下查
			CXttile: '', // 查询弹框的名称
			CXcheckVal: '', // 选择的查询radio
			OrderNoNames: [], // 选择的单据订单号集合
			getCustomerNumList: [], // 退货客户
			reset: false, // 重置
		};
	},
	methods: {
		Init() {
			let vuexKey = [
				'dcurrency',
				'customer',
				'settletype',
				'recondtion',
				'warehouseAll',
				'dicRetrunOrder',
			];
			let setVuexData = [
				'setDcurrency',
				'setCustomer',
				'setSettletype',
				'setRecondtion',
				'setWarehouseAll',
				'setDicRetrunOrder',
			];
			let requestQueryUrl = [
				'SysConfig/GetDcurrency',
				'SysConfig/GetCustomer',
				'SysConfig/GetSettletype',
				'SysConfig/GetReccondition',
				'SysConfig/GetWarehouse',
				'Dictionary/RetrunOrder',
			];
			let resDataQuery = [
				'dcurrency',
				'getCustomerNumList',
				'settleType',
				'reccondition',
				'warehouse',
				'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.statusType && JSON.stringify(this.statusType) !== '{}') {
				for (const key in this.statusType.is211) {
					// 是否211
					this.statusForm.is211.push({
						id: key,
						name: this.statusType.is211[key],
					});
				}
				for (const key in this.statusType.isFree) {
					// 是否赠品
					this.statusForm.isFree.push({
						id: key,
						name: this.statusType.isFree[key],
					});
				}
				for (const key in this.statusType.returnStockType) {
					// 退货类型
					this.statusForm.returnTypes.push({
						id: key,
						name: this.statusType.returnStockType[key],
					});
				}
				for (const key in this.statusType.documentStatus) {
					// 单据状态
					this.statusForm.documentStatus.push({
						id: key,
						name: this.statusType.documentStatus[key],
					});
				}
			} else {
				this.$http.get('ReturnStockOrder/GetStatus').then((r) => {
					if (r.status == 200) {
						this.setStatusType(r.data);
						for (const key in r.data.is211) {
							// 是否211
							this.statusForm.is211.push({
								id: key,
								name: r.data.is211[key],
							});
						}
						for (const key in r.data.isFree) {
							// 是否赠品
							this.statusForm.isFree.push({
								id: key,
								name: r.data.isFree[key],
							});
						}
						for (const key in r.data.returnStockType) {
							// 退货类型
							this.statusForm.returnTypes.push({
								id: key,
								name: r.data.returnStockType[key],
							});
						}
						for (const key in r.data.documentStatus) {
							// 单据状态
							this.statusForm.documentStatus.push({
								id: key,
								name: r.data.documentStatus[key],
							});
						}
					}
				});
			}

			// 公共人员接口 - 'SysConfig/GetUserList','testUserPerson','setGetAllUserList' 'getAllUserList'
			if (this.getAllUserList && this.getAllUserList.length > 0) {
				this.statusForm.salePersons = this.getAllUserList;
			} else {
				getStaffListApiUser().then((r) => {
					r.status == 200 ? this.setGetAllUserList(r.data) : false;
					r.status == 200
						? (this.statusForm.salePersons = r.data)
						: (this.statusForm.salePersons = []);
				});
			}

			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);
		},
		getStatusTypeObject() {
			this.statusForm.saleOrgs = this.orgIdArr;
		},
		async getStatusTypeInfo() {
			this.statusForm.saleDept = await getStatusType(
				'SysConfig/GetDept/',
				this.getOrgId,
				'post'
			); // 销售部门
		},
		dateCom(val, keyS, keyE) {
			// 公共日期
			if (!val) {
				delete this.search[keyS];
				delete this.search[keyE];
				// this.searchList()
			} else {
				this.search[keyS] = val[0];
				this.search[keyE] = val[1];
				// this.searchList()
			}
		},
		clearCom(val, key) {
			// 公共删除
			if (!val) {
				delete this.search[key];
				this.searchList();
			}
		},

		searchCom(val, key) {
			// 公共搜索方法
			this.search[key] = val;
			if (val) {
				this.search[key] = val;

				// this.searchList()
			} else {
				delete this.search[key];
				// this.searchList()
			}
		},
		searchComS(val, key) {
			// 公共搜索方法
			this.search[key] = val;
			if (val) {
				this.search[key] = val;

				this.searchList();
			} else {
				delete this.search[key];
				this.searchList();
			}
		},

		getList(search) {
			// 获取列表
			this.tableLoading = true;
			this.$http.post('ReturnStockOrder/GetList', search).then((r) => {
				if (r.status == 200) {
					this.tableLoading = false;
					this.tableData = r.data.list;
					this.realQtySm = r.data.realQtySm;
					this.allAmountLcSm = r.data.allAmountLcSm;
					this.total = r.data.total;
					if (
						this.multipleSelection.length &&
						this.multipleSelection.length > 0
					) {
						let detailsIds = [
							...new Set(
								this.multipleSelection.map((item) => {
									return item.id;
								})
							),
						];
						this.tableData.forEach((el, ind) => {
							if (detailsIds.indexOf(el.id) !== -1) {
								this.$refs.tableInfoRef.toggleRowSelection(
									this.tableData[ind],
									true
								);
							}
						});
					}
				}
			});
		},
		actionhandel(type) {
			// 判断顶部点击的方法
			this.actionType = type;
			if (type == 'add') {
				this.$router.push({
					path: '/sale/salemange/createReturnOrder',
					query: { type: 'add', id: '', sjtag: Math.random() * 36 },
				});
			}
			if (this.multipleSelection && this.multipleSelection.length > 0) {
				let nweArysize = 0;
				let newArry = [
					...new Set(
						this.multipleSelection.map((item) => {
							return item.orderId;
						})
					),
				];
				let orderNoArry = [
					// 取单据订单号
					...new Set(
						this.multipleSelection.map((item) => {
							return item.billNo;
						})
					),
				];
				this.OrderNoNames = [];
				this.OrderNoNames = orderNoArry;
				let detailsIds = [
					...new Set(
						this.multipleSelection.map((item) => {
							return item.id;
						})
					),
				];
				this.OrdedetailIds = [];
				this.OrdedetailIds = detailsIds;
				nweArysize = new Set(
					this.multipleSelection.map((item) => {
						return item.orderId;
					})
				).size;
				if (type === 'copy') {
					// 点击复制
					if (nweArysize !== 1 && nweArysize > 0) {
						this.$message.error({
							message: '只能克隆单号相同的数据',
							showClose: true,
						});
					} else {
						this.$router.push({
							path: '/sale/salemange/createReturnOrder',
							query: {
								type: 'copy',
								id: this.multipleSelection[0].orderId,
								detailsIds: detailsIds,
								sjtag: Math.random() * 36,
							},
						});
					}
				}
				if (type === 'submit') {
					// 点击提交
					this.toolarFn('ReturnStockOrder/CommitList', newArry);
				}
				if (type === 'revoke') {
					// 点击撤销
					this.toolarFn('ReturnStockOrder/Revoke', newArry);
				}
				if (type === 'audit') {
					// 点击审核
					this.toolarFn('ReturnStockOrder/Check', newArry);
				}
				if (type === 'de-approval') {
					// 点击反审核
					this.toolarFn('ReturnStockOrder/NoCheck', newArry);
				}
				if (type === 'delet') {
					// 点击删除
					this.toolarFn('ReturnStockOrder/Delete', newArry);
				}
				if (type === 'push-down') {
					// 点击下推
					if (detailsIds.length <= 20) {
						if (
							this.multipleSelection.filter((it) => {
								return it.documentStatus !== '已审核';
							}).length > 0
						) {
							this.$message.error({
								message:
									'单据状态错误，只能选择单据状态为已审核单据，请重新选择！',
								showClose: true,
							});
						} else {
							this.$http
								.showLoading(false)
								.get('ReturnStockOrder/GetOrderType')
								.then((r) => {
									// 得到下推单据的类型
									if (r.status == 200) {
										this.pushDownList = [];
										for (const key in r.data.orderType) {
											this.pushDownList.push({
												id: key,
												value: r.data.orderType[key],
											});
										}
									}
								});
							this.dialogxt = true;
						}
					} else {
						this.$message.warning({
							message: '最多一次性下推二十条明细',
							showClose: true,
						});
					}
				}
				if (type === 'up-check') {
					// 点击查询--上查
					this.$http
						.post('ReturnStockOrder/GetUpNumber', {
							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 === 'down-check') {
					// 点击查询--下查
					this.$http
						.post('ReturnStockOrder/GetDownNumber', {
							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('returnDate', 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') {
				exporthandel(
					type === 'list-export'
						? 'ReturnStockOrder/Export'
						: 'ReturnStockOrder/ExportAll',
					this.search
				);
			}
			if (type === 'refresh') {
				// 点击刷新
				this.getList(this.search);
			}
		},
		toolarFn(path, newArry, type) {
			// 顶部操作方法
			let formdata = { ids: newArry };
			this.$http.post(path, formdata).then((res) => {
				if (res.status === 200) {
					this.dialogVisible = true;
					this.tableInfo = res.data;
					this.getList(this.search);
				}
			});
		},
		searchList() {
			// 搜索
			this.getList(this.search);
		},
		// 订单号 复制按钮
		saleOrderCopy() {
			orderCopy();
		},
		headerRigthMethod({ row, column, rowIndex, columnIndex }) {
			return 'headerRight';
		},
		handleSelectionChange(val) {
			// 表格选中条目发生改变
			this.selectItem = val.length;
			this.multipleSelection = val;
		},
		handleSizeChange(val) {
			// 分页 N/页
			this.pageSize = val;
			this.search.pageSize = this.pageSize;
			this.getList(this.search);
		},
		handleCurrentChange(val) {
			// 分页 当前页
			this.pageNo = val;
			this.search.pageNo = this.pageNo;
			this.getList(this.search);
		},
		openRow() {
			// 展开的行
			this.isDown = !this.isDown;
			this.rowShow = !this.rowShow;
			if (this.rowShow) {
				this.heightNum = this.heightNum - 40;
			} else {
				this.heightNum = this.heightNum + 40;
			}
		},
		openDetails(row, column) {
			// 点击销售退货单进入详情
			if (column.property == 'billNo') {
				this.$router.push({
					path: '/sale/salemange/createReturnOrder',
					query: {
						type: 'details',
						id: row.orderId,
						status: row.documentStatus,
					},
				});
			}
		},
		openDetailsDB(row, column) {
			// 双击进入
			this.$router.push({
				path: '/sale/salemange/createReturnOrder',
				query: {
					type: 'details',
					id: row.orderId,
					status: row.documentStatus,
				},
			});
		},
		sumitPushdown(id) {
			// 下推确定
			if (id) {
				let pathName = '';
				let routerName = '';
				if (this.pushdownVal === '7') {
					// 销售订单
					pathName = 'ReturnStockOrder/PushDownSalOrder';
					routerName = 'SaleOrderCreate';
				}
				this.$http
					.post(pathName, { ids: this.OrdedetailIds })
					.then((res) => {
						if (res.status === 200) {
							this.dialogxt = false;
							this.$router.push({
								name: routerName,
								query: {
									type: 'pushDown',
									pushDownData: JSON.stringify(res.data),
									sjtag: Math.random() * 36,
								},
							});
							this.dialogxt = false;
						}
					});
			} else {
				this.$message.error({
					message: '请先选中在确认',
					showClose: true,
				});
			}
		},
		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.length > 0) {
					this.heightNum = this.heightNum - 36;
				}
				if (this.OrderNoNameslist) {
					this.OrderNoNameslist.forEach((it, index) => {
						if (index < 5) {
							ordNostr = ordNostr + it + ',';
						}
					});
				}
				this.OrderNoNameStr = ordNostr;
			}
			this.getList(this.search);
		},
		submitCX() {
			// 确定上下查
			if (this.CXcheckVal) {
				let pathName = '';
				if (this.CXcheckVal === 7) {
					pathName = 'SaleOrder'; // 销售订单
				}
				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(this.OrderNoNames),
						},
					});
					this.dialogCX = false;
				}
			} else {
				this.$message.error({
					message: '请先选择在确认',
					showClose: true,
				});
			}
		},
		// 自定义条件赋值
		resultCom(result, fieldSale, fieldObj, tempObj) {
			let returnResult = resultComMethods(
				result,
				this[fieldSale],
				toRaw(result.outResult),
				this[tempObj],
				this.search
			);
			this[fieldSale] = returnResult.fieldSale;
			this[fieldObj] = returnResult.fieldObj;
			this[tempObj] = returnResult.tempObj;
			this.search = returnResult.search;
		},
		resetSearch() {
			// 重置
			let arr = [
				'billNo',
				'documentStatus',
				'returnData',
				'retcust',
				'product',
			];
			for (let index = 0; index < arr.length; index++) {
				this[arr[index]] = '';
			}
			this.reset = true;
			this.search = {
				retcust: '',
				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 = '';
			setTimeout(() => {
				this.reset = false;
			}, 200);
			this.searchList();
		},
		...mapActions({
			setSettletype: 'storeData/setSettletype',
			setDcurrency: 'storeData/setDcurrency',
			setCustomer: 'storeData/setCustomer',
			setRecondtion: 'storeData/setRecondtion',
			setWarehouseAll: 'storeData/setWarehouseAll',
			setStatusType: 'storeData/setStatusType',
			setDicRetrunOrder: 'storeData/setDicRetrunOrder',
			setGetAllUserList: 'storeData/setGetAllUserList',
		}),
		addListRetrun(event) {
			if (event.keyCode === 13) {
				this.searchList();
			}
		},
	},

	activated() {
		document.addEventListener('keyup', this.addListRetrun, false);
	},
	deactivated() {
		document.removeEventListener('keyup', this.addListRetrun, false);
	},
	created: function () {
		this.getcheckInfo();
		this.Init();
		this.getStatusTypeInfo();
		this.getStatusTypeObject();
	},

	computed: {
		...mapState({
			orgId: (state) => state.user.orgId,
			orgIdArr: (state) => state.user.orgIdArr,
			getOrgId: (state) => state.org.orgId,
			settletype: (state) => state.storeData.settletype,
			dcurrency: (state) => state.storeData.dcurrency,
			customer: (state) => state.storeData.customer,
			recondtion: (state) => state.storeData.recondtion,
			warehouseAll: (state) => state.storeData.warehouseAll,
			statusType: (state) => state.storeData.statusType,
			dicRetrunOrder: (state) => state.storeData.dicRetrunOrder,
			getAllUserList: (state) => state.storeData.getAllUserList,
		}),
		// 监听ID
		orgIdChange() {
			return this.$store.state && this.$store.state.org
				? this.$store.state.org.orgId
				: null;
		},
	},
	watch: {
		// 监听组织
		orgIdChange: function () {
			this.search.orgId =
				this.$store.state && this.$store.state.org
					? this.$store.state.org.orgId
					: 0;
			// console.log('触发了？', this.search.orgId);
			// 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/returnOrder.scss';
@import '@/assets/style/saleOrder/common.scss';
</style>
