<template>
	<div>
		<by-bill ref="bill" :propConfig="config" @search="search" @onSmt="onSmt" @onAdd="onAdd" @onDelete="onDelete"
			@onRefresh="resert" @resert="resert" @detail="openEdit" @onSave="onSave" @clickTab="clickTab"
			@onChangeRow="onChangeRow" @pagination="pagination" @onReturnSmt="onReturnSmt2" @printBefore="printBefore"
			@getSupplierModal="getSupplierModal" @onReturn="onReturn">
		</by-bill>
		<!-- 选择物料 -->
		<productModal ref="product" :mulit="true" @confirm="confirmProduct" />
		<vxe-modal v-model="value" id="synchronousOrderModal" width="500" height="200" @hide="value=false" transfer
			show-footer>
			<template #title>
				<span>入库单同步</span>
			</template>
			<template>
				<div class="cont">
					<div class="t-title"><span>* </span>时间：</div>
					<div class="right-cont">
						<el-date-picker v-model="time" style="width: 100%;" size="mini" type="daterange" clearable
							value-format="yyyy-MM-dd" range-separator="-" :start-placeholder="beginDate" @change="change"
							:end-placeholder="endDate">
						</el-date-picker>
					</div>
				</div>
			</template>
			<template #footer>
				<div class="btn">
					<el-button type="primary" size="small" plain @click="value=false">取消</el-button>
					<el-button type="primary" size="small" @click="confirm">确定</el-button>
				</div>
			</template>
		</vxe-modal>
	</div>
</template>

<script lang="ts">
	import { Component, Prop, Vue, Watch } from "vue-property-decorator";
	import api from "@/api/order";
	import productModal from "../orderTask/components/productModal.vue";
	import Focus from "../orderTask/components/focus.vue";
	import towInOne from "./components/towInOne.vue";
	import BusinessType from "./components/businessType.vue";
	import { add } from '@/benyun/utils/accuracy'
	@Component({ components: { productModal } })
	export default class OrderTask extends Vue {
		// 左边
		tabType : any = 'all';
		isAdd : any = false;
		baseInfo : any = {};
		isSubmit : any = false
		isChange : any = false
		dataId : any = ''
		beginDate : any = this.getNowDate(3)
		endDate : any = this.getNowDate(0)
		time : any = '';
		value : any = false;
		// 右边
		supplierInfo : any = {} // 机构信息
		businessType : any = '' // 业务类型
		tableData : any = []
		mergeObj : any = []
		setStatusData : any = ''
		timeNum = 0;
		calculateCount = 0;
		timer : any = null
		config : any = {
			attr: {
				activeName: 'all',
			},
			// 单据
			bill: {
				tool: {
					tools: {
						save: true,
						return: true
					}
				},
				log: {
					request: {
						url: '/supply/supplyEntryOrderOperateLog/page',
						method: 'GET'
					}
				},
				form: {
					attr: {
						size: 'medium',
						readonly: false,
						rules: {
							businessType: [{
								required: true, message: '请选择业务类型', trigger: 'change'
							}],
						}
					},
					columns: [
						[
							{
								span: 8,
								label: '单据编号',
								prop: 'orderNumber',
								component: 'by-input',
								compConfig: {
									attr: {
										readonly: true
									},
								}
							},
							{
								span: 8,
								label: '机构名称',
								prop: 'organizationName',
								component: Focus,
							},
							{
								span: 8,
								label: '经办人',
								prop: 'operator',
								component: 'by-input',
							},
							{
								span: 8,
								label: '业务类型',
								prop: 'businessType',
								component: BusinessType,
							},
							{
								span: 8,
								label: '财务编号',
								prop: 'financeOrderId',
								component: 'by-input',
								compConfig: {
									attr: {
										readonly: true,
									},
								}
							},
							{
								span: 8,
								label: 'WMS编码',
								prop: 'wmsId',
								component: 'by-input',
								compConfig: {
									attr: {
										readonly: true,
									},
								}
							},
						],
						[
							{
								span: 28,
								label: '备注',
								slot: true,
								prop: 'remark',
								component: 'by-input',
								compConfig: {
									attr: {
										size: 'mini',
										placeholder: '请输入备注',
										type: 'textarea'
									},
								}
							},
						]
					]
				},
				tableConfig: [
					{
						tool: {
							tools: {
								add: true
							}
						},
						table: {
							attr: {
								size: 'mini',
								align: 'left',
								readonly: true
							},
							columns:
								[
									{
										width: 250,
										title: '物料名称',
										field: 'materialName',
									},
									{
										width: 150,
										title: '财务编号',
										field: 'financeSuborderId',
									},
									{
										width: 220,
										title: '仓库，仓位',
										field: 'towInOne',
										component: towInOne,
									},
									{
										width: 80,
										title: '单位',
										field: 'unit',
									},
									{
										width: 80,
										title: '单位编码',
										field: 'unitCode',
									},
									{
										width: 110,
										title: '数量',
										field: 'number',
										component: 'by-input',
										compConfig: {
											attr: {
												size: 'mini',
												type: 'integer',
												defaultValue: 1,
											},
										}
									},
									{
										width: 110,
										title: '已交付数量',
										field: 'deliveryNumber',
									},
									{
										width: 110,
										title: '单价',
										field: 'unitPrice',
										component: 'by-input',
										compConfig: {
											attr: {
												size: 'mini',
												type: 'number',
											},
										}
									},
									{
										width: 110,
										title: '合计金额',
										field: 'totalPrice',
									},
									{
										width: 90,
										title: '操作',
										action: true,
										plugins: [{
											icon: 'el-icon-delete',
											name: '删除',
											audit: '',
											event: {
												click: (item : any) => {
													(this as any).changeTable(item)
												}
											}
										}]
									},
								]
						}
					}
				]
			},
			// 已提交
			smt: {
				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',
					},
					columns: [
						{
							width: 200,
							title: '单据编号',
							field: 'orderNumber',
							isDetail: true,
							fixed: 'left'
						},
						{
							width: 200,
							title: '机构名称',
							field: 'organizationName',
						},
						{
							width: 200,
							title: '财务编号',
							field: 'financeOrderId',
						},
						{
							width: 200,
							title: 'WMS编号',
							field: 'wmsId',
						},
						{
							width: 120,
							title: '经办人',
							field: 'operator',
						},
						{
							width: 90,
							title: '业务类型',
							field: 'businessType',
						},
						{
							width: 110,
							title: '操作',
							action: true,
							plugins: [{
								name: '查看',
								audit: '',
								event: {
									click: (e : any) => {
										(this as any).setReadonly(e)
									}
								}
							}, {
								name: '反提交',
								event: {
									click: (item : any) => (this as any).onReturnSmt(item)
								}
							}]
						}
					]
				}
			},
			// 草稿箱
			draftsBox: {
				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: {
						smt: true,
						delete: true,
						search: true,
						refresh: true
					}
				},
				table: {
					attr: {
						size: 'mini',
						seq: true,
						align: 'left',
						checkbox: true,
						triggerRowCheck: 'row'
					},
					columns: [
						{
							width: 200,
							title: '单据编号',
							field: 'orderNumber',
							isDetail: true,
							fixed: 'left'
						},
						{
							width: 200,
							title: '机构名称',
							field: 'organizationName',
						},
						{
							width: 200,
							title: '财务编号',
							field: 'financeOrderId',
						},
						{
							width: 200,
							title: 'WMS编号',
							field: 'wmsId',
							compConfig: {
								attr: {
									readonly: true,
								},
							}
						},
						{
							width: 120,
							title: '经办人',
							field: 'operator',
						},
						{
							width: 90,
							title: '业务类型',
							field: 'businessType',
						},
						{
							width: 110,
							title: '操作',
							action: true,
							plugins: [{
								icon: 'el-icon-edit',
								name: '编辑',
								audit: '',
								event: {
									click: (e : any) => {
										(this as any).openEdit(e)
									}
								}
							}, {
								name: '删除',
								event: {
									click: (item : any) => (this as any).doDelete2(item)
								}
							}]
						}
					]
				}
			},
			// 综合
			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: {
						add: true,
						delete: true,
						search: true,
						refresh: true
					},
					customTools: [
						{
							name: '同步入库单', icon: 'el-icon-top', audit: [''], event: {
								click: () => {
									(this as any).syncEntry()
								}
							}
						},
					]
				},
				table: {
					attr: {
						size: 'mini',
						seq: true,
						align: 'left',
						checkbox: true,
						triggerRowCheck: 'row',
						height: "620"
					},
					columns: [
						{
							width: 200,
							title: '单据编号',
							field: 'orderNumber',
							isDetail: true,
							fixed: 'left'
						},
						{
							width: 200,
							title: '机构名称',
							field: 'organizationName',
						},
						{
							width: 200,
							title: '财务编号',
							field: 'financeOrderId',
						},
						{
							width: 200,
							title: 'WMS编号',
							field: 'wmsId',
						},
						{
							width: 120,
							title: '经办人',
							field: 'operator',
						},
						{
							width: 90,
							title: '业务类型',
							field: 'businessType',
						},
						{
							width: 120,
							title: '操作',
							action: true,
							plugins: [{
								icon: 'el-icon-edit',
								name: '编辑',
								audit: '',
								event: {
									click: (e : any) => {
										(this as any).openEdit(e)
									}
								}
							}, {
								name: '删除',
								event: {
									click: (item : any) => (this as any).doDelete2(item)
								}
							}]
						}
					]
				}
			},
			// 回收站
			recycleBin: {
				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',
					},
					columns: [
						{
							width: 200,
							title: '单据编号',
							field: 'orderNumber',
							isDetail: true,
							fixed: 'left'
						},
						{
							width: 200,
							title: '机构名称',
							field: 'organizationName',
						},
						{
							width: 200,
							title: '财务编号',
							field: 'financeOrderId',
						},
						{
							width: 200,
							title: 'WMS编号',
							field: 'wmsId',
						},
						{
							width: 120,
							title: '经办人',
							field: 'operator',
						},
						{
							width: 90,
							title: '业务类型',
							field: 'businessType',
						},
						{
							width: 110,
							title: '操作',
							action: true,
							plugins: [{
								name: '查看',
								audit: '',
								event: {
									click: (e : any) => {
										(this as any).openEdit(e)
									}
								}
							}]
						}
					]
				}
			}
		}
		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.smt.table.attr.height = window.innerHeight - 300;
			this.config.draftsBox.table.attr.height = window.innerHeight - 300;
			this.config.all.table.attr.height = window.innerHeight - 300;
			this.config.recycleBin.table.attr.height = window.innerHeight - 300;
			(this as any).$refs.bill.$refs.all.$refs.table.recalculate();
		}
		// 获取当前日期
		getNowDate(num : number) {
			let date = new Date();
			let year = date.getFullYear();
			let month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
			let day : any = date.getDate();
			if (day - num < 0) {
				day = '01';
			} else day = day - num < 10 ? '0' + (day - num) : day - num;
			return year + '-' + month + '-' + day;
		}
		// 同步入库单
		syncEntry() {
			this.value = true;
		}
		printBefore(param : any) {
			param.templateId = 11;
			if (param.templateData && param.templateData.suborderList) {
				param.templateData.totalNumber = 0;
				param.templateData.totalDeliveryNumber = 0;
				param.templateData.totalPri = 0;
				for (const item of param.templateData.suborderList) {
					if (Number(item.number)) {
						param.templateData.totalNumber = add(param.templateData.totalNumber, Number(item.number));
					}
					if (Number(item.deliveryNumber)) {
						param.templateData.totalDeliveryNumber = add(param.templateData.totalDeliveryNumber, Number(item.deliveryNumber))
					}
					if (Number(item.totalPrice)) {
						param.templateData.totalPri = add(param.templateData.totalPri, Number(item.totalPrice))
					}
				}
			}
		}
		// 获取时间段
		change(v : any) {
			if (v) {
				this.beginDate = v[0];
				this.endDate = v[1];
			} else {
				this.beginDate = '';
				this.endDate = '';
			}
		}
		// 确认同步入库单
		confirm() {
			if (this.beginDate == '' || this.endDate == '') return this.$message.warning('请选择时间段');
			let loading = this.$loading({ target: '.main-container' });
			api.getFinanceEntryOrder({
				stime: this.beginDate,
				etime: this.endDate,
			}).then((res : any) => {
				this.value = false;
				this.$message.success('同步成功');
				loading.close();
				if (res.code === 200) this.getPageList({}, 'all'); // 综合数据
			}).catch(() => loading.close());
		}
		// 获取机构数据
		getSupplierModal(e : any) {
			this.supplierInfo = e;
		}
		// 监听表格数据变化
		onChangeRow(e : any) {
			this.isChange = true;
			this.isSubmit = false;
			e.totalPrice = parseFloat((e.number * e.unitPrice).toFixed(2));
		}
		// 保存
		onSave(e : any) {
			// console.log('保存 ==>', this.tabType);
			(this as any).$refs.bill.$refs.billForm.validate().then(() => {
				let query = (this.$refs.bill as any).getBillFormValue(); // 获取单据数据
				console.log('单据表单数据 ==> ', query);
				// 供应商数据
				if (!query.organizationName) query.organizationName = this.supplierInfo.name;
				if (!query.organizationId) query.organizationId = this.supplierInfo.id;
				if (!query.organizationName) return this.$message.warning('请选择机构名称');
				// 子表数据
				query.suborderList = this.tableData;
				if (query.suborderList.length > 0) {
					for (let v of query.suborderList) {
						if (!v.number || v.number <= 0) return this.$message.warning('请输入正确物料数量');
						if (!v.unitPrice || v.unitPrice <= 0) return this.$message.warning('请输入正确物料单价');
						if (v.prePrice.length <= 0 || v.prePrice < 0) return this.$message.warning('请输入正确优惠金额');
						if (query.id) v.taskOrderId = query.id;
					}
				};
				// 表格删除
				query.suborderList.map((v : any, i : any) => {
					if (v.isDeleted === 1 && !v.id) {
						query.suborderList.splice(i, 1)
					}
				});
				console.log('保存参数query ==> ', query);
				let loading = this.$loading({ target: '.main-container' });
				if (this.isAdd) {
					api.saveList(query, 'supplyEntryOrder').then((res : any) => {
						loading.close();
						if (res.code === 200) {
							this.isAdd = false;
							this.isChange = false;
							this.isSubmit = true;
							this.$message.success('保存成功');
							this.dataId = res.data;
						} else this.$message.error(res.msg);
					}).catch(() => loading.close());
				} else {
					query.submitState = 0;
					console.log('保存 ==>', query);
					api.updateList(query, 'supplyEntryOrder').then((res : any) => {
						loading.close();
						if (res.code === 200) {
							this.$message.success('保存成功');
							this.isChange = false;
							this.isSubmit = true;
						} else this.$message.error(res.msg);
					}).catch(() => loading.close());
				};

			})
		}
		// 打开新增
		onAdd(e : any) {
			console.log('新增 ==>', e);
			if (e === 'billTable_0') {
				(this.$refs.product as any).setShow(true); // 打开sku弹窗
			} else {
				let billConfig = (this as any).$lodash.cloneDeep(this.config.bill);
				billConfig.form.attr.readonly = false; // 设置只读
				billConfig.tool.tools = { return: true, save: true, };
				if (e === 'all') {
					billConfig.tool.tools = { return: true, save: true, smt: true };
					this.tableData = [];
					(this.$refs.bill as any).setBillTableValue(this.tableData, 0);// 设置第1张单据表格数据
				}
				billConfig.tableConfig[0].tool.tools = { add: true };
				this.isAdd = true;
				(this.$refs.bill as any).setBillConfig(billConfig);
				// 设置单据表单数据
				(this.$refs.bill as any).setBillFormValue({ operator: (this as any).$store.getters.userInfo.userName });
			};
			(this.$refs.bill as any).showTab = 'bill';
		}
		// 打开编辑
		openEdit(e : any) {
			console.log('编辑this.tabType ==>', this.tabType);
			e = e.row ? e.row : e;
			console.log('编辑 ==>', e);
			let loading = this.$loading({ target: '.main-container' });
			api.single({ id: e.id }, 'supplyEntryOrder').then((res : any) => {
				if (res.code === 200) {
					let parentData = res.data ? res.data : e;
					this.baseInfo = parentData;
					if (this.tabType === 'all' || this.tabType === 'draftsBox') {
						if (parentData.submitState === 1) {
							loading.close();
							return this.setReadonly(parentData)
						}
						let data = parentData.suborderList
						data.map((v : any) => {
							if (!v.prePrice) v.prePrice = '0';
							v.towInOne = v.storehouseName + ',' + v.storingLocationName; // 仓库仓位
						});
						let billConfig = (this as any).$lodash.cloneDeep(this.config.bill);
						if (billConfig.tableConfig[0].table.columns.at(-1).title != '操作') {
							billConfig.tableConfig[0].table.columns.push({
								width: 90,
								title: '操作',
								action: true,
								plugins: [{
									icon: 'el-icon-delete',
									name: '删除',
									audit: '',
									event: {
										click: (item : any) => {
											(this as any).changeTable(item)
										}
									}
								}]
							})
						};
						billConfig.form.attr.readonly = false; // 设置只读
						billConfig.tool.tools = { return: true, save: true, };
						if (this.tabType === 'draftsBox' || parentData.submitState === 0) billConfig.tool.tools = { return: true, save: true, smt: true };
						billConfig.tableConfig[0].tool.tools = { add: true };
						this.tableData = data;
						(this.$refs.bill as any).setBillConfig(billConfig);
						(this.$refs.bill as any).setBillFormValue(parentData);// 设置单据表单数据
						(this.$refs.bill as any).setBillTableValue(data, 0);// 设置第1张单据表格数据
						this.isAdd = false;
						this.dataId = '';
						(this.$refs.bill as any).showTab = 'bill';
						loading.close();
					} else {
						loading.close();
						this.setReadonly(parentData);
					};
				}
			}).catch(() => loading.close());

		}
		// 设置只读
		setReadonly(e : any) {
			let readonly = (this as any).$lodash.cloneDeep(this.config.bill);
			readonly.form.attr.readonly = true; // 设置只读
			readonly.tool.tools = { return: true };
			if (this.tabType != 'recycleBin') {
				if (e.submitState === 1) readonly.tool.tools = { return: true, returnSmt: true, print: true };
			}
			readonly.tableConfig[0].table.columns = readonly.tableConfig[0].table.columns.filter((v : any) => v.title != '操作');
			readonly.tableConfig[0].tool.tools = {};
			let data = e.suborderList;
			data.map((v : any) => {
				if (!v.prePrice) v.prePrice = '0';
				v.towInOne = v.storehouseName + ',' + v.storingLocationName; // 仓库仓位
			});
			(this.$refs.bill as any).setBillConfig(readonly);
			(this.$refs.bill as any).setBillFormValue(e);// 设置单据表单数据
			(this.$refs.bill as any).setBillTableValue(data, 0);// 设置第1张单据表格数据
			this.isAdd = false;
			(this.$refs.bill as any).showTab = 'bill';

		}
		// 工具栏提交
		onSmt(e : any) {
			if (e === 'draftsBox') {
				let selectData = (this.$refs.bill as any).getTableSelectData('draftsBox'); // 获取表格选中数据
				if (selectData.length <= 0) return this.$message.warning('请选择提交数据');
				if (selectData.length > 1) return this.$message.warning('只能提交一条数据');
				this.$confirm('确定提交吗！', '注意', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning',
					center: true
				}).then(() => {
					let loading = this.$loading({ target: '.main-container' });
					api.submitTo({ id: selectData[0].id }, 'supplyEntryOrder').then((res : any) => {
						loading.close();
						if (res.code === 200) {
							this.$message.success('提交成功');
							this.clickTab('draftsBox');
						} else this.$message.error(res.msg)
					}).catch(() => loading.close());
				}).catch(() => this.$message.info('已取消提交'));
			} else {
				if (this.isAdd) return this.$message.warning('新增数据必须先保存才能提交');
				console.log('isChange ==> ', this.isChange);
				console.log('isSubmit ==> ', this.isSubmit);
				if (this.isSubmit) {
					this.$confirm('确定提交吗！', '注意', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning',
						center: true
					}).then(() => {
						let id = this.dataId ? this.dataId : this.baseInfo.id;
						let loading = this.$loading({ target: '.main-container' });
						api.submitTo({ id: id }, 'supplyEntryOrder').then((res : any) => {
							loading.close();
							if (res.code === 200) {
								this.$message.success('提交成功');
								this.isSubmit = false;
								this.dataId = '';
								this.clickTab(this.tabType);
							} else this.$message.error(res.msg)
						}).catch(() => loading.close());
					}).catch(() => this.$message.info('已取消提交'));
				} else {
					// 表单旧数据
					let oldbillForm : any = (this as any).$lodash.cloneDeep(this.baseInfo);
					if (oldbillForm.suborderList) delete oldbillForm.suborderList;
					oldbillForm = JSON.stringify(oldbillForm);
					// 表单新数据
					let newBillForm : any = (this as any).$refs.bill.getBillFormValue();
					if (newBillForm.suborderList) delete newBillForm.suborderList;
					newBillForm = JSON.stringify(newBillForm);
					// 表格旧数据
					this.baseInfo.suborderList.map((v : any) => {
						if (v._X_ROW_KEY) delete v._X_ROW_KEY;
					});
					let oldData : any = JSON.stringify(this.baseInfo.suborderList);
					// 表格新数据
					let newData : any = JSON.stringify((this as any).$refs.bill.getBillTableData(0));
					// 数据有变进保存
					if (this.isChange || oldbillForm != newBillForm || oldData != newData) {
						return this.$message.warning('数据变更必须先保存再提交');
					} else {
						this.$confirm('确定提交吗！', '注意', {
							confirmButtonText: '确定',
							cancelButtonText: '取消',
							type: 'warning',
							center: true
						}).then(() => {
							let loading = this.$loading({ target: '.main-container' });
							api.submitTo({ id: this.baseInfo.id }, 'supplyEntryOrder').then((res : any) => {
								loading.close();
								if (res.code === 200) {
									this.$message.success('提交成功');
									this.clickTab(this.tabType);
								} else this.$message.error(res.msg)
							}).catch(() => loading.close());
						}).catch(() => this.$message.info('已取消提交'));
					}
				}
			}
		}
		// 操作反提交
		onReturnSmt(e : any) {
			console.log('反提交 ==>', e);
			this.$confirm('确定反提交吗！', '注意', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				center: true
			}).then(() => {
				let loading = this.$loading({ target: '.main-container' });
				api.cancelSubmission({ id: e.id }, 'supplyEntryOrder').then((res : any) => {
					loading.close();
					if (res.code === 200) {
						this.$message.success('反提交成功');
						this.clickTab(this.tabType);
					} else this.$message.error(res.msg)
				}).catch(() => loading.close());
			}).catch(() => this.$message.info('已取消反提交'));
		}
		// 工具栏反提交
		onReturnSmt2() {
			console.log('工具栏反提交 ==>', this.baseInfo);
			this.$confirm('确定反提交吗！', '注意', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				center: true
			}).then(() => {
				let loading = this.$loading({ target: '.main-container' });
				api.cancelSubmission({ id: this.baseInfo.id }, 'supplyEntryOrder').then((res : any) => {
					loading.close();
					if (res.code === 200) {
						this.$message.success('反提交成功');
						this.clickTab(this.tabType);
					} else this.$message.error(res.msg)
				}).catch(() => loading.close());
			}).catch(() => this.$message.info('已取消反提交'));
		}
		// 切换tab栏
		clickTab(e : any) {
			console.log('切换tab ==> ', e);
			this.tabType = e;
			(this.$refs.bill as any).showTab = e;
			// submitState: 提交状态
			if (e === 'all') this.getPageList({}, 'all'); // 草稿箱数据
			if (e === 'draftsBox') this.getPageList({ submitState: 0 }, 'draftsBox'); // 草稿箱数据
			if (e === 'smt') this.getPageList({ submitState: 1 }, 'smt'); // 已提交数据
			if (e === 'recycleBin') this.getSelectDeleteList({}); // 回收站数据
			if (e === 'bill') {
				let billConfig = (this as any).$lodash.cloneDeep(this.config.bill);
				billConfig.tool.tools = {};
				billConfig.tableConfig[0].tool.tools = {};
				(this.$refs.bill as any).setBillConfig(billConfig);
			}
		}
		// 初始化
		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(this.tabType);
			query.pageNo = data.pageNo;
			query.pageSize = data.pageSize;
			let loading = this.$loading({ target: '.main-container' });
			api.pageList(query, 'supplyEntryOrder').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());
		}
		// 分页
		pagination(e : any) {
			let query : any = (this as any).$refs.bill.getSearchValue(e.type);
			query.pageNo = e.page.pageNum;
			query.pageSize = e.page.pageSize;
			console.log('分页 ==> ', query);
			let loading = this.$loading({ target: '.main-container' });
			if (e.type === 'recycleBin') {
				api.selectDeleteList(query, 'supplyEntryOrder').then((res : any) => {
					loading.close();
					if (res.code === 200) {
						(this.$refs.bill as any).setTabTableValue('recycleBin', res.data.records);
					} else this.$message.error(res.msg)
				}).catch(() => loading.close());
			} else {
				api.pageList(query, 'supplyEntryOrder').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());
			}
		}
		// 回收站数据
		getSelectDeleteList(query : any) {
			let data = (this.$refs.bill as any).getTablePage(this.tabType);
			query.pageNo = data.pageNo;
			query.pageSize = data.pageSize;
			let loading = this.$loading({ target: '.main-container' });
			api.selectDeleteList(query, 'supplyEntryOrder').then((res : any) => {
				loading.close();
				if (res.code === 200) {
					(this.$refs.bill as any).setTabTableValue('recycleBin', res.data.records);
					let page = {
						pageNo: res.data.current, //当前页
						pageSize: res.data.size, //每页条数
						total: res.data.total //总条数
					};
					(this.$refs.bill as any).setTablePage('recycleBin', page)
				} else this.$message.error(res.msg)
			}).catch(() => loading.close());
		}
		// 返回
		onReturn() {
			this.clickTab(this.tabType);
		}
		// 搜索
		search(parames : any) {
			console.log('搜索 ==> ', parames);
			let query = parames.value
			if (parames.type === 'draftsBox') query.submitState = 0 // 草稿箱数据
			if (parames.type === 'smt') query.submitState = 1 // 已提交数据
			if (parames.type === 'recycleBin') {
				this.getSelectDeleteList(query); // 回收站分页数据
			} else {
				this.getPageList(query, parames.type) // 获取分页数据
			}
		}
		// 刷新/重置
		resert(e : any) {
			let data : any = e.type ? e.type : e;
			(this as any).$refs.bill.$refs[data].clearSearch();
			if (data === 'all') this.getPageList({}, 'all'); // 综合数据
			if (data === 'draftsBox') this.getPageList({ submitState: 0 }, 'draftsBox'); // 草稿箱数据
			if (data === 'smt') this.getPageList({ submitState: 1 }, 'smt'); // 已提交数据
			if (data === 'recycleBin') this.getSelectDeleteList({}) // 回收站数据
		}
		// 确定商品新增
		confirmProduct(e : any) {
			let nowData = (this.$refs.bill as any).getBillTableData(0);
			let names = '';
			if (e.length > 0) {
				for (const item of e) {
					let t = true;
					for (const t_item of nowData) {
						if (item.id == t_item.materialSku) {
							if (!names) {
								names = item.skuTitle
							} else {
								names = names + ',' + item.skuTitle
							}
							t = false;
							break;
						}
					}
					if (t) {
						let obj : any = {};
						obj.materialName = item.skuTitle
						obj.materialId = item.materialId
						obj.prePrice = 0
						obj.unitPrice = item.price
						obj.materialSku = item.id
						obj.unit = item.unit
						obj.unitCode = item.unitCode
						obj.financeSuborderId = item.financialCode
						obj.prePrice = "0"
						obj.isDeleted = 0
						this.tableData.push(obj);
						nowData.push(obj);
					}
				}
				(this.$refs.bill as any).setBillTableValue(nowData, 0);// 设置第1张单据表格数据
				if (names) {
					this.$alert('商品 “' + names + '”已存在！', '提示', {
						confirmButtonText: '确定',
					});
				}
			}
		}
		// 单据表格删除
		changeTable(item : any) {
			this.$confirm('确定删除吗！', '注意', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				center: true
			}).then(() => {
				let data = (this.$refs.bill as any).getBillTableData(0);
				let i = -1;
				let j = -1;
				for (const v of data) {
					i++;
					if (v.materialSku == item.materialSku) {
						break
					}
				};

				for (const v of this.tableData) {
					j++;
					if (v.materialSku == item.materialSku && item.isDeleted === 0) {
						break
					}
				};

				this.tableData[j].isDeleted = 1;
				data.splice(i, 1);
				(this.$refs.bill as any).setBillTableValue(data, 0);// 设置第1张单据表格数据
				this.$message.success('删除成功');
			}).catch(() => this.$message.info('已取消删除'));
		}
		// 工具栏删除
		onDelete(e : any) {
			console.log('工具栏删除 ==> ', e);
			let selectData = (this.$refs.bill as any).getTableSelectData(e)
			let ids = '';
			if (selectData.length > 0) {
				selectData.map((v : any) => {
					ids += v.id + ','
				})
			} else return this.$message({ type: 'warning', message: '请选择删除数据' })
			ids = ids.slice(0, ids.length - 1);
			this.$confirm('确定删除吗,此操作不能撤销！', '注意', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				center: true
			}).then(() => {
				let loading = this.$loading({ target: '.main-container' });
				api.deleteList({ ids: ids }, 'supplyEntryOrder').then((res : any) => {
					loading.close();
					if (res.code === 200) {
						this.$message.success('删除成功');
						this.clickTab(this.tabType);
					} else this.$message.error(res.msg)
				}).catch(() => loading.close());
			}).catch(() => this.$message.info('已取消删除'));
		}
		// 操作删除
		doDelete2(item : any) {
			this.$confirm('确定删除吗,此操作不能撤销！', '注意', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				center: true
			}).then(() => {
				let loading = this.$loading({ target: '.main-container' });
				api.deleteList({ ids: item.id }, 'supplyEntryOrder').then((res : any) => {
					loading.close();
					if (res.code === 200) {
						this.$message.success('删除成功');
						this.clickTab(this.tabType);
					} else this.$message.error(res.msg)
				}).catch(() => loading.close());
			}).catch(() => this.$message.info('已取消删除'));
		}
	}
</script>
<style scoped lang="scss">
	.btn {
		width: 100%;
		display: flex;
		justify-content: flex-end;
	}

	.type {
		width: 100%;
		padding: 8px 0;
	}

	.cont {
		width: 100%;
		display: flex;
		align-items: center;
		padding: 8px 0;

		.t-title {
			width: 60px;
			text-align: right;

			>span {
				color: #F00;
			}
		}

		.right-cont {
			width: calc(100% - 60px);
		}
	}
</style>