<template>
	<!-- 配置弹窗 -->
	<vxe-modal v-model="setValue" width="60%" height="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>
			<by-form v-if="type=='setCForm'" key="setCForm" :propConfig="setCForm" ref="setCForm"></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: '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: '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
							}
						}
					}
				]
			]
		}
		setCForm : 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: '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
							}
						}

					}
				]
			]
		}
		// 打开弹窗
		openSetting() {
			this.setValue = true;
			let loading = this.$loading({ target: '.main-container' });
			api.single({ id: 1 }, "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]);
							}
						}, 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());
					
				})
			}
			if (this.type == "setCForm") {
				(this as any).$refs.setCForm.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 })
					}
					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());
					
				})
			}
		}
	}
</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>