<template>
	<uv-navbar :fixed="false" @leftClick="$onClickLeft" bgColor="#09b4f1">
		<template v-slot:left>
			<uv-icon name="arrow-left" size="19" color="#ffffff"></uv-icon>
		</template>
		<template v-slot:center>
			<text style="color:#ffffff">{{title}}</text>
		</template>
	</uv-navbar>
	<view class="container">
		<view class="bare">
			<!-- 顶部导航高度 -->
			<view class="statusBar"></view>
		</view>
		<view class="notice">
			<view class="belong">
				<view class="left flex-1">
					<view class="store">{{customer.name}}</view>
				</view>
				<view class="flex">
					<uv-text v-if="customer.dealStatus === 1" color="#84bd00" suffixIcon="arrow-right"
						iconStyle="font-size: 16px;color:#84bd00" text="成交"></uv-text>
					<uv-text v-else color="#f9ae3d" suffixIcon="arrow-right" iconStyle="font-size: 16px;color:#f9ae3d"
						text="未成交"></uv-text>
				</view>
			</view>
			<!-- 标签 -->
			<view class="tap" v-if="customer.tags !='' ">
				<uv-tags :text="item" plain type="warning" size="mini" v-for="(item,i) in customer.tags" :key="i" />
			</view>
			<view class="relation flex">
				<view class="left">手机：<text class="dial" @click="call(customer.mobile)">{{customer.mobile}}</text></view>
			</view>
			<view class="bottom flex">
				<view class="client_time">最后跟进：{{timeFormats(customer.followTime)}}</view>
				<view class="right">{{customer.ownUserName ? customer.ownUserName : '--'}}</view>
			</view>

			<view class="more flex" @click="onLookMore">查看更多<uv-icon name="arrow-down" size="16"></uv-icon></view>
		</view>
		<!-- 工具项 -->
		<view class="region mt-3">
			<view class="border-bottom">
				<uv-tabs :list="tapList" :scrollable="false" :current="current" @change="change"></uv-tabs>
			</view>
			<scroll-view scroll-y class="sv" :style="{height:scrollHeight+'px'}" :scroll-top="scrollTop"
				@scrolltolower="reachBottom">
				<view class="page-box">
					<!-- 跟进记录 -->
					<block v-if="current == 0">
						<block v-if="recordList.length > 0">
							<view class="list-view" v-for="(item, index) in recordList" :key="index">
								<view class="top">
									<uv-parse class="left" :content="item.content"></uv-parse>
								</view>
								<view class="bottom">
									<view class="time">{{timeFormats(item.createTime)}}</view>
									<view class="way">{{item.recordTypeName}}</view>
								</view>
							</view>
							<uv-load-more :status="listStatus" />
						</block>
						<uv-empty text="暂无数据" v-else margin-top="100" mode="list"></uv-empty>
					</block>

				</view>
			</scroll-view>
		</view>
		<!-- 上拉菜单 -->
		<view class="popup-content">
			<uv-popup mode="bottom" round="38" ref="moreShowRef">
				<view class="popup-title">
					<view class="" @click="look()" style="width: 45px;">
					</view>
					<text class="">更多操作</text>
					<view class="" @click="moreShowClose" style="width: 45px;">
						<uv-icon name="close" color="#909399" size="20"></uv-icon>
					</view>
				</view>
				<view class="" style="height: 300rpx;">
					<uv-grid :col="4">
						<uv-grid-item v-for="(item,index) in moreList" @click="moreClick(item.text)" :key="index">
							<uv-icon :name="item.icon" :size="26"></uv-icon>
							<view class="grid-text">{{item.text}}</view>
						</uv-grid-item>
					</uv-grid>
				</view>
			</uv-popup>
		</view>
		<!-- 底部按钮 -->
		<view class="bottom-btn">
			<view style="margin-right: 20rpx;">
				<uv-button type="info" text="更多" customStyle="padding:0 50rpx;background-color:#09b4f1;color:#fff"
					@click="moreShow">更多</uv-button>
			</view>
			<view>
				<uv-button type="success" text="跟进" customStyle="padding:0 50rpx;" @click="onAdd">跟进</uv-button>
			</view>
		</view>
	</view>
	<transfer ref="transRef" @doTrans="onTrans"></transfer>
</template>

<script setup scoped>
	import {
		ref,
		unref,
		computed
	} from 'vue'
	import {
		onLoad,
		onShow,
		onReady
	} from '@dcloudio/uni-app'
	import {
		getDictData,
		getRecordPage
	} from '@/api/customer'
	import {
		getClues,
		openClues,
		delClues,
		transferClues
	} from '@/api/clues'
	import {
		formatDateTime,
		prePage
	} from '@/utils/util'
	const title = ref('线索详情')
	const customerId = ref(0)
	const customer = ref({
		purchaseTotal: 0,
	})
	const scrollTop = ref('')
	const tapList = ref([{
		name: '跟进记录'
	}])
	const moreList = ref([{
			text: '编辑线索',
			icon: 'edit-pen',
			show: true,
		},
		{
			text: '放入线索池',
			icon: 'trash',
			show: true,
		},
		{
			text: '转移线索',
			icon: 'share',
			show: true,
		},
		{
			text: '转化成客户',
			icon: 'account',
			show: true,
		},
		{
			text: '删除线索',
			icon: 'trash',
			show: true,
		}
	])
	const recordList = ref([])
	const contactList = ref([])
	const businessList = ref([])
	const selectList = ref([])
	const htList = ref([])
	const hkList = ref([])
	const current = ref(0)
	const navbar = ref(false)
	const pH = ref(0)
	const scrollHeight = ref(0)
	const page = ref(1)
	const pageSize = ref(10)
	const lastPage = ref(false)
	const listStatus = ref('loadmore')

	onReady(() => {
		uni.getSystemInfo({
			success(res) {
				pH.value = res.windowHeight
				let scrollH = uni.createSelectorQuery().select(".sv")
				scrollH.boundingClientRect(data => {
					let pH0 = pH.value
					scrollHeight.value = pH0 - data.top - 80
				}).exec()
			}
		})
	})

	onLoad((e) => {
		console.log('e:', e)
		customerId.value = e.id
		console.log('customerId.value:', customerId.value)
		getData()
	})

	onShow(() => {
		getData()
		switch (current.value) {
			case 0:
				page.value = 1
				lastPage.value = false
				getCustomerRecord()
				break;
			default:
				break;
		}
	})

	const purchaseTotalFormat = computed(() => {
		console.log('')
		if (customer.value.purchaseTotal) {
			let num;
			if (customer.value.purchaseTotal > 9999) { //大于9999显示x.xx万
				num = (Math.floor(customer.value.purchaseTotal / 100) / 100) + '万';
			} else if (customer.value.purchaseTotal <= 9999 && customer.value.purchaseTotal > -9999) {
				num = customer.value.purchaseTotal
			}
			return num;
		} else {
			return 0;
		}
	})


	// 查看更多
	const onLookMore = () => {
		uni.navigateTo({
			url: '/pages/components/pages/clues/detail?id=' + customerId.value
		});
	}
	// 拨打电话
	const call = (p) => {
		uni.makePhoneCall({
			phoneNumber: p
		});
	}

	const moreShowRef = ref()
	const moreShow = () => {
		unref(moreShowRef).open()
	}

	const moreShowClose = () => {
		unref(moreShowRef).close()
	}

	const transRef = ref()
	const onTrans = async (userId) => {
		let data = await transferClues({
			cluesIds: [customerId.value],
			ownerAdminIds: [userId]
		})
		transRef.value.selectShowClose3()
		moreShowClose()
		uni.showToast({
			title: '操作成功',
			icon: 'success',
			duration: 2000
		})
		prePage().$vm.isRefresh = true
		setTimeout(() => {
			uni.navigateBack();
		}, 1500);
	}

	// 更多操作
	const moreClick = async (index) => {
		switch (index) {
			case "编辑线索":
				// 编辑客户
				uni.navigateTo({
					url: '/pages/components/pages/clues/add?id=' + customer.value.id + '&type=edit'
				});
				break;
			case "放入线索池":
				uni.showModal({
					title: '提示',
					content: '确定将用户放入线索池吗？',
					success: async function(res) {
						if (res.confirm) {
							await openClues({
								id: customerId.value
							})

							uni.showToast({
								title: '操作成功',
								icon: 'success',
								duration: 2000
							})
							prePage().$vm.isRefresh = true
							setTimeout(() => {
								uni.navigateBack()
							}, 1000)

						}
					}
				})
				break;
			case "转移线索":
				transRef.value.doopen()
				break;
			case "转化成客户":
				uni.navigateTo({
					url: '/pages/components/pages/clues/add?id=' + customer.value.id + '&type=transfer'
				});
				break;
			case "删除线索":
				unref(moreShowRef).close()
				uni.showModal({
					title: '提示',
					content: '确定删除该线索吗？',
					success: async function(res) {
						if (res.confirm) {
							await delClues({
								id: customerId.value
							})
							uni.showToast({
								title: '操作成功',
								icon: 'success',
								duration: 2000
							})
							prePage().$vm.isRefresh = true
							setTimeout(() => {
								uni.navigateBack();
							}, 1500);

						}
					}
				});
				break;
			default:
				break;
		}
	}


	// 格式化时间
	const timeFormats = (val) => {
		if (val) {
			return formatDateTime(val)
		} else {
			return '--'
		}
	}
	// 获取客户详情
	const getData = async () => {
		let data = await getClues({
			id: customerId.value
		})
		data.tags = data.tags.split(',')
		customer.value = data
	}
	// 获取客户跟进记录
	const getCustomerRecord = async (isNextPage, pages) => {
		let data = await getRecordPage({
			pageNo: page.value,
			pageSize: pageSize.value,
			types: 'clues',
			typesId: customerId.value
		})
		// 不够一页
		if (data.list.length < 10) {
			listStatus.value = 'nomore'
		}
		// 最后一页
		if (data.list.length == 0) {
			lastPage.value = true
		}
		// 第二页开始
		if (isNextPage) {
			recordList.value = recordList.value.concat(data.list)
			return
		}
		recordList.value = data.list
		console.log('recordList.value:', recordList.value)

	}

	const reachBottom = () => {
		switch (current.value) {
			case 0:
				if (lastPage.value || listStatus.value == 'loading') return;
				listStatus.value = 'loading'
				setTimeout(() => {
					if (lastPage.value) return;
					getCustomerRecord(true, ++page.value)
					if (recordList.value.length >= 10) listStatus.value = 'loadmore';
					else listStatus.value = 'loading';
				}, 1200)
				break;

			default:
				return '--'
				break;
		}
	}
	// 切换导航栏
	const change = (e) => {
		let index = e.index
		current.value = index;
		switch (index) {
			case 0:
				page.value = 0
				lastPage.value = false
				getCustomerRecord()
				break;
			default:
				break;
		}
	}

	// 跟进
	const onAdd = (e) => {
		uni.navigateTo({
			url: '/pages/components/pages/clues/followUp?cluesId=' + customer.value.id + '&cluesName=' + customer.value
				.name
		})
	}
</script>

<style lang="scss" scoped>
	.container {
		background-color: #F7F7F7 !important;
	}

	.bare {
		color: #fff;
		background-color: #09b4f1;
	}

	.notice {
		margin: -68px 8px 10px 8px;
		left: 0;
		right: 0;
		background-color: #fff;
		border-radius: 10px;
		padding: 25rpx 20rpx 35rpx;

		.belong {
			display: flex;
			justify-content: space-between;

			.left {
				display: flex;
				align-items: center;
				padding-right: 25rpx;

				.store {
					font-size: 30rpx;
					font-weight: bold;
					word-break: break-all;
				}
			}

			.right {
				color: #FF7159;
			}

			.right1 {
				color: #FF7159;
			}
		}

		.tap {
			display: flex;
			align-items: center;
			flex-wrap: wrap;
			margin: 20rpx 0 0;

			.tap-item {
				background-color: #FF6146;
				color: #fff;
				font-size: 25rpx;
				padding: 10rpx 25rpx;
				border-radius: 8rpx;
				margin-left: 10rpx;
				margin-bottom: 10rpx;
			}
		}

		.relation {
			justify-content: space-between;
			margin-top: 15rpx;
			font-size: 26rpx;

			.dial {
				color: #2979ff;
				font-size: 14px;
				border-bottom: 1px solid #2979ff;
				padding-bottom: 0px;
			}
		}

		.bottom {
			margin-top: 25rpx;
			justify-content: space-between;

			.client_time {
				color: #777;
				font-size: 26rpx;
			}
		}

		.more {
			font-size: 28rpx;
			padding-top: 25rpx;
			justify-content: center;
			color: #2979ff;
		}

		.details {
			padding: 45rpx 0rpx 0rpx;

			.item {
				flex: 1;
				margin-bottom: 25rpx;

				.text {
					font-size: 25rpx;
					color: #777;
				}
			}
		}
	}

	.statusBar {
		position: relative;
		z-index: -1;
		height: 188rpx;
	}

	.page-box {
		padding: 20rpx 20rpx 45rpx;
	}

	.list-view {
		width: 710rpx;
		background-color: #ffffff;
		margin-bottom: 20rpx;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
		font-size: 28rpx;

		.top {
			display: flex;
			justify-content: space-between;

			.left {
				display: flex;
				align-items: center;
				word-break: break-all;
				width: 100%;

				.store {
					margin: 0 10rpx;
					font-size: 28rpx;
					font-weight: bold;
				}
			}

			.right {
				color: #F76046;
			}
		}

		.item {
			display: flex;
			margin: 20rpx 0 0;

			.left {
				margin-right: 20rpx;

				.image {
					width: 120rpx;
					height: 120rpx;
					border-radius: 10rpx;
				}

				.video {
					width: 140rpx;
					height: 140rpx;
				}
			}

			.content {
				flex: 1;

				.title {
					font-size: 28rpx;
					line-height: 50rpx;
				}

				.type {
					margin: 20rpx 0;
					font-size: 26rpx;
					color: $uv-tips-color;
				}

				.delivery-time {
					color: #e5d001;
					font-size: 24rpx;
				}
			}
		}

		.contact {
			padding: 25rpx 0;
			display: flex;
			justify-content: space-between;

			.name {}

			.dial {
				color: #2979ff;
				font-size: 14px;
				border-bottom: 1px solid #2979ff;
				padding-bottom: 0px;
			}
		}

		.bottom {
			display: flex;
			margin-top: 10rpx;
			justify-content: space-between;
			align-items: center;

			.time {
				color: #777;
				font-size: 26rpx;
			}

			.btn {
				line-height: 60rpx;
				width: 160rpx;
				border-radius: 5px;
				font-size: 26rpx;
				text-align: center;
				color: $uv-primary-dark;
			}

			.entity {
				color: #fff;
				background-color: #09b4f1;
			}
		}
	}

	.bottom-btn {
		position: fixed;
		display: flex;
		align-items: center;
		justify-content: flex-end;
		bottom: 0;
		left: 0;
		right: 0;
		padding: 25rpx 30rpx 45rpx;
		background-color: #fff;
		z-index: 100;
	}

	.popup-content {
		.popup-title {
			display: flex;
			align-items: center;
			justify-content: space-between;
			position: relative;
			font-size: 35rpx;
			font-weight: 600;
			text-align: center;
			height: 50px;
			padding-right: 25rpx;
		}

		.grid-text {
			font-size: 28rpx;
			margin-top: 4rpx;
			margin-bottom: 16rpx;
		}
	}
</style>