<template>
	<view>
		<view class="search"><u-search placeholder="企业名称" bgColor="white" v-model="companyName" :showAction="false" @search="getData()" @clear="clear()"></u-search></view>
		<view class="list">
			<view class="item" v-for="(item, index) in list" :key="index" @click="detail(item)">
				<text>{{ item.companyName }}</text>
				<text class="icon">&#xe62b;</text>
			</view>
			<view class="loading" v-if="loadMore"><u-loadmore :status="loadMore ? 'loading' : 'nomore'" /></view>
			<u-empty v-if="!loadMore && list.length == 0"></u-empty>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			list: [],
			companyName: '',
			param: { pageNum: 1, pageSize: 10 },
			loadMore: false
		};
	},
	watch: {
		companyName(val) {
			if (val.length > 3) {
				this.refresh();
			}
		}
	},
	methods: {
		getData() {
			this.http.request({
				url: '/app/company/list',
				data: { companyName: this.companyName },
				success: (res) => {
					this.loadMore = res.data.pages > this.param.pageNum ? true : false;
					res.data.rows.forEach((item) => {
						this.list.push(item);
					});
				}
			});
		},
		detail(item) {
			uni.showModal({
				title: '提示',
				content: '确定关联该企业?',
				success: (res) => {
					if (res.confirm) {
						this.http.request({
							url: '/app/relate/add',
							data: { companyId: item.id, way: '手动关联' },
							method: 'POST',
							success: (res) => {
								uni.showModal({
									title: '提示',
									content: '关联成功',
									showCancel: false,
									success: (res) => {
										uni.$emit('company');
										uni.navigateBack();
									}
								});
							}
						});
					}
				}
			});
		},
		clear() {
			this.list = [];
		},
		//刷新数据
		refresh() {
			this.loadMore = true;
			this.param.pageNum = 1;
			this.list = [];
			this.getData();
		}
	},
	//上拉加载
	onReachBottom() {
		if (this.loadMore) {
			this.param.pageNum++;
			this.getData();
		}
	}
};
</script>

<style lang="scss">
.search {
	padding: 12px 20px 12px 20px;
}
.list {
	.item {
		padding: 17px;
		background-color: white;
		margin-top: 10px;
		.icon {
			float: right;
		}
	}
}
</style>
