<template>
	<view>
		<u-index-list :indexList="indexList" v-if="!loading">
			<view  slot="header" style="padding: 20rpx;background-color: #E75F37;">
				<u-search placeholder="请输入品牌名称" v-model="name" @search="custom" @clear='custom' :showAction='false'
					bgColor='#EFEFEF' placeholderColor='#B6B6B6' searchIconColor='#E75F37'></u-search>
			</view>
			<template v-for="(item, index) in itemArr">
				<!-- #ifdef APP-NVUE -->
				<u-index-anchor :text="indexList[index]" :key="index"></u-index-anchor>
				<!-- #endif -->
				<u-index-item :key="index">
					<!-- #ifndef APP-NVUE -->
					<u-index-anchor :text="indexList[index]"></u-index-anchor>
					<!-- #endif -->
					<view class="list" v-for="(item1, index1) in item" :key="index1">
						<view class="list__item" @click="choice(item1,index1)">
							<text class="list__item__user-name">{{item1.brand}}</text>
						</view>
						<u-line></u-line>
					</view>
				</u-index-item>
			</template>
		</u-index-list>
		<u-loading-page loading-mode="spinner" :loading="loading"></u-loading-page>
		<u-popup :show="vehicleSeries" @close="vehicleSeries = false" mode="right" :closeable="true">
			<view style="padding-top: 10rpx;width: 650rpx;">
				<swiper class="box-c" style="height: 100vh;">
					<swiper-item class="child">
						<scroll-view :scroll-y="true">
							<view class="list" style="background-color: #E75F37;"
								v-for="(item,index) in vehicleSeriesList" :key="index">
								<view class="list__item">
									<text class="list__item__user-name" style="color: #fff;">{{item.name}}</text>
								</view>
								<u-line></u-line>
								<view class="list" style="background-color: #fff;" v-for="(li,i) in item[item.name]"
									:key="i">
									<view class="list__item" @click="vehicleSeriesClick(li)">
										<text class="list__item__user-name">{{li.seriesName}}</text>
									</view>
									<u-line></u-line>
								</view>
							</view>
						</scroll-view>
					</swiper-item>
				</swiper>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		queryAllCustomer,
		queryInitialsCustomer,
		brandList
	} from '@/api/views/vehicle/brand.js'
	const indexList = () => {
		const indexList = []
		const charCodeOfA = 'A'.charCodeAt(0)
		for (let i = 0; i < 26; i++) {
			indexList.push(String.fromCharCode(charCodeOfA + i))
		}
		return indexList
	}
	export default {
		data() {
			return {
				name: '',
				indexList: indexList(),
				itemArrTwo: [],
				loading: true,
				vehicleSeries: false,
				vehicleSeriesList: [],
				form: {},
				params: {}
			}
		},
		onLoad(params) {
			this.params = {
				...params,
				adminProfiles: uni.getStorageSync('userInfo').user_id
			}
			queryAllCustomer(this.params).then(res => {
				this.itemArrTwo = res.data
				this.loading = false
			})
		},
		computed: {
			itemArr() {
				return this.indexList.map((item, index) => {
					return this.itemArrTwo[index]
				})
			}
		},
		methods: {
			// queryInitials(item,index){
			// 	queryInitialsCustomer({initials:item}).then(res=>{
			// 		console.log(res.data)
			// 	})
			// },
			custom() {
				this.loading = true
				queryAllCustomer({
					...this.params,
					brand: this.name
				}).then(res => {
					this.itemArrTwo = res.data
					this.loading = false
				})
			},
			choice(item1, index1) {
				this.form = item1
				uni.showLoading({
					title: '查询车系中',
					mask: true
				});
				brandList({
					brandId: item1.brandId
				}).then(res => {
					this.vehicleSeriesList = res.data
					uni.hideLoading();
					this.vehicleSeries = true
				})
			},
			vehicleSeriesClick(item) {
				uni.$u.route('/pages/views/vehicle/vehicleSeries', {
					brand: item.brand,
					seriesName: item.seriesName,
					brandId: item.brandId,
					seriesId: item.seriesId
				});
			}
			// confirm() {
			// 	let pages = getCurrentPages() // 获取当前页面栈的实例，以数组形式按栈的顺序给出，第一个元素为首页，最后一个元素为当前页面。
			// 	let prevPage = pages[pages.length - 2] //上一页页面实例
			// 	prevPage.$vm.otherFun(this.form)
			// 	uni.navigateBack()
			// }
		}
	}
</script>

<style lang="scss"  scoped>
	.box-c,
	.child {
		display: flex;
		flex: 1;
	}

	.list {

		&__item {
			@include flex;
			padding: 6px 12px;
			align-items: center;
			justify-content: space-between;

			&__user-name {
				font-size: 32rpx;
				color: $u-main-color;
			}

			&__user-tel {
				font-size: 32rpx;
				margin-right: 20rpx;
				color: $u-main-color;
			}
		}

		&__footer {
			color: $u-tips-color;
			font-size: 14px;
			text-align: center;
			margin: 15px 0;
		}
	}
</style>