<template>
	<view style="background-color: #f2f2f6;padding-bottom: 0.01rpx;" :class="mask ? 'tl-show': ''">
		<u-navbar title="客户" :autoBack="true" :placeholder="true" leftIconColor="#fff"
			:titleStyle="{ color: '#ffffff' }" bgColor="#FD4B09">
			<template slot="right">
				<u-icon name="plus" color="#fff" @click="jumpCommodities"></u-icon>
			</template>
		</u-navbar>
		<view
			style="background-color: #fff;position: fixed;top: calc(var(--status-bar-height) + 44px);width: 100%;z-index: 10;">
			<u-tabs :list="tabsList" itemStyle="height:88rpx;" lineColor="#FD4B09" @click="tabsClick"
				:current="current">
				<!-- <view slot="right" style="font-size: 15px;display: flex;">
					<text style="margin-left: 10rpx;">|</text>
					<u--text text="排序" margin="0rpx 15rpx" @click="sortOpen = !sortOpen,mask=!mask"></u--text>
				</view> -->
			</u-tabs>
			<view style="background-color: #fff;padding: 10rpx 20rpx;">
				<u-search placeholder="请输入客户名称" v-model="form.cname" @custom="custom"></u-search>
			</view>
		</view>
		<view class="dropdown" v-if="sortOpen" @click.stop="dropdownOpen()">
			<view style="background-color: #fff;">
				<u-cell-group>
					<u-cell title="客户名称" @click="sortQuery(1)">
						<view style="display: flex;" slot="value" @click.stop="sortQuery(1,form.sort == 0?'1':'0')">{{form.typeSort == 1?form.sort==0?'正序':'倒序':''}}<u-icon :name="form.typeSort == 1?form.sort==0?'arrow-up-fill':'arrow-down-fill':''"></u-icon></view>
					</u-cell>
					<u-cell title="电话" @click="sortQuery(2)">
						<view style="display: flex;" slot="value" @click.stop="sortQuery(2,form.sort == 0?'1':'0')">{{form.typeSort == 2?form.sort==0?'正序':'倒序':''}}<u-icon :name="form.typeSort == 2?form.sort==0?'arrow-up-fill':'arrow-down-fill':''"></u-icon></view>
					</u-cell>
				</u-cell-group>
			</view>
		</view>
		<view class="content" style="margin-top: 100px;">
			<view class="contentBox" v-for="(item,index) in dataList" :key="index">
				<view style="width: 100%;margin: 0 auto;">
					<view class="textBox" @click="choice(item,index)">
						<view style="font-size: 36rpx;">{{item.cname}}</view>
						<view>编辑信息</view>
					</view>
					<view class="textBox">
						<view>
							<text style="color: #FD4B09;"
								@click="makePhoneCall(item.tel)">电话:{{item.tel}}</text></view>
						<view>发货仓库:{{item.deliveryWarehouseName}}</view>
					</view>
					<view class="textBox">
						<view>商城价格:{{item.priceSystem}}</view>
					</view>
				</view>
			</view>
		</view>
		<u-empty v-if="total == 0" style="position: absolute;top: 45%;left: 50%;transform:translate(-50%,-50%)"
			mode="data" icon="http://cdn.uviewui.com/uview/empty/data.png" />
		<uni-calendar ref="calendar" :insert="false" :range="true" @confirm="confirmCalendar" />
		<u-picker :show="show" :columns="columns" keyName="cname" @cancel="show = false" @confirm="confirm"></u-picker>
		<u-loadmore v-if="total !== 0 && dataList.length != 0" :status="status" />
	</view>
</template>

<script>
	import {
		queryList,
		corpsDescList,
		customerTypeAllList
	} from '@/api/views/customer/index.js'
	export default {
		data() {
			return {
				dataList: [],
				columns: [],
				sortOpen: false,
				show: false,
				mask: false,
				current: 0,
				total: 0,
				status: 'loadmore',
				name: '',
				form: {
					current: 1,
					size: 10,
				},
				tabsList: [{
					name: '全部',
					badge: {
						value: 0,
					}
				}]
			}
		},
		created() {
			customerTypeAllList({
				corpType: 'KH'
			}).then(res => {
				for (let item of res.data) {
					this.tabsList.push({
						name: item.title,
						id: item.id
					})
				}
			})
		},
		onShow() {
			this.dataList = []
			this.queryListfun()
		},
		onReachBottom() {
			this.status = 'loading'
			if (this.dataList.length < this.total) {
				this.form.current++
				this.queryListfun()
			} else {
				this.status = 'nomore'
			}
		},
		methods: {
			dropdownOpen(){
				this.sortOpen = false
				this.mask = false
			},
			sortQuery(val,sort = 0){
				this.form = {
					current: 1,
					size: 10,
					sort:sort,
					typeSort:val
				}
				this.dataList = []
				this.sortOpen = false
				this.mask = false
				this.current = 0
				this.queryListfun()
			},
			custom() {
				this.form = {
					current: 1,
					size: 10,
					cname: this.form.cname
				}
				this.dataList = []
				this.queryListfun()
			},
			makePhoneCall(tel) {
				uni.makePhoneCall({
					phoneNumber: tel,
					success: function() {
						console.log('拨打电话成功');
					},
					fail() {
						uni.showToast({
							title: '拨打电话失败',
							icon: 'none',
							mask: true
						});
					}
				})
			},
			choice(item1, index1) {
				uni.$u.route('/pages/views/customer/customerDetails', {
					id: item1.id
				});
			},
			jumpCommodities() {
				uni.$u.route('/pages/views/customer/customerDetails');
			},
			//查询
			queryListfun() {
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				queryList({
					...this.form,
					corpType: 'KH'
				}).then(res => {
					if (res.data.records) {
						this.dataList = this.dataList.concat(res.data.records)
					}
					this.total = res.data.total || 0
					uni.hideLoading();
					if (this.total < 10) {
						this.status = 'nomore'
					}
				})
			},
			tabsClick(item) {
				this.current = item.index
				this.form = {
					current: 1,
					size: 10,
					corpsTypeId: item.id
				}
				this.dataList = []
				this.queryListfun()
			},
			confirm(e) {
				this.form = {
					current: 1,
					size: 10,
					corpId: e.value[0].id
				}
				this.dataList = []
				this.queryListfun()
				this.show = false
			},
			//确认日期
			confirmCalendar(e) {
				this.form = {
					current: 1,
					size: 10,
					businesStartDate: e.range.before + ' 00:00:00',
					businesEndDate: e.range.after + ' 23:59:59'
				}
				this.dataList = []
				this.queryListfun()
			},
		}
	}
</script>

<style scoped>
	.contentBox {
		width: 96%;
		margin: 20rpx auto;
		background-color: #FFFFFF;
		border-radius: 20rpx;
		/* box-shadow: 0 5rpx 14rpx 0 rgba(101, 176, 249, 0.42); */
		padding-top: 15rpx;
		padding-bottom: 10rpx;
	}

	.textBox {
		padding: 0 15px;
		display: flex;
		justify-content: space-between;
		font-size: 24rpx;
		margin-bottom: 10rpx;
	}

	.dropdown {
		position: absolute;
		z-index: 99;
		background: rgba(0, 0, 0, .3);
		width: 100%;
		left: 0;
		top: calc(var(--status-bar-height) + 90px);
		bottom: 0;
	}

	.tl-show {
		overflow: hidden;
		position: fixed;
		height: 100%;
		width: 100%;
	}
</style>
