<template>
	<view>
		<view class="tab">
			<u-tabs :list="tab" :current="current" @click="click"></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="add" @click="del(item)" v-if="current == 0">点击关联</text>
					<text class="del" @click="del(item)" v-else>解除</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: '企业大厅' }, { name: '我的关联' }],
			show: false,
			current: 0,
			actions: [{ name: '扫码关联' }, { name: '搜索企业' }],
			list: [],
			param: { pageNum: 1, pageSize: 10 },
			loadMore: true
		};
	},
	onLoad(e) {
		this.getData();
		uni.$on('company', (res) => {
			this.refresh();
		});
	},
	methods: {
		getData() {
			this.http.request({
				url: this.current == 0 ? '/app/company/list' : '/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);
					});
				}
			});
		},
		click(e) {
			this.current = e.index;
			this.param.state = e.index === 1 ? 0 : 1;
			this.refresh();
		},
		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, way: '扫码关联' },
							method: 'POST',
							success: (res) => {
								uni.showModal({
									title: '提示',
									content: '关联成功',
									showCancel: false,
									success: (res) => {
										this.refresh();
									}
								});
							}
						});
					},
					fail: (res) => {}
				});
			}
		},
		del(item) {
			uni.showModal({
				title: '提示',
				content: this.current == 0 ? '确定关联该企业?' : '确定解除该企业关联?',
				success: (res) => {
					if (res.confirm) {
						if (this.current == 0) {
							this.http.request({
								url: '/app/relate/add',
								data: { companyId: item.id, way: '手动关联' },
								method: 'POST',
								success: (res) => {
									uni.showModal({
										title: '提示',
										content: '关联成功',
										showCancel: false,
										success: (res) => {
											this.current = 1;
											this.refresh();
										}
									});
								}
							});
						} else {
							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;
			}
			.add {
				float: right;
				color: $main-color;
			}
		}
	}
}
.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>