<template>
	<view class="main">
		<view class="message _info" v-if="item.state == 0 && item.updateTime">
			<text class="icon">&#xe642;</text>
			<text>后台审核信息中...</text>
		</view>
		<view class="message _success" v-if="item.state == 1">
			<text class="icon">&#xe612;</text>
			<text>审核信息通过</text>
		</view>
		<view class="message _error" v-if="item.state == 2">
			<view>信息审核失败：</view>
			<view class="msg">{{ item.msg }}</view>
		</view>
		<view class="form_group">
			<view class="lable re">企业名称</view>
			<input type="text" placeholder="请输入企业名称" v-model="item.name" :disabled="item.state == 1" />
		</view>
		<view class="form_group">
			<view class="lable re">法定代表人</view>
			<input type="text" placeholder="请输入法定代表人" v-model="item.legalPerson" :disabled="item.state == 1" />
		</view>
		<view class="form_group">
			<view class="lable re">电话</view>
			<input type="number" placeholder="请输入电话" v-model="item.phone" :disabled="item.state == 1" />
		</view>
		<view class="form_group">
			<view class="lable re">邮箱</view>
			<input type="text" placeholder="请输入邮箱" v-model="item.email" :disabled="item.state == 1" />
		</view>
		<view class="form_group">
			<view class="lable re">注册资本(万)</view>
			<input type="number" placeholder="请输入注册资本" v-model="item.capital" :disabled="item.state == 1" />
		</view>
		<view class="form_group">
			<view class="lable re">企业地址</view>
			<view class="bgm" v-if="item.state != 1">
				<input type="text" placeholder="请输入详细地址" v-model="item.address" :disabled="item.state == 1" class="input"/>
				<view class="msg" @click="select()">选择</view>
			</view>
			<input v-else v-model="item.address" :disabled="item.state == 1" />
		</view>
		<view class="form_group" style="border: 0px">
			<view class="lable re">企业简介</view>
			<textarea cols="30" rows="10" placeholder="请输入企业简介" v-model="item.contents" :disabled="item.state == 1"></textarea>
		</view>
		<view class="form_group" style="border: 0px">
			<view class="lable re">营业执照</view>
			<card v-model="item.p1" :read="item.state == 1"></card>
		</view>
		<button class="btn" @click="save()" v-if="item.state != 1">{{ item.id ? '编辑' : '提交' }}</button>
	</view>
</template>

<script>
export default {
	data() {
		return {
			ip: this.http.ip,
			item: {}
		};
	},
	onLoad() {
		this.http.request({
			url: '/app/enterprise/index',
			success: (res) => {
				if (res.data.data) {
					this.item = res.data.data;
				}
			}
		});
	},
	methods: {
		select() {
			uni.chooseLocation({
				success: (res) => {
					let reg = /.+?(省|市|自治区|自治州|县|区)/g;
					let addressList = res.address.match(reg).toString().split(',');
					//注意区分直辖市;
					let city = addressList.length >= 3 ? addressList[1] : addressList[0];
					let region = addressList.length >= 3 ? addressList[2] : addressList[1];
					this.item.regionName = region;
					this.item.address = res.address;
					this.item.longitude = res.longitude;
					this.item.latitude = res.latitude;
					this.$forceUpdate();
				}
			});
		},
		save() {
			let rule = [
				{ name: 'name', checkType: 'notnull', errorMsg: '请输入企业名称' },
				{ name: 'legalPerson', checkType: 'notnull', errorMsg: '请输入法定代表人' },
				{ name: 'phone', checkType: 'notnull', errorMsg: '请输入电话' },
				{ name: 'email', checkType: 'email', errorMsg: '请输入邮箱' },
				{ name: 'address', checkType: 'notnull', errorMsg: '请输地址' },
				{ name: 'contents', checkType: 'notnull', errorMsg: '请输入企业简介' },
				{ name: 'p1', checkType: 'notnull', errorMsg: '请上传营业执照' }
			];
			if (!this.verify.check(this.item, rule)) {
				uni.showModal({ content: this.verify.error, showCancel: false });
				return false;
			}
			this.http.request({
				url: '/app/enterprise/edit',
				data: this.item,
				method: 'POST',
				success: (res) => {
					uni.showModal({
						title: '提示',
						content: '提交成功，等待后台审核。',
						showCancel: false,
						success: (res) => {
							uni.navigateBack();
						}
					});
				}
			});
		}
	}
};
</script>

<style lang="scss"></style>
