<template>
	<view>
		<view style="padding: 20rpx;">
			<u-search placeholder="请输入客户名称" v-model="name" @custom="custom"></u-search>
		</view>
		<u-index-list :indexList="indexList" v-if="!loading">
			<template v-for="(item, index) in itemArr">
				<!-- #ifdef APP-NVUE -->
				<u-index-anchor :text="indexList[index]" :key="index" v-if="item.length>0"></u-index-anchor>
				<!-- #endif -->
				<u-index-item :key="index">
					<!-- #ifndef APP-NVUE -->
					<u-index-anchor :text="indexList[index]" v-if="item.length>0"></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.cname}}</text>
							<text class="list__item__user-tel">{{item1.tel}}</text>
						</view>
						<u-line></u-line>
					</view>
				</u-index-item>
			</template>
			<!-- <view slot="footer" class="u-safe-area-inset--bottom"> -->
				<!-- <text class="list__footer">共305位好友</text> -->
			<!-- </view> -->
		</u-index-list>
		<u-loading-page loading-mode="spinner" :loading="loading"></u-loading-page>
		<u-modal :show="show" :content="'是否确认选择：'+this.form.cname" @confirm="confirm" @cancel="show = false"
			:showCancelButton="true" style="text-align: center;"></u-modal>
	</view>
</template>

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

<style lang="scss">
	.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>
