<template>
    <view
        v-if="feedbackIsShow"
        :class="['feedback-btn-box', !isUpfold ? 'upfold' : '']"
        @tap.stop="checkTap"
    >
        <view class="feedback-icon"/>
        <view class="feedback-font">
            <view>咨询</view>
            <view>客服</view>
        </view>
    </view>
</template>

<script>
/**
 * @file 客服
 * @author zhangqian51@baidu.com
 * @categories 业务组件
 * @tags [component]
*/

/* global getApp, getCurrentPages */
import {navigate} from '../../utils/navigate';
import * as core from '../../utils/core';
export default {
    config: {
        component: true
    },
    options: {
        addGlobalClass: true
    },
    props: {
        /* 是否显示客服悬浮窗 */
        feedbackIsShow: {
            type: Boolean,
            default: true
        },
        /* 展开或者收起 */
        isUpfold: {
            type: Boolean,
            default: true
        },
        /* 点击客服跳转路径 */
        toFeedbackUrl: {
            type: String,
            /* eslint-disable max-len */
            default: '/pages/gy/consult/index?rs_id=11100000&vac_id=dg159854715211784025302114975817.61&pd=med&openapi=1&from_sf=1&resource_id=5576&vn=med&atn=consult&word=&lid=2999910445&referlid=2999910445'
        },
        /* 客服类型： url-im，phone-电话 */
        type: {
            type: String,
            default: 'url'
        }
    },
    data() {
        return {
            disabled: true
        };
    },
    methods: {
        checkTap() {
            if (this.type === 'phone') {
                if (!this.toFeedbackUrl) {
                    return;
                }
                this.$api.makePhoneCall({
                    phoneNumber: this.toFeedbackUrl,
                    fail(err) {
                        this.$api.showModal({
                            title: '拨打失败',
                            content: '请检查是否输入了正确的电话号码',
                            showCancel: false
                        });
                    }
                });
            } else {
                // 拿到页面URL
                let pages = getCurrentPages();
                let currentPage = pages[pages.length - 1];
                let feedbackurl = currentPage.route;

                // 拿到页面相关参数
                let feedbackQuery = '';
                if (process.env.MARS_ENV === 'h5') {
                    feedbackQuery = core.getQueryObject(window.location.href);
                } else if (getApp().$api.getURLQuery) {
                    feedbackQuery = getApp().$api.getURLQuery();
                }
                feedbackQuery = JSON.stringify(feedbackQuery || {});

                // 跳转
                navigate({
                    url: `${this.toFeedbackUrl}&feedbackUrl=${feedbackurl}&feedbackQuery=${feedbackQuery}`
                });
            }
        }
    }
};
</script>
<style lang="less" scoped>
.feedback-btn-box {
    padding: 0.07rem 0;
    border-radius: 0.5rem 0 0 0.5rem;
    display: flex;
    align-items: center;
    background-color: #fff;
    position: fixed;
    right: 0px;
    bottom: 70%;
    border-right: none;
    box-shadow: 0 0 5px 1.667px rgba(85, 85, 85, 0.08);
    transition: transform 0.5s;
    transform: translateZ(0);
    z-index: 10;
    .feedback-icon {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 0.3rem;
        height: 0.3rem;
        margin: 0 0.1rem 0 0.05rem;
        background: url(https://medactivity.cdn.bcebos.com/hpv%2F%E5%AE%A2%E6%9C%8D.png)
            no-repeat;
        background-size: 100%;
        border-radius: 50%;
        margin: 0 0.07rem;
        .icon {
            color: #fff;
            font-size: 0.23rem;
        }
    }
    .feedback-font {
        font-size: 0.13rem;
        color: #555;
        line-height: 16px;
        margin-right: 17px;
        font-weight: bold;
    }
    &.upfold {
        transform: translateX(0.46rem);
    }
}
</style>
