<template>
	<view class="container">
		<view class="top">
			<view class="search">
				<uni-easyinput class="uni-input" prefixIcon="search" v-model="formData.name" placeholder="请输入商户名称"></uni-easyinput>
				<button class="searchBtn" type="default" size="mini" @click="handleSearch">搜索</button>
			</view>
		</view>
		<uni-list border class="list">
			<uni-list-item
				v-for="(item, index) in resultList"
				:key="index"
				:ellipsis="2"
				:title="item.district"
				:note=getAddress(item)
				:rightText=getMerchantName(item)
				showArrow
				clickable
				@click="listItemBtn(item)">				
			</uni-list-item>
		</uni-list>
		<uni-load-more :status="more" />
	</view>
</template>

<script>
	import dictApi from '../../api/dict.js';
	import shopApi from '../../api/shop.js';
	import { BASE_URL } from '../../env.js';	
	export default {
		data() {
			return {				
				statusOptions: [],
				formData: {
					pageNum: 0,
					pageSize: 20,
					district: null,
					tag: null,
					status: null,
					name: null,
				},
				resultList: [],
				more: 'more'
			}
		},
		onLoad: function (option) {
			this.formData.district = option.district;
			this.formData.tag = option.tag;
			if(option.status==1){
				this.formData.status = "已使用";
			}else{
				this.formData.status = "未使用";
			}			
		},		
		onShow: function() {
			// this.init();
		},
		// 页面生命周期中onReachBottom（页面滚动到底部的事件）
		onReachBottom() {
			if(this.more != 'noMore') {
				this.more = 'more';
				this.getShopList();
			}
		},		
		async mounted() {
			await this.getDicts();
			await this.getShopList();
		},
		methods: {
			async getDicts() {
				const resp_vacant = await dictApi.getDict('vacant_status');
				if (resp_vacant.code == 200) 
					this.statusOptions = resp_vacant.data.map(l => ({ ...l, value: l.dictValue, text: l.dictLabel }));
			},
			// 搜索函数
			async getShopList() {
				this.more = 'loading';

				const queryParams = {};
				this.getFormParams(queryParams);
				const resp = await shopApi.getShopList({...queryParams});
				this.resultList.push(...resp.rows);

				this.updateMoreStatus(resp.total);
			},
			// 搜索函数
			async handleSearch() {
				this.resultList.length = 0;
				this.formData.pageNum = 0;

				await this.getShopList();
			},
			async handleReset() {								
				this.formData.pageNum = 0;
				this.formData.name = null;
				// this.formData.status = null;

				this.resultList.length = 0;

				await this.getShopList();
			},
			// 列表点击函数
			listItemBtn(e) {
				uni.navigateTo({ url: `/pages/shop/info?id=${e.shopId}` })
			},
			getAddress(shop){
				return shop.tag + "  铺位号:" + shop.number;
			},
			getMerchantName(shop){
				if(shop.status === "未使用")
					return shop.status;
				
				if(shop.name != null && shop.name != '' && shop.name != undefined)
					return shop.name;

				return "未知";					
			},
			getFormParams(queryParams){
				this.formData.pageNum += 1;
				for (const key in this.formData) {
					if (this.formData[key] !== null 
						&& this.formData[key] !== undefined
						&& this.formData[key] !== '') {
						queryParams[key] = this.formData[key];
					}
				}
			},
			updateMoreStatus(total){
				// 根据总数 算页数  如果当前页 = 总页数就是没有数据  否则就是上拉加载
				this.more = this.formData.pageNum >= Math.ceil(total / this.formData.pageSize) ? 'noMore' : 'more';
			}
		}
	}
</script>

<style>
	.container {
		width: 100%;
		background-color: #fff;
	}
	.top {
		width: 100%;
		z-index: 999;
		background-color: #fff;
		overflow: hidden;
	}
	.tabsBox {
		width: 100%;
		border-bottom: 1px solid #d3d3d3;
		display: flex;
	}
	.tab {
		width: 25%;
	}
	.text {
		display: block;
		width: 80%;
		margin: 0 auto;
		text-align: center;
	}
	.current {
		color: #ff9302;
		border-bottom: 1px solid #ff9302;
	}
	.search {
		width: 90%;
		height: 2em;
		border: 1px solid #f3f3f3;
		background-color: #fff !important;
		border-radius: 12px;
		display: flex;
		margin: 20px auto;
	}
	.uni-easyinput {
		width: 70%;
		margin-left: 5%;
		height: 100%;
		font-size: 14px;
	}
	.uni-easyinput .uni-easyinput__content {
		border: none !important;
		height: 100%;
		line-height: 2em;
	}
	.searchBtn {
		width: 20%;
		background-color: #ff9302 !important;
		border: none;
		color: #fff !important;
		height: 2em;
		margin-top: 0.2em;
		margin-left: 4%;
		border-radius: 12px;
	}
	.list {
		display: block;
	}
</style>