<template>
	<view>
		<view class="perfect-title">编辑企业信息</view>
		<view class="cu-form">
			<!-- 企业名称 -->
			<view class="title">{{information[0]}}：</view>
			<view class="iptIcon">
				<input name="input" v-model="name" />
				<view class="triangle1" :class="perfectShow ? 'show' : ''" @click="noShow">
					<icon class="iconfont icon-tongguo bingo triangle"></icon>
				</view>
			</view>
			<view class="perfect-box" :class="perfectShow ? 'show' : ''">
				<view class="perfect-con" v-for="(item,index) in perfect" :key="index" @click="changePerfect(item)">{{item.name}}</view>
			</view>
			<!-- 企业联系人 -->
			<view class="title">{{information[1]}}：</view>
			<input name="input" v-model="contacts"></input>
			<!-- 企业联系电话 -->
			<view class="title">{{information[2]}}：</view>
			<input name="input" type="number" v-model="contactPhone"></input>
			<!-- 孵化器类型 -->
			<view class="title">{{information[3]}}：</view>
			<radio-group class="radio-box" name="Incubator" @change="change">
				<label>
					<view>
						<radio value="0" :checked="incubatorType-0 === 0"/><text> 虚拟孵化器</text>
					</view>
					<view>
						<radio value="1" :checked="incubatorType-0 === 1"/><text> 实体孵化器</text>
					</view>
				</label>
			</radio-group>
			<!-- 孵化器名称 -->
			<view class="title">{{information[4]}}：</view>
			<input name="input" v-model="incubator" disabled @click="changeIncubatorShow"></input>
			<view class="incubator-box" :class="incubatorShow ? 'show' : ''" >
				<view class="incubator-content" v-for="(item,index) in incubatorList" :key="index" @click="changeIncubator(item)">
					{{item.name}}
				</view>
			</view>
			<!-- 税务号 -->
			<view class="title">{{information[5]}}：</view>
			<input name="input" v-model="theTaxNo"></input>
			<!-- 注册地址 -->
			<view class="title">{{information[6]}}：</view>
			<view>
				<input @tap="handleTap" type="text" v-model="label2" disabled=""/>
				<lb-picker ref="picker2"
					v-model="value2"
					mode="multiSelector"
					:list="list"
					:level="3"
					@confirm="handleConfirm"
					:props="props"
					>
				</lb-picker>
			</view>
			<!-- 详细地址 -->
			<view class="title">{{information[7]}}：</view>
			<input name="input" v-model="registrationPlace"></input>
			<!-- 注册时间 -->
			<view class="title">{{information[8]}}：</view>
			<picker mode="date" :value="date" start="1800-01-01" end="2100-01-01" @change="bindDateChange">
				<input name="input" v-model="registrationDate" disabled></input>
			</picker>
			<!-- 企业类型 -->
			<view class="title">{{information[9]}}：</view>
			<view name="input" @click="changeType" class="type-box">
				<view v-if="typeList[0] !== 'undefined'">{{typeList[0]}}</view>
				<view v-if="typeList[1] !== 'undefined'">{{typeList[1]}}</view>
				<view v-if="typeList[2] !== 'undefined'">{{typeList[2]}}</view>
				<view v-if="typeList[3] !== 'undefined'">{{typeList[3]}}</view>
				<view v-if="typeList[4] !== 'undefined'">{{typeList[4]}}</view>
			</view>
			
		</view>
	
		<view class="eject-box" :class="isShow ? 'show' : ''">
			<view class="eject">
				<view class="eject-content" v-for="(item,index) in enterpriseType" :key="index">
					<label>
						<checkbox :checked="item.checked" @click="choice(item,index)"/><view class="size">{{item}}</view>
					</label>
				</view>
				<button class="btnsure" type="default" @click="changeShow">确定</button>
			</view>
		</view>
		
		<view class="padding flex flex-direction">
			<button class="cu-btn bg-red margin-tb-sm lg" @click="commit">提交</button>
		</view>
	</view>
</template>

<script>
	import { add } from 'api/add'
	import { areaquery } from 'api/area'
	import { typeSelection } from 'api/type'
	import { detail } from 'api/detail'
	import { incubator } from 'api/incubator'
	import { update } from 'api/update'
	import { bindMsg } from 'api/bindMsg'
	import { queryByFuzzyName } from 'api/queryByFuzzyName'
	
	export default {
		onShareAppMessage(res) {
			if (res.from === 'button') {// 来自页面内分享按钮
				console.log(res.target)
			}
			return {
				title: '',
				path: '/pages/Edit/Edit'
			}
		},
		data() {
			return {
				value2: [],
				label2: '',
				name:'', // 企业名称
				name1:'', // 多一个企业名称，用于比较是否更改了名字
				contacts:'', // 企业联系人
				contactPhone:'', // 企业联系电话
				theTaxNo:'', // 统一社会信用代码
				incubatorType:100,	// 孵化器类型
				incubatorId:'',	// 孵化器id
				incubator:'',	// 孵化器名字
				registrationPlace:'',	// 注册地
				registrationDate:'', // 注册时间
				province:'', // 注册省份
				city:'', // 注册城市
				county:'', // 注册区县
				category:[], // 企业类型
				information:['企业名称','联系人','联系电话','孵化器类型','孵化器名称','统一社会信用代码(税务号)','注册地','详细地址','注册时间','企业类型(最多5类,多出无效)'],
				list: [],
				props: {label:'areaName',value:'id',children:'city'},
				enterpriseType:[],
				isShow:false,
				typeList:[],
				incubatorList:[] ,//孵化器列表
				incubatorShow:false,
				choose:0,
				perfect: [],
				perfectShow: false
			}
		},
		async onLoad(options) {
			const that = this
			uni.showLoading({
				icon:'none',
				title:'加载中...'
			})
			try {
				const params ={
					"param": {
						"areaParentId": 0,
						"areaType": 0,
						"deep": 3
					},
				}
				const data = await areaquery(params)
				that.list = data
				uni.hideLoading()
			} catch (err) {
				uni.hideLoading()
				console.log(err)
			}
			if(that.$store.state.user.bindMsg.bindStatus-0 === 1){
				const params = {
					param:{
					}
				}
				const data1 = await detail(params)
				const params2 = {
					param: {
						type:data1.incubatorType
					}
				}
				const data2 = await incubator(params2)
				that.incubatorList = data2.list
				that.name = data1.name								// 企业名称
				that.name1 = data1.name								// 企业名称
				that.contacts = data1.contacts,						// 企业联系人
				that.contactPhone = data1.contactPhone,				// 企业联系电话
				that.theTaxNo = data1.theTaxNo,						// 统一社会信用代码
				that.incubatorType = data1.incubatorType,				// 孵化器类型
				that.incubator = data1.incubator,						// 孵化器名称
				that.incubatorId = data1.incubatorId,						// 孵化器Id
				that.registrationPlace = data1.registrationPlace,		// 注册地
				that.registrationDate = data1.registrationDate,		// 注册时间
				that.province = data1.province,						// 注册省份
				that.city = data1.city,								// 注册城市
				that.county = data1.county,							// 注册区县
				that.typeList = data1.category						// 企业类型
				
				if(data1.cities[0] !== undefined){
					that.label2 = data1.cities[0] + '-' + data1.cities[1] + '-' + data1.cities[2]
				}
			}
		},
		methods: {
			handleTap () {
				this.$refs.picker2.show()
			},
			handleConfirm (item) {
				var address = [item.item[0].areaName,item.item[1].areaName,item.item[2].areaName]
				this.province = item.item[0].id
				this.city = item.item[1].id
				this.county = item.item[2].id
				this.label2 = address.join('-')
				console.log(this.province,this.city,this.county)
			},
			async commit(){
				let arr = []
				const that = this
				for(let i=0;i<5;i++){
					arr.push(this.typeList[i])
				}
				for(let j=0;j<arr.length;j++){
					if(arr[j] === undefined){
						arr.splice(j,1)
						j -= 1
					}
				}
				console.log(arr)
				if(this.$store.state.user.bindMsg.bindStatus-0 === 1){
						try{
							const params = {
								"param": {
									"category": arr,
									"city": that.city,
									"contactPhone": that.contactPhone,
									"contacts": that.contacts,
									"county": that.county,
									"incubatorId": that.incubatorId,
									"incubatorType": that.incubatorType - 0,
									"name": that.name,
									"province": that.province,
									"registrationDate": that.registrationDate,
									"registrationPlace": that.registrationPlace,
									"theTaxNo": that.theTaxNo
								},
							}
							const data = await update(params)
							that.$store.state.user.ischange++
							uni.showModal({
								title:'提交成功',
								content:"是否继续完善企业信息",
								success: function(res) {
									if(res.confirm){
										uni.navigateTo({
											url:`../Binding/Binding`
										})
									}else {
										uni.switchTab({
											url:`../tabBar/My`											
										})
									}
								}
							})
						}catch(err){
							uni.showToast({
								title:'提交失败 :' + err,
								icon:'none'
							})
							console.log(err)
							//TODO handle the exception
						}
				}else{
						try{
							const params = {
								"param": {
									"category": arr,
									"city": that.city,
									"contactPhone": that.contactPhone,
									"contacts": that.contacts,
									"county": that.county,
									"incubatorId": that.incubatorId,
									"incubatorType": that.incubatorType - 0,
									"name": that.name,
									"province": that.province,
									"registrationDate": that.registrationDate,
									"registrationPlace": that.registrationPlace,
									"theTaxNo": that.theTaxNo
								},
							}
							const data = await add(params)
							that.$store.state.user.ischange++
							const params1 = {}
							const data1 = await bindMsg(params1)
							that.$store.state.user.bindMsg = data1
							uni.showModal({
								title:'提交成功',
								content:"是否继续完善企业信息",
								success: function(res) {
									if(res.confirm){
										uni.navigateTo({
											url:`../Binding/Binding`
										})
									}else {
										uni.switchTab({
											url:`../tabBar/My`
										})
									}
								}
							})
						}catch(err){
							uni.showToast({
								title:'提交失败 :' + err,
								icon:'none'
							})
							//TODO handle the exception
						}
				}
			},
			async change(e){
				const that = this
				that.incubatorType = e.detail.value
				const params = {
					param:{
						pageNum:1,
						pageSize:100,
						type:e.detail.value
					}
				}
				const data = await incubator(params)
				that.incubatorList = data.list
				that.incubator = that.incubatorList[0].name
				that.incubatorId = that.incubatorList[0].id
			},
			bindDateChange: function(e) {
				this.registrationDate = e.detail.value.split('-').join('/')
			},
			async changeType(){
				const that = this
					try{
						const params = {
							"param": {
								"type":1
							}
						}
						const data = await typeSelection(params)
						that.enterpriseType = data
					}catch(err){
						console.log(err)
					}
				that.isShow = true
			},
			changeShow(){
				this.isShow = false
			},
			choice(item,index){
				const that = this
				if(that.typeList.length == 0){
					that.typeList.push(item)
					console.log(that.typeList)
				}else{
					for(let i=0;i<that.typeList.length;i++){
						if(that.typeList[i] == item){
							that.typeList.splice(i,1)
							break
						}else if(i == that.typeList.length-1){
							that.typeList.push(item)
							break
						}
					}
				}
				if(that.typeList.length > 5){
					uni.showModal({
						title:'最多只能选择5项，多出无效'
					})
				}
			},
			changeIncubatorShow(){
				const that = this
				if(that.incubatorShow === true){
					that.incubatorShow = false
				}else{
					that.incubatorShow = true
				}
				console.log(that.incubatorShow)
			},
			changeIncubator(item){
				const that = this
				that.incubator = item.name
				that.incubatorId = item.id
				that.incubatorShow = false
			},
			changePerfect(index){
				const that = this
				that.name = index.name
				that.name1 = index.name
				that.theTaxNo = index.unifiedSocialCreditCode
				that.perfectShow = false
			},
			noShow(){
				const that = this
				that.perfectShow = false
			}
		},
		watch:{
			isShow(val){
				const that = this
				if(that.choose === 0){
					that.typeList = []
					that.choose++
				}
			},
			async name(val){
				const that = this
				if(val !== that.name1){
					try{
						const params = {
							param: {
								name: val
							}
						}
						const data = await queryByFuzzyName(params)
						if(data[0]){
							that.perfect = data
							that.perfectShow = true							
						}
					}catch(e){
						//TODO handle the exception
					}
				}
				if(that.name == ''){
					that.name1 == ''
					that.theTaxNo = ''
					that.perfectShow = false
				}
				for(var i=0;i<that.perfect.length;i++){
					if(that.name === that.perfect[i].name){
						that.name1 = that.perfect[i].name
						that.theTaxNo = that.perfect[i].unifiedSocialCreditCode
						that.perfectShow = false
					}
				}
			}
		}

	}
</script>

<style lang="scss" scoped>
.iptIcon{
	position: relative;
}
.triangle{
	color: $green;
	height: 30px;
	font-size: 30px;
	width: 30px;
}
.triangle1{
	z-index: 100;
	height: 30px;
	width: 30px;
	position: absolute;
	top: 0;
	right: 5px;
	display: none;
}
.perfect-box{
	display: none;
	z-index: 10;
	position: absolute;
	max-height: 300px;
	background-color: $white;
	overflow-y: auto;
	width: 92%;
	border: 1px solid $grey;
}
.perfect-con{
	padding-left: 20upx;
	height: 26px;
	line-height: 26px;
	font-size: 12px;
}
.perfect-title{
	display: flex;
	background-color: $white;
	padding: 1upx 30upx;
	font-size: 20px;
	font-weight: 600;
	align-items: center;
	min-height: 100upx;
}
.cu-form {
	background-color: $white;
	padding: 1upx 30upx;
	align-items: center;
	min-height: 100upx;
	justify-content: space-between;
}
.title {
	text-align: justify;
	padding-right: 30upx;
	font-size: 16px;
	position: relative;
	line-height: 50upx;
	padding-bottom: 10upx;
	margin-top: 20px;
}
.cu-form input {
	flex: 1;
	font-size: 14px;
	color: #555;
	padding-right: 20upx;
	border: 1px solid #eee;
	border-radius: 5px;
	margin-top: 10upx;
	height: 30px;
	padding-left: 10px;
}
.radio-box>label{
	width: 600upx;
	display: flex;
	justify-content: space-between;
	align-items: center;
}
.eject-box{
	display: none;
	width: 750upx;
	height: 100vh;
	position: fixed;
	bottom: 0;
	z-index: 10;
	padding:3vh 25upx;
	background-color: rgba($color: #000000, $alpha: 0.5);
}
.eject{
	display: flex;
	flex-wrap: wrap;
	border-radius: 10px;
	height: 93vh;
	width: 700upx;
	padding: 20upx;
	background-color: $white;
	overflow-y:auto;
	
	button{
		width: 100%;
	}
}
.eject-content{
	width: 45%;
	margin: 10upx;
	padding: 0 5upx;
	height: 40px;
	font-size: 14px;
	position: relative;
}
.size{
	position: absolute;
	top: 0;
	left: 34px;
}
.show{
	display: block !important;
}
.type-box{
	flex: 1;
	padding-right: 20upx;
	border-radius: 5px;
	margin-bottom: 10upx;
	padding-left: 10px;
	font-size: 14px;
	height: auto;
	min-height: 30px;
	line-height: 30px;
	border: 1px solid #eee;
}
.incubator-box{
	display: none;
	background-color: $white;
	padding-left: 20upx;
	padding-right: 20upx;
	border-radius: 0 0 5px 5px;
	position: absolute;
	z-index: 10;
	width: 690upx;
	border: 1px solid $grey;
	border-top: none !important;
}
.incubator-content{
	line-height: 30px;
	margin-bottom: 4px;
	height: 30px;
	border-bottom: 1px solid #eee;
}
.btnsure{
	height: 40px;
	line-height: 40px;
}
</style>
