<template>
	<div>
		<common @asynCallBack="asynCallBack" :projectCheck="false"></common>
		<top-header :pageTitle="pageTitle"></top-header>

		<div class="mui-content vongi-xzdw">
			<div class="mui-content-padded vongi-xzdw-search">
				<input @focusin="searchIn" @focusout="searchOut" @keyup="doSearchCompany" v-model="searchForm.name" type="search"
				 placeholder="请输入公司名称">
				<span class="mui-icon mui-icon-search"></span>
			</div>
			<div v-show="showType=='select'">
				<div class="mui-content-padded fyy-date mui-clearfix">
					<h5>{{firstAreaName}} > {{secondAreaName}}</h5>
				</div>
				<!--地区选择-->
				<div v-show="nowLevel==1" class="mui-row mui-fullscreen vongi-xzdw-city">
					<div class="mui-col-xs-3">
						<div class="mui-segmented-control mui-segmented-control-inverted mui-segmented-control-vertical">
							<a @click="clickFirstArea(item)" v-for="(item,index) in firstAreaList" :class="'mui-control-item '+(item.name==firstAreaName?'mui-active':'')"
							 v-text="item.name"></a>
						</div>
					</div>
					<div class="mui-col-xs-9">
						<div class="mui-control-content">
							<ul class="mui-table-view">
								<li @click="clickSecondArea(item)" v-for="(item,index) in secondAreaList" class="mui-table-view-cell" v-text="item.name"></li>
							</ul>
						</div>
					</div>
				</div>
				<!--公司选择-->
				<div v-show="nowLevel==2" class="mui-row mui-fullscreen vongi-xzdw-city">
					<div class="mui-col-xs-3">
						<div class="mui-segmented-control mui-segmented-control-inverted mui-segmented-control-vertical">
							<a @click="clickFirstCompany(item)" v-for="(item,index) in firstCompanyList" :class="'mui-control-item '+(item.name==secondAreaName?'mui-active':'')"
							 v-text="item.name"></a>
						</div>
					</div>
					<div class="mui-col-xs-9">
						<div class="mui-control-content">
							<ul class="mui-table-view">
								<li @click="clickSecondCompany(item)" v-for="(item,index) in secondCompanyList" class="mui-table-view-cell"
								 v-text="item.name"></li>
							</ul>
						</div>
					</div>
				</div>

			</div>
		</div>
		<!--搜索结果的状态-->
		<div v-show="showType=='search'" class="mui-content vongi-xzdw" style="padding-top: 0;">
			<ul class="mui-table-view vongi-xzyljg">
				<li v-for="(item,index) in searchCompanyList" class="mui-table-view-cell mui-media">
					<a @click="clickSearchCompany(item)" v-text="item.name">
				
					</a>
				</li>
			</ul>
		</div>

		<loading :visible="isLoading"></loading>
	</div>
</template>

<script>
	import * as API_Person from '@/apis/person'
	import Common from '$project/components/Common.vue'
	import Loading from '$project/components/Loading.vue'
	import TopHeader from '$project/components/TopHeader.vue'
	import isReachBottom from '$project/utils/isReachBottom'
	import {
		mapGetters,
		mapMutations
	} from 'vuex'
	import * as types from '$project/store/mutation-types'
	export default {
		name: 'RegisterResidenceStep2',
		components: {
			Common,
			Loading,
			TopHeader
		},
		data() {
			return {
				pageTitle: '选择小区',

				subForm: {
					sceneId: '',
					sceneName: '',
					personRoleId: '',
					roleName: '',
					name: '',
					telephone: '',
					personId: '',
					companyId: '',
					companyName: '请选择',
					companyStructureId: '',
					//companyStructureName: '请选择',
					//最终选择的数据
					selectCompanyStructureList: [],
					address: '',
					idCard: '',
					faceImageUrl: '',
					popularizePersonId: ''
				},

				isLoading: false,

				firstAreaList: [],
				secondAreaList: [],
				firstAreaName: '',
				secondAreaName: '',
				//当前选择的层次
				nowLevel: 1,

				companyForm: {
					sceneId: '',
					regionId: '',
				},
				firstCompanyList: [],
				secondCompanyList: [],

				showType: 'select',
				searchForm: {
					sceneId: '',
					name: '',
					totalPage: 1,
					pageIndex: 1,
					pageSize: 20,
				},
				searchCompanyList: [],
				inputIng: false,
			}
		},
		created() {
			this.subForm = this.register_form_data;
			this.companyForm.sceneId = this.subForm.sceneId;
			this.searchForm.sceneId = this.subForm.sceneId;
		},
		methods: {
			//获取一级地区列表
			getFirstAreaList() {
				this.firstAreaName = '';
				this.getRegionList();
			},
			//获取地区列表
			getRegionList() {
				this.isLoading = true;
				API_Person.getRegionList(this.firstAreaName).then(response => {
					this.isLoading = false;
					if (this.firstAreaName) {
						for (var i = 0; i < response.length; i++) {
							if (response[i].name == this.firstAreaName) {
								this.secondAreaList = response[i].list;
								break;
							}
						}
					} else {
						this.firstAreaList = response;
						this.firstAreaName = response[0].name;
					}
				}).catch(error => {
					this.isLoading = false;
					mui.toast(error);
				})
			},
			//一级地区选择点击
			clickFirstArea(item) {
				this.firstAreaName = item.name;
			},
			//二级地区选择点击
			clickSecondArea(item) {
				this.companyForm.regionId = item.id;
				this.secondAreaName = item.name;
				this.nowLevel = 2;
				this.firstCompanyList = this.secondAreaList;
				//this.companyForm.regionId = this.secondAreaList[0].id;
			},
			//获取公司列表
			getCompanyList() {
				this.isLoading = true;
				API_Person.getCompanyList(this.companyForm).then(response => {
					this.isLoading = false;
					this.secondCompanyList = response.list;
				}).catch(error => {
					this.isLoading = false;
					mui.toast(error);
				})
			},
			//一级公司选择
			clickFirstCompany(item) {
				this.secondAreaName = item.name;
				this.companyForm.regionId = item.id;
			},
			//二级公司选择
			clickSecondCompany(item) {
				this.subForm.companyId = item.id;
				this.subForm.companyName = item.name;
				this.set_register_form_data(this.subForm);
				this.$router.push({
					name: 'RegisterResidenceStep1'
				})
			},
			//搜索出来的公司选择
			clickSearchCompany(item) {
				this.subForm.companyId = item.id;
				this.subForm.companyName = item.name;
				this.set_register_form_data(this.subForm);
				this.$router.push({
					name: 'RegisterResidenceStep1'
				})
			},
			//进入搜索
			searchIn() {
				this.inputIng = false;
				this.showType = 'search';
			},
			//离开搜索
			searchOut() {
				if (!this.searchForm.name) {
					this.showType = 'select';
				}
			},
			//搜索
			doSearchCompany() {
				if (this.inputIng) {
					return false
				}
				this.inputIng = true;
				var _this = this;
				setTimeout(function() {
					if (_this.searchForm.name) {
						_this.searchForm.pageIndex = 1;
						_this.getSearchCompanyList();
					}
				}, 500);
			},
			//根据公司名称搜索
			getSearchCompanyList() {
				this.isLoading = true;
				API_Person.getCompanyListBySeach(this.searchForm).then(response => {
					this.isLoading = false;
					if (response) {
						if (this.searchForm.pageIndex == 1) {
							this.searchCompanyList = response.data;
							this.searchForm.pageIndex = response.pageNumber;
							this.searchForm.totalPage = response.totalPage;
						} else {
							this.searchCompanyList = [
								...this.searchCompanyList,
								...response.data
							];
						}
					}
					this.searchForm.pageIndex++;
					this.inputIng = false;
				}).catch(error => {
					this.isLoading = false;
					mui.toast(error);
				})
			},
			//下拉事件
			handleScrool() {
				if (isReachBottom()) {
					console.log('到达底部')
					if (this.listForm.pageIndex <= this.listForm.totalPage && this.isLoading == false) {
						this.getSearchCompanyList();
					} else {
						return;
					}
				}
			},
			asynCallBack() {

			},
			...mapMutations({
				set_register_form_data: types.SET_REGISTER_FORM_DATA,
			})
		},
		mounted() {
			this.getFirstAreaList();
			//监控下拉加载事件
			var _this = this;
			window.addEventListener('scroll', _this.handleScrool);
		},
		destroyed() {
			//销毁监听事件
			var _this = this;
			window.removeEventListener('scroll', _this.handleScrool);
		},
		computed: {
			...mapGetters({
				openId: 'wx_openid',
				token: 'token',
				person_data: 'person_data',
				person_popedom: 'person_popedom',
				register_form_data: 'register_form_data'
			})
		},
		watch: {
			'firstAreaName': function(val) {
				this.getRegionList();
			},
			'companyForm.regionId': function(val) {
				this.getCompanyList();
			}
		}
	}
</script>

<style scoped src="$project/assets/css/xpwyfyy.css"></style>
<style scoped src="$project/assets/css/sczpfyy.css"></style>
<style src="$project/assets/css/iconfont.css"></style>
<style>
</style>
