<template>
	<view>
		<scroll-view scroll-y="true">
			<view class="form">
				<view class="card">
					<u-form :model="model" labelWidth="200" ref="uForm">
						<u-form-item :required="true" prop="enterpriseName" label="企业名称">
							<u-input type="text" v-model="model.enterpriseName" placeholder="请填写企业名称"></u-input>
						</u-form-item>
						<u-form-item :required="true" label="法定代表人" prop="representative">
							<u-input type="text" v-model="model.representative" placeholder="请输入法定代表人"></u-input>
						</u-form-item>
						<u-form-item label="所属区域" :required="true" prop="residentialName">
							<u-input type="select" @click="residentialShow=true" :select-open="residentialShow"
								v-model="model.residentialName" placeholder="请选择所属区域"></u-input>
						</u-form-item>
						<u-form-item label="企业位置">
							<u-input disabled v-model="location" placeholder="请选择企业位置" @click="chooseLocation">
							</u-input>
							<u-icon name="map-fill" slot="right"></u-icon>
						</u-form-item>
						<u-form-item :required="true" label="详细地址" prop="region">
							<u-input type="text" v-model="model.region" placeholder="请输入详细地址"></u-input>
						</u-form-item>
						<u-form-item :required="true" label="统一信用代码" prop="creditCode">
							<u-input type="text" v-model="model.creditCode" placeholder="请输入统一信用代码"></u-input>
						</u-form-item>
						<u-form-item label="企业类型">
							<u-input type="select" @click="enterpriseTypeShow=true" :select-open="enterpriseTypeShow"
								v-model="model.enterpriseTypeName" placeholder="请选择企业类型"></u-input>
						</u-form-item>

						<u-form-item label="组织机构代码">
							<u-input type="text" v-model="model.organizationCode" placeholder="请输入组织机构代码"></u-input>
						</u-form-item>

						<u-form-item label="注册资金">
							<u-input type="text" v-model="model.registeredCapital" placeholder="请输入注册资金"></u-input>
						</u-form-item>
						<u-form-item label="注册地址">
							<u-input type="text" v-model="model.registeredAddress" placeholder="请输入注册地址"></u-input>
						</u-form-item>
						<u-form-item label="成立日期">
							<u-input v-model="model.registeredDate" type="select" @click="registeredDateShow=true" :select-open="registeredDateShow" placeholder="请输入成立日期"></u-input>
						</u-form-item>
						<u-form-item label="办公电话">
							<u-input v-model="model.officeNum" placeholder="请输入办公电话"></u-input>
						</u-form-item>
						<u-form-item label="企业简介">
							<u-input type="textarea" height="200" v-model="model.enterpriseIntroduce"
								placeholder="请输入企业简介">
							</u-input>
						</u-form-item>

						<u-form-item class="" label="请上传营业执照" label-position="top">
							<view class="upload-img">
								<view class="" @click="chooseImage">
									<upload-img :width="width" :height="height" :currentImage="model.businessLicense">
									</upload-img>
									<view class="text-center  padding-top-20 base-color">
										<text class="cuIcon-camera padding-right-sm" style="font-size: 30rpx;"></text>
										<text v-if="$isEmpty(model.businessLicense)">请上传营业执照</text>
										<text style="margin-top: 40rpx;display: inline-block;" v-else>点击重新上传</text>
									</view>
								</view>
							</view>
						</u-form-item>

						<u-form-item class="" label="请上传公司logo" label-position="top">
							<view class="upload-img">
								<view class="" @click="chooseImage('logo')">
									<upload-img :width="width" :height="height" :currentImage="model.logo">
									</upload-img>
									<view class="text-center  padding-top-20 base-color">
										<text class="cuIcon-camera padding-right-sm" style="font-size: 30rpx;"></text>
										<text v-if="$isEmpty(model.logo)">请上传公司logo</text>
										<text style="margin-top: 40rpx;display: inline-block;" v-else>点击重新上传</text>
									</view>
								</view>
							</view>
						</u-form-item>

					</u-form>
				</view>
			</view>
			<view>
				<view class="bg-white" style="height: 80rpx;"></view>
				<view class="" @click="submit" style="width: 90%;margin: 0 auto ;margin-bottom: 10%;">
					<view class="cu-btn round base-bg-color" style="padding: 40rpx;width: 100%;">
						<text class="cuIcon-add margin-right-10"></text>
						<text>确认提交</text>
					</view>
				</view>
			</view>
		</scroll-view>
		<!-- 区域 -->
		<u-select @confirm="residentialConfirm" label-name="name" value-name="id" v-model="residentialShow"
			:list="residentialList"></u-select>
		<!-- 企业类型 -->
		<u-select @confirm="enterpriseTypeConfirm" v-model="enterpriseTypeShow" :list="enterpriseTypeList"></u-select>
		<!-- 成立日期 -->
		<u-picker v-model="registeredDateShow" mode="time" :params="params" @confirm="registeredDateConfirm"></u-picker>
	</view>
</template>

<script>
	import uploadImg from '@/components/uploadimg/uploadImg.vue'
	export default {
		components: {
			uploadImg
		},
		data() {
			return {
				width: 480,
				height: 320,

				model: {
					enterpriseName: '',
					representative: '',
					region: '',
					creditCode: '',
					businessLicense: '',
					logo: ''
				},

				//区域
				residentialList: [],
				residentialShow: false,

				//企业位置
				location: '',
				//企业类型
				enterpriseTypeShow: false,
				enterpriseTypeList: [],
				//成立日期
				registeredDateShow: false,
				params: {
					year: true,
					month: true,
					day: true,
					hour: false,
					minute: false,
					second: false
				},

				rules: {
					enterpriseName: [{
						required: true,
						message: '请输入企业名称',
						trigger: ['change', 'blur'],
					}],
					representative: [{
						required: true,
						message: '请输入法定代表人',
						trigger: ['change', 'blur'],
					}],
					region: [{
						required: true,
						message: '请输入详细地址',
						trigger: ['change', 'blur'],
					}],
					creditCode: [{
						required: true,
						message: '请输入统一信用代码',
						trigger: ['change', 'blur'],
					}],
					residentialName: [{
						required: true,
						message: '请选择所属区域',
						trigger: ['change', 'blur'],
					}]
				}
			}
		},
		onLoad() {
			this.fetchResidentialList()
			this.fetchEnterpriseList()
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			//区域列表
			fetchResidentialList() {
				let params = {
					agencyId: this.$cache.get('agencyId'),
					size: 300,
				}
				this.$api.residential.page(params).then(res => {
					this.residentialList = res.data.records
				})
			},
			residentialConfirm(e) {
				this.model.residentialId = e[0].value
				this.model.residentialName = e[0].label
			},
			//企业类型
			enterpriseTypeConfirm(e) {
				this.model.enterpriseType = e[0].value
				this.model.enterpriseTypeName= e[0].label
			},
			fetchEnterpriseList() {
				this.enterpriseTypeList = []
				this.$api.dict({
					code: 'enterprise_type'
				}).then(res => {
					res.data.forEach(item => {
						let obj = {
							label: item.dictValue,
							value: item.dictKey
						}
						this.enterpriseTypeList.push(obj)
					})
				})
			},
			//成立日期
			registeredDateConfirm(e) {
				this.model.registeredDate = e.year + '-' + e.month + '-' + e.day
			},
			chooseLocation() {
				let that = this
				uni.chooseLocation({
					success: function(res) {
						that.location = `经纬度：(${res.longitude},${res.latitude})`
						that.model.latitude = res.latitude
						that.model.longitude = res.longitude
						that.model.region = res.address
					}
				});
			},
			//上传照片
			async chooseImage(type) {
				let tempFilePaths = await this.$mpi.chooseImage()
				let res = await this.$api.uploadFile.submit(tempFilePaths[0])
				if (type == 'logo') {
					this.model.logo = res.data.link
				} else {
					this.model.businessLicense = res.data.link
				}
			},
			//提交
			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						this.save()
					}
				});
			},
			async save() {
				let res = await this.$api.enterprise.submit(this.model)
				if (res.success == true) {
					this.$dialog.showModal('添加成功', false).then(() => {
						this.$util.isReloadAndBack()
					})
				}
			}
		}
	}
</script>

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

	.form {
		padding: 40rpx 30rpx;

		.tag {
			width: 35rpx;
			height: 35rpx;
			display: block;
			padding-top: 6rpx;
			padding-right: 10rpx;
		}

		.card {
			margin-top: 20rpx;
			padding: 0 30rpx;
			box-sizing: border-box;
			border-radius: 12rpx;
			box-shadow: 0 -10rpx rgba(248, 248, 248, .9), 0 10rpx rgba(248, 248, 248, .9), -10rpx 0rpx rgba(248, 248, 248, .9), 10rpx 0rpx rgba(248, 248, 248, .9);

			.item {
				padding: 30rpx 0;
				display: flex;
				justify-content: space-between;
			}

			.upload-img {
				padding: 50rpx 0 30rpx;
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}
	}
</style>
