<template>
	<view>
		<view class="tab">
			<u-tabs :list="tab" :current="0"></u-tabs>
		</view>
		<view class="list">
			<view class="item" v-for="(item, index) in list" :key="index">
				<view class="title">{{ item.companyName }}</view>
				<view class="op">
					<text>{{ item.createTime }}</text>
					<text class="del" @click="del(item)">解除</text>
				</view>
			</view>
			<view class="loading" v-if="loadMore"><u-loadmore :status="loadMore ? 'loading' : 'nomore'" /></view>
			<u-empty v-if="!loadMore && list.length == 0" text="尚未关联公司"></u-empty>
		</view>
		<view class="footer">
			<view class="db"><button class="btn" @click="show = true">关联企业</button></view>
		</view>
		<u-action-sheet round="20" :actions="actions" @select="selectClick" cancelText="取消" :show="show" @close="show = false"></u-action-sheet>
	</view>
</template>

<script>
export default {
	data() {
		return {
			tab: [{ name: '全部' }],
			show: false,
			actions: [{ name: '扫码关联' }, { name: '手动关联' }],
			list: [],
			param: { pageNum: 1, pageSize: 10, orderByColumn: 'createTime', isAsc: 'desc' },
			loadMore: true
		};
	},
	onLoad(e) {
		this.getData();
		uni.$on('company', (res) => {
			this.refresh();
		});
	},
	methods: {
		getData() {
			this.http.request({
				url: '/app/relate/list',
				data: this.param,
				loading: 'false',
				success: (res) => {
					this.loadMore = res.data.pages > this.param.pageNum ? true : false;
					res.data.rows.forEach((item) => {
						item.createTime = uni.$u.timeFrom(Date.parse(item.createTime));
						this.list.push(item);
					});
				}
			});
		},
		selectClick(e) {
			if (e.name == '手动关联') {
				uni.navigateTo({ url: '/pages/company/search' });
			} else {
				uni.scanCode({
					success: (res) => {
						this.http.request({
							url: '/app/relate/add',
							data: { companyId: res.result },
							method: 'POST',
							success: (res) => {
								uni.showModal({
									title: '提示',
									content: '关联成功',
									showCancel: false,
									success: (res) => {
										this.refresh();
									}
								});
							}
						});
					},
					fail: (res) => {}
				});
			}
		},
		del(item) {
			uni.showModal({
				title: '提示',
				content: '确定解除该企业关联?',
				success: (res) => {
					if (res.confirm) {
						this.http.request({
							url: '/app/relate/remove',
							data: { companyId: item.companyId },
							method: 'POST',
							success: (res) => {
								uni.showToast({ title: '解除成功' });
								this.list.splice(this.list.indexOf(item), 1);
							}
						});
					}
				}
			});
		},
		//刷新数据
		refresh() {
			this.loadMore = true;
			this.param.pageNum = 1;
			this.list = [];
			this.getData();
		}
	},
	//下拉刷新
	onPullDownRefresh() {
		setTimeout(() => {
			this.refresh();
			uni.stopPullDownRefresh();
		}, 1000);
	},
	//上拉加载
	onReachBottom() {
		if (this.loadMore) {
			this.param.pageNum++;
			this.getData();
		}
	}
};
</script>

<style lang="scss">
.list {
	padding: 10px 10px 90px 10px;
	.item {
		background-color: white;
		padding: 15px;
		border-radius: 5px;
		margin-bottom: 10px;
		.title {
			padding-bottom: 10px;
		}
		.op {
			border-top: 1px solid $line;
			padding-top: 10px;
			color: #676767;
			font-size: 14px;
			.del {
				float: right;
				color: #f44336;
			}
		}
	}
}
.footer {
	position: fixed;
	width: 100%;
	bottom: 0px;
	padding-bottom: 13px;
	background-color: white;
	border-top: 1px solid $line;
	.db {
		padding: 15px 35px 10px 35px;
	}
}
</style>
