<template>
	<view>
		<view class="box">
			<view class="top">
				<text class="title">企业注册</text>
			</view>
			<view class="item">
				<view class="l">企业名称：</view>
				<view class="r">
					<u-input v-model="form.name" placeholder="输入企业名称" />
				</view>
			</view>
			<view class="item">
				<view class="l">联系人：</view>
				<view class="r">
					<u-input v-model="form.dutyPeople" placeholder="输入联系人" />
				</view>
			</view>
			<view class="item">
				<view class="l">联系电话：</view>
				<view class="r">
					<u-input maxlength="11" v-model="form.phone" placeholder="输入电话号码" />
				</view>
			</view>
			<view class="item">
				<view class="l">结算方式：</view>
				<view class="r">
					<radio-group @change="payChange">
						<label class="radio">
							<radio value="1" checked />现结
						</label>
						<label class="radio" style="margin-left: 20rpx;">
							<radio value="2" />月结
						</label>
					</radio-group>
				</view>
			</view>
			<!-- 	<view class="item">
				<view class="l">验证码：</view>
				<view class="r">
					<u-input placeholder="输入验证码"/>
					<view class="btn">发送</view>
				</view>
			</view> -->
			<view class="item">
				<view class="l">营业执照：</view>
				<view class="r">
					<view class="img">
						<uni-file-picker v-model="imageValue" fileMediatype="image" mode="grid" limit="1"
							@select="select" @progress="progress" @success="success" @fail="fail"
							@delete="form.businessLicence=''" :image-styles="imageStyles" />
					</view>
				</view>
			</view>
		</view>
		<view class="common-btn" @click="confirm">确认注册</view>
	</view>
</template>

<script>
	import request from '../../utils/request.js'
	export default {
		data() {
			return {
				imgList: [],
				imageValue: [],
				imageStyles: {
					width: 150,
					height: 100,
					border: {
						color: "#eee",
						width: 1,
						style: 'dashed',
						radius: '5px'
					}
				},
				imgList: [],
				uploadImageUrl: request.server + '/upload/image',
				form: {
					name: '',
					dutyPeople: '',
					phone: '',
					payType: '1',
					businessLicence: ''
				}
			}
		},
		methods: {
			payChange(e) {
				this.form.payType = e.detail.value
			},
			// 获取上传状态
			select(e) {
				let that = this;
				uni.uploadFile({
					url: that.uploadImageUrl,
					filePath: e.tempFilePaths[0],
					name: 'file',
					success: (resp => {
						console.log(JSON.parse(resp.data).data)
						that.form.businessLicence = JSON.parse(resp.data).data;
					})
				})
				console.log('选择文件：', e)
			},
			// 获取上传进度
			progress(e) {
				console.log('上传进度：', e)
			},

			// 上传成功
			success(e) {
				console.log('上传成功')
			},

			// 上传失败
			fail(e) {
				console.log('上传失败：', e)
			},
			payChange(e) {
				this.form.payType = e.detail.value
			},
			check() {
				if (!this.form.name) {
					this.$common.toast('请录入名称');
					return false;
				}
				if (!this.form.dutyPeople) {
					this.$common.toast('请录入联系人');
					return false;
				}
				if (!this.$common.isPhone(this.form.phone)) {
					this.$common.toast('联系号码不正确');
					return false;
				}
				if (!this.form.businessLicence) {
					this.$common.toast('请上传营业执照');
					return false;
				}

				return true;
			},
			confirm() {
				if (this.check()) {
					this.$api.register(this.form).then(resp => {
						if (resp.code == 200) {
							this.$common.to('/pages/ok/registerSuccess')
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #fff;
	}

	@import '@/common/common.scss'
</style>
