<template>
	<div>
		<by-bill :propConfig="config" @search="search" ref="bill" @resert="resert" @detail="openEdit" @onRefresh="resert"
			@pagination="pagination" @clickTab="clickTab" @onOrder="onOrder" @onSave="onSave" @onReturn="onReturn">
		</by-bill>
		<!-- 拆单弹窗 -->
		<vxe-modal v-model="vxeValue" width="90%" height="90%" show-zoom resize transfer show-footer>
			<template #title>
				<span>拆单</span>
			</template>
			<template #default>
				<div class="fl" style="width: 45%;margin-right: 10%;">
					<div style="margin-bottom: 20px;">主单销售数据</div>
					<by-table ref="leftTable" :propConfig="leftTableConfig"></by-table>
				</div>
				<div class="fl" style="width: 45%;">
					<div style="margin-bottom: 20px;">拆单销售数据</div>
					<by-table ref="rightTable" :propConfig="rightTableConfig" @onChangeRow="rightChange"></by-table>
				</div>
			</template>
			<template #footer>
				<div class="btn">
					<el-button type="primary" size="small" @click="doConfirm">确定</el-button>
				</div>
			</template>
		</vxe-modal>
		<SettingConfig ref="setForm" type="setXForm" title="销售订单" />
	</div>
</template>

<script lang="ts">
	import { Component, Prop, Vue, Watch } from "vue-property-decorator";
	import api from "@/api/order";
	import OrderType from "./components/orderType.vue";
	import SettingConfig from "./components/settingConfig.vue";
	import Assembly from "@/components/Assembly/saleOrder.vue";
	import TerminateOrder from "@/components/Assembly/terminateOrder.vue";
	@Component({ components: { SettingConfig } })
	export default class SaleOrder extends Vue {
		tabType : any = 'all';
		vxeValue = false
		isCitation = false
		timeNum = 0;
		calculateCount = 0;
		timer : any = null
		baseInfo : any = {} // 订单数据
		regularData : any = [] // 固定值

		leftTableConfig : any = {
			attr: {
				size: 'mini',
				height: 490,
				align: 'left',
			},
			columns:
				[
					{
						width: 300,
						title: '物料名称',
						field: 'materialName',
					},
					{
						width: 120,
						title: '数量',
						field: 'number',
					},
					{
						width: 120,
						title: '已拆单数量',
						field: 'splitOrderNumber',
					},
					{
						width: 120,
						title: '可拆单数量',
						field: 'splitNumber',
					}
				]
		}
		rightTableConfig : any = {
			attr: {
				size: 'mini',
				height: 490,
				align: 'left',
			},
			columns:
				[
					{
						width: 300,
						title: '物料名称',
						field: 'materialName',
					},
					{
						width: 120,
						title: '数量',
						field: 'number',
						component: 'by-input',
						compConfig: {
							attr: {
								size: 'mini',
								type: 'integer',
								placeholder: '请输入数量'
							},
						}
					},
				]
		}
		config : any = {
			attr: {
				activeName: 'all',
				calculateH: true
			},
			// 引单
			source: [
				{
					title: '订单任务',
					search: {
						attr: {
							size: 'medium',
						},
						columns: [
							[
								{
									span: 6,
									label: '单据编号',
									prop: 'orderNumber',
									component: 'by-input',
									compConfig: {
										attr: {
											placeholder: '请输入单据编号',
											clearable: true
										}
									}
								},
								{
									span: 6,
									label: '机构名称',
									prop: 'organizationName',
									component: 'by-input',
									compConfig: {
										attr: {
											placeholder: '请输入机构名称',
											clearable: true
										},
									},
								},
							],
						]
					},
					tool: {
						tools: {
							search: true,
							refresh: true
						},
						customTools: [
							{
								name: '引单', icon: 'el-icon-bottom', audit: [''], event: {
									click: () => {
										(this as any).citationOrder()
									}
								}
							},
						]
					},
					table: {
						attr: {
							size: 'mini',
							seq: true,
							align: 'left',
							radio: true,
							triggerRowCheck: 'row',
						},
						columns: [
							{
								width: 160,
								title: '单据编号',
								field: 'orderNumber',
								fixed: 'left'
							},
							{
								width: 120,
								title: '机构名称',
								field: 'organizationName',
							},
							{
								width: 80,
								title: '来源类型',
								field: 'sourceType',
							},
							{
								width: 120,
								title: '交货人',
								field: 'delivery',
							},
							{
								width: 150,
								title: '交货时间',
								field: 'deliveryData',
							},
							{
								width: 200,
								title: '交货地点',
								field: 'deliveryAddress',
							},
							{
								width: 130,
								title: '联系方式',
								field: 'deliveryPhone',
							},
							{
								width: 130,
								title: '交货方式',
								field: 'deliveryWay',
							},
						]
					}
				},
			],
			// 单据
			bill: {
				tool: {
					tools: {
						save: true,
						return: true
					},
					customTools: []
				},
				log: {
					request: {
						url: '/supply/supplyPurchaseOrderOperateLog/page',
						method: 'GET'
					}
				},
				form: {
					attr: {
						size: 'medium',
						rules: {
							planDeliveryData: [{
								required: true, message: '请选择交货时间', trigger: 'change'
							}],
						}
					},
					columns: [
						[
							{
								span: 8,
								label: '单据编号',
								prop: 'orderNumber',
								component: 'by-input',
								compConfig: {
									attr: {
										readonly: true,
									},
								}
							},
							{
								span: 8,
								label: '机构名称',
								prop: 'organizationName',
								component: 'by-input',
								compConfig: {
									attr: {
										readonly: true,
									},
								}
							},
							{
								span: 8,
								label: '联系方式',
								prop: 'phone',
								component: 'by-input',
								compConfig: {
									attr: {
										readonly: true,
									},
								}
							},
							{
								span: 8,
								label: '收货人',
								prop: 'consignee',
								component: 'by-input',
								compConfig: {
									attr: {
										readonly: true,
									},
								}
							},
							{
								span: 8,
								label: '交货时间',
								prop: 'planDeliveryData',
								component: 'by-date-picker',
								compConfig: {
									attr: {
										format: 'yyyy-MM-dd HH:mm:ss',
										type: 'datetime',
										readonly: true,
									},
								}
							},
							{
								span: 8,
								label: '确定时间',
								prop: 'affirmDeliveryData',
								component: 'by-input',
								compConfig: {
									attr: {
										readonly: true,
									},
								}
							},
							{
								span: 8,
								label: '交货地点',
								prop: 'address',
								component: 'by-input',
								compConfig: {
									attr: {
										readonly: true,
									},
								}
							},
							{
								span: 8,
								label: '订单状态',
								prop: 'status',
								component: TerminateOrder,
								compConfig: {
									attr: {
										readonly: true,
									},
								}
							},
						],
						[
							{
								span: 28,
								label: '备注',
								slot: true,
								prop: 'remark',
								component: 'by-input',
								compConfig: {
									attr: {
										size: 'mini',
										placeholder: '请输入备注',
										type: 'textarea',
										readonly: false,
									},
								}
							},
						]
					]
				},
				tableConfig: [
					{
						table: {
							attr: {
								size: 'mini',
								align: 'left',
								readonly: true
							},
							columns:
								[{
									width: 250,
									title: '物料名称',
									field: 'materialName',
								},
								{
									width: 180,
									title: '财务编号',
									field: 'financeSuborderId',
								},
								{
									width: 110,
									title: '单位',
									field: 'unit',
								},
								{
									width: 110,
									title: '单位编码',
									field: 'unitCode',
								},
								{
									width: 110,
									title: '数量',
									field: 'number',
								},
								{
									width: 110,
									title: '已交付数量',
									field: 'outNumber',
								},
								{
									width: 110,
									title: '单价',
									field: 'unitPrice',
								},
								{
									width: 110,
									title: '实际金额',
									field: 'realityPrice',
								},
								{
									width: 110,
									title: '优惠金额',
									field: 'prePrice',
								},
								{
									width: 110,
									title: '合计金额',
									field: 'totalPrice',
								},
								]
						}
					}
				]
			},
			// 综合
			all: {
				search: {
					attr: {
						size: 'mini',
					},
					columns: [
						[
							{
								span: 6,
								label: '单据编号',
								prop: 'orderNumber',
								component: 'by-input',
								compConfig: {
									attr: {
										placeholder: '请输入单据编号',
										clearable: true
									}
								}
							},
							{
								span: 6,
								label: '机构名称',
								prop: 'organizationName',
								component: 'by-input',
								compConfig: {
									attr: {
										placeholder: '请输入机构名称',
										clearable: true
									},
								},
							},
						],
					]
				},
				tool: {
					tools: {
						search: true,
						refresh: true
					},
					customTools: [
						{
							name: '终止订单', icon: 'el-icon-close', audit: [''], event: {
								click: () => {
									(this as any).doBackOrder()
								}
							}
						},
						{
							name: '同步到财务系统', icon: 'el-icon-top', audit: [''], event: {
								click: () => {
									(this as any).syncSystem()
								}
							}
						},
						{
							name: '参数配置', icon: 'el-icon-setting', audit: [''], event: {
								click: () => {
									(this as any).configuration()
								}
							}
						},
					]
				},
				table: {
					attr: {
						size: 'mini',
						triggerRowCheck: 'row',
						align: 'left',
						radio: true,
						height: '620'
					},
					columns: [
						{
							width: 144,
							title: '单据编号',
							field: 'orderNumber',
							isDetail: true,
							fixed: 'left'
						},
						{
							width: 50,
							component: OrderType,
							fixed: 'left'
						},
						{
							width: 100,
							title: '机构名称',
							field: 'organizationName',
						},
						{
							width: 150,
							title: '财务编号',
							field: 'financeOrderId',
						},
						{
							width: 90,
							title: '收货人',
							field: 'consignee',
						},
						{
							width: 100,
							title: '联系方式',
							field: 'phone',
						},
						{
							width: 130,
							title: '计划交货时间',
							field: 'planDeliveryData',
						},
						{
							width: 130,
							title: '确定交货时间',
							field: 'affirmDeliveryData',
						},
						{
							width: 250,
							title: '交货地点',
							field: 'address',
						},
						{
							width: 70,
							title: '状态',
							field: 'status',
							align: 'center',
							component: Assembly,
						},
						{
							width: 90,
							title: '操作',
							action: true,
							plugins: [
								{
									name: '查看',
									event: {
										show: (item : any) => {
											return item.status != 0
										},
										click: (item : any) => {
											(this as any).openEdit(item)
										}
									}
								},
								// {
								// 	name: '编辑',
								// 	event: {
								// 		show: (item : any) => {
								// 			return item.status === 0
								// 		},
								// 		click: (item : any) => {
								// 			(this as any).openEdit(item)
								// 		}
								// 	}
								// },
								{
									name: '拆单',
									event: {
										show: (item : any) => {
											return item.isMaster === 1 && item.status === 0
										},
										click: (item : any) => (this as any).orderTaking(item)
									}
								},
							]
						}
					]
				}
			},
			// 终止订单
			returnOrder: {
				search: {
					attr: {
						size: 'mini',
					},
					columns: [
						[
							{
								span: 6,
								label: '单据编号',
								prop: 'orderNumber',
								component: 'by-input',
								compConfig: {
									attr: {
										placeholder: '请输入单据编号',
										clearable: true
									}
								}
							},
							{
								span: 6,
								label: '机构名称',
								prop: 'organizationName',
								component: 'by-input',
								compConfig: {
									attr: {
										placeholder: '请输入机构名称',
										clearable: true
									},
								},
							},
						],
					]
				},
				tool: {
					tools: {
						search: true,
						refresh: true
					},
				},
				table: {
					attr: {
						size: 'mini',
						align: 'left',
						height: '620'
					},
					columns: [
						{
							width: 168,
							title: '单据编号',
							field: 'orderNumber',
							isDetail: true,
							fixed: 'left'
						},
						{
							width: 130,
							title: '机构名称',
							field: 'organizationName',
						},
						{
							width: 150,
							title: '财务编号',
							field: 'financeOrderId',
						},
						{
							width: 120,
							title: '收货人',
							field: 'consignee',
						},
						{
							width: 120,
							title: '联系方式',
							field: 'phone',
						},
						{
							width: 130,
							title: '计划交货时间',
							field: 'planDeliveryData',
						},
						{
							width: 130,
							title: '确定交货时间',
							field: 'affirmDeliveryData',
						},
						{
							width: 250,
							title: '交货地点',
							field: 'address',
						},
						{
							width: 70,
							title: '状态',
							field: 'status',
							align: 'center',
							component: Assembly,
						},
						{
							width: 90,
							title: '操作',
							action: true,
							plugins: [{
								icon: 'el-icon-edit',
								name: '查看',
								audit: '',
								event: {
									click: (item : any) => {
										(this as any).openEdit(item)
									}
								}
							},
							]
						}
					]
				}
			},
		}
		created() {
			this.timer = setInterval(() => {
				this.getDataList();
				this.initTable();
			}, 500)
		}
		// 计算高度
		initTable() {
			if (!this.$refs.bill) {
				this.calculateCount++;
				if (this.calculateCount > 5) return;
				setTimeout(() => {
					this.initTable()
				}, 500)
				return
			}
			this.config.source[0].table.attr.height = window.innerHeight - 300;
			this.config.all.table.attr.height = window.innerHeight - 300;
			this.config.returnOrder.table.attr.height = window.innerHeight - 300;
			(this as any).$refs.bill.$refs.all.$refs.table.recalculate();
		}
		// 参数配置
		configuration() {
			(this as any).$refs.setForm.openSetting();
		}
		// 引单
		citationOrder() {
			let parentData : any = (this as any).$refs.bill.getSourceTableSelectData(0);
			if (parentData.length <= 0) return this.$message.warning('请选择引单数据');
			let loading = this.$loading({ target: '.main-container' });
			api.single({ id: parentData[0].id }, 'supplyTaskOrder').then((res : any) => {
				if (res.code === 200) {
					parentData = res.data ? [res.data] : parentData;
					parentData = parentData.map((item : any) => {
						item.address = item.deliveryAddress
						item.consignee = item.delivery
						item.phone = item.deliveryPhone
						item.planDeliveryData = item.deliveryData
						const { ...newData } = item
						return newData
					});
					let billConfig = (this as any).$lodash.cloneDeep(this.config.bill);
					billConfig.tool.tools = { order: true };
					billConfig.tool.customTools = [];
					this.isCitation = true;
					(this.$refs.bill as any).showTab = 'bill';
					loading.close();
					setTimeout(() => {
						(this.$refs.bill as any).setBillConfig(billConfig);
						(this.$refs.bill as any).setBillFormValue(parentData[0]); // 设置单据表单数据
						(this.$refs.bill as any).setBillTableValue(parentData[0].suborderList, 0);// 设置第1张单据表格数据
					}, 0)
				} else loading.close();
			}).catch(() => loading.close());
		};
		// 接单
		onOrder() {
			this.$confirm('确定接单吗', '注意', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				center: true
			}).then(() => {
				let data = (this.$refs.bill as any).getBillFormValue();
				let loading = this.$loading({ target: '.main-container' });
				api.ordersaccepting({ ids: data.id }, 'supplyTaskOrder').then((res : any) => {
					loading.close();
					if (res.code === 200) {
						this.getDataList();
						this.$message.success('接单成功');
						(this.$refs.bill as any).showTab = 'all';
					} else this.$message.error(res.msg);
				}).catch(() => loading.close());
			}).catch(() => this.$message.info('已取消接单'));
		}
		// 右拆单数据变化
		rightChange(e : any) {
			if (0 <= e.number && e.number <= this.regularData[e.dataIndex].splitNumber) {
				let leftTableData = (this.$refs.leftTable as any).getValue();
				leftTableData[e.dataIndex].splitNumber = leftTableData[e.dataIndex].number - e.number;
				(this.$refs.leftTable as any).setValue(leftTableData);
			} else {
				let leftTableData = (this.$refs.leftTable as any).getValue();
				let rifhtTableData = (this.$refs.rightTable as any).getValue();
				rifhtTableData[e.dataIndex].number = '0';
				leftTableData[e.dataIndex].splitNumber = leftTableData[e.dataIndex].number - leftTableData[e.dataIndex].splitOrderNumber;
				(this.$refs.leftTable as any).setValue(leftTableData);
				(this.$refs.rightTable as any).setValue(rifhtTableData);
				this.$message.warning('输入值只能是自然数且不能大于可拆单数量');
			}
		}
		// 打开拆单
		orderTaking(item : any) {
			let loading = this.$loading({ target: '.main-container' });
			api.single({ id: item.id }, 'supplyPurchaseOrder').then((res : any) => {
				loading.close();
				// console.log(res);
				if (res.code === 200) {
					this.vxeValue = true;
					this.baseInfo = res.data;
					this.regularData = (this as any).$lodash.cloneDeep(res.data.suborderList);
					let rightData = (this as any).$lodash.cloneDeep(res.data.suborderList);
					this.regularData.map((v : any) => {
						v.splitNumber = v.number - v.splitOrderNumber;
					})
					rightData.map((v : any, i : any) => {
						v.number = '0';
						v.dataIndex = i;
						v.splitNumber = v.number - v.splitOrderNumber;
					});
					setTimeout(() => {
						(this.$refs.leftTable as any).setValue(this.regularData);
						(this.$refs.rightTable as any).setValue(rightData);
					}, 0)
				}
			}).catch(() => loading.close());
		}
		// 确认拆单
		doConfirm() {
			let rifhtNewData : any = [];
			let rifhtTableData = (this.$refs.rightTable as any).getValue();
			rifhtTableData.map((v : any) => {
				rifhtNewData.push({
					id: v.id,
					inSplitOrderNumber: v.number
				})
			});
			let isTrue = rifhtNewData.filter((v : any) => v.inSplitOrderNumber != 0);
			let obj : any = {
				id: this.baseInfo.id,
				splitList: rifhtNewData,
			};
			if (isTrue.length > 0) {
				this.$confirm('确定拆单吗！', '注意', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning',
					center: true
				}).then(() => {
					let loading = this.$loading({ target: '.main-container' });
					api.splitOrder(obj, 'supplyPurchaseOrder').then((res : any) => {
						loading.close();
						if (res.code === 200) {
							this.$message.success('拆单成功');
							this.getPageList({}, 'all'); // 综合数据
							this.vxeValue = false;
						} else this.$message.error(res.msg);
					}).catch(() => loading.close());
				}).catch(() => this.$message.info('已取消拆单'));
			} else this.$message.warning('请输入拆单销售数据');
		}
		getDataList() {
			if (!this.$refs.bill) {
				if (this.timeNum > 5) {
					clearInterval(this.timer)
				}
				this.timeNum++;
				return
			}
			clearInterval(this.timer);
			this.getPageList({}, 'all'); // 综合数据
		}
		// 获取分页数据
		getPageList(query : any, type : any) {
			let data = (this.$refs.bill as any).getTablePage('all');
			query.pageNo = data.pageNo;
			query.pageSize = data.pageSize;
			let loading = this.$loading({ target: '.main-container' });
			api.pageList(query, 'supplyPurchaseOrder').then((res : any) => {
				loading.close();
				if (res.code === 200) {
					(this.$refs.bill as any).setTabTableValue(type, res.data.records);
					let page = {
						pageNo: res.data.current, //当前页
						pageSize: res.data.size, //每页条数
						total: res.data.total //总条数
					};
					(this.$refs.bill as any).setTablePage(type, page)
				} else this.$message.error(res.msg);
			}).catch(() => loading.close());
		}
		// 获取引单分页数据
		getCitationList(query : any) {
			query.submitState = 1; // 已提交
			query.status = 0; // 未接单
			let loading = this.$loading({ target: '.main-container' });
			api.pageList(query, 'supplyTaskOrder').then((res : any) => {
				loading.close();
				if (res.code === 200) {
					(this.$refs.bill as any).setSourceTableData(0, res.data.records);
					let page = {
						pageNo: res.data.current, //当前页
						pageSize: res.data.size, //每页条数
						total: res.data.total //总条数
					};
					(this.$refs.bill as any).setSourcePage(0, page);
				} else this.$message.error(res.msg)
			}).catch(() => loading.close());
		}
		// 保存
		onSave() {
			let query = (this.$refs.bill as any).getBillFormValue(); // 获取单据数据
			// console.log('保存参数query ==> ', query);
			let loading = this.$loading({ target: '.main-container' });
			api.updateList(query, 'supplyPurchaseOrder').then((res : any) => {
				loading.close();
				if (res.code === 200) {
					this.$message.success('保存成功');
					this.getPageList({}, 'all'); // 综合数据
					(this.$refs.bill as any).showTab = 'all';
				} else this.$message.error(res.msg);
			}).catch(() => loading.close());
		}
		// 打开编辑
		openEdit(e : any) {
			e = e.row ? e.row : e;
			let loading = this.$loading({ target: '.main-container' });
			api.single({ id: e.id }, 'supplyPurchaseOrder').then((res : any) => {
				if (res.code === 200) {
					let parentData = res.data ? res.data : e;
					let billConfig = (this as any).$lodash.cloneDeep(this.config.bill);
					if (parentData.status === 2) {
						billConfig.tool.customTools = [];
					} else {
						billConfig.tool.customTools = [
							{
								name: '终止订单', icon: 'el-icon-close', audit: [''], event: {
									click: () => {
										(this as any).doBackOrder2(parentData)
									}
								}
							}
						];
						// 主单
						if (parentData.isMaster === 1) {
							billConfig.tool.customTools = [
								{
									name: '拆单', icon: 'el-icon-minus', audit: [''], event: {
										click: () => {
											(this as any).orderTaking(parentData)
										}
									}
								},
								{
									name: '终止订单', icon: 'el-icon-close', audit: [''], event: {
										click: () => {
											(this as any).doBackOrder2(parentData)
										}
									}
								},
								{
									name: '同步到财务系统', icon: 'el-icon-top', audit: [''], event: {
										click: () => {
											(this as any).syncSystem2(parentData)
										}
									}
								},
							];
						}
					};
					billConfig.tool.tools = { return: true };
					billConfig.form.columns[0].map((v : any) => {
						v.compConfig.attr.readonly = true;
					})
					billConfig.form.columns[1][0].compConfig.attr.readonly = true;
					// if (parentData.status === 0) {
					// 	billConfig.form.columns[0][2].compConfig.attr.readonly = false;
					// 	billConfig.form.columns[0][3].compConfig.attr.readonly = false;
					// 	billConfig.form.columns[0][4].compConfig.attr.readonly = false;
					// 	billConfig.form.columns[0][6].compConfig.attr.readonly = false;
					// 	billConfig.form.columns[1][0].compConfig.attr.readonly = false;
					// 	billConfig.tool.tools = { return: true, save: true };
					// };
					(this.$refs.bill as any).setBillConfig(billConfig);
					(this.$refs.bill as any).setBillFormValue(parentData); // 设置单据表单数据
					(this.$refs.bill as any).setBillTableValue(parentData.suborderList, 0);// 设置第1张单据表格数据
					(this.$refs.bill as any).showTab = 'bill';
					loading.close();
				} else loading.close();
			}).catch(() => loading.close());

		}
		// 导航切换
		clickTab(e : any) {
			// submitState: 提交状态
			this.tabType = e;
			(this.$refs.bill as any).showTab = e;
			if (e === 'source') {
				this.getCitationList({});
			}
			if (e === 'all') this.getDataList();
			if (e === 'returnOrder') this.getPageList({ status: 2 }, 'returnOrder');
			if (e === 'bill') {
				let billConfig = (this as any).$lodash.cloneDeep(this.config.bill);
				billConfig.tool.tools = {};
				billConfig.tool.customTools = [];
				(this.$refs.bill as any).setBillConfig(billConfig);
			}
		}
		// 获取当前时间
		getNowDate() {
			let date = new Date();
			let years : any = date.getFullYear();
			let months : any = date.getMonth() + 1;
			let days : any = date.getDate();
			let hours : any = date.getHours();
			let minutes : any = date.getMinutes();
			let seconds : any = date.getSeconds();
			months = months < 10 ? '0' + months : months;
			days = days < 10 ? '0' + days : days;
			hours = hours < 10 ? '0' + hours : hours;
			minutes = minutes < 10 ? '0' + minutes : minutes;
			seconds = seconds < 10 ? '0' + seconds : seconds;
			return `${years}-${months}-${days} ${hours}:${minutes}:${seconds}`;
		}
		// 同步销售订单到财务系统
		syncSystem() {
			let selectData = (this as any).$refs.bill.getTableSelectData('all');
			if (selectData.length > 0) {
				if (selectData[0].isMaster !== 1 || selectData[0].status !== 0) return this.$message.warning('只能同步未出库的主单');
				if (selectData[0].financeOrderId) return this.$message.warning('此数据已同步到财务系统');
				let planData = selectData[0].planDeliveryData.replaceAll(/[-:\s]/g, '');
				let nowDate = this.getNowDate().replaceAll(/[-:\s]/g, '');
				if (planData < nowDate) return this.$message.warning('计划交货时间小于当前时间，不能同步到财务系统');
				this.$confirm('确定同步订单吗', '注意', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning',
					center: true
				}).then(() => {
					let loading = this.$loading({ target: '.main-container' });
					api.addSaleOrder({ ids: selectData[0].id }).then((res : any) => {
						loading.close();
						if (res.code === 200) {
							this.$message.success('同步订单成功');
							this.getDataList();
							(this.$refs.bill as any).showTab = 'all';
						} else this.$message.error(res.msg)
					}).catch(() => loading.close());
				}).catch(() => this.$message.info('已取消同步订单'));
			} else this.$message.warning('请选择同步数据');
		}
		// 单据操作同步
		syncSystem2(e : any) {
			this.$confirm('确定同步订单吗', '注意', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				center: true
			}).then(() => {
				if (e.financeOrderId) return this.$message.warning('此数据已同步到财务系统');
				let planData = e.planDeliveryData.replaceAll(/[-:\s]/g, '');
				let nowDate = this.getNowDate().replaceAll(/[-:\s]/g, '');
				if (planData < nowDate) return this.$message.warning('计划交货时间小于当前时间，不能同步到财务系统');
				let loading = this.$loading({ target: '.main-container' });
				api.addSaleOrder({ ids: e.id }).then((res : any) => {
					loading.close();
					if (res.code === 200) {
						this.$message.success('同步订单成功');
						this.getDataList();
						(this.$refs.bill as any).showTab = 'all';
					} else this.$message.error(res.msg)
				}).catch(() => loading.close());
			}).catch(() => this.$message.info('已取消同步订单'));
		}
		// 工具栏终止订单
		doBackOrder() {
			let selectData = (this as any).$refs.bill.getTableSelectData('all');
			// console.log(selectData);
			// if (selectData.length > 1) return this.$message.warning('只能选择一条终止订单数据');
			let ids : any = [];
			if (selectData.length > 0) {
				selectData.map((v : any) => {
					ids.push(v.id);
				})
			} else return this.$message.warning('请选择终止订单数据');
			// console.log(ids);
			this.$confirm('确定终止订单吗！', '注意', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				center: true
			}).then(() => {
				let loading = this.$loading({ target: '.main-container' });
				api.stoporder({ ids: ids }, 'supplyPurchaseOrder').then((res : any) => {
					loading.close();
					if (res.code === 200) {
						this.$message.success('终止订单成功');
						this.getDataList();
						(this.$refs.bill as any).showTab = 'all';
					} else this.$message.error(res.msg)
				}).catch(() => loading.close());
			}).catch(() => this.$message.info('已取消终止订单'));
		}
		// 单据操作终止订单
		doBackOrder2(e : any) {
			this.$confirm('确定终止订单吗！', '注意', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				center: true
			}).then(() => {
				let loading = this.$loading({ target: '.main-container' });
				api.stoporder({ ids: e.id }, 'supplyPurchaseOrder').then((res : any) => {
					loading.close();
					if (res.code === 200) {
						this.$message.success('终止订单成功');
						this.getDataList();
						(this.$refs.bill as any).showTab = 'all';
					} else this.$message.error(res.msg)
				}).catch(() => loading.close());
			}).catch(() => this.$message.info('已取消终止订单'));
		}
		// 分页
		pagination(e : any) {
			// console.log('分页 ==> ', e);
			if (e.type === 'source_0') {
				let query : any = (this as any).$refs.bill.$refs.source_0[0].getSearchValue();
				query.pageNo = e.page.pageNum;
				query.pageSize = e.page.pageSize;
				this.getCitationList(query);
			} else {
				let query : any = (this as any).$refs.bill.getSearchValue(e.type);
				query.pageNo = e.page.pageNum;
				query.pageSize = e.page.pageSize;
				if (e.type === 'returnOrder') query.status = 2; // 终止订单数据
				let loading = this.$loading({ target: '.main-container' });
				api.pageList(query, 'supplyPurchaseOrder').then((res : any) => {
					loading.close();
					if (res.code === 200) {
						(this.$refs.bill as any).setTabTableValue(e.type, res.data.records);
					} else this.$message.error(res.msg);
				}).catch(() => loading.close());
			}
		}
		// 返回
		onReturn() {
			this.clickTab(this.tabType);
		}
		// 搜索
		search(parames : any) {
			let query = parames.value
			if (parames.type === 'source_0') {
				this.getCitationList(query); // 引单数据
			} else {
				if (query.type === 'returnOrder') query.status = 2; // 终止订单数据
				this.getPageList(query, parames.type) // 获取综合分页数据
			}
		}
		// 刷新/重置
		resert(e : any) {
			let data : any = e.type ? e.type : e;
			// console.log('刷新/重置 ==> ', data);
			if (data === 'source_0') {
				(this as any).$refs.bill.$refs.source_0[0].clearSearch();
				this.getCitationList({}); // 引单数据
			} else {
				(this as any).$refs.bill.$refs[data].clearSearch();
				if (data === 'all') this.getPageList({}, 'all'); // 综合数据
				if (data === 'returnOrder') this.getPageList({ status: 2 }, 'returnOrder'); // 终止订单数据
			}
		}
	}
</script>