<template>
	<view class="partner">

		<!-- 表单组件 -->
		<view class="form-wrapper" v-if="detail">
			<u-form :model="form" ref="uForm" label-width="240rpx">
				<u-form-item>
					<image :src="detail.imageUrl" mode="widthFix" class="banner"></image>
					<view class="brand-name">{{detail.name}}</view>
				</u-form-item>
				<u-form-item>
					<view class="shop-box">
						<text class="shop-name">品牌介绍</text>
						<text>{{detail.describe}}</text>
					</view>
				</u-form-item>
				<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" 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/brand/index.js'
	import * as ApplyApi from '@/api/brand/apply.js'
	import SelectRegion from '@/components/select-region/select-region'
	export default {
		components: {
			SelectRegion
		},
		data() {
			return {
				form,
				disabled: false,
				detail: {},
				brandId: 0,
				sex
			}
		},
		onLoad(options) {
			this.brandId = options.brandId
			this.init()
		},
		methods: {
			// 获取当前用户信息
			init() {
				const app = this
				const {
					brandId
				} = this
				console.log(brandId);
				Api.detail(brandId).then(result => {
					app.detail = result.data.detail
				})
			},
			groupChange(e) {
				this.form.sex = e
			},

			// 表单提交
			handleSubmit() {
				const app = this
				// if (app.disabled) {
				// 	return false
				// }
				const {
					form,
					brandId
				} = this
				form.brand_id = brandId
				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;
				}
			}

			.logo {
				width: 100rpx;
			}

			.shipin {
				width: 400rpx;
			}
		}

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

	.brand-name {
		text-align: center;
	}

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

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