<template>
	<view class="chooseCustomer-content">
		<view class="search">
			<view class="flex justify_between align_center search-title">
				<text class="search-label">客户名称</text>
				<view class="search-btn" v-if="backPage == 'addSales'||backPage == 'authPass'">
					<text>没有搜索到？</text>
					<text>点击</text>
					<text @click="addCustomer">新增客户</text>
				</view>
			</view>
			<u-search
				placeholder="请输入客户名称搜索" 
				v-model="queryWord" 
				height="90"
				@input="searchBrand"
				@blur="searchBrand"
				@search="searchBrand"
				@clear="clearSearch"
				:showAction="false">
			</u-search>
		</view>
		<scroll-view class="picker-content flex_1" scroll-y >
			<view class="picker-main">
				<view class="picker-main-item" v-for="(item, index) in listData" :key="item.id" @click="chooseItem(item)">
					<view class="item-name">{{item.customerName}}</view>
				</view>
				<view v-if="listData&&listData.length>=20" style="color:#999;font-size: 26upx;text-align: center;padding: 10upx;">最多显示前20条匹配客户，请尝试输入更多内容</view>
				<view v-if="listData && listData.length == 0">
					<u-empty text="暂无匹配客户" mode="list" :img-width="200" :margin-top="60"></u-empty>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import {customerList} from '@/api/customer.js'
	export default{
		data(){
			return{
				listData:[],
				queryWord:'',
				pageSize:20,
				pageNo:1,
				scrollH:300,
				backPage:''
			}
		},
		onLoad(opts) {
			uni.$on('clear',this.clearSearch)
			this.backPage = opts.backPage
		},
		onShow() {
			this.init()
		},
		onUnload() {
			uni.$off('clear',this.clearSearch)
		},
		methods:{
			// 初始数据
			init(){
				this.listData=[]
				this.queryWord=''
				this.pageSize=20
				this.pageNo=1
			},
			// 新增客户
			addCustomer(){
				uni.navigateTo({
					url:'/pages/sales/addCustomer'
				})
			},
			getData(){
				console.log('调用')
				uni.showLoading({title:'加载中...',mask:true})
				customerList({queryWord:this.queryWord,pageNo:this.pageNo,pageSize:this.pageSize}).then(res => {
					if(res.status == 200 && res.data){
						uni.hideLoading({})
						this.listData = res.data.list
					}else{
						this.listData = []
					}
				})
			},
			// 搜索品牌
			searchBrand(){
				if(this.queryWord != ''){
					this.getData()
				}
			},
			clearSearch(){
				this.queryWord = ''
				this.listData = []
			},
			chooseItem(item){
				console.log(item)
				this.queryWord=item.customerName
				// 新增销售单
				if(this.backPage == 'addSales'){
					uni.navigateTo({
						url:'/pages/sales/addSales?pageType=edit&nowId='+item.id
					})
				}
				// 销售记录
				if(this.backPage == 'salesRecord'){
					uni.$emit("searchSalesRecord",item)
					uni.navigateBack()
				}
				// 门店审核通过
				if(this.backPage == 'authPass'){
					uni.$emit("addCustome",item)
					uni.navigateBack()
				}
			}
		},
	}
</script>

<style lang="scss">
	.chooseCustomer-content{
		width: 100%;
		height: 100vh;
		background-color: #fff;
		padding:0 30upx;
		display: flex;
		flex-direction: column;
		.search{
			.search-title{
				padding: 20upx 0;
				.search-label{
					font-size: 30upx;
					font-weight: 600;
				}
				.search-btn{
					font-size: 28upx;
					color: #999;
					text:last-child{
						display: inline-block;
						padding-left: 10upx;
						color:#007AFF
					}
				}
			}
		}
		.picker-content{
			padding: 6upx 0 20upx;
			width: 100%;
			height: calc(100vh-144upx);
			flex: 1;
			overflow-y: scroll;
			.picker-main{
				width: 100%;
				.picker-main-item{
					padding: 0 20upx;
					.item-name{
						padding: 30upx 0;
						border-bottom: 1rpx solid #e5e6eb;
					}
					.item-name:not(:last-child){
						border-bottom:none
					}
				}
			}
		}
	}
</style>
