<template>
	<u-popup v-model="isShow" class="brand-picker" @close="isShow=false" mode="bottom">
		<view class="brand-header flex justify_between align_center">
			<text class="brand-picker-btn" @click="onClean">清空</text>
			<text>产品品牌</text>
			<text class="brand-picker-btn color-blue" @click="isShow=false">
				<u-icon name="close"></u-icon>
			</text>
		</view>
		<view class="search-box">
			<u-search
				placeholder="请输入品牌名称搜索" 
				v-model="queryWord" 
				:show-action="isGobleSearch||queryWord!=''"
				@focus="isGobleSearch=true"
				@blur="isGobleSearch=false"
				@custom="searchBrand"
				@search="searchBrand"
				@clear="clearSearch"
				:action-style="{'color': '#fff', 'font-size': '24upx', 'background-color': $config('primaryColor'), 'border-radius': '6upx', 'padding': '12upx 0'}">
			</u-search>
		</view>
		<scroll-view class="picker-content" scroll-y>
			<view class="picker-main">
				<view class="picker-main-item" v-for="(item, index) in listData" :key="item.brandSn" @click="chooseItem(index)">
					<view class="item-name">{{item.brandName}}</view>
					<u-icon v-show="item.brandSn==brandSn" class="item-icon" name="checkbox-mark" :color="$config('primaryColor')" size="28"></u-icon>
				</view>
				<view v-if="listData && listData.length == 0 && status!='loading'">
					<u-empty text="没有找到品牌" mode="list" :img-width="150" :margin-top="-60"></u-empty>
				</view>
				<view style="padding-bottom: 20upx;">
					<u-loadmore v-if="totalNum>listData.length || status=='loading'" :status="status" />
				</view>
			</view>
		</scroll-view>
	</u-popup>
</template>

<script>
	import { dealerProductBrandQuery } from '@/api/dealerProductBrand'
	export default {
		props: {
			openModal: { //  弹框显示状态
				type: Boolean,
				default: false
			},
			itemSn: {
				type: String,
				default: ''
			}
		},
		data() {
			return {
				isShow: this.openModal, //  是否打开弹框
				listData: [],
				queryWord: '',
				isGobleSearch: false,
				brandSn: this.itemSn || null,
				status: 'loadmore',
				totalNum: 0
			}
		},
		mounted() {
			this.clearSearch()
		},
		methods: {
			// 搜索品牌
			searchBrand(){
				this.brandSn = null
				this.getBrandList()
			},
			clearSearch(){
				this.queryWord = ''
				this.listData = []
				this.getBrandList()
			},
			getBrandList(){
				this.status = "loading"
				dealerProductBrandQuery({ brandName: this.queryWord }).then(res => {
					if(res.status == 200 && res.data){
						this.listData = res.data
						this.totalNum = res.data.count || 0
					}else{
						this.listData = []
						this.totalNum = 0
					}
					this.status = "loadmore"
				})
			},
			//  选择
			chooseItem(ind){
				this.brandSn = this.listData[ind].brandSn
				this.$emit('choose', this.listData[ind])
				this.isShow = false
			},
			// 清空
			onClean(){
				this.queryWord = ''
				this.brandSn = null
				this.$emit('clean')
				this.isShow = false
			}
		},
		watch: {
			//  父页面传过来的弹框状态
			openModal (newValue, oldValue) {
				this.isShow = newValue
			},
			//  重定义的弹框状态
			isShow (newValue, oldValue) {
			  if (!newValue) {
			    this.$emit('close')
			  }
			}
		}
	}
</script>

<style lang="scss" scoped>
	.brand-picker{
		.brand-header{
			padding: 20upx 30upx;
			position: relative;
			.brand-picker-btn{
				color: rgb(96, 98, 102);
			}
		}
		.brand-header:after{
			content: "";
			position: absolute;
			border-bottom: 1px solid #eaeef1;
			-webkit-transform: scaleY(.5);
			transform: scaleY(.5);
			bottom: 0;
			right: 0;
			left: 0;
		}
		.search-box{
			padding: 10upx 20upx;
		}
		.picker-content{
			height: 300upx;
			padding: 6upx 0 20upx;
			position: relative;
			.picker-main{
				position: absolute;
				top: 50%;
				left: 50%;
				transform: translate(-50%,-50%);
				width: 100%;
				max-height: 300upx;
				.picker-main-item{
					position: relative;
					padding: 0 30upx;
					.item-name{
						padding: 14upx 0;
						text-align: center;
					}
					.item-icon{
						position: absolute;
						right: 100upx;
						top: 19upx;
					}
				}
			}
		}
	}
</style>
