<template>
	<view class="cus-wrap-component">
		<image class="top-image" mode="widthFix" v-if="!isPop" :src="qnFile(imageName)"></image>
		<view class="content-view">
			<view :class="['title-view', {'is-mine': isMine}]">
				<text :class="['title-text', {'is-mine': isMine}]">{{titleText[typeIndex]}}</text>
				<text>{{tipText[typeIndex]}}</text>
			</view>
			<u-image :class="['qrcode', {'is-mine': isMine}]" :src="qnFile(cusQrcode)" mode="widthFix" :fade="false">
				<u-loading slot="loading"></u-loading>
				<view slot="error" style="font-size: 24rpx;">加载失败</view>
			</u-image>
			<!-- 我的页面进来下载图片，其他页面进来open-type为客服，跳转到客服页面 -->
			<button :class="['settle-button', {'is-mine': isMine}]" type="primary" :open-type="openTypes[typeIndex]" show-message-card
				send-message-title="点击下方客服会话添加订单助手" shape="circle" @click="download" :send-message-img="qnFile('kefuzxiaoxi.png')"
				send-message-path="/pages/index/index">
				{{buttonText[typeIndex]}}
			</button>
			<view class="bottom-view" v-if="isMine">
				<view class="line-view"></view>
				<view class="phone-view">
					<view class="phone-left-view">
						<text class="phone-text">客服电话</text>
						<text>{{phone}}</text>
					</view>
					<image class="phone-image" :src="phoneIcon" @click="phoneToCustomer">
						</u-image>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import util from '@/utils/util.js'
	export default {
		props: {
			type: { // exclusive：专属; mine:我的页面客服; pop:弹窗；
				type: String,
				default: 'mine'
			}
		},
		data() {
			return {};
		},
		computed: {
			cusQrcode() {
				return this.isMine ? 'erweima-rk.png' : 'dingdanzhushou-n.png';
			},
			imageName() {
				return this.isMine ? 'kefufuwu-n.png' : 'kefutuzs.png'
			},
			isMine() { // exclusive：专属; mine:我的页面客服; pop:弹窗；
				return this.type == 'mine'
			},
			isExclusive() {
				return this.type == 'exclusive'
			},
			isPop() {
				return this.type == 'pop'
			},
			phoneIcon() {
				return process.uniEnv.qn_base_url + 'dianhua.png'
			},
			types() {
				return ['exclusive', 'pop', 'mine']
			},
			typeIndex() {
				return this.types.indexOf(this.type)
			},
			phone() {
				return '18124099271'
			},
			openTypes() {
				return ['contact', 'contact', '']
			},
			buttonText() {
				return ['添加', '添加', '点击下载']
			},
			titleText() {
				return ['添加订单助手', '添加订单助手', '客服微信']
			},
			tipText() {
				return ['在对话框输入“加入”，即可获取二维码，添加订单助手', '在对话框输入“加入”，即可获取二维码，添加订单助手', '下载二维码添加官方客服微信']
			}
		},
		methods: {
			qnFile(src) {
				return process.uniEnv.qn_base_url + src
			},
			phoneToCustomer() { // 联系客户
				uni.makePhoneCall({
					phoneNumber: this.phone, //仅为示例
					complete() {}
				});
			},
			// 我的页面下载图片，其他页面要跳到客服页面
			download() {
				if (!this.isMine) {
					this.$emit('click')
					return
				}
				let self = this
				uni.downloadFile({ //获得二维码的临时地址
					url: self.cusQrcode,
					success: (result) => {
						if (result.statusCode == 200) {
							uni.saveImageToPhotosAlbum({
								filePath: result.tempFilePath, //传入临时地址
								success(e) {
									self.$u.toast('保存成功!');
									self.$emit('click')
								},
								fail(res) {
									if (res.errMsg.indexOf('auth') != -1) {
										uni.showModal({
											title: '提示',
											content: '获取权限失败，请到设置页面开启权限',
											confirmText: '好的',
											success(res) {
												if (res.confirm) {
													uni.openSetting({
														success(res) {
															console.log(res
																.authSetting)
														}
													});
												}

											}
										})
									} else if (res.errMsg) {
										self.$u.toast('保存失败!');
									}
								},
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.cus-wrap-component {
		background: #FFFFFF;
		border-radius: 12rpx;

		.top-image {
			width: 100%;
		}

		.content-view {
			padding: 30rpx;
			display: flex;
			flex-direction: column;
			align-items: center;

			.title-view {
				text-align: center;
				display: flex;
				justify-content: center;
				align-items: center;
				flex-direction: column;
				color: #999999;
				font-size: 28rpx;
				padding-left: 40rpx;
				padding-right: 60rpx;
				margin-top: 20rpx;

				&.is-mine {
					display: inline;
					font-size: 24rpx;
					color: #666666;
					width: 100%;
					margin-top: 10rpx;
				}

				.title-text {
					font-weight: bold;
					color: #333333;
					font-size: 40rpx;
					margin: 0 0 30rpx 0;

					&.is-mine {
						font-size: 32rpx;
						margin-right: 30rpx;
					}
				}
			}

			.qrcode {
				width: 600rpx;
				height: 440rpx;
				margin-top: 30rpx;
				margin-bottom: 40rpx;

				&.is-mine {
					margin-top: 60rpx;
					margin-bottom: 60rpx;
					width: 400rpx;
					height: 400rpx;
				}
			}

			.desc {
				margin-top: 40rpx;
				font-size: 24rpx;
				font-weight: 400;
				color: #666666;
				line-height: 24rpx;
			}

			.bottom-view {
				width: 100%;

				.line-view {
					background-color: #F4F5F7;
					height: 2rpx;
					width: 100%;
					margin-top: 46rpx;
				}

				.phone-view {
					display: flex;
					align-items: center;
					width: 100%;
					justify-content: space-between;
					padding: 40rpx 0 30rpx 0;

					.phone-left-view {
						display: flex;
						align-items: center;
						color: #999999;
						font-size: 26rpx;

						.phone-text {
							color: #333333;
							font-size: 32rpx;
							font-weight: bold;
							margin-right: 40rpx;
						}
					}

					.phone-image {
						width: 84rpx;
						height: 56rpx;
					}
				}
			}

			.settle-button {
				margin-bottom: 14rpx;
				width: 400rpx;
				height: 104rpx;
				font-size: 32rpx;
				background-color: #2272FF;
				border-radius: 52rpx;
				line-height: 104rpx;
				font-weight: bold;
				&.is-mine {
					width: 300rpx;
					height: 80rpx;
					border-radius: 40rpx;
					line-height: 80rpx;
				}
			}

			.pop-tip-view {
				text-align: center;
				color: #333333;
				font-size: 28rpx;
				margin: 0rpx 60rpx 40rpx 60rpx;
			}

			.tip-view {
				margin: 46rpx 60rpx 50rpx 60rpx;
				text-align: center;
				color: #333333;
				font-size: 28rpx;
			}
		}
	}
</style>
