<template>
	<view class="page-container">
		<view class="search-box">
			<view class="search-container">
				<view class="search-input-icon"></view>
				<input class="search-input" :value="keywords" @input="inputKeywords" @confirm="doSearchKeyWord"
					type="text" confirm-type="search" placeholder="搜索" />
				<view v-if="keywords !=''" @click="clearSearch" class="icon-clear" style="width: 20px;height: 20px;">
				</view>
			</view>
			<view class="add-btn" @click="showCreateDialog">创建</view>
		</view>
		<ContainerLoading :loadingVisible="loadingState" style="height: 83vh;margin-top: 30rpx;"
			@doRefresh="doRefreshList">
			<scroll-view scroll-y style="height: 83vh;" :lower-threshold="100" refresher-background="#FFFFFF00"
				@scrolltolower="doLoadMore">
				<view class="contact-order-box">
					<block v-for="(item,index) in contactList" :key="index">
						<view class="order-item">
							<view class="contact-desc">
								<view class="contact-icon">
									<view class="ic-user" style="width: 24px;height: 24px;"></view>
									<view class="ml-10 name">{{item.name}}</view>
								</view>
								<view>{{item.phone}}</view>
								<view class="options-box">
									<view class="icon-edit-blue size-48 mr-10" @click="showEditDialog"
										:data-value="item">
									</view>
									<view class="icon-del-red size-48" @click="showDelDialog" :data-value="item">
									</view>
								</view>
							</view>
							<view class="service-desc ml-10">{{item.company}}</view>
							<view class="service-desc ml-10">专属客服 : {{item.csaNo}}</view>
						</view>
					</block>
					<uni-load-more :status="loadMore"></uni-load-more>
				</view>
			</scroll-view>
		</ContainerLoading>
	</view>
	<view>
		<!-- 确认弹窗 -->
		<uni-popup type="dialog" ref="confirmDialog">
			<uni-popup-dialog type="warning" cancelText="取消" confirmText="删除" title="警告" @confirm="doDelContact"
				:content="alertMsg"></uni-popup-dialog>
		</uni-popup>
		<!-- 创建或编辑弹窗 :is-mask-click="false" -->
		<uni-popup ref="popup" type="bottom" background-color="#fff" border-radius="15rpx 15rpx 0rpx 0rpx">
			<view class="bottom-dialog-container">
				<view class="dialog-title-box">
					<view class="icon-close size-48" @click="closePopup"></view>
					<view class="dialog-title-txt has-icon">{{title}}</view>
				</view>
				<view class="form-box">
					<view class="form-item">
						<text class="form-item-title star">姓&emsp;&emsp;名</text>
						<input adjust-position :cursor-spacing="50" type="text" confirm-type="next" :value="contactName"
							placeholder="请输入姓名" class="form-item-content" @input="inputContactName" />
					</view>
					<view class="form-item">
						<text class="form-item-title star">联系电话</text>
						<input adjust-position :cursor-spacing="50" :value="contactPhone" placeholder="请输入联系电话"
							class="form-item-content" @input="inputContactPhone" />
					</view>
					<view class="form-item">
						<view class="form-item-title">联系邮箱</view>
						<input adjust-position :cursor-spacing="50" type="text" confirm-type="next"
							:value="contactEmail" placeholder="请输入联系邮箱" class="form-item-content"
							@input="inputContactEmail" />
					</view>
					<view class="form-item">
						<text class="form-item-title">公&emsp;&emsp;司</text>
						<input adjust-position :cursor-spacing="50" type="text" confirm-type="next"
							:value="contactCompany" placeholder="请输入公司名称" class="form-item-content"
							@input="inputContactCompany" />
					</view>
				</view>
				<button v-if="isCreate" class="confirm-btn" @click="doSaveContact">{{btnTxt}}</button>
				<button v-if="!isCreate" class="confirm-btn" @click="doEditContact">{{btnTxt}}</button>
			</view>
		</uni-popup>
		<!-- 消息弹窗 -->
		<uni-popup type="message" ref="msg">
			<uni-popup-message :type="msgType" :message="msgTxt" :duration="2000"></uni-popup-message>
		</uni-popup>
	</view>
</template>

<script>
	import ContainerLoading from '@/components/container-loading.vue'
	import UserApi from '@/common/js/net/UserApi'
	import {
		isValidPhone,
		isValidEmail
	} from '@/common/js/validator.js'
	export default {
		components: {
			ContainerLoading
		},
		data() {
			return {
				contactList: [], //联系人列表
				pageData: {
					page: 1,
					rows: 10,
					keywords: ''
				}, //检索参数
				loadingState: 'loading', //加载状态
				listRefreshTrig: false, //list刷新状态
				isLoadMore: false, //加载更多的状态
				hasMore: true, //是否有更多数据
				keywords: '', //搜索关键字
				showCreateContact: false, //创建联系人
				contactCompany: '', //公司名称
				contactEmail: '', //联系邮箱
				contactName: '', //名字
				contactPhone: '', //联系电话
				showError: false,
				errorHint: '',
				showSuccess: false,
				successHint: '',
				csaNo: '', //客服号
				title: '创建联系人',
				btnTxt: '保存',
				isCreate: true, //创建还是编辑
				tempContact: null, //修改的联系人
				loadMore: 'more',
				alertMsg: '',
				msgType: 'info',
				msgTxt: ''
			}
		},
		onLoad(options) {
			uni.setNavigationBarTitle({
				title: '产权联系人'
			})
			uni.setNavigationBarColor({
				frontColor: '#000000', // 必写项，字体颜色仅支持#ffffff和#000000
				backgroundColor: '#F0F0F0', // 传递的颜色值，仅支持十六进制颜色
				animation: {
					duration: 500,
					timingFunc: 'linear'
				}
			})
			this.doGetCsaNo()
			this.doGetMineContactList()
		},
		onPullDownRefresh() {
			this.doRefreshList()
			uni.stopPullDownRefresh()
		},
		methods: {
			inputKeywords(e) {
				this.keywords = e.detail.value
			},
			inputContactName(e) {
				this.contactName = e.detail.value
			},
			inputContactPhone(e) {
				this.contactPhone = e.detail.value
			},
			inputContactEmail(e) {
				this.contactEmail = e.detail.value
			},
			inputContactCompany(e) {
				this.contactCompany = e.detail.value
			},
			//关闭弹窗
			closePopup() {
				this.$refs.popup.close()
			},
			//显示创建联系人
			showCreateDialog() {
				this.title = '创建联系人'
				this.isCreate = true
				this.$refs.popup.open()
			},
			//创建联系人
			doSaveContact() {
				//校验参数
				const _self = this
				if (_self.contactName == '') {
					_self.msgTxt = '请输入联系人姓名'
					_self.msgType = 'error'
					_self.$refs.msg.open()
					return
				}
				if (_self.contactPhone == '' || !isValidPhone(_self.contactPhone)) {
					_self.msgTxt = '请输入正确的联系电话'
					_self.msgType = 'error'
					_self.$refs.msg.open()
					return
				}
				if (_self.contactEmail != '') {
					if (!isValidEmail(_self.contactEmail)) {
						_self.msgTxt = '请输入正确的邮箱'
						_self.msgType = 'error'
						_self.$refs.msg.open()
						return
					}
				}
				uni.showLoading({
					title: '创建中...',
				})
				_self.$refs.popup.close()
				const data = {
					company: _self.contactCompany,
					csaNo: _self.csaNo,
					name: _self.contactName,
					phone: _self.contactPhone,
					email: _self.contactEmail,
				}
				UserApi.doCreateContact(data)
					.then(res => {
						uni.hideLoading()
						_self.msgTxt = '新建成功'
						_self.msgType = 'success'
						_self.$refs.msg.open()
						_self.contactEmail = ''
						_self.contactPhone = ''
						_self.contactName = ''
						_self.contactCompany = ''
						//获取一遍联系人
						_self.doRefreshList()
					})
					.catch(err => {
						uni.hideLoading()
						_self.msgTxt = err.msg ? err.msg : '新建失败,请稍后重试'
						_self.msgType = 'error'
						_self.$refs.msg.open()
					})
			},
			//显示编辑联系人
			showEditDialog(e) {
				this.tempContact = e.currentTarget.dataset.value
				this.title = '编辑'
				this.isCreate = false
				this.contactCompany = this.tempContact.company
				this.contactName = this.tempContact.name
				this.contactEmail = this.tempContact.email
				this.contactPhone = this.tempContact.phone
				this.$refs.popup.open()
			},
			//保存修改联系人
			doEditContact() {
				//校验参数
				const _self = this
				if (_self.contactName == '') {
					_self.msgTxt = '请输入联系人姓名'
					_self.msgType = 'error'
					_self.$refs.msg.open()
					return
				}
				if (_self.contactPhone == '' || !isValidPhone(_self.contactPhone)) {
					_self.msgTxt = '请输入正确的联系电话'
					_self.msgType = 'error'
					_self.$refs.msg.open()
					return
				}
				if (_self.contactEmail != '') {
					if (!isValidEmail(_self.contactEmail)) {
						_self.msgTxt = '请输入正确的邮箱'
						_self.msgType = 'error'
						_self.$refs.msg.open()
						return
					}
				}
				uni.showLoading({
					title: '保存中...',
				})
				const data = {
					company: _self.contactCompany,
					csaNo: _self.tempContact.csaNo,
					name: _self.contactName,
					phone: _self.contactPhone,
					email: _self.contactEmail,
				}
				UserApi.doUpdateContactList(_self.tempContact.projContactId, data)
					.then(res => {
						uni.hideLoading()
						_self.$refs.popup.close()
						_self.msgTxt = '修改成功'
						_self.msgType = 'success'
						_self.contactEmail = ''
						_self.contactPhone = ''
						_self.contactName = ''
						_self.contactCompany = ''
						_self.tempContact = null
						//获取一遍联系人
						_self.doRefreshList()
					})
					.catch(err => {
						uni.hideLoading()
						_self.$refs.popup.close()
						_self.msgTxt = err.msg ? err.msg : '新建失败,请稍后重试'
						_self.msgType = 'error'
						_self.$refs.msg.open()
					})
			},
			//显示删除
			showDelDialog(e) {
				this.tempContact = e.currentTarget.dataset.value
				this.alertMsg = '一旦删除，该联系人信息将无法恢复，你仍要删除吗？'
				this.$refs.confirmDialog.open()
			},
			//删除联系人
			doDelContact() {
				const _self = this
				const id = _self.tempContact.projContactId
				uni.showLoading({
					title: '删除中...',
				})
				UserApi.doDelContact(id)
					.then(res => {
						uni.hideLoading()
						_self.msgTxt = '删除成功'
						_self.msgType = 'success'
						_self.$refs.msg.open()
						_self.doRefreshList()
					})
					.catch(err => {
						uni.hideLoading()
						_self.msgTxt = err.msg ? err.msg : '删除失败,请稍后重试'
						_self.msgType = 'error'
						_self.$refs.msg.open()
					})
			},
			//获取客服NO
			doGetCsaNo(isShow) {
				const _self = this
				UserApi.doGetCsaNo()
					.then(res => {
						console.log(res)
						_self.csaNo = res.csaNo
						_self.isShowContact = isShow
					}, err => {
						console.log(err)
					})
			},
			//获取我的联系人列表 isRefresh false 加载更多  true 刷新
			doGetMineContactList(isRefresh) {
				const _self = this
				_self.contactList = isRefresh ? [] : _self.contactList
				_self.loadingState = isRefresh ? 'loading' : ''
				UserApi.doGetMineContactList(_self.pageData)
					.then(res => {
						console.log(res)
						var status = 'success'
						status = res.rows && res.rows.length > 0 ? 'success' : 'empty'
						_self.loadingState = isRefresh ? status : ''
						_self.contactList = _self.contactList.concat(res.rows)
						_self.listRefreshTrig = false
						_self.isLoadMore = false
						_self.hasMore = _self.contactList.length < res.total
						_self.loadMore = _self.hasMore ? 'more' : 'noMore'
					})
					.catch(err => {
						_self.loadingState = 'error'
						_self.listRefreshTrig = false
						_self.isLoadMore = false
						_self.hasMore = true
						_self.loadMore = 'more'
					})
			},
			//清除搜索内容
			clearSearch() {
				const _self = this
				_self.keywords = ''
				_self.doRefreshList()
			},
			//发起搜索
			doSearchKeyWord() {
				const _self = this
				_self.doRefreshList()
			},
			//刷新列表
			doRefreshList() {
				console.log('正在刷新...')
				const _self = this
				_self.listRefreshTrig = true
				_self.loadingState = 'loading'
				_self.hasMore = true
				_self.pageData.page = 1
				_self.pageData.keywords = _self.keywords
				_self.isLoadMore = false
				_self.loadMore = 'more'
				_self.doGetMineContactList(true)
			},
			//加载更多
			doLoadMore() {
				//判断是否正在加载中 与是否存在更多数据
				const _self = this
				if (_self.isLoadMore || _self.loadMore == 'noMore') {
					return
				}
				_self.isLoadMore = true
				_self.loadMore = 'loading'
				_self.pageData.page = ++_self.pageData.page
				_self.keywords = _self.keywords
				_self.doGetMineContactList(false)
			},
		}
	}
</script>

<style lang="scss">
	.ic-user {
		background-image: url('data:image/svg+xml;charset=utf-8;base64,PHN2ZyB0PSIxNzQzOTg4MzkwNjkwIiBjbGFzcz0iaWNvbiIgdmlld0JveD0iMCAwIDEwMjQgMTAyNCIgdmVyc2lvbj0iMS4xIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHAtaWQ9IjM0MDkiIHdpZHRoPSIxNiIgaGVpZ2h0PSIxNiI+PHBhdGggZD0iTTUxMiAxMDI0QzIyOS4yMDUzMzMgMTAyNCAwIDc5NC43OTQ2NjcgMCA1MTJTMjI5LjIwNTMzMyAwIDUxMiAwczUxMiAyMjkuMjA1MzMzIDUxMiA1MTItMjI5LjIwNTMzMyA1MTItNTEyIDUxMnogbTAtNDk2LjQ2OTMzM2ExNzAuNjY2NjY3IDE3MC42NjY2NjcgMCAxIDAgMC0zNDEuMzMzMzM0IDE3MC42NjY2NjcgMTcwLjY2NjY2NyAwIDAgMCAwIDM0MS4zMzMzMzR6IG0yNjMuNzY1MzMzIDI2My43MjI2NjZhMjYzLjc2NTMzMyAyNjMuNzY1MzMzIDAgMSAwLTUyNy41MzA2NjYgMGg1MjcuNTMwNjY2eiIgcC1pZD0iMzQxMCIgZmlsbD0iIzEyOTZkYiI+PC9wYXRoPjwvc3ZnPg==');
		background-size: cover;
		background-repeat: no-repeat;
	}


	.search-box {
		display: flex;
		flex-direction: row;
	}

	.search-container {
		align-self: center;
		border-radius: 5px;
		background-color: rgba(255, 255, 255, 1);
		font-family: -regular;
		padding: 5px;
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: baseline;
		flex: 1;
		margin-right: 15rpx;
	}

	.search-input {
		box-sizing: border-box;
		color: $text-color;
		font-size: 28rpx;
		align-self: center;
		text-align: center;
		flex: 1;
	}

	.search-input-icon {
		content: '';
		width: 20px;
		height: 20px;
		background-size: cover;
		background-image: url('data:image/svg+xml;charset=utf-8;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB2aWV3Qm94PSI2NCA2NCA4OTYgODk2IiB3aWR0aD0iMTQiIGhlaWdodD0iMTkiIHN0eWxlPSIiIGZpbHRlcj0ibm9uZSI+CiAgICAKICAgIDxnPgogICAgPHBhdGggZD0iTTkwOS42IDg1NC41TDY0OS45IDU5NC44QzY5MC4yIDU0Mi43IDcxMiA0NzkgNzEyIDQxMmMwLTgwLjItMzEuMy0xNTUuNC04Ny45LTIxMi4xLTU2LjYtNTYuNy0xMzItODcuOS0yMTIuMS04Ny45cy0xNTUuNSAzMS4zLTIxMi4xIDg3LjlDMTQzLjIgMjU2LjUgMTEyIDMzMS44IDExMiA0MTJjMCA4MC4xIDMxLjMgMTU1LjUgODcuOSAyMTIuMUMyNTYuNSA2ODAuOCAzMzEuOCA3MTIgNDEyIDcxMmM2NyAwIDEzMC42LTIxLjggMTgyLjctNjJsMjU5LjcgMjU5LjZhOC4yIDguMiAwIDAgMCAxMS42IDBsNDMuNi00My41YTguMiA4LjIgMCAwIDAgMC0xMS42ek01NzAuNCA1NzAuNEM1MjggNjEyLjcgNDcxLjggNjM2IDQxMiA2MzZzLTExNi0yMy4zLTE1OC40LTY1LjZDMjExLjMgNTI4IDE4OCA0NzEuOCAxODggNDEyczIzLjMtMTE2LjEgNjUuNi0xNTguNEMyOTYgMjExLjMgMzUyLjIgMTg4IDQxMiAxODhzMTE2LjEgMjMuMiAxNTguNCA2NS42UzYzNiAzNTIuMiA2MzYgNDEycy0yMy4zIDExNi4xLTY1LjYgMTU4LjR6IiBmaWxsPSJyZ2JhKDIwNCwyMDQsMjA0LDEpIj48L3BhdGg+CiAgICA8L2c+CiAgPC9zdmc+');
		background-repeat: no-repeat;
	}


	.add-btn {
		border-radius: 4px;
		background-color: $btn-green-color;
		color: $white-color;
		font-size: 24rpx;
		line-height: 28rpx;
		height: 28rpx;
		align-self: center;
		padding: 15rpx 15rpx;
		text-align: center;
	}

	.add-btn:active {
		background-color: $btn-green-color-active;
	}

	.content-container {
		height: 86vh;
		margin-top: 30rpx;
	}


	.contact-order-box {
		display: flex;
		flex-direction: column;
		justify-content: center;
	}

	.order-item {
		display: flex;
		flex-direction: column;
		background-color: white;
		border-radius: 15rpx;
	}

	.order-item:nth-of-type(n+2) {
		margin-top: 15px;
	}

	.contact-desc {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		padding: 15px;
		align-items: center;
	}

	.contact-icon {
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
	}

	.name {
		line-height: 23rpx;
		color: rgba(0, 0, 0, 1);
		font-size: 32rpx;
		text-align: left;
		font-weight: bold;
		font-family: SourceHanSansSC-black;
	}

	.service-desc {
		padding: 0px 15px 15px 15px;
		line-height: 20px;
		color: rgba(0, 0, 0, 1);
		font-size: 14px;
		text-align: left;
		font-family: SourceHanSansSC-regular;
	}

	.edit {
		line-height: 20px;
		border-radius: 4px;
		background-color: rgba(122, 196, 131, 0.42);
		color: rgba(255, 255, 255, 1);
		font-size: 24rpx;
		text-align: center;
		font-family: PingFangSC-regular;
		padding: 5rpx 10rpx;
	}

	.del {
		line-height: 20px;
		border-radius: 4px;
		background-color: rgba(247, 49, 42, 0.42);
		color: rgba(255, 255, 255, 1);
		font-size: 24rpx;
		text-align: center;
		font-family: PingFangSC-regular;
		padding: 5rpx 10rpx;
		margin-left: 5px;
	}

	.del:active {
		background-color: rgba(247, 49, 42, 0.7);
	}

	.options-box {
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
	}

	.edit:active {
		background-color: rgba(122, 196, 131, 0.6);
	}


	.form-box {
		display: flex;
		flex-direction: column;
		justify-content: center;
		font-size: 14px;
		margin-top: 30rpx;
	}

	.form-item {
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		align-items: center;
		padding: 10px;
	}

	.form-item-title {
		flex: .3;
		font-size: 28rpx;
		color: black;
		font-weight: 500;
		text-align: left;
	}


	.form-item-content {
		flex: .7;
		text-align: right;
		font-size: 28rpx;
		border: 10rpx;
		padding: 10rpx;
		background-color: $bg-gray-input-color;
	}

	.confirm-btn {
		color: white;
		height: 80rpx;
		line-height: 80rpx;
		border-radius: 4rpx;
		background-color: $btn-green-color;
		color: rgba(255, 255, 255, 1);
		font-size: 32rpx;
		text-align: center;
		font-family: PingFangSC-medium;
		width: 85vw;
		margin-top: 40rpx;
	}

	.confirm-btn:active {
		background-color: $btn-green-color-active;
	}
</style>