<template>
	<view class="selSupplier">
		<view class="search-view">
			<view class="input-view">
				<input type="text" @confirm="search" confirm-type="search" placeholder-class="input-pl" placeholder="商铺名称" v-model="keyword" />
				<u-icon class="add-icon" name="search" size="40" color="#666666" @click="search"></u-icon>
			</view>
		</view>
		<view class="customer-ul" :style="{ paddingBottom: is_check ? '90rpx' : '0' }">
			<view class="customer-li" v-for="(item, index) in shop_list" :key="index" @click="changeData(item)">
				<view class="customer-name">{{ item.name }}</view>
				<view class="other" :style="{ paddingLeft: is_check ? '80rpx' : '20rpx' }">
					<view class="check-tag" v-if="!!is_check">
						<text v-if="!item.check" class="custom-icon custom-icon-xuanze"></text>
						<text v-else class="custom-icon custom-icon-xuanze_xuanzhong"></text>
					</view>
					<view class="concat clearfix">
						<text class="float_left">{{ item.contactName }}</text>
						<view class="float_right" v-if="item.mobile">
							<u-icon name="phone-fill" size="26"></u-icon>
							<text class="phone-text">{{ item.mobile }}</text>
						</view>
					</view>
					<view class="address">{{ item.area.provinceName }}-{{ item.area.cityName }}-{{ item.area.districtName }}-{{ item.area.address }}</view>
				</view>
			</view>
			<u-loadmore :status="load_status" />
		</view>
		<view class="bottom-view" @click="checkConfirm" v-if="is_check">确定</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			keyword: '',
			load_status: 'nomore',
			page: 1,
			pageSize: 10,
			total: 0,
			shop_list: [],
			is_check: '',
			sel_id: []
		};
	},
	onLoad(options) {
		this.is_check = options.check || '';
		if (options.selId) {
			this.sel_id = JSON.parse(options.selId);
		}
		this.getAllShop();
	},
	onPullDownRefresh() {
		this.page = 1;
		this.getAllShop();
	},
	// 上拉加载
	onReachBottom() {
		if (this.total / this.pageSize > this.page) {
			this.page += 1;
			this.getAllShop();
		}
	},
	methods: {
		changeData(item) {
			if (this.is_check) {
				item.check = !item.check;
			} else {
				// 选择返回上一页
				this._prePage().shopData = item;
				uni.navigateBack();
			}
		},
		checkConfirm() {
			const list = this.shop_list.filter(item => item.check);
			// 选择返回上一页
			this._prePage().shopData = list;
			uni.navigateBack();
		},
		search() {
			this.page = 1;
			if(this.keyword){
				this.shopSearch();
			}else{
				this.getAllShop();
			}
		},
		shopSearch() {
			this.loading_status = 'loading';
			this.$u.api
				.shopSearch({
					page: this.page,
					pageSize: this.pageSize,
					enableStatus: 5,
					name:this.keyword
				})
				.then(res => {
					uni.stopPullDownRefresh();
					if (this.page === 1) {
						this.shop_list = res.data;
					} else {
						this.shop_list = this.shop_list.concat(res.data);
					}
					this.shop_list = this.shop_list.map(item => {
						return {
							...item,
							check: this.sel_id.findIndex(selId => parseInt(selId) === parseInt(item.id)) > -1
						};
					});
					this.total = res.pageTotal;
					this.load_status = this.$utils.loadStatus(this.page, this.pageSize, this.total);
				})
				.catch(err => {
					uni.stopPullDownRefresh();
				});
		},
		getAllShop() {
			this.loading_status = 'loading';
			this.$u.api
				.getAllShop({
					page: this.page,
					pageSize: this.pageSize,
					enableStatus: 5
				})
				.then(res => {
					uni.stopPullDownRefresh();
					if (this.page === 1) {
						this.shop_list = res.data;
					} else {
						this.shop_list = this.shop_list.concat(res.data);
					}
					this.shop_list = this.shop_list.map(item => {
						return {
							...item,
							check: this.sel_id.findIndex(selId => parseInt(selId) === parseInt(item.id)) > -1
						};
					});
					this.total = res.pageTotal;
					this.load_status = this.$utils.loadStatus(this.page, this.pageSize, this.total);
				})
				.catch(err => {
					uni.stopPullDownRefresh();
				});
		}
	}
};
</script>

<style lang="scss" scoped>
.search-view {
	position: fixed;
	z-index: 99;
	top: 0;
	left: 0;
	width: 100%;
	background-color: #ffffff;
	border-bottom: 1px solid #f5f5f5;
	padding: 20rpx 0;
	.input-view {
		padding: 0 24rpx;
		input {
			height: 60rpx;
			line-height: 60rpx;
			padding: 0 24rpx;
			display: inline-block;
			width: 580rpx;
			border: 1px solid #f5f5f5;
			vertical-align: middle;
			border-radius: 8rpx;
			background-color: #f7f8fa;
		}
		.add-icon {
			margin-left: 14rpx;
			display: inline-block;
			vertical-align: middle;
		}
	}
}
.bottom-view {
	z-index: 99;
	width: 100%;
	position: fixed;
	left: 0;
	bottom: 0;
	line-height: 90rpx;
	color: #ffffff;
	background-color: $uni-color-primary;
	text-align: center;
}
.customer-ul {
	padding-top: 90rpx;
	padding-bottom: 20rpx;
	.customer-li {
		width: 702rpx;
		margin: 0 auto;
		background-color: #ffffff;
		border-radius: 10rpx;
		margin-top: 20rpx;
		.customer-name {
			padding: 0 20rpx;
			line-height: 80rpx;
			border-bottom: 1px solid #f5f5f5;
		}
		.other {
			font-size: 24rpx;
			padding: 10rpx 20rpx;
			line-height: 50rpx;
			position: relative;
			.check-tag {
				position: absolute;
				left: 20rpx;
				top: 50%;
				transform: translateY(-50%);
				font-size: 40rpx;
				color: #999999;
				.custom-icon-xuanze_xuanzhong {
					color: $uni-color-primary;
				}
			}
			.concat {
				.float_right {
					color: $uni-color-primary;
					.phone-text {
						margin-left: 10rpx;
					}
				}
			}
		}
	}
}
</style>
