<template>
	<!-- 配置弹窗 -->
	<vxe-modal v-model="setValue" width="70%" :height="type=='setCForm'?'100%':'auto'" show-zoom resize transfer show-footer>
		<template #title>
			<span>{{title}}参数配置</span>
		</template>
		<template #default>
			<by-form v-if="type=='setXForm'" key="setXForm" :propConfig="setXForm" ref="setXForm"></by-form>
			<div v-if="type=='setCForm'" style="font-size: 22px;font-weight: 400;color: #1f2f3d;margin: 25px;">销售参数配置</div>
			<by-form v-if="type=='setCForm'" key="setCForm" :propConfig="setCForm" ref="setCForm"></by-form>
			<div v-if="type=='setCForm'" style="font-size: 22px;font-weight: 400;color: #1f2f3d;margin: 25px;">调拨参数配置</div>
			<by-form v-if="type=='setCForm'" key="setCForms" :propConfig="setCForms" ref="setCForms"></by-form>
		</template>
		<template #footer>
			<div class="btn">
				<el-button type="primary" size="small" @click="determine">保存</el-button>
			</div>
		</template>
	</vxe-modal>
</template>

<script lang="ts">
	import { Component, Prop, Vue, Watch } from "vue-property-decorator";
	import VueViews from '@/benyun/compVue/VueViews'
	import api from "@/api/order";
	@Component
	export default class SettingConfig extends VueViews {
		setValue : any = false
		baseInfo : any = ""
		get getDepId() {
			return this.$store.state.settings.depId;
		}
		@Prop()
		type : any;
		@Prop()
		title : any;
		setXForm : any = {
			attr: {
				size: 'medium',
			},
			columns: [
				[
					{
						span: 8,
						label: '单据类型',
						prop: 'FBillTypeID',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入单据类型', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入单据类型',
								clearable: true
							}
						}
					},
					{
						span: 8,
						label: '客户',
						prop: 'FCustId',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入客户', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入客户',
								clearable: true
							}
						}
					},
					{
						span: 8,
						label: '收货方',
						prop: 'FReceiveId',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入收货方', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入收货方',
								clearable: true
							}
						}
					},
				],
				[
					{
						span: 8,
						label: '结算方',
						prop: 'FSettleId',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入结算方', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入结算方',
								clearable: true
							}
						}
					},
					{
						span: 8,
						label: '付款方',
						prop: 'FNAME',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入付款方', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入付款方',
								clearable: true
							}
						}
					},
					{
						span: 8,
						label: '汇率类型',
						prop: 'FExchangeTypeId',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入汇率类型', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入汇率类型',
								clearable: true
							}
						}
					},
				],
				[
					{
						span: 8,
						label: '产品类型',
						prop: 'FRowType',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入产品类型', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入产品类型',
								clearable: true
							}
						}
					},
					{
						span: 8,
						label: '货主',
						prop: 'FOwnerID',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入货主', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入货主',
								clearable: true
							}
						}
					},
					{
						span: 8,
						label: '货主类型',
						prop: 'FOwnerTypeId',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入货主类型', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入货主类型',
								clearable: true
							}
						}
					},
				],
				[
					{
						span: 8,
						label: 'BOM版本',
						prop: 'FBomId',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入BOM版本', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入BOM版本',
								clearable: true
							}
						}
					},
					{
						span: 8,
						label: '预留类型',
						prop: 'FReserveType',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入预留类型', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入预留类型',
								clearable: true
							}
						}
					},
					{
						span: 8,
						label: '超发控制',
						prop: 'FOUTLMTUNIT',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入超发控制', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入超发控制',
								clearable: true
							}
						}
					}
				],
				[
					{
						span: 8,
						label: '结算币别',
						prop: 'FSettleCurrId',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入结算币别', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入结算币别',
								clearable: true
							}
						}
					},
					{
						span: 8,
						label: '库存组织',
						prop: 'FStockOrgId',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入库存组织', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入库存组织',
								clearable: true
							}
						}
					},
				]
			]
		}
		setCForm : any = {
			attr: {
				size: 'medium',
				labelWidth: '140px'
			},
			columns: [
				[
					{
						span: 8,
						label: '单据类型',
						prop: 'FBillTypeID',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入单据类型', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入单据类型',
								clearable: true
							}
						}
					},
					{
						span: 8,
						label: '客户',
						prop: 'FCustomerID',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入客户', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入客户',
								clearable: true
							}
						}
					},
					{
						span: 8,
						label: '收货方',
						prop: 'FReceiverID',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入收货方', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入收货方',
								clearable: true
							}
						}
					},
				],
				[
					{
						span: 8,
						label: '结算方',
						prop: 'FSettleID',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入结算方', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入结算方',
								clearable: true
							}
						}
					},
					{
						span: 8,
						label: '付款方',
						prop: 'FPayerID',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入付款方', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入付款方',
								clearable: true
							}
						}
					},
					{
						span: 8,
						label: '汇率类型',
						prop: 'FExchangeTypeID',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入汇率类型', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入汇率类型',
								clearable: true
							}
						}
					},
				],
				[
					{
						span: 8,
						label: '汇率',
						prop: 'FExchangeRate',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入汇率', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入汇率',
								clearable: true
							}
						}
					},
					{
						span: 8,
						label: '结算币别',
						prop: 'FSettleCurrID',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入结算币别', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入结算币别',
								clearable: true
							}
						}
					},
					{
						span: 8,
						label: '产品类型',
						prop: 'FRowType',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入产品类型', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入产品类型',
								clearable: true
							}
						}
					},
				],
				[
					{
						span: 8,
						label: '货主类型',
						prop: 'FOwnerTypeIdHead',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入货主类型', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入货主类型',
								clearable: true
							}
						}
					},
					{
						span: 8,
						label: '本位币',
						prop: 'FLocalCurrID',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入本位币', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入本位币',
								clearable: true
							}
						}
					},
					{
						span: 8,
						label: '货主',
						prop: 'FOwnerID',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入货主', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入货主',
								clearable: true
							}
						}
					}
				],
				[
					{
						span: 8,
						label: '库存状态',
						prop: 'FStockStatusID',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入库存状态', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入库存状态',
								clearable: true
							}
						}

					}
				]
			]
		}
		setCForms : any = {
			attr: {
				size: 'medium',
				labelWidth: '140px'
			},
			columns: [
				[
					{
						span: 8,
						label: '单据类型',
						prop: 'FBillTypeID',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入单据类型', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入单据类型',
								clearable: true
							}
						}
					},
					{
						span: 8,
						label: '业务类型',
						prop: 'FBizType',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入业务类型', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入业务类型',
								clearable: true
							}
						}
					},
					{
						span: 8,
						label: '调拨方向',
						prop: 'FTransferDirect',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入调拨方向', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入调拨方向',
								clearable: true
							}
						}
					},
				],
				[
					{
						span: 8,
						label: '调拨类型',
						prop: 'FTransferBizType',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入调拨类型', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入调拨类型',
								clearable: true
							}
						}
					},
					{
						span: 8,
						label: '调出货主类型',
						prop: 'FOwnerTypeOutIdHead',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入调出货主类型', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入调出货主类型',
								clearable: true
							}
						}
					},
					{
						span: 8,
						label: '调出货主',
						prop: 'FOwnerOutIdHead',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入调出货主', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入调出货主',
								clearable: true
							}
						}
					},
				],
				[
					{
						span: 8,
						label: '汇率类型',
						prop: 'FExchangeTypeId',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入汇率类型', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入汇率类型',
								clearable: true
							}
						}
					},
					{
						span: 8,
						label: '调入货主类型',
						prop: 'FOwnerTypeIdHead',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入调入货主类型', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入调入货主类型',
								clearable: true
							}
						}
					},
					{
						span: 8,
						label: '结算币别',
						prop: 'FSETTLECURRID',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入结算币别', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入结算币别',
								clearable: true
							}
						}
					},
				],
				[
					{
						span: 8,
						label: '汇率',
						prop: 'FExchangeRate',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入汇率', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入汇率',
								clearable: true
							}
						}
					},
					{
						span: 8,
						label: '调入货主',
						prop: 'FOwnerIdHead',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入调入货主', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入调入货主',
								clearable: true
							}
						}
					},
					{
						span: 8,
						label: '本位币',
						prop: 'FBaseCurrId',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入本位币', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入本位币',
								clearable: true
							}
						}
					}
				],
				[
					{
						span: 8,
						label: '产品类型',
						prop: 'FRowType',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入产品类型', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入产品类型',
								clearable: true
							}
						}
					},
					{
						span: 8,
						label: '调出库存状态',
						prop: 'FSrcStockStatusId',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入调出库存状态', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入调出库存状态',
								clearable: true
							}
						}
							
					},
					{
						span: 8,
						label: '调入库存状态',
						prop: 'FDestStockStatusId',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入调入库存状态', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入调入库存状态',
								clearable: true
							}
						}
							
					}
				],
				[
					{
						span: 8,
						label: '调出货主类型',
						prop: 'FOwnerTypeOutId',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入调出货主类型', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入调出货主类型',
								clearable: true
							}
						}
					},
					{
						span: 8,
						label: '调出货主',
						prop: 'FOwnerOutId',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入调出货主', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入调出货主',
								clearable: true
							}
						}
					},
					{
						span: 8,
						label: '调入货主类型',
						prop: 'FOwnerTypeId',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入调入货主类型', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入调入货主类型',
								clearable: true
							}
						}
					}
				],
				[
					{
						span: 8,
						label: '调入货主',
						prop: 'FOwnerId',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入调入货主', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入调入货主',
								clearable: true
							}
						}
					},
					{
						span: 8,
						label: '调入保管者类型',
						prop: 'FKeeperTypeId',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入调入保管者类型', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入调入保管者类型',
								clearable: true
							}
						}
					},
					{
						span: 8,
						label: '调入保管者',
						prop: 'FKeeperId',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入调入保管者', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入调入保管者',
								clearable: true
							}
						}
					}
				],
				[
					{
						span: 8,
						label: '调出保管者类型',
						prop: 'FKeeperTypeOutId',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入调出保管者类型', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入调出保管者类型',
								clearable: true
							}
						}
					},
					{
						span: 8,
						label: '调出保管者',
						prop: 'FKeeperOutId',
						component: 'by-input',
						rules: [
							{ required: true, message: '请输入调出保管者', trigger: 'blur' }
						],
						compConfig: {
							attr: {
								placeholder: '请输入调出保管者',
								clearable: true
							}
						}
					}
				]
			]
		}
		// 打开弹窗
		openSetting() {
			this.setValue = true;
			let loading = this.$loading({ target: '.main-container' });
			api.getSingle("supplyFinanceSetting").then((res : any) => {
				loading.close();
				if (res.data) {
					this.baseInfo = res.data;
					if (this.type == "setXForm") {
						setTimeout(() => {
							if (res.data.saleorderSetting && res.data.saleorderSetting.indexOf(this.getDepId) != -1) {
								(this as any).$refs.setXForm.setValue(JSON.parse(res.data.saleorderSetting)[this.getDepId]);
							}
						}, 0)
					} else if (this.type == "setCForm") {
						setTimeout(() => {
							if (res.data.outorderSetting && res.data.outorderSetting.indexOf(this.getDepId) != -1) {
								(this as any).$refs.setCForm.setValue(JSON.parse(res.data.outorderSetting)[this.getDepId]);
							}
							if (res.data.transferdirectSetting && res.data.transferdirectSetting.indexOf(this.getDepId) != -1) {
								(this as any).$refs.setCForms.setValue(JSON.parse(res.data.transferdirectSetting)[this.getDepId]);
							}
						}, 0)
					}
				}
			}).catch(() => {
				loading.close();
				this.setValue = false;
			})
		}
		created() {

		}
		determine() {
			let value : any = "";
			let data : any = {}
			if (this.type == "setXForm") {
				(this as any).$refs.setXForm.validate().then(() => {
					value = (this as any).$refs.setXForm.getValue();
					if (this.baseInfo.saleorderSetting) {
						let obj : any = JSON.parse(this.baseInfo.saleorderSetting);
						if (obj[this.getDepId]) {
							obj[this.getDepId] = value
						} else {
							if (this.getDepId == "kingdee") {
								obj.kingdee = value
							} else obj.yonbip = value
						}
						data.saleorderSetting = JSON.stringify(obj);
					} else {
						data.saleorderSetting = JSON.stringify({ [this.getDepId]: value })
					}
					if (this.baseInfo) data.id = this.baseInfo.id;
					let loading = this.$loading({ target: '.main-container' });
					api.updateList(data, 'supplyFinanceSetting').then((res : any) => {
						if (res.code === 200) {
							this.setValue = false;
							loading.close();
							this.$message.success("保存成功");
						} else loading.close();
					}).catch(() => loading.close());
					
				}).catch(() => {});
			}
			if (this.type == "setCForm") {
				(this as any).$refs.setCForm.validate().then(() => {
					(this as any).$refs.setCForms.validate().then(() => {
						
						value = (this as any).$refs.setCForm.getValue();
						if (this.baseInfo.outorderSetting) {
							let obj : any = JSON.parse(this.baseInfo.outorderSetting);
							if (obj[this.getDepId]) {
								obj[this.getDepId] = value
							} else {
								if (this.getDepId == "kingdee") {
									obj.kingdee = value
								} else obj.yonbip = value
							}
							data.outorderSetting = JSON.stringify(obj);
						} else {
							data.outorderSetting = JSON.stringify({ [this.getDepId]: value })
						}
						
						value = (this as any).$refs.setCForms.getValue();
						if (this.baseInfo.transferdirectSetting) {
							let obj : any = JSON.parse(this.baseInfo.transferdirectSetting);
							if (obj[this.getDepId]) {
								obj[this.getDepId] = value
							} else {
								if (this.getDepId == "kingdee") {
									obj.kingdee = value
								} else obj.yonbip = value
							}
							data.transferdirectSetting = JSON.stringify(obj);
						} else {
							data.transferdirectSetting = JSON.stringify({ [this.getDepId]: value })
						}
						
						if (this.baseInfo) data.id = this.baseInfo.id;
						let loading = this.$loading({ target: '.main-container' });
						api.updateList(data, 'supplyFinanceSetting').then((res : any) => {
							if (res.code === 200) {
								this.setValue = false;
								loading.close();
								this.$message.success("保存成功");
							} else loading.close();
						}).catch(() => loading.close());
						
					}).catch(() => {});
					
				}).catch(() => {});
			}
		}
	}
</script>

<style scoped lang="scss">
	.orderType {
		.typeText {
			width: 40px;
			height: 18px;
			line-height: 18px;
			text-align: center;
			background-color: #0089ff;
			border-radius: 12px;
			color: #fff;
			font-size: 12px;
			margin-left: -10px;
		}

		.typeText2 {
			background-color: #E6A23C;
		}
	}
</style>