<template>
	<view class="page-container">
		<cTop></cTop>
		<view class="content">
			<view class="title">Hi，欢迎加入亿职赞</view>
			<view class="desc">填写企业信息，开启高效招聘方式</view>
			<view class="sub-title">公司名称</view>
			<view class="select-bar flex align-center justify-between" @click="handleToSelectPage">
				<view class="name" v-if="companyName">{{ companyName }}</view>
				<view class="placeholder" v-else>请输入关键字搜索，选择已经认证的公司</view>
				<u-icon name="arrow-down" color="rgba(198, 197, 202, 1)"></u-icon>
			</view>
			<image src="/static/images/company.png" class="company-bg"></image>
		</view>
		
		<view class="fixed-section">
			<u-button type="primary" class="submit-btn" @click="handleNext">下一步</u-button>
		</view>
	</view>
</template>

<script>
	import cTop from '@/components/c-top.vue'
	export default {
		components: {
			cTop
		},
		data() {
			return {
				companyName: '',
				companyId: '',
				loading: false
			};
		},
		onLoad() {
			uni.$on('selectCompany', data => {
				this.companyId = data.companyId
				this.companyName = data.companyName
			})
		},
		onUnload() {
			uni.$off('selectCompany')
		},
		methods: {
			handleToSelectPage() {
				uni.navigateTo({
					url: '/pages/my/searchCompanys'
				})
			},
			handleNext() {
				if (this.companyId) {
					this.createCompany()
				} else {
					uni.showToast({
						title: '请选择公司',
						icon :"none",
						duration: 2000
					});
				}
			},
			// 创建公司
			createCompany() {
				if (this.loading) return
				this.loading = true
				
				uni.showLoading({
					title: '创建中'
				})
				this.$Request
					.postJson('/app/company/insertCompany', {
						companyId: this.companyId
					})
					.then(res => {
						if (res.code === 0) {
							// 跳转至招聘岗位页面
							uni.navigateTo({
								url: `/pages/my/jobPosting?companyName=${this.companyName}&companyId=${res.data.companyId}&isBack=${false}&status=${res.data.status}`
							})
						} else {
							uni.showToast({
								title: res.msg || '提交失败，请重试',
								icon: 'none'
							})
						}
						uni.hideLoading()
						this.loading = false
					})
					.catch(() => {
						uni.hideLoading()
						this.loading = false
						uni.showToast({
							title: '网络异常，请稍后重试',
							icon: 'none'
						})
					})
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import '@/common.scss';
	.page-container {
		font-family: DM Sans;
		.content {
			padding: 60rpx 40rpx;
			.title {
				color: rgba(29, 33, 41, 1);
				font-size: 36rpx;
				font-weight: 500;
				line-height: 44rpx;
				margin-bottom: 20rpx;
			}
			.desc {
				color: rgba(102, 102, 102, 1);
				font-size: 24rpx;
				font-weight: 400;
				line-height: 32rpx;
				margin-bottom: 20rpx;
			}
			.sub-title {
				color: rgba(31, 44, 55, 1);
				font-size: 28rpx;
				font-weight: 500;
				line-height: 44rpx;
				margin-bottom: 16rpx;
			}
			.select-bar {
				padding: 32rpx 44rpx;
				border-radius: 6px;
				box-shadow: 0px 8px 150px 0px rgba(0, 0, 0, 0.06);
				background: rgba(255, 255, 255, 1);
				line-height: 40rpx;
				.placeholder {
					color: rgba(153, 153, 153, 1);
				}
			}
			.company-bg {
				display: block;
				width: 550rpx;
				height: 550rpx;
				margin: 200rpx auto 100rpx;
			}
		}
	}
</style>