<template>
	<view>
		<wm-watermark :text="userInfo2.phone" :num="50"></wm-watermark>

		<view class="customer-list-wrap">
			<view v-if="customerList.length == 0" class="empty-wrap"><u-empty mode="list" text="暂无相关客户"></u-empty></view>
			<view v-for="(item, index) in customerList" class="customer-item" :key="index">
				<view class="info">
					<view class="customer">
						<view class="relname">
							{{ item.name }}
							<view style="font-size: 26rpx;display: inline-block;padding-left: 20rpx;" @click="dial(item.phone)">({{item.phone}})</view>
						</view>

						<view class="item"  @click="dial(item.create_user.phone)">
							报备人:{{item.create_user.nickname}}
							<u-icon class="u-m-r-5" name="phone" size="22"></u-icon>
							{{ item.create_user.phone }}
						</view>
						<view class="scoped-p1">
							报备时间:{{item.report_at}}
						</view>
						<view class="scoped-p1">
							到访时间:{{item.visit_at}}
						</view>
						<!-- <view class="item">
							<u-icon class="u-m-r-5" name="rmb" size="22"></u-icon>
							预期最高收益{{ item.commission }}元
						</view> -->
					</view>
					<view class="data-wrap">
						<view class="date u-m-t-5 u-m-b-20" v-if="item.referrer" @click="openReferrerPopup(item)">推荐人:{{item.referrer || '未填'}}</view>
						<!--  -->
						<u-tag
							@click="pageTo(`/pages/agent/report/deal?id=${item.id}&name=${item.name}-${item.phone}`)"
							text="审核"
							type="primary"
						></u-tag>
					</view>
				</view>
				<view class="scoped-list-img-more">
					<view class="slim-tips" @click="itemImgShowHandle(index)">
						<view class="li">{{item.estate_name}}</view>
						<view class="b">{{item.isImgShow ? '收缩图片<' : '展开图片>'}}</view>
					</view>
					<view class="slim-content" v-if="item.isImgShow">
						<view class="op">
							<view class="t">第三方水印</view>
							<image class="img" :src="item.img1" mode="aspectFit" @click="previewImgHandle(item.img1, [item.img1])"></image>
						</view>
						<view class="op">
							<view class="t">带看单</view>
							<image class="img" :src="item.img2" mode="aspectFit" @click="previewImgHandle(item.img2, [item.img2])"></image>
						</view>
						<view class="op" v-for="(moreUrl, moreI) in (item.more ? JSON.parse(item.more) : [])" :key="moreI">
							<view class="t" v-if="item.estate_report_conf">{{item.estate_report_conf.split(',')[moreI]}}</view>
							<image class="img" :src="moreUrl" mode="aspectFit" @click="previewImgHandle('more', JSON.parse(item.more), moreI)"></image>
						</view>
					</view>
				</view>
			</view>
		</view>

		<view class="float-search" @click="searchPopupShow = true"><u-icon name="search" size="42" color="#fff"></u-icon></view>

		<!-- 平台佣金政策弹窗 -->
		<u-popup v-model="searchPopupShow" mode="center" width="80%" height="440rpx" border-radius="20">
			<view class="bwin-popup">
				<view class="popup-header">用户搜索</view>
				<view class="popup-body">
					<u-input v-model="searchFormData.nickname" border placeholder="请输入昵称(支持模糊搜索)"></u-input>
					<u-divider marginTop="10" marginBottom="10">或</u-divider>
					<u-input v-model="searchFormData.phone" border placeholder="请输入手机号(可仅输入一部分)"></u-input>
				</view>
				<view class="popup-footer" style="position: absolute;">
					<u-button size="medium" @click="searchPopupShow = false">取消</u-button>
					<u-button size="medium" type="primary" @click="searchHandle()">搜索</u-button>
				</view>
			</view>
		</u-popup>
		
		<!-- 修改推荐人 -->
		<u-popup v-model="isReferrerShow" mode="center" width="80%" height="320rpx" border-radius="20">
			<view class="bwin-popup">
				<view class="popup-header">修改{{curObj.nickname}}的推荐人</view>
				<view class="popup-body">
					<u-input v-model="curReferrer" border placeholder="请输入推荐人"></u-input>
				</view>
				<view class="popup-footer" style="position: absolute;">
					<u-button size="medium" @click="isReferrerShow = false">关闭</u-button>
					<u-button size="medium" type="primary" @click="closeReferrerPopup()">确定</u-button>
				</view>
			</view>
		</u-popup>

		<u-loadmore
			v-if="customerList.length > 0"
			marginTop="32"
			:line="true"
			:status="loadmore.status"
			:loading-text="loadmore.loadingText"
			:loadmore-text="loadmore.defaultText"
			:nomore-text="loadmore.nomoreText"
		/>
	</view>
</template>
<script>
import { arrToObj } from '@/utils'
import wmWatermark from '@/components/wm-watermark/wm-watermark.vue'
import uniCopy from '@/js_sdk/xb-copy/uni-copy.js'
export default {
	components: {
		wmWatermark
	},
	data() {
		return {
			isReferrerShow: false,
			curReferrer: '',
			curObj: {},
			searchKeyword: null,
			searchPopupShow: false,
			searchFormData: {
				name: '',
				phone: ''
			}, // 搜索栏数据
			orderMethod: 0,
			filterDropdownValue: {}, // 默认筛选
			customerList: [], // 客户列表
			stepList: ['审核中', '未到访', '已到访', '已认购', '已签约', '已结佣'],
			loadmore: {
				status: 'loadmore',
				loadingText: '努力加载中',
				defaultText: '轻轻上拉 查看更多',
				nomoreText: '实在没有了',
				currnetPage: 1
			},
			curRoles: '',
			userInfo2: {},
			authStateObj: {},
			groupTypeObj: {},
		};
	},
	onLoad(params) {

		this.getDataList()
		
		this.userInfo2 = uni.getStorageSync('MD_userInfo2')
		this.curRoles = uni.getStorageSync('MD_userInfo2') ? uni.getStorageSync('MD_userInfo2').roles : ''
		
		const dictObj = uni.getStorageSync('MD_dict')
		this.authStateObj = arrToObj(dictObj.auth_state)
		this.groupTypeObj = arrToObj(dictObj.group_type)
	},
	onReachBottom() {
		if (this.loadmore.status == 'nomore') return;
		this.loadmore.currnetPage++
		this.getDataList()
	},
	onPullDownRefresh() {
		// 防止频繁刷新
	},
	methods: {
		openReferrerPopup (item) {
			this.curReferrer = item.referrer
			this.curObj = item || {}
			this.isReferrerShow = true
		},
		closeReferrerPopup () {
			// uni.api.estate.apireportsubscrDeal({
			// 	id: this.curObj.id,
			// 	referrer: this.curReferrer
			// }).then(res => {
			// 	uni.$msg('修改成功')
			// 	this.isReferrerShow = false
			// 	this.getDataList()
			// })
		},
		dial(tel) {
			uni.makePhoneCall({
				phoneNumber: tel
			});
		},
		previewImgHandle (current, urls, i){
			if (current === 'more') {
				uni.previewImage({
					current: urls[i],
					urls: [urls[i]]
				})
			} else {
				uni.previewImage({
					current,
					urls
				})
			}
		},
		itemImgShowHandle (index) {
			let list = this.customerList || []
			list[index].isImgShow = !list[index].isImgShow
			this.customerList = JSON.parse(JSON.stringify(list))
		},
		copyTextHandle (item) {
			uniCopy({
				content: `${item.name}-${item.sex == 'male'?'男':'女'}-${item.phone}`,
				success:(res)=>{
					uni.showToast({
						title: res,
						icon: 'none'
					})
				},
				error:(e)=>{
					uni.showToast({
						title: e,
						icon: 'none',
						duration:3000,
					})
				}
			})
		},
		getDataList (bc) {
			const that = this
			let params = {
				// auth_state: 2,
				...this.searchFormData
			}
			
			if (that.orderMethod > 0) {
				if (that.orderMethod === 1) {
					params.order_by = JSON.stringify([{
						field: 'create_at',
						sort: 'desc',
					}])
				}
				if (that.orderMethod === 2) {
					params.order_by = JSON.stringify([{
						field: 'create_at',
						sort: 'asc',
					}])
				}
			}
			uni.api.estate.apireportsubscrlist({
				page: that.loadmore.currnetPage,
				...params,
			}).then(res => {
				let list = res.list || []
				list.map(item => {
					item.isImgShow = false
				})
				if (list.length < 10) {
					that.loadmore.status = 'nomore';
				}
				if (res.current_page === 1) {
					if (list.length === 0) {
						uni.$msg('暂无待审核人员', 'none');
					}
					that.customerList = [...list]
				} else {
					that.customerList = that.customerList.concat(list)
				}
				if (bc) bc()
			})
		},
		customBack() {
			uni.navigateBack();
		},
		// 带监听器跳转
		pageTo(url, data) {
			/// xxxxx
			uni.navigateTo({
				url: url,
				events: {
					// 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
					update: function(data) {
						// 当B页面运行 eventChannel.emit('update', { a:1 }); 时，会运行这里的代码逻辑。
					}
				},
				success: function(res) {
					// 通过eventChannel向被打开页面传送数据
					res.eventChannel.emit('data', data);
				}
			});
		},
		searchHandle() {
			this.loadmore.currnetPage = 1
			this.getDataList(() => {
				this.searchPopupShow = false
			})
		},
		getTime(n) {
			let date = n ? new Date(n) : new Date()
			let year = date.getFullYear()
			let month = date.getMonth() + 1
			month = month > 9 ? month : '0' + month
			let day = date.getDate()
			day = day > 9 ? day : '0' + day
			let hour = date.getHours()
			hour = hour > 9 ? hour : '0' + hour
			let minute = date.getMinutes()
			minute = minute > 9 ? minute : '0' + minute
			let second = date.getSeconds()
			second = second > 9 ? second : '0' + second
			return `${year}-${month}-${day} ${hour}:${minute}:${second}`
		},
	}
};
</script>
<style lang="scss">
.search-wrap {
	padding: 20rpx;
}

.filter-wrap {
	position: relative;
}

// 列表
.customer-list-wrap {
	width: 100%;

	.customer-item {
		display: flex;
		flex-direction: column;
		align-items: flex-start;
		font-size: $u-p2;
		color: $u-content-color;
		border-bottom: 1rpx solid $u-border-color;
		padding: 24rpx 32rpx;

		.info {
			display: flex;
			align-items: flex-start;
			justify-content: space-between;
			width: 100%;
			margin-bottom: 10rpx;

			.customer {
				display: flex;
				flex-direction: column;
				align-items: flex-start;

				.relname {
					color: $u-main-color;
					font-size: $u-p;
					font-weight: bold;
					margin-bottom: 10rpx;
				}

				.item {
					margin-bottom: 10rpx;
				}
			}

			.data-wrap {
				text-align: right;
				.date {
					font-size: $u-p;
				}
			}
		}

		.tool-wrap {
			display: flex;
			align-items: center;
			justify-content: space-between;
			width: 100%;
		}
	}
}

.float-search {
	opacity: 0.9;
	position: fixed;
	right: 20rpx;
	bottom: 40rpx;
	padding: 20rpx;
	border-radius: 50%;
	background-color: $u-theme-color;
}

.scoped-list-more-info {
	position: absolute;
	right: 20rpx;
}


.scoped-list-img-more {
	.slim-tips {
		padding-left: 120rpx;
		position: relative;
		padding-bottom: 10rpx;
		&.pl0 {
			padding-left: 0;
		}
		.b {
			position: absolute;
			left: 0;
			top: 0;
			color: #2080f0;
		}
	}
	.op {
		display: inline-block;
		vertical-align: middle;
		margin-right: 10rpx;
		margin-bottom: 10rpx;
		border-radius: 10rpx;
		text-align: center;
		border: 1PX dotted #dcdcdc;
		padding: 10rpx;
		&:last-child {
			margin-right: 0;
		}
		.img {
			height: 130rpx;
			width: 130rpx;
		}
	}
}
</style>
