<template>
	<view class="page-container">
		<nav-bar title="公司信息">
			<view class="nav-bar-right-btn" slot="right" @click="toNext">跳过</view>
		</nav-bar>
		
		<view class="content">
			<view class="num-title">
				<text class="current-num">3</text>
				<text>/6</text>
			</view>
			
			<view class="main-title">编辑公司简介</view>
			<view>可以简单介绍一下公司发展状况、服务领域、主要产品等信息</view>
			<view class="small-desc">一句短介绍</view>
			<view class="content-index">
				<view class="check-box">
					<textarea v-model="text" placeholder="填写公司介绍（最少10个字）" maxlength="500"
						class="textarea"></textarea>
					<view class="word-count">
						<text>{{ text ? text.length : 0 }}</text> /500
					</view>
				</view>
			</view>
		</view>

		<view class="fixed-button">
			<view class="button" @click="goJobPostingSecond">保存</view>
		</view>
	</view>
</template>

<script>
	import navBar from "@/components/nav-bar/index.vue";
	export default {
		data() {
			return {
				companyId: '',
				loading: false,
				text: '',
			};
		},
		components: {
			navBar,
		},
		onLoad(options) {
			let companyInfo = uni.getStorageSync('companyInfo');
			this.text = companyInfo.companyContent
			this.getCompanyInfo()
		},
		methods: {
			toNext() {
				uni.redirectTo({
					url: '/my/renzheng/mainWorkIntro'
				})
			},
			// 获取公司信息
			getCompanyInfo() {
				uni.showLoading({ title: '加载中' })
				this.$Request.get('/app/company/selectCompanyByUserId')
					.then((res) => {
						if (res.code != 0) {
							uni.showToast({
								title: res.msg || '查询状态失败',
								icon: 'none'
							});
							return
						}
						this.companyId = res.data.companyId
						this.text = res.data.companyContent || ''
						uni.hideLoading()
					})
					.catch((err) => {
						uni.hideLoading()
						uni.showToast({
							title: '网络异常',
							icon: 'none'
						})
					})
			},
			goJobPostingSecond() {
				if (this.loading) return
				if (this.text === '') {
					uni.showToast({
						title: "请填写公司介绍",
						icon: "none",
					});
					return;
				}
				if (this.text.length < 10) {
					uni.showToast({
						title: "公司介绍最少10个字",
						icon: "none",
					});
					return;
				}
				
				this.loading = true
				uni.showLoading({ title: '保存中' })
				let companyData = {
					companyId: this.companyId,
					companyContent: this.text
				}
				this.$Request.postJson('/app/company/updateCompany', companyData)
					.then(res => {
						if (res.code == 0) {
							uni.showToast({
								title: '提交成功',
								duration: 1500,
							});
							setTimeout(() => {
								uni.$emit('updateCompanyInfo')
								uni.navigateBack()
							}, 500)
						}
						this.loading = false
						uni.hideLoading()
					})
					.catch(() => {
						this.loading = false
						uni.hideLoading()
					})
			}
		},
	};
</script>

<style lang="scss" scoped>
	@import './company.scss';
	
	.check-box {
		width: 100%;
		border-radius: 12rpx;
		background: rgba(240, 240, 240, 1);
		padding: 34rpx;
		padding-top: 40rpx;
		box-sizing: border-box;
		margin: 20rpx 0;

		.word-count {
			font-family: DM Sans;
			font-size: 20rpx;
			font-weight: 400;
			line-height: 26rpx;
			text-align: right;

			text {
				color: #016bf6;
			}
		}
	}
	
	.desc {
		color: rgba(102, 102, 102, 1);
		width: 100%;
		font-family: DM Sans;
		font-size: 24rpx;
		font-weight: 400;
		line-height: 32rpx;
		letter-spacing: 0.5%;
		text-align: left;
		box-sizing: border-box;
		margin-bottom: 20rpx;
	}

	.small-desc {
		color: rgba(34, 37, 42, 1);
		font-family: DM Sans;
		font-size: 32rpx;
		font-weight: 400;
		line-height: 48rpx;
		width: 100%;
		margin: 20rpx 0 12rpx; 
	}

	.content-index {
		width: 100%;
	}
	
	::v-deep .textarea-placeholder {
		color: rgba(153, 153, 153, 1);
		font-family: DM Sans;
		font-size: 20rpx !important;
		font-weight: 400;
		line-height: 26rpx;
	}
	
	::v-deep .uni-textarea-textarea {
		font-size: 20rpx;
	}
</style>