<template>
	<view class="hangye-list u-flex-col">
		<view class="hangye-search">
			<view class="search-box u-flex">
				<u-icon name="search" size="30"></u-icon>
				<input type="text" class="input" placeholder="搜索行业" v-model="keyword1" confirm-type="search" @confirm="tosearch">
			</view>
		</view>
		<view class="change-scroll u-flex">
			<text class="chaneg-text">已选（{{hangyelist.length}}）</text>
			<scroll-view scroll-x="true" class="change-right u-flex-1">
				<view class="change-right-item u-flex" v-for="(item,index) in hangyelist" :key="index">
					<text>{{item.name}}</text>
					<image src="static/del.png" @click="del(index)" mode=""></image>
				</view>
			</scroll-view>
		</view>
		<view class="u-flex-1 scroll-box u-flex">
			<scroll-view scroll-y="true" class="scroll-left">
				<view class="left-item u-flex u-row-between" :class="{leftactive:leftindex == index}" @click="changeleft(index)" v-for="(item,index) in leftlist" :key="index">
					<text></text>
					<text>{{item.name}}</text>
					<text></text>
				</view>
			</scroll-view>
			<scroll-view scroll-y="true" class="scroll-right">
				<view class="right-item">
					<view class="u-flex u-row-between u-flex-wrap right-list">
						<text class="right-text u-line-1" @click="changeright(a)" :class="{rightactive:getright(a)}" v-for="(a,b) in rightlist" :key="b">{{a.name}}</text>
					</view>
				</view>
			</scroll-view>
		</view>
		<view class="hangye-down">
			<view class="u-flex u-row-between hangye-btn">
				<text class="text1" @click="hangyelist = []">重置</text>
				<text class="text2" @click="save">保存</text>
			</view>
			<view class="safe-area-inset-bottom"></view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				leftindex: 0,
				leftlist: [],
				rightlist: [],
				keyword1: '',
				keyword: '',
				hangyelist: []
			}
		},
		onLoad() {
			this.getlist()
		},
		methods: {
			save(){
				if(this.hangyelist.length == 0){
					this.$u.toast("请选择行业")
					return
				}
				const eventChannel = this.getOpenerEventChannel();
				eventChannel.emit('changehangye', this.hangyelist);
				uni.navigateBack()
			},
			getright(item){
				var index = this.hangyelist.findIndex(val => val.id == item.id)
				return index > -1 ? true : false
			},
			del(index){
				this.hangyelist.splice(index,1)
			},
			changeright(item) {
				var arr = []
				arr.push({
					id: this.leftlist[this.leftindex].id,
					name: this.leftlist[this.leftindex].name
				})
				arr.push({
					id: item.id,
					name: item.name
				})
				var index = this.hangyelist.findIndex(val => val.id == item.id)
				if (index > -1) {
					this.hangyelist.splice(index, 1)
				} else {
					this.hangyelist.push({
						id: item.id,
						name: item.name
					})
				}

				// console.log(arr);
				// const eventChannel = this.getOpenerEventChannel();
				// eventChannel.emit('changehangye', arr);
				// uni.navigateBack()
			},
			tosearch() {
				this.keyword = this.keyword1
				this.shaixuan()
			},
			shaixuan() {
				if (this.keyword) {
					var rightlist = JSON.parse(JSON.stringify(this.leftlist[this.leftindex].childlist))
					this.rightlist = []
					rightlist.forEach(item => {
						if (item.name.indexOf(this.keyword) > -1) {
							this.rightlist.push(item)
						}
					})
				} else {
					this.rightlist = this.leftlist[this.leftindex].childlist
				}
			},
			changeleft(index) {
				this.leftindex = index
				this.shaixuan()
			},
			getlist() {
				this.$u.post('/api/commons/get_category_industry').then(res => {
					this.leftlist = res.data
					this.rightlist = this.leftlist[this.leftindex].childlist
				})
			}
		}
	}
</script>

<style lang="scss">
	.hangye-list {
		height: 100vh;

		.change-scroll {
			height: 112rpx;
			padding: 0 32rpx;
			border-bottom: 2rpx solid #F0F0F0;

			.change-right {
				white-space: nowrap;
				min-width: 1rpx;
				.change-right-item {
					display: inline-flex;
					height: 56rpx;
					background: rgba(12, 102, 194, 0.1);
					border-radius: 6rpx;
					margin-right: 20rpx;
					padding: 0 20rpx;

					image {
						width: 20rpx;
						height: 20rpx;
					}

					text {
						font-size: 24rpx;
						font-family: SFPro-Regular, SFPro;
						font-weight: 400;
						color: #0C66C2;
						margin-right: 12rpx;
					}
				}
			}

			.chaneg-text {
				width: 132rpx;
				font-size: 24rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #222222;
			}
		}

		.hangye-down {
			.hangye-btn {
				padding: 8rpx 32rpx;

				.text1 {
					width: 256rpx;
					line-height: 84rpx;
					background: #F3F3F3;
					border-radius: 12rpx;
					text-align: center;
					font-size: 32rpx;
					font-family: PingFangSC-Medium, PingFang SC;
					font-weight: 500;
					color: #333333;
				}

				.text2 {
					width: 406rpx;
					line-height: 84rpx;
					background: #0C66C2;
					border-radius: 12rpx;
					text-align: center;
					font-size: 32rpx;
					font-family: PingFangSC-Medium, PingFang SC;
					font-weight: 500;
					color: #FFFFFF;
				}
			}
		}

		.scroll-box {
			min-height: 1rpx;

			.scroll-right {
				flex: 1;
				min-width: 1rpx;
				height: 100%;
				padding: 0 32rpx 0 26rpx;

				.right-item {
					padding: 34rpx 0;

					.right-list {
						.right-text {
							width: 212rpx;
							line-height: 72rpx;
							background: #F3F3F3;
							border-radius: 4rpx;
							text-align: center;
							font-size: 24rpx;
							font-family: SFPro-Regular, SFPro;
							font-weight: 400;
							color: #222222;
							margin-bottom: 20rpx;
						}

						.rightactive {
							background-color: rgba(12, 102, 194, 0.1);
							color: #0C66C2;
						}
					}

					.item-title {
						padding: 30rpx 0 24rpx;
						font-size: 36rpx;
						font-family: PingFangSC-Medium, PingFang SC;
						font-weight: 500;
						color: #222222;
					}
				}
			}

			.scroll-left {
				width: 240rpx;
				border-right: 2rpx solid #F0F0F0;
				height: 100%;

				.left-item {
					padding: 26rpx 0;

					text:first-child {
						width: 4rpx;
						height: 32rpx;
					}

					text:nth-child(2) {
						flex: 1;
						padding: 0 28rpx;
						font-size: 28rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #333333;
					}

					text:last-child {
						width: 4rpx;
						height: 32rpx;
					}
				}

				.leftactive {
					text:first-child {
						background-color: #0C66C2;
					}

					text:nth-child(2) {
						color: #0C66C2;
						font-weight: bold;
					}
				}
			}
		}

		.hangye-search {
			border-bottom: 2rpx solid #F0F0F0;
			padding: 20rpx 32rpx 0 32rpx;

			.search-box {
				height: 76rpx;
				background: #F3F3F3;
				border-radius: 16rpx;
				padding: 0 24rpx;

				.input {
					flex: 1;
					font-size: 30rpx;
					margin-left: 10rpx;
				}
			}
		}
	}
</style>