<template>
	<view class="page">
		<view class="block">
			<view class="title">上传营业执照</view>
			<image class="credential" :src="businessLicensePicture || 'https://occupationlab.com/images/preschoolEdu/credentials1.png'" mode="widthFix" @click="uploadBusinessLicense"></image>
		</view>
		
		<view v-if="isPreschool" class="block">
			<view class="title">上传办学许可证</view>
			<image class="credential" :src="licenseForRunningSchool || 'https://occupationlab.com/images/preschoolEdu/credentials2.png'" mode="widthFix" @click="uploadSchoolLicense"></image>
		</view>
		
		<view class="block">
			<view class="form-list">
				<view class="line with-arrow">
					<view class="name">{{ isPreschool ? '幼儿园' : '企业' }}名称</view>
					<input type="text" :placeholder="'请输入' + platformName + '名称'" v-model="form.companyName" />
					<uni-icons type="right" size="18" color="#ababab"></uni-icons>
				</view>
				<view class="line with-arrow">
					<view class="name">统一社会信用代码</view>
					<input type="text" placeholder="请输入证件号码" v-model="form.creditCode" />
					<uni-icons type="right" size="18" color="#ababab"></uni-icons>
				</view>
				<view class="line with-arrow">
					<view class="name">法人</view>
					<input type="text" placeholder="请输入法人" v-model="form.legalPerson" />
					<uni-icons type="right" size="18" color="#ababab"></uni-icons>
				</view>
			</view>
		</view>
		
		<view class="btn-wrap">
			<view class="btn" @click="submit">认证信息</view>
		</view>
	</view>
</template>

<script>
	import { creditCodeAuthentication, businessLicensePictureVerification, updateTeamInfo, organizationCertification, kindergartenWeChatApplication } from '@/apis/modules/user.js'
	import OSS from '@/libs/Oss/upload'
	export default {
		data() {
			return {
				openId: uni.getStorageSync('openId'),
				form: {
					companyName: '',
					creditCode: '',
					legalPerson: '',
				},
				platformId: '',
				businessLicensePicture: '',
				licenseForRunningSchool: '',
			}
		},
		computed: {
			// 幼儿园
			isPreschool() {
				return this.platformId == 5
			},
			// 平台名字
			platformName() {
				return this.platformId == 6 ? '供应商' : this.platformId == 5 ? '幼儿园' : ''
			},
		},
		onShow() {
			const pages = getCurrentPages()
			const { options } = pages[pages.length - 1]
			this.platformId = +options.platformId
		},
		methods: {
			// 上传营业执照
			uploadBusinessLicense() {
				const that = this
				uni.chooseImage({
					success: (res) => {
						const file = res.tempFilePaths[0];
						OSS(file, async ({ url }) => {
							that.businessLicensePicture = url
							const { data } = await businessLicensePictureVerification(url)
							if (data) {
								that.form = {
									companyName: data.name,
									creditCode: data.reg_num,
									legalPerson: data.person,
									address: data.address,
									business: data.business,
									capital: data.capital,
									establishDate: data.establish_date,
									type: data.type,
									validPeriod: data.valid_period,
									platformSource: this.platformId,
								}
							}
						})
					}
				});
			},
			// 上传办学许可证
			uploadSchoolLicense() {
				const that = this
				uni.chooseImage({
					success: (res) => {
						const file = res.tempFilePaths[0];
						OSS(file, async ({ url }) => {
							that.licenseForRunningSchool = url
						})
					}
				});
			},
			// 消息推送授权
			publicNotice() {
				const that = this
				uni.requestSubscribeMessage({
					tmplIds: ['ZB6wTenlv13mivxXwi-DWwjgVoNJ6eUr1vBvNdQGbJw'],
					success: (res) => {
						that.$util.sucMsg('提交成功，请重新登录！')
						setTimeout(() => {
							uni.clearStorageSync()
							that.$util.to('/pages/login/login')
						}, 1500)
					},
					fail: function(err) {
						uni.showToast({
							title: err,
							duration: 2000,
							icon: 'error'
						});
					}
				})
			},
			// 提交
			async submit() {
				if (this.submiting) return false
				const { form } = this
				if (!form.companyName) return this.$util.errMsg(`请输入${platformName}名称！`)
				if (!form.creditCode) return this.$util.errMsg('请输入统一社会信用代码！')
				if (!form.legalPerson) return this.$util.errMsg('请输入法人！')
				this.submiting = true
				uni.showLoading({
					title: '提交中'
				})
				const data = {
					...form,
					auditStatus: 1,
					authenticationStatus: 1,
					businessLicensePicture: this.businessLicensePicture,
					licenseForRunningSchool: this.licenseForRunningSchool,
					openId: this.openId
				}
				try {
					// 个人
					if (uni.getStorageSync('platformId') === 7) {
						// 个人认证企业后新增一个企业角色
						const res = await kindergartenWeChatApplication({
							openId: this.openId,
							platformId: this.platformId,
							organizationName: form.companyName
						})
						
						data.accountId = res.data.registerId
						await organizationCertification(data)
					} else { // 幼儿园/供应商
						await creditCodeAuthentication(data)
						
						// 企业认证后要修改组织名字
						await updateTeamInfo({
							id: uni.getStorageSync('teamId'),
							classificationName: form.companyName
						})
					}
					uni.hideLoading()
					this.publicNotice()
				} catch(e) {
					uni.hideLoading()
					this.submiting = false
				}
			},
		}
	}
</script>

<style scoped lang="scss">
.page {
	padding-bottom: 170rpx;
	-webkit-overflow-scrolling: touch;
}
.avatar {
	width: 80rpx;
	height: 80rpx;
	border: 0;
	border-radius: 50%;
}
.block {
	padding: 24rpx;
}
.title {
	margin-bottom: 20rpx;
	font-size: 28rpx;
}
.credential {
	width: 100%;
}
.form-list {
	.name {
		min-width: 250rpx;
	}
}
</style>
