<template>
	<view class="partner">
		<!-- 表单组件 -->
		<view class="form-wrapper" v-if="setting">
			<u-form :model="form" ref="uForm" label-width="240rpx">
				<view class="brand-poster">
					<image :src="setting.background" mode="widthFix" class="banner"></image>
					<view class="brand-name" v-html="setting.content"></view>
				</view>
				<view class="buy-desc">如果有意向，请填写以下信息给平台留言</view>
				<!-- 表单组件 -->
				<view class="form-wrapper">
					<u-form :model="form" ref="uForm" label-width="140rpx">
						<u-form-item label="姓名" prop="real_name">
							<u-input v-model="form.real_name" :disabled="disabled" placeholder="请输入姓名" />
						</u-form-item>
						<u-form-item label="电话" prop="phone">
							<u-input v-model="form.phone" maxlength="11" :disabled="disabled" placeholder="请输入联系电话" />
						</u-form-item>
						<u-form-item label="性别" prop="sex">
							<u-radio-group v-model="form.sex">
								<u-radio @change="groupChange" v-for="(item, index) in sex" :key="index"
									:name="item.name" :disabled="item.disabled">
									{{item.name}}
								</u-radio>
							</u-radio-group>
						</u-form-item>
						<u-form-item label="定制预算" prop="money">
							<u-input type="number" v-model="form.money" :disabled="disabled" maxlength="11" placeholder="请输入定制预算" />
						</u-form-item>
						<u-form-item label="微信号" prop="weixin">
							<u-input v-model="form.weixin" :disabled="disabled" placeholder="请输入微信号" />
						</u-form-item>
						<u-form-item label="意向区域" prop="region">
							<select-region ref="sRegion" :disabled="disabled" v-model="form.region" />
						</u-form-item>
						<u-form-item label="整体需求" prop="comments">
							<u-input v-model="form.comments" :disabled="disabled" placeholder="请输入整体需求" />
						</u-form-item>

					</u-form>
				</view>
				<!-- 操作按钮 -->
				<view class="footer">
					<view class="btn">
						<u-button type="primary" shape="circle" @click="handleSubmit()">提交</u-button>
					</view>
				</view>
			</u-form>
		</view>
		<!-- 操作按钮 -->
		<!-- <view class="footer">
			<view class="btn">
				<u-button type="primary" shape="circle">更新资料</u-button>
			</view>
		</view> -->
	</view>
</template>

<script>
	// 表单字段元素
	const form = {
		name: '',
		phone: '',
		sex: '',
		money: '',
		region: [],
		weixin: '',
		comments: '',
	}

	const sex = [{
			name: '男',
			value: 1,
			disabled: false
		},
		{
			name: '女',
			value: 2,
			disabled: false
		}
	]

	import * as Api from '@/api/solution/index.js'
	import * as ApplyApi from '@/api/solution/apply.js'
	import * as SettingApi from '@/api/solution/setting.js'
	import SelectRegion from '@/components/select-region/select-region'
	export default {
		components: {
			SelectRegion
		},
		data() {
			return {
				form,
				disabled: false,
				detail: {},
				solutionId: 0,
				sex,
				setting: {}
			}
		},
		onLoad() {
			this.init()
		},
		methods: {
			// 获取当前用户信息
			init() {
				const app = this
				SettingApi.data().then(result => {
					app.setting = result.data.setting
					// 设置当前页面标题
					app.setPageTitle()
				})
			},
			
			// 设置当前页面标题
			setPageTitle() {
			  uni.setNavigationBarTitle({ title: this.setting.title })
			},
			
			groupChange(e) {
				this.form.sex = e
			},

			// 表单提交
			handleSubmit() {
				const app = this
				// if (app.disabled) {
				// 	return false
				// }
				const {
					form,
					solutionId
				} = this
				form.brand_id = solutionId
				app.$refs.uForm.validate(valid => {
					if (valid) {
						app.disabled = true
						app.onSubmit()
					}
				})
			},

			// 提交到后端
			onSubmit() {
				const app = this
				const {
					form
				} = this
				let req;
				if (form.apply_status == 30) {
					req = ApplyApi.edit(form.apply_id, form)
				} else {
					console.log('add');
					req = ApplyApi.add(form)
				}
				req.then(result => {
						console.log("result: ", result);
						app.$toast(result.message)
						setTimeout(function() {
							uni.navigateBack()
						}, 2000)

					})
					.finally(() => app.disabled = false)
			}
		}
	}
</script>

<style lang="less">
	.partner {
		.banner {
			padding-top: 20rpx;
			width: 100%;
		}

		.form-wrapper {
			margin: 20rpx auto 20rpx auto;
			padding: 0 40rpx;
			width: 94%;
			box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
			border-radius: 16rpx;
			background: #fff;

			.shop-box {
				text-align: center;
				display: flex;
				flex-direction: column;

				.shop-name {
					font-weight: bold;
					font-size: 36rpx;
				}
			}
			
			.brand-poster{
				display: flex;
				flex-direction: column;
			}

			.logo {
				width: 100rpx;
			}

			.shipin {
				width: 400rpx;
			}
		}

		.footer {
			width: 500rpx;
			margin: 50rpx auto;
			padding-bottom: 50rpx !important;
		}
	}

	.brand-name {
		padding: 10rpx;
		text-align: center;
	}

	.buy-desc {
		text-align: center;
		color: #ff0000;
		margin-top: 40rpx;
	}

	.buy-text {
		text-align: center;
	}
</style>
