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


		<div class="mui-content">
			<div class="mui-content-padded vongi-xzdw-search">
				<input @focusin="searchIn" @keyup="doSearchCompany" v-model="searchForm.name" type="search" placeholder="请输入机构名称">
				<span class="mui-icon mui-icon-search"></span>
			</div>
			<!--搜索结果的状态-->
			<!-- <div class="mui-content-padded flew-items flew-sp">
				<div class="vongi-xzdw-search mui-col-xs-10">
					<input type="search" placeholder="请输入姓名">
					<span class="mui-icon mui-icon-search"></span>
				</div>
				<a class="color4fc5f7">取消</a>
			</div> -->
			<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 {
		mapGetters,
		mapMutations
	} from 'vuex'
	import * as types from '$project/store/mutation-types'
	export default {
		name: 'RegisterPensionStep2',
		components: {
			Common,
			Loading,
			TopHeader
		},
		data() {
			return {
				pageTitle: '选择养老机构',

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

				isLoading: false,

				showType: 'search',
				searchForm: {
					sceneId: '',
					name: '',
					totalPage: 1,
					pageIndex: 1,
					pageSize: 20,
				},
				searchCompanyList: [],
				inputIng: false,
			}
		},
		created() {
			this.subForm = this.register_form_data;
			this.searchForm.sceneId = this.subForm.sceneId;
		},
		methods: {
			//搜索出来的公司选择
			clickSearchCompany(item) {
				this.subForm.companyId = item.id;
				this.subForm.companyName = item.name;
				this.set_register_form_data(this.subForm);
				this.$router.push({
					name: 'RegisterPensionStep1'
				})
			},
			//进入搜索
			searchIn() {
				this.inputIng = false;
				this.showType = 'search';
			},
			//搜索
			doSearchCompany() {
				if (this.inputIng) {
					return false
				}
				this.inputIng = true;
				var _this = this;
				setTimeout(function() {
					if (_this.searchForm.name) {
						_this.searchForm.pageIndex = 1;
						_this.getSearchCompanyList();
					} else {
						_this.inputIng = false;
					}
				}, 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() {
			//监控下拉加载事件
			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'
			})
		}
	}
</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>
