<template>
	<view>
		<view class="topBgline plr20">
			<view class="justify-start p30 bgcolf radius12">
				<u-avatar :src='detailUser.avatar'></u-avatar>
				<view class="ml16">
					<view class="bold fs28">{{detailUser.name}}</view>
					<view class="fs24 base-color-3">备注：{{detailUser.remark}}</view>
				</view>
			</view>
		</view>
		<view class="bgf p20">
			<view>会员资料</view>
			<view class="justify-between">
				<view class="justify-start align-center fs24 mt20 widthpath" >
					<view class="base-color-6 mr40">性别</view>
					<view>{{moreInfo.sex?moreInfo.sex:'未知'}}</view>
				</view>
				<view class="justify-start align-center fs24 mt20 widthpath" >
					<view class="base-color-6 mr40">年龄</view>
					<view>{{moreInfo.age?moreInfo.age:'未知'}}</view>
				</view>
			</view>
			<view class="justify-start align-center fs24 mtb20">
				<view class="base-color-6 mr40">行为习惯</view>
				<view>{{moreInfo.habits?moreInfo.habits:'无'}}</view>
			</view>
			<view class="justify-start align-center fs24">
				<view class="base-color-6 mr40">患病时间</view>
				<view>{{moreInfo.illnessTime?moreInfo.illnessTime:'无'}}</view>
			</view>
			<view class="justify-start align-center fs24 mt20" >
				<view class="base-color-6 mr40">身体状态</view>
				<view>{{moreInfo.body?moreInfo.mebodydicine:'无'}}</view>
			</view>
			<view class="justify-start align-center fs24 mt20" >
				<view class="base-color-6 mr40">家人的疾病</view>
				<view>{{moreInfo.familyDisease?moreInfo.familyDisease:'无'}}</view>
			</view>
			<view class="justify-between align-center">
				<view class="justify-start align-center fs24 mt20 widthpath">
					<view class="base-color-6 mr40">是否线下就诊</view>
					<view>{{moreInfo.isLine?'已线下就诊':'无'}}</view>
				</view>
				<view class="justify-start align-center fs24 mt20 widthpath" >
					<view class="base-color-6 mr40 ">体质</view>
					<view>{{moreInfo.constitution?moreInfo.constitution:'无'}}</view>
				</view>
			</view>
			<view class="justify-start align-center fs24 mt20" >
				<view class="base-color-6 mr40">使用药品</view>
				<view>{{moreInfo.medicine?moreInfo.medicine:'无'}}</view>
			</view>
			<view class="justify-start align-center fs24 mt20" >
				<view class="base-color-6 mr40">咨询产品</view>
				<view>{{moreInfo.consultProduct?moreInfo.consultProduct:'无'}}</view>
			</view>
			<view class="justify-start align-center fs24 mt20" >
				<view class="base-color-6 mr40">是否已经购买产品</view>
				<view>{{moreInfo.isBuy?'已购买':'无'}}</view>
			</view>
		</view>
		<view class="center bgf fs28">看课记录</view>
		<view class="bgf p20">
			<view class="justify-start ">
				<view v-for="(item,index) in titname" :key="index" class="justify-start align-center ml28">
					<view :style="{ backgroundColor: statusColors[index] || '#CCCCCC' }" 
					class="w24 h24 radius4"></view>
					<view class="fs24 mr6 ml10">{{item}}</view>
				</view>
			</view>
			<view class="fs24 mt20 base-color-6" >近{{typestate==0?'7':'30'}}天看课记录</view>
			<view class="justify-between mt20">
				<view class="justify-start align-center mt12 " style="width: 80%;flex-wrap: wrap">
					<view class="w54 h54 mr20 radius4 mb12" v-for="(item,index) in answerlist" :key="index"
					>
						<view class="fs24 base-color-6">{{item.date.slice(-2)}}</view>
						<view :style="{ backgroundColor: statusColors[item.logType] || '#CCCCCC' }" 
						class="w30 h30 radius4"></view>
					</view>
				</view>
				<view class="u-border lh45 plr20 fs24 radius40 base-color-3 h50"
				 @click="todetail">{{typestate==0?'详情':'收起'}}</view>
			</view>
			<view class="pt120"></view>
		</view>
		<view class="justify-around botfun bgf">
			<view class="justify-start align-center bottom-btns base-bg" 
			@click="navTo('/pages/user/courseManage?userId='+userId)" >
				<view class="colorf ml12">课程管理</view>
			</view>
			<view class="justify-start align-center bottom-btns base-bg" 
			@click="navTo('/pages/user/UrgeClasses?userId='+userId)">
				<view class="colorf ml12" >催课管理</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getcustomerlist,
		getcustomer
	} from "@/api/user.js";
	import { getConfigSignature,getQwExternalContactDetails } from '@/api/common.js'
	export default {
		data() {
			return {
				typeOption: [{
					label: '全部',
					value: 0
				}, {
					label: '今天',
					value: 1
				}, {
					label: '昨天',
					value: 2
				}, {
					label: '前天',
					value: 3
				}, {
					label: '近七天',
					value: 4
				}],
				tabindex: 0,
				detailUser: [],
				user: [],
				answerlist:[],
				answerText:'禁用',
				id:'',
				dateTag:'',
				qwUserId:'',
				startTime:"",
				endTime:'',
				todayday: uni.$u.timeFormat(new Date(), 'yyyy-mm-dd'),
				moreInfo:{},
				statusColors: {
					0: '#909399',
					1: '#0bc6ff',
					2: '#67c23a',
					3: '#f55a4f',
					4: '#ffd700',
				  },
				titname:['未看课','看课中','完课','待看课','看课中断',],
				statelist:[
				{  status: 4 },
				{  status: 1 },
				{  status: 2 },
				{  status: 5 },
				{  status: 3 },
				{  status: 5 },
				{  status: 3 },
				  ],
				typestate:0,
				externalUserId:'',
				userId:''
			}
		},
		onShow() {
			this.getuser()
			// this.detailUser =uni.getStorageSync('detailUser')
			// if(this.detailUser.status==0){
			// 	this.answerText='取消禁用'
			// }
			this.qwUserId=uni.getStorageSync('qwUserId')
			this.$nextTick(()=>{
				setTimeout(()=>{
					this.getAnswerlists()
				},200)
			})
		},
		mounted() {
			
		},
		onLoad(option) {
			// this.id=option.id
			this.corpId=options.corpId;
			// uni.setStorageSync("corpId",this.corpId);
		},
		methods: {
			getQwExternalContactDetails(){
				var that=this
				var data = {
					corpId: this.corpId,
					userId:this.userId,
					externalUserId:this.externalUserId,
				};
				getQwExternalContactDetails(data).then(res => {
					if(res.code==200){
						this.user=res.data;
						
					}else{
						
					}
				});
			},
			getConfigSignature(){
				var data = {
					corpId: this.corpId,
					url: this.url,
					code: this.code
				};
				if(this.userId!=null){
					data.isGetUserId=0;
				}
				else{
					data.isGetUserId=1;
				}
				var that=this;
				// var obj={cmd:"log",data:"getConfigSignature开始"};
				// uni.$emit('sendMsg',obj); 
				getConfigSignature(data).then(
					res => {
						console.log(res)
						// var obj={cmd:"log",data:JSON.stringify(res)};
						// uni.$emit('sendMsg',obj); 
						if(res.code==200){
							that.isConfig=true;
							const agentConfigSignature = res.config.agentConfigSignature;
							const configSignature = res.config.configSignature;
							const corpId = res.config.corpId;
							const nonceStr = res.config.nonceStr;
							const timestamp = res.config.timestamp;
							const agentId = res.config.agentId;
							
							if(res.config.userid!=null&&res.config.userid!=''){
								that.userId = res.config.userid;//当前员工ID
								uni.setStorageSync("qwUserId",that.userId);
								console.log("获取到USERID")
							}
							else{
								console.log("不需要获取USERID")
							}
							console.log(this.userId)
							jWeixin.config({
								beta: true,
								debug: false,
								appId: corpId,
								timestamp: timestamp,
								nonceStr: nonceStr,
								signature: configSignature,
								jsApiList: ['getCurExternalContact','sendChatMessage','openEnterpriseChat']
							});
							// const newSignature={
							// 	'timestamp':timestamp,
							// 	'nonceStr':nonceStr,
							// 	'signature':configSignature,
							// }
							// ww.register({
							//   corpId: corpId,       // 必填，当前用户企业所属企业ID
							//   jsApiList: ['openEnterpriseChat'], // 必填，需要使用的JSAPI列表
							//   newSignature                 // 必填，根据url生成企业签名的回调函数
							// })
							jWeixin.ready(() => {
								jWeixin.agentConfig({
									corpid: corpId,
									agentid: agentId,
									timestamp: timestamp,
									nonceStr: nonceStr,
									signature: agentConfigSignature,
									jsApiList: ['getCurExternalContact','sendChatMessage','openEnterpriseChat'],
									success: (res) => {
										jWeixin.invoke('getCurExternalContact', {}, async (res) => {
											if (res.err_msg === "getCurExternalContact:ok") {
												that.externalUserId = res.userId;
												uni.setStorageSync("externalUserId",res.userId)
												that.getQwExternalContactDetails()
												// that.initInterval=setInterval(function(){
												// 	if(that.isInit){
												// 		clearInterval(that.initInterval);
												// 		that.getQwSopLogsByJsApi();
												// 	}
												// },200)
												
											} else {
												// 错误处理
											}
										});
										
										// jWeixin.openEnterpriseChat({
										// 		// 注意：userIds和externalUserIds至少选填一个。内部群最多2000人；外部群最多500人；如果有微信联系人，最多40人
										// 	externalUserIds: 'wmfFKfDQAAhvgwhJr9fqkRWjvN0CzFcA', // 参与会话的外部联系人列表，格式为userId1;userId2;…，用分号隔开。
										// 	groupName: '',  // 会话名称。单聊时该参数传入空字符串""即可。
										// 	chatId: "", // 若要打开已有会话，需指定此参数。如果是新建会话，chatId必须为空串
										// 	success: function(res) {
										// 		 var chatId = res.chatId; //返回当前群聊ID，仅当使用agentConfig注入该接口权限时才返回chatId
										// 		// 回调
										// 		that.externalUserId=res.chatId;
										// 		console.log(res)
										// 		setTimeout(function(){
										// 			jWeixin.invoke('sendChatMessage', {
										// 				msgtype:"news", //消息类型，必填
										// 				enterChat: true,
										// 				text: {
										// 					content:"你好11111111111111111", //文本内容
										// 				},
										// 				news:
										// 					{
										// 						link: "https://www.baidu.com/", //H5消息页面url 必填
										// 						title: "百度一下", //H5消息标题
										// 						desc: "百度一下你就知道", //H5消息摘要
										// 						imgUrl: "https://cos.his.cdwjyyh.com/fs/20240914/efc6995ee755489b8a3e0c8413846a4e.jpg", //H5消息封面图片URL
										// 					},
										// 			}, function(res) {
																				
										// 				if (res.err_msg == 'sendChatMessage:ok') {
										// 					//发送成功
										// 					uni.showToast({
										// 						title: "回车发送",
										// 						icon: 'none',
										// 					});
										// 				}
										// 			})
										// 		},500);
												
												
												
										// 	},
										// 	fail: function(res) {
										// 		if(res.errMsg.indexOf('function not exist') > -1){
										// 			alert('版本过低请升级')
										// 		}
										// 	}
										// });
									},
									fail: (res) => {
										console.error('agentConfig fail:', res);
									},
									
									
								});
							});
							jWeixin.error((res) => {
								
								console.error('wx.error:', res);
							});
							
						}
						else{
							// uni.setStorageSync("qwUserId",null);
							console.log("获取USERID失败")
							// setTimeout(function(){
							// 	window.location.reload();
							// },5000);
							
						}
						
					},
					rej => {
						setTimeout(function(){
							window.location.reload();
						},5000);
					}
				);
			},
			todetail(){
				if(this.typestate==0){
					this.typestate=1
				}else{
					this.typestate=0
				}
				this.getAnswerlists()
			},
			getuser(){
				const data={
					qwExternalContactId:this.externalUserId
				}
				getcustomer(data).then(res=>{
					if (res.code == 200) {
						this.detailUser=res.data
						this.moreInfo=res.moreInfo
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						})
					}
				})
			},
			getAnswerlists() {
				const params = {
					type:this.typestate,
					qwExternalContactId:this.externalUserId
				}
				getcustomerlist(params).then(res => {
					if (res.code == 200) {
						this.answerlist=res.data.data
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						})
					}
				})
			},
			navTo(url){
				uni.navigateTo({
					url
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.widthpath {
		width: 50%;
	}

	.topBgline {
		background: linear-gradient(to right, rgba(225, 238, 255, 1), rgba(223, 224, 254, 1));
		padding-top: 40rpx;
	}

	.bgcolf {
		background: rgba(255, 255, 255, 0.4);
	}

	.actsel {
		background-color: #1773ff;
		color: #fff;
		transition: background-color 0.4s ease-in-out;
	}

	.notact {
		background-color: #f8f8f8;
	}

	.livebox {
		width: calc(50% - 10rpx);
		background-color: #f8f8f8;
		padding: 20rpx 10rpx;
		border-radius: 12rpx;
	}

	.liveboxs {
		width: calc(33% - 10rpx);
		background-color: #f8f8f8;
		padding: 20rpx 10rpx;
		border-radius: 12rpx;
	}

	.bottom-btns {
		padding: 10rpx 40rpx;
		border-radius: 50rpx;
	}

	.botfun {
		position: fixed;
		width: 100%;
		bottom: 0rpx;
		padding: 20rpx 0;
	}
</style>