<template>
	<view>
		<!-- 状态栏 -->
		<status-nav :titleVal="'客户列表'" :statusTitle="true"></status-nav>
		<!-- 自定义二级分类 -->
		<!-- 列表 -->
		<view :style="{paddingTop: statusHeight+'px'}" class="pad-zy30 pad-x20">
			<view class="radius20 fon28 col3 mar-sx20">
				<view class="disac">
					<view @tap="openXial" class="disjbac width100 radius10 pad-zy20 xialak posir">
						<view class="" style="color: #B3B3B3;">{{category}}</view>
						<image :class="isZhuan?'zhuan':'nozhuan'" src="/static/public/sanj.png" mode="aspectFill"></image>
						<!-- 下拉列表 -->
						<view v-if="isZhuan" class="posia bacf radius10 xial-box">
							<input @tap.stop="entryEv" @confirm="searchEv" type="text" class="pad20" v-model="searchVal" placeholder="请输入昵称/姓名/手机" />
							<view class="pad-zy20 xial-item-box">
								<view @tap="chooseXia(indexl)" :class="xialCurrent==indexl?'pcol':''" v-for="(iteml,indexl) in xialone" :key="indexl">{{iteml.title}}</view>
							</view>
						</view>
					</view>
					<view @tap="searchEv" class="flexs tc mar-z30 colf radius10 customer-btn" :style="{background:publicColor}">搜索</view>
				</view>
			</view>
			<!-- 列表 -->
			<view v-if="dataList.length!=0" class="dis bacf radius10 pad20 mar-x20 posir" v-for="(item,index) in dataList" :key="index">
				<image @tap="goDetail(item.id)" lazy-load class="mar-y20 flexs" :src="item.imgSrc" style="width: 98rpx;height: 98rpx;" mode="aspectFill"></image>
				<view class="fon24 col3 width100 mar-x40" style="margin-bottom: 120rpx;">
					<view class="bold disjbac">
						<view class="fon28 clips1">{{item.name}}</view>
						<view class="flexs">来源：{{item.come}}</view>
					</view>
					<view style="margin: 15rpx 0;" @tap="copyPhone(item.phone)">联系方式：{{item.phone || '暂无'}}</view>
					<view>客服：{{item.service || '暂无'}}</view>
					<view class="col80 disjbac" style="margin: 15rpx 0 0 0;">
						<view>{{item.time}}</view>
						<view class="col3">{{item.qudao}}</view>
					</view>
				</view>
				<!-- 真实姓名 -->
				<view class="posia col3 fon24 radius10 tc disac" style="left: 20rpx;bottom: 100rpx;">
					姓名：
					<input v-if="item.allow" :auto-focus="item.allow" type="text" v-model="realName" style="width: 100rpx;text-align: left;" />
					<text v-else>{{item.real_name || '暂无'}}</text>
					<text @tap="updateName(item.id,item.real_name,index)" class="mar-z10 mar-y20 pcol flexs">{{item.updateText}}</text>
				</view>
				<!-- 分配客服 -->
				<!-- <view @tap="fenCustomer(index)" class="posia colf fon24 radius10 tc customer-btn" style="right: 20rpx;top: 80rpx;z-index: 1;" v-if="item.customer=='' || item.customer==null" :style="{background:publicColor}">分配客服</view> -->
				<view @tap="fenCustomer(index)" class="posia colf fon24 radius10 tc customer-btn" style="right: 20rpx;top: 80rpx;z-index: 1;" v-if="isFen" :style="{background:publicColor}">分配客服</view>
				<!-- 设置标签 -->
				<view @tap="tuneUpTag(index)" v-if="item.isTag" class="posia colf fon24 radius10 tc customer-btn" style="right: 20rpx;bottom: 20rpx;z-index: 1;" :style="{background:publicColor}">设置标签</view>
				<view class="fon24 col3 posia bold disac" style="bottom: 60rpx;left: 20rpx;right: 20rpx;"><text class="flexs">标签：</text>
					<block v-if="item.tags.length">
						<scroll-view scroll-x  style="max-width: 60%;">
							<view class="disac"><view v-for="(item1,tagIndex) in item.tags" :key="tagIndex" class="pcol pad-zy10 flexs">{{item1}}</view></view>
						</scroll-view>
					</block>
					<block v-else class="col9">无</block>
				</view>
				<view class="fon24 col3 posia clips1 bold" style="bottom: 20rpx;left: 20rpx;right: 20rpx;max-width: 70%;">详细来源：{{item.source_detail}}</view>
			</view>
			<view  v-if="dataList.length==0" class="disjcac fc" style="margin-top: 50%;">
				<image class="zanw-img" src="/static/public/nothing.png" mode="aspectFill"></image>
				<view class="fon24 col3">您搜索的内容暂无结果，换个关键词试试吧</view>
			</view>
			<!-- 分配客服弹框 -->
			<pu-po :category="xialtList" @chooseXiaT="chooseXiaT" :isXiala="true" :isShowT="isShowT" :clearVal="'暂不分配'" :comfrimVal="'立即分配'" @comfirmev="comfirmev" @cancleev="cancleev"></pu-po>
			<!-- 设置标签弹框 -->
			<!-- <pu-po :category="tagList" @chooseXiaT="chooseTag" :isXiala="true" :isTag="true" :isShowT="isShowTag" :clearVal="'暂不设置'" :comfrimVal="'立即设置'" @comfirmev="comfirmevTag" @cancleev="cancleevTag"></pu-po> -->
			<!-- 返回顶部 -->
			<!-- <back-top :showTop="showTop" @backTop="backTop"></back-top> -->
		</view>
		<!-- 底部客服 -->
		<public-customer :nbottom="100"></public-customer>
		<view v-if="isShowTag" class="disjcac posAll" @tap="closeXial">
			<view class="bacf radius20 width100 tank-box">
				<view class="pad-x20">
					<view class="mar-s30 mar-x40 tc">请选择标签</view>
					<view @tap.stop="openXia" class="disjbac radius10 pad-zy20 mar-zy20 xialak mar-x50 posir">
						<view class="col3 disac fon28 width100"><input class="width100 mar-y20" disabled v-model="tagStr"></view>
						<image :class="isZhuan1?'zhuan':'nozhuan'" src="/static/public/sanj.png" mode="aspectFill"></image>
						<!-- 下拉列表 -->
						<view v-if="isZhuan1" class="posia bacf radius10 xial-box">
							<!-- 标签下拉 -->
							<view class="pad-zy20 xial-item-box">
								<view @tap.stop="chooseXiaTag(index)" class="disac fon28 mar-sx10" :class="item.checked?'pcol':''" v-for="(item,index) in tagList" :key="index">
									<view v-if="" class="disac" style="transform: scale(.8);">
										<checkbox style="width: 60rpx;height: 60rpx;display: inherit;" color="#3875F6" :data-id="item.id" :checked="item.checked" />
									</view>
									{{item.title}}
								</view>
							</view>
						</view>
					</view>
				</view>
				<view class="disjb fon28 colf pad-x30 pad-zy30 tc">
					<view @tap="cancleevTag" class="pad-sx20 radius10 tank-btn" style="background-color: rgba(230, 230, 230,1);color: #000000;">暂不设置</view>
					<view @tap="comfirmevTag" :style="{background:publicColor}" class="pad-sx20 radius10 tank-btn">立即设置</view>
				</view>
			</view>
		</view>
		<!-- 用户信息授权，手机号授权 -->
		<auth-userInfo-mobileInfo></auth-userInfo-mobileInfo>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				category:'',
				dataList:[],
				showTop:false,
				isShowT:false,
				isXiala:true,
				isZhuan:false,
				isZhuan1:false,
				xialCurrent:0,
				xialone:['全部'],
				xialtList:[],
				staff_id:'',//员工ID
				customer_id:'',//客户ID
				page:1,
				size:10,
				total:'',
				isZanw:true,
				ntype:'all',
				tagList:[],
				tag_id:'',
				isShowTag:false,
				tagId:'',
				searchVal:'',
				tagIds:[],
				tagVal:[],
				tagStr:'',
				isFen:uni.getStorageSync('isFen'),
				realName:'',//姓名
			}
		},
		computed: {
			// 主题颜色
			publicColor() {
				return this.$store.state.publicColor
			},
			statusHeight() {
				return this.$store.state.statusHeight
			}
		},
		onPageScroll(e) {
		    e.scrollTop > 360 ? this.showTop = true : this.showTop = false
		},
		onReachBottom() {//触底事件
			// for (let i = 0; i < 4; i++) {
			// 	let obj = {imgSrc:'/static/public/like.png',name:'USER',come:'顾客分享',phone:'18888888888',customer:'恒美植发客服-01',time:'2021-07-15 14:25',qudao:'第二渠道'}
			// 	this.dataList.push(obj)
			// }
			if(this.total!=this.dataList.length){
				this.page++
				this.checkCL(this.ntype)
			} else {
				if(this.isZanw) this.$toolAll.tools.showToast('暂无更多列表数据')
				this.isZanw = false
			}
		},
		onLoad() {
			if(uni.getStorageSync('phone_active')!=0 && uni.getStorageSync('is_active')!=0){
				this.checkKF()//查询客服列表
				this.checkCL()//客户列表
				this.checkLY()//渠道来源
				this.obtainTag();//获取标签列表
			}
		},
		methods: {
			// 查看客户详情事件
			goDetail(id){
				uni.navigateTo({
					url:`/pagesB/customDetail/customDetail?id=${id}`
				})
			},
			// 修改客户昵称
			updateName(id,name,index){
				if(this.dataList[index].updateText=='修改'){
					this.dataList.forEach(item=>item.allow=false)
					this.dataList[index].allow = true;
					this.dataList[index].updateText = '确认修改';
					this.realName = name;
				} else {
					if(this.dataList[index].real_name != this.realName) {
						this.$requst.post('user/update-info',{customer_id:id,field:'real_name',value:this.realName}).then(res=>{
							this.$toolAll.tools.showToast('修改成功');
							this.dataList.forEach(item=>item.allow=false)
							this.dataList[index].updateText = '修改';
							this.checkCL();
						})
					} else {
						this.dataList.forEach(item=>item.allow=false)
						this.dataList[index].updateText = '修改';
					}
				}
			},
			// 复制电话号码
			copyPhone(e){
				uni.setClipboardData({
					data: e,//要被复制的内容
					success:(res) => {
						uni.showToast({title:'复制成功' })
					}
				});
			},
			// 关闭下拉
			closeXial(){
				// this.isShowTag = false;
				this.isZhuan1 = false;
			},
			openXia(){
				this.isZhuan1 = !this.isZhuan1
			},
			// 标签单选
			chooseXiaTag(index){
				let isexistence = this.tagIds.indexOf(this.tagList[index].id);
				this.tagList[index].checked = !this.tagList[index].checked;
				if(isexistence!=-1){
					this.tagIds.splice(isexistence,1);
					this.tagVal.splice(isexistence,1)
				} else {
					this.tagIds.push(this.tagList[index].id)
					this.tagVal.push(this.tagList[index].title)
				}
				this.tagStr = this.tagVal.join(',');
				this.tagId = this.tagIds.join(',');
				// 单选时，控制全选按钮是否选中
				if(this.tagIds.length==this.tagList.length){
					this.isAll = true;
				} else this.isAll = false;
				if(!this.tagIds.length) {
					this.tagStr = '请选择'
				}
			},
			obtainTag(){//获取标签列表
				this.$requst.post('user/tag-list').then(res=>{
					// console.log('查询客户列表：',res);
					if(res.code==0){
						this.tagList = [];
						if(res.data.length!=0){
							res.data.forEach(item=>{
								let xuan = false;
								let obj = {
									title:item.name,
									id:item.id,
									checked:xuan
								}
								this.tagList.push(obj)
							})
						}
					}
				},error=>{})
			},
			// 调起设置标签弹框
			tuneUpTag(index){
				// console.log('设置标签调起弹框');
				// 查询已有标签
				this.getTagsCur(this.dataList[index].id);
				this.isShowTag = true
				this.tagStr = '';
				this.tagId = '';
				this.tagList.forEach((item,v)=>{
					item.checked = false;
					if(this.dataList[index].tags.length) {
						this.dataList[index].tags.forEach(item1=>{
							if(item.title == item1){
								this.tagList[v].checked = true;
							}
						})
						if(item.checked){
							this.tagIds.push(item.id);
							this.tagVal.push(item.title)
						}
					}
				})
				this.tagStr = this.tagVal.join(',');
				this.tagId = this.tagIds.join(',');
				// 客服列表
				this.customer_id = this.dataList[index].id//客户ID
				if(this.tagList.length==0) this.tagList = ['暂无可设置的标签']
			},
			// 查询已有标签
			getTagsCur(id){
				this.tagVal = [];
				this.$requst.get('user/customer-tags',{customer_id:id}).then(res=>{
					if(res.code==0){
						console.log('已有标签：',res);
						this.tagIds = res.data.ids;
						let tagsArr = [];
						res.data.list.forEach(item=>{
							tagsArr.push(item.name);
						})
						this.tagVal = tagsArr;
					}
				},error=>{})
			},
			
			checkKF(){//查看客户列表
				this.$requst.post('user/servicer-list').then(res=>{
					// console.log('查询客户列表：',res);
					if(res.code==0){
						if(res.data.length!=0){
							res.data.forEach(item=>{
								let obj = {
									title:item.name,
									id:item.id
								}
								this.xialtList.push(obj)
							})
							this.staff_id = this.xialtList[0].id
						}
					}
				},error=>{})
			},
			searchEv(){//搜索事件
				this.page = 1;
				this.isZanw = true;
				this.checkCL(this.ntype)
			},
			checkLY(){//查询来源渠道事件
				this.$requst.post('user/channel').then(res=>{
					// console.log('来源渠道：',res);
					if(res.code==0){
						for (let key in res.data) {
							let lyObj = {
								title:res.data[key],
								name:key
							}
							this.xialone.push(lyObj)
						}
						this.xialone.unshift({title:"全部",name:'all'})
						this.category = this.xialone[0].title
					}
				},error=>{})
			},
			checkCL(ntype){//查询客户列表事件
				this.$requst.post('user/customer',{page:this.page,size:this.size,type:ntype,keyword:this.searchVal}).then(res=>{
					// console.log('客户列表：',res);
					if(res.code==0){
						if(this.page==1) this.dataList = []
						this.total = res.data.total
						if(res.data.list.length!=0){
							res.data.list.forEach(item=>{
								let cuObj = {
									id:item.id,
									imgSrc:item.headimgurl,
									name:item.nickname,
									come:item.channel_text,
									phone:item.mobile,
									customer:item.service[0],
									time:item.created_at,
									qudao:item.tag[0],
									service:item.service,
									source_detail:item.source_detail,
									tags:item.tag,
									isTag:!item.tag.includes('员工'),
									real_name:item.real_name,
									allow:false,
									updateText:'修改'
								}
								this.dataList.push(cuObj);
							})
						}
						this.isZhuan = false;
					} else this.$toolAll.tools.showToast(res.msg)
				},error=>{})
			},
			fenCustomer(index){//分配弹框调起事件
				// console.log('分配客服调起弹框');
				this.isShowT = true
				// 客服列表
				this.customer_id = this.dataList[index].id//客户ID
				if(this.xialtList.length==0) this.xialtList = ['暂无可分配客服']
			},
			backTop(){//回到顶部事件
			    uni.pageScrollTo({
			        scrollTop: 0,
			        duration: 300
			    });
			},
			comfirmev(){//立即分配执行事件
				// console.log('立即分配');
				this.isShowT = false
				this.$requst.post('user/customer-allot',{staff_id:this.staff_id,customer_id:this.customer_id}).then(res=>{
					// console.log('分配客服:',res);
					if(res.code==0){
						this.page = 1
						this.$toolAll.tools.showToast('分配成功')
						this.checkCL(this.ntype)//客户列表
					} else this.$toolAll.tools.showToast(res.msg)
				},error=>{})
			},
			// 立即设置标签
			comfirmevTag(){
				this.$toolAll.tools.showToast('设置中...')
				// console.log('立即分配');
				this.$requst.post('user/set-tags',{tag_ids:this.tagId,ids:this.customer_id}).then(res=>{
					// console.log('分配客服:',res);
					if(res.code==0){
						this.page = 1
						this.$toolAll.tools.showToast('设置成功')
						this.obtainTag();
						this.isShowTag = false
						this.checkCL(this.ntype)//客户列表
					} else this.$toolAll.tools.showToast(res.msg)
				},error=>{})
			},
			cancleev(){
				this.isShowT = false
			},
			// 暂不设置标签
			cancleevTag(){
				this.isShowTag = false
			},
			openXial(){
				this.isZhuan = !this.isZhuan
				this.searchVal = '';
				// console.log('开启下拉');
			},
			chooseXia(index){
				this.xialCurrent = index;
				this.ntype = this.xialone[index].name;
				this.category = this.xialone[index].title;
			},
			chooseXiaT(e){
				this.staff_id = e.id
			},
			entryEv(){
				this.isXiala = true;
			}
		}
	}
</script>

<style>
	
</style>
