<template>
	<view class="container qualifications" :style="{ paddingBottom: isIphoneX ? '68rpx' : '0' }">
		<view class="remarks-content-club" @click="handleClickClunInfo">
			<view class="content-club-le">
				<view class="club-le-text">
					<text class="label">机构名称：</text> <text class="text">{{ clubInfo.name }}</text>
				</view>
				<view class="club-le-text">
					<text class="label">联系人：</text> <text class="text">{{ clubInfo.linkMan }}</text>
					<text class="label phone">手机号：</text> <text class="text">{{ clubInfo.contractMobile }}</text>
				</view>
				<view class="club-le-text">
					<text class="label">地址：</text>
					<text class="text" v-if="clubInfo.provincialAddress">{{ clubInfo.provincialAddress }}{{ clubInfo.address }}</text>
				</view>
			</view>
			<view class="content-club-ri"> <text class="iconfont icon-xiayibu"></text> </view>
		</view>
		<view class="remarks-content">
			<view class="list-view-title">
				<view class="list-view-h1"><text>*</text>咨询人：</view>
			</view>
			<view class="remarks-input">
				<input
					class="input"
					type="text"
					v-model="remarksParams.questionMan"
					value=""
					placeholder="请输入和你聊天的客户姓名"
					maxlength="15"
				/>
			</view>
			<view class="list-view-title">
				<view class="list-view-h1"><text>*</text>咨询类别：</view>
			</view>
			<view class="remarks-category">
				<view
					class="checkbox-list"
					:class="category.isChecked ? 'checked' : ''"
					v-for="(category, index) in categorys"
					:key="index"
					@click="choiceCategorys(category, index)"
				>
					<text class="iconfont" :class="category.isChecked ? 'icon-yixuanze' : 'icon-weixuanze'"></text>
					{{ category.className }}
				</view>
			</view>
			<view class="list-view-title">
				<view class="list-view-h1"><text>*</text>关键词记录：</view>
			</view>
			<view class="remarks-textarea">
				<textarea
					class="textarea"
					v-model="remarksParams.remarks"
					value=""
					placeholder="请总结你和客户的聊天内容，以关键词形式填入框内，关键词之间用中文逗号隔开"
					maxlength="500"
					@input="conInput"
				/>
				<text class="limit-text">{{ min }}/{{ max }}</text>
			</view>
			<view class="list-view-title">
				<view class="list-view-h1">上传图片</view>
				<view class="list-view-p">(可上传与客户的聊天截图或其他重要图片资料，最多10张)</view>
			</view>
			<view class="list-view-upload clearfix">
				<view class="photo-item" v-for="(image, imageIndex) in remarksParams.imageList" :key="imageIndex">
					<image
						:src="image"
						mode="aspectFill"
						@click.stop="previewImg(remarksParams.imageList, imageIndex)"
					></image>
					<text
						class="iconfont icon-iconfontguanbi"
						@click.stop="deletePhotoFn(remarksParams.imageList, imageIndex)"
					></text>
				</view>
				<view
					class="photo-item add"
					@click.stop="uploadPhotoFn(remarksParams.imageList)"
					v-if="remarksParams.imageList.length < 10 || remarksParams.imageList.length == 0"
				>
					<text class="iconfont icon-jiahao"></text>
				</view>
			</view>
			<view class="list-view-title">
				<view class="list-view-h1">上传文件</view>
				<view class="list-view-p">(可上传与客户相关的文件资料，最多10份，支持word,excel,ppt和pdf格式文件)</view>
			</view>
			<view class="list-view" v-for="(file, fileIndex) in remarksParams.fileList" :key="fileIndex">
				<view class="list-view-text">
					<view class="input">{{ file.fileName }}</view>
					<view class="delbtn" @click.stop="deleteFileFn(remarksParams.fileList, fileIndex)">删除</view>
				</view>
			</view>
			<view class="list-view">
				<view class="list-view-file" @click="uploadFile(remarksParams.fileList)">选择文件</view>
			</view>
		</view>
		<view class="remarks-btn" :style="{ paddingBottom: isIphoneX ? '68rpx' : '34rpx' }">
			<view class="edit-button" @click="editButtonConfim">确定</view>
		</view>
	</view>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import authorize from '@/common/config/authorize.js'
import { uploadFileImage, uploadFilePdfDocDocxXlsx } from '@/services/public.js'

var isPreviewImg
export default {
	data() {
		return {
			isIphoneX: this.$store.state.isIphoneX,
			shopOrderId: 0,
			logisticsBatchId: 0,
			productActions: [],
			remarksParams: {
				clubId: 0,
				questionMan: '',
				remarks: '',
				fileList: [],
				imageList: [],
				serviceProviderId: 0,
				consult:''
			},
			min: 0,
			max: 500,
			handleType: '',
			clubUserId: 0,
			clubInfo: {},
			userInfo: {},
			checkedCategorysList:[],
			categorys:[],
			isConfirmLoding:false
		}
	},
	onLoad(option) {
		console.log(option)
		if (option.type == 'edit') {
			this.getUserClubConsults()
			this.getUserClubRemarksDetail(option.remarksId)
			this.handleType = option.type
			uni.setNavigationBarTitle({ title: '修改记录' })
		} else {
			this.clubUserId = option.userId
			this.getCulbInfo()
			this.getUserClubConsults()
		}
	},
	methods: {
		...mapMutations(['login']),
		getCulbInfo() {
			// 查询机构信息
			this.UserService.OrganizationUpdateModifyInfo({ userId: this.clubUserId })
				.then(response => {
					this.clubInfo = response.data.club
					this.userInfo = response.data.user
					this.remarksParams.clubId = this.clubInfo.clubId
					if(this.handleType != 'edit'){
						this.remarksParams.questionMan = this.clubInfo.linkMan
					}
				})
				.catch(error => {
					console.log('=============>', error.msg)
				})
		},
		getUserClubRemarksDetail(remarksId) {
			//修改回显资料备注信息
			this.UserService.getUserClubRemarksDetail({
				remarksId: remarksId
			})
				.then(response => {
					let data = response.data
					this.clubUserId = data.userId
					this.remarksParams.remarks = data.remarks.join('，')
					this.remarksParams.remarksId = data.remarksId
					this.remarksParams.fileList = data.fileList
					this.remarksParams.imageList = data.imageList
					if(this.handleType == 'edit'){
						this.remarksParams.questionMan = data.questionMan
						this.remarksParams.consult = data.consult
						this.checkedCategorysList = data.consult.split(',').map(i => parseInt(i, 0))
						this.categorys = this.categorys.map((el, index) => {
							if(data.consult.includes(el.id)){
								el.isChecked = true
							}else{
								el.isChecked = false
							}
							return el
						})
					}
					this.getCulbInfo()
				})
				.catch(error => {
					this.$util.msg(error.msg, 2000)
				})
		},
		getUserClubConsults() {
			//查询咨询类别
			this.UserService.getUserClubConsults()
				.then(response => {
					this.categorys = response.data.map((el, index) => {
						el.isChecked = false
						return el
					})
				})
				.catch(error => {
					console.log('=========>获取咨询类别列表失败')
				})
		},
		async editButtonConfim() {
			//保存资料备注
			if(this.isConfirmLoding){return}
			const userInfo = await this.$api.getStorage()
			if (this.remarksParams.questionMan == '') {
				this.$util.msg('请输入咨询人姓名', 2000)
				return
			}
			if (this.remarksParams.consult == '') {
				this.$util.msg('请选择咨询类别', 2000)
				return
			}
			if (this.remarksParams.remarks == '') {
				this.$util.msg('请输入关键词记录', 2000)
				return
			}
			this.remarksParams.serviceProviderId = userInfo.serviceProviderId ? userInfo.serviceProviderId : 0
			this.isConfirmLoding = true
			this.UserService.getUserClubRemarksSave({
				params: JSON.stringify(this.remarksParams)
			})
				.then(response => {
					this.$util.msg('保存成功', 3000, true, 'success')
					this.isConfirmLoding = false
					setTimeout(() => {
						uni.navigateBack({
							delta: 1
						})
					}, 1000)
				})
				.catch(error => {
					this.$util.msg(error.msg, 2000)
					this.isConfirmLoding = false
				})
		},
		choiceCategorys(category,index){
			// 选择类别
			category.isChecked = !category.isChecked
			if (category.isChecked) {
				if(!this.contains(this.checkedCategorysList,category.id)){
					this.checkedCategorysList.push(category.id)
				}
			} else {
				this.checkedCategorysList.splice(this.checkedCategorysList.indexOf(category.id),1)
			}
			this.remarksParams.consult = this.checkedCategorysList.join(',')
		},
		contains(arr, val) {// 校验
			return arr.some(item => item === val)
		},
		uploadFile(array) {
			//上传资质文件
			console.log(array)
			uploadFilePdfDocDocxXlsx()
				.then(res => {
					let data = JSON.parse(res.data).data
					let obj = {
						fileName: uni.getStorageSync('fileName'),
						ossName: data.ossName
					}
					array.push(obj)
					console.log('array', array)
				})
				.catch(err => {
					console.log(err)
				})
		},
		uploadPhotoFn(array) {
			//添加图片
			uploadFileImage().then(res => {
				array.push(JSON.parse(res.data).data)
			})
		},
		deleteFileFn(array, index) {
			console.log(array)
			//删除文件
			this.UploadService.PostFileDelete({
				ossName: array[index].ossName
			})
				.then(res => {
					array.splice(index, 1)
				})
				.catch(error => {
					console.log('删除文件异常提示===>', error.msg)
				})
		},
		deletePhotoFn(array, index) {
			//删除图片
			array.splice(index, 1)
		},
		previewImg(image, index) {
			//顶部商品图片预览
			isPreviewImg = true
			let previewUrls = image
			uni.previewImage({
				current: index, //图片索引
				urls: previewUrls, //必须是http图片，本地图片无效
				longPressActions: ''
			})
		},
		conInput(e) {
			//备注文字字数限制
			let value = e.detail.value
			let len = parseInt(value.length)
			if (len > this.max) return
			this.min = len
			if (this.min == 200) {
				this.$util.msg('您输入的字数已达上限', 2000)
			}
		},
		handleClickClunInfo() {
			//修改机构资料
			if (this.userInfo.userIdentity === 2) {
				this.$api.navigateTo(`/pages/seller/login/apply?userID=${this.userInfo.userId}`)
			} else if (this.userInfo.userIdentity === 4) {
				this.$api.navigateTo(`/pages/seller/login/information?userID=${this.userInfo.userId}`)
			}
		}
	},
	onShow() {}
}
</script>

<style lang="scss">
page {
	height: auto;
	background: #ffffff;
}
.remarks-content-club {
	width: 100%;
	height: 252rpx;
	padding: 24rpx;
	box-sizing: border-box;
	border-bottom: 20rpx solid #f7f7f7;
	.content-club-le {
		width: 660rpx;
		float: left;
		.club-le-text {
			width: 100%;
			height: 60rpx;
			line-height: 60rpx;
			font-size: $font-size-28;
			color: #333333;
			.label {
				color: #999999;
				&.phone {
					margin-left: 48rpx;
				}
			}
		}
	}
	.content-club-ri {
		width: 40rpx;
		height: 160rpx;
		float: right;
		line-height: 160rpx;
		text-align: center;
		color: #b2b2b2;
	}
}
.remarks-content {
	width: 100%;
	height: auto;
	box-sizing: border-box;
	padding: 0 24rpx;
	padding-bottom: 160rpx;
	.list-view-title {
		width: 100%;
		height: auto;
		margin-bottom: 16rpx;
		margin-top: 40rpx;
		.list-view-h1 {
			line-height: 40rpx;
			font-size: $font-size-28;
			color: #333333;
			text-align: left;
			text {
				color: #ff2a2a;
			}
		}
		.list-view-p {
			line-height: 30rpx;
			color: #fea785;
			font-size: $font-size-20;
		}
	}
	.remarks-category{
		width: 100%;
		float: left;
		.checkbox-list {
			height: 60rpx;
			font-size: $font-size-28;
			line-height: 60rpx;
			border-radius: 10rpx;
			margin-right: 20rpx;
			margin-bottom: 10rpx;
			box-sizing: border-box;
			float: left;
			.icon-yixuanze{
				margin-right: 10rpx;
				color: #E15616;
			}
			.icon-weixuanze{ 
				margin-right: 10rpx;
				color: #B2B2B2;
			}
		}
		.item-text {
			display: inline-block;
			font-size: 26rpx;
			color: #333333; 
			border-radius: 28rpx;
			line-height: 50rpx;
		}
	}
	.remarks-input {
		width: 100%;
		height: 90rpx;
		padding: 0 16rpx;
		margin: 20rpx 0 0 0;
		border-radius: 6rpx;
		position: relative;
		border: 1px solid #b2b2b2;
		box-sizing: border-box;
		.input {
			width: 100%;
			height: 90rpx;
			line-height: 90rpx;
			font-size: $font-size-26;
			color: $text-color;
			z-index: 1;
		}
	}
	.remarks-textarea {
		width: 100%;
		height: 340rpx;
		padding: 16rpx;
		margin: 20rpx 0 0 0;
		border-radius: 6rpx;
		position: relative;
		border: 1px solid #b2b2b2;
		box-sizing: border-box;
		.textarea {
			width: 100%;
			height: 100%;
			line-height: 36rpx;
			font-size: $font-size-26;
			color: $text-color;
			z-index: 1;
		}
		.limit-text {
			position: absolute;
			right: 20rpx;
			bottom: 16rpx;
			line-height: 44rpx;
			font-size: $font-size-24;
			color: #b2b2b2;
		}
	}
	.list-view {
		width: 100%;
		height: 40rpx;
		margin-top: 20rpx;
		.list-view-file {
			width: 132rpx;
			height: 44rpx;
			line-height: 44rpx;
			font-size: $font-size-20;
			text-align: center;
			color: #ffffff;
			background-color: $color-system;
			border-radius: 8rpx;
			float: left;
			margin-top: 10rpx;
		}
		.list-view-text {
			width: 100%;
			float: left;
			.input {
				width: 560rpx;
				height: 44rpx;
				box-sizing: border-box;
				line-height: 44rpx;
				color: #333333;
				text-overflow: ellipsis;
				overflow: hidden;
				display: -webkit-box;
				-webkit-line-clamp: 1;
				line-clamp: 1;
				-webkit-box-orient: vertical;
				float: left;
			}
			.delbtn {
				width: 96rpx;
				height: 44rpx;
				border-radius: 8rpx;
				background-color: #fff2ec;
				font-size: $font-size-24;
				color: #e15616;
				line-height: 44rpx;
				text-align: center;
				float: left;
			}
		}
	}
	.list-view-upload {
		width: 100%;
		height: auto;
		.photo-item {
			display: inline-block;
			width: 112rpx;
			height: 112rpx;
			margin: 10rpx 0;
			margin-right: 25rpx;
			border-radius: 10rpx;
			border: 1px solid #f5f5f5;
			position: relative;
			float: left;
			&.add {
				width: 112rpx;
				height: 112rpx;
				border-color: #b2b2b2;
				text-align: center;
				line-height: 112rpx;
				margin-right: 0rpx;
				.icon-jiahao {
					font-size: $font-size-44;
					color: #b2b2b2;
					font-weight: bold;
				}
			}
			.icon-iconfontguanbi {
				width: 30rpx;
				height: 30rpx;
				border-radius: 50%;
				display: block;
				position: absolute;
				right: -10rpx;
				top: -10rpx;
				background: #f94b4b;
				text-align: center;
				line-height: 30rpx;
				color: #ffffff;
				font-size: $font-size-22;
			}
			image {
				width: 112rpx;
				height: 112rpx;
				border-radius: 10rpx;
			}
		}
		.photo-list {
			width: 100%;
			height: 116rpx;
			overflow: hidden;
			white-space: nowrap;
			display: flex;
			align-items: flex-start;
		}
	}
}
.remarks-btn {
	width: 100%;
	padding-top: 20rpx;
	position: fixed;
	bottom: 0;
	left: 0;
	background-color: #ffffff;
	.edit-button-canel {
		width: 100%;
		height: 88rpx;
		line-height: 88rpx;
		text-align: center;
		color: #e15616;
		font-size: $font-size-24;
	}
	.edit-button {
		width: 600rpx;
		height: 90rpx;
		background: $btn-confirm;
		line-height: 90rpx;
		text-align: center;
		color: #ffffff;
		font-size: $font-size-30;
		margin: 0 auto;
		border-radius: 45rpx;
	}
}
</style>
