<!--
 * @Description:
 * @Version: 2.0
 * @Autor: kangpuqing
 * @Date: 2021-11-15 20:14:40
 * @LastEditors: kangpuqing
 * @LastEditTime: 2021-12-13 12:47:43
-->
<template>
    <view class="btn-box">
        <im-bottom-btn :content="btns" @bottomBtnTap="btnTap"/>
    </view>
</template>

<script>
/**
 * @file 问诊-订单详情btns
 * @author kangpuqing01
 */
// 文案逻辑wiki http://wiki.baidu.com/pages/viewpage.action?pageId=1678250918
import {navigate} from '../../../utils/navigate';
import * as core from '../../../utils/core';
import * as conf from '../../../utils/conf';
import {request} from '@utils/request';
import {pay} from '../../../utils/pay';
import ImBottomBtn from '../../../components/im/im-bottom-btn';
import {ubcSend} from '@utils/log';

/* global getApp */
export default {
    config: {
        component: true
    },
    props: {
        status: {
            type: Number
        },
        price: {
            type: Number
        },
        ask: {
            type: Boolean
        },
        directImg: {
            type: Boolean
        },
        qid: {
            type: String
        },
        word: {
            type: String
        },
        skuDirectConsult: {
            type: Object,
            default: () => {
                return {
                    text: ''
                };
            }
        },
        skuReask: {
            type: Object,
            default: () => {
                return {
                    text: ''
                };
            }
        },
        skuInfo: {
            type: Object,
            default: () => {
                return {
                    service_type: 0,
                    title: ''
                };
            }
        },
        doctorInfo: {
            type: Object,
            default: () => {
                return {
                    name: '',
                    is_open_tel_consult: 0,
                    expert_id: ''
                };
            }
        },
        type: {
            type: Number
        },
        joinIm: {
            type: Number
        }
    },
    data() {
        return {
            api: {
                im: '/pages/wz/chat/index',
                pay: '/zhuanjia/gatewaypay',
                supplement: '/pages/wz/triage/index',
                search: '/pages/brand/search/index',
                doctor: 'pages/wz/experthome/index',
                phone: '/pages/wz/service-info/index',
                duty: '/pages/register/patient',
                askAndPrice: '/med/api/posttextdirect'
            }
        };
    },
    computed: {
        btns() {
            let arr = [];
            switch (this.status) {
                // 待支付
                case 0:
                    arr.push('pay');
                    break;
                // 待补充病情
                case 1:
                    arr.push('supplement');
                    break;
                case 2:
                case 3:
                case 4:
                case 5:
                    // 待接诊、待通话、问诊中、审核中
                    break;
                // 审核失败
                case 6:
                    // arr.push('reduty');
                    break;
                // 已完成
                case 7:
                    arr.push('request');
                    // 定向电话  留存 迭代备用
                    // if (this.skuInfo.service_type === 203) {
                    //     // 定向电话开
                    //     if (this.doctorInfo.is_open_tel_consult) {
                    //         arr.push('consultDoctor', 'consultOther');
                    //         break;
                    //     }
                    //     arr.push('consultOther');
                    //     break;
                    // } else {
                    //     // 开通定向图文和追问包
                    //     if (this.directImg && this.ask) {
                    //         arr.push('askAndPrice', 'imgAndPrice');
                    //         break;
                    //     }
                    //     // 未开通定向图文，开通追问包
                    //     else if (!this.directImg && this.ask) {
                    //         arr.push('askAndPrice', 'consultOther');
                    //         break;
                    //     }
                    //     // 开通定向图文，未开通追问包
                    //     else if (this.directImg && !this.ask) {
                    //         arr.push('askDoctorAndPrice', 'consultOther');
                    //         break;
                    //     }
                    //     // 未开通定向图文和开通追问包
                    //     else if (!this.directImg && !this.ask) {
                    //         arr.push('request');
                    //         break;
                    //     }
                    // }
                    break;
                case 8:
                case 9:
                case 10:
                    // 已取消、退款中、退款完成
                    arr.push('reask');
                // 已取消、退款中、退款完成  留存迭代备用
                // 定向图文
                // if (this.skuInfo.service_type === 201) {
                //     if (this.doctorInfo.is_open_direct_consult) {
                //         arr.push('askDoctorAndPrice', 'consultOther');
                //         break;
                //     } else {
                //         arr.push('consultOther');
                //         break;
                //     }
                // }
                // // 定向电话
                // else if (this.skuInfo.service_type === 203) {
                //     if (this.doctorInfo.is_open_tel_consult) {
                //         arr.push('consultDoctor', 'consultOther');
                //         break;
                //     } else {
                //         arr.push('consultOther');
                //         break;
                //     }
                // }
                // // 非定向图文、非定向电话
                // else if (this.skuInfo.service_type === 101 || this.skuInfo.service_type === 102) {
                //     arr.push('reask');
                //     break;
                // }
            }
            return {
                btn: arr.map((item, index) => {
                    return this.btnTemplate(item, index, arr.length);
                }),
                bgColor: 'transparent'
            };
        }
    },
    components: {
        ImBottomBtn
    },

    methods: {
        // btn文案全量
        btnTemplate(btnType, index, len) {
            let type = '';
            let title = '';
            let desc = '';
            let boxShadow = '0 3px 7px 0 rgba(0,200,200,0.30)';
            let bgColor
                = index === 0 && len > 1
                    ? 'linear-gradient(134deg, #FFA500 0%, #FFBC10 55%, #FFD36C 100%)'
                    : 'linear-gradient(134deg, #00CFA3 0%, #05CFCD 61%, #00D3EA 100%)';
            let fontColor = '#fff';

            switch (btnType) {
                case 'pay':
                    type = 'pay';
                    title = `去支付 ￥${(this.price / 100).toFixed(2)}`;
                    break;
                case 'reduty':
                    type = 'reduty';
                    title = '重新报到';
                    break;
                case 'supplement':
                    type = 'supplement';
                    title = '补充病情';
                    break;
                case 'consultDoctor':
                    type = 'consultDoctor';
                    title = `咨询${(this.doctorInfo && this.doctorInfo.name) || ''}医生`;
                    break;
                case 'consultOther':
                    type = 'consultOther';
                    title = '咨询其他医生';
                    break;
                case 'askAndPrice':
                    type = 'askAndPrice';
                    title = '追问包';
                    desc = `${this.skuReask.text || ''}`;
                    break;
                case 'imgAndPrice':
                    type = 'imgAndPrice';
                    title = '图文问诊';
                    desc = `${this.skuDirectConsult.text || ''}`;
                    break;
                case 'askDoctorAndPrice':
                    type = 'askDoctorAndPrice';
                    title = `咨询${(this.doctorInfo && this.doctorInfo.name) || ''}医生`;
                    desc = `${this.skuDirectConsult.text || ''}`;
                    break;
                case 'reask':
                    type = 'reask';
                    title = '重新提问';
                    break;
                case 'request':
                    type = 'request';
                    title = '再次咨询';
                    break;
            }
            return {
                type,
                title,
                desc,
                bgColor,
                fontColor,
                boxShadow
            };
        },
        btnTap({type}) {
            let logType = '';
            let url = '';
            switch (type) {
                case 'pay':
                    // 去支付
                    logType = 'detail_pay';
                    request({
                        url: conf.apiHost + this.api.pay,
                        data: {
                            cb: 'json',
                            qid: this.qid,
                            pd: 'med',
                            vn: 'med'
                        }
                    }).then(resp => {
                        pay({
                            url: resp && resp.data && resp.data.payUrl
                        });
                    });
                    break;
                case 'askAndPrice':
                    // 追问包
                    logType = 'detail_continue_ask';
                    request({
                        url: conf.apiHost + this.api.askAndPrice,
                        data: {
                            pd: 'med',
                            vn: 'med',
                            atn: 'posttextdirect',
                            from_sf: 1,
                            openapi: 1,
                            question_id: this.qid,
                            ref: 'inner_wz_redirect_bottombtn',
                            resource_id: 5216,
                            service_type: 'renew'
                        }
                    }).then(resp => {
                        pay({
                            url: resp && resp.data && resp.data.payUrl
                        });
                    });
                    break;
                case 'reduty':
                    // 重新报到，进入患者报到补充资料页面
                    url += this.api.duty;

                    url = core.urlAddQuery(url, {
                        question_id: this.qid,
                        questionId: this.qid
                    });

                    this.onGo(url);
                    break;
                case 'supplement':
                    // 跳转主诉收集
                    logType = 'detail_supplement';
                    url += this.api.supplement;

                    url = core.urlAddQuery(url, {
                        openapi: 1,
                        from_sf: 1,
                        question_id: this.qid,
                        isDirPhone: 1,
                        questionId: this.qid,
                        pd: 'med',
                        vn: 'med',
                        word: '问医生',
                        expert_id: this.doctorInfo.expert_id
                    });
                    this.onGo(url);
                    break;
                // 跳转定向主诉收集页
                case 'imgAndPrice':
                case 'askDoctorAndPrice':
                    url += this.api.supplement;

                    url = core.urlAddQuery(url, {
                        atn: 'triage',
                        vn: 'med',
                        pd: 'med',
                        openapi: 1,
                        expert_id: this.doctorInfo.expert_id,
                        isDirected: 1,
                        word: '问医生'
                    });

                    this.onGo(url);
                    break;
                // 跳转非定向主诉收集页
                case 'reask':
                    logType = 'detail_ask_again';
                    this.goIm();
                    // 跳转非定向主诉收集页-存档备迭代
                    // url += this.api['supplement'];

                    // url = core.urlAddQuery(url, {
                    //     atn: 'triage',
                    //     vn: 'med',
                    //     pd: 'med',
                    //     openapi: 1,
                    //     word: '问医生'
                    // });

                    // this.onGo(url);
                    break;
                case 'consultDoctor':
                    // 咨询当前医生
                    url += this.api.phone;

                    url = core.urlAddQuery(url, {
                        search_channel: 'wz',
                        expert_id: this.doctorInfo.expert_id,
                        from_sf: 1,
                        idDirected: 1,
                        openapi: 1,
                        vn: 'med',
                        pd: 'med',
                        service_type: 'dirphone'
                    });

                    this.onGo(url);
                    break;
                case 'consultOther':
                case 'request':
                    logType = 'detail_consult_again';
                    this.goIm();
                    // 咨询其他医生 留存等待迭代备用
                    // url += this.api['search'];

                    // url = core.urlAddQuery(url, {
                    //     search_channel: 'wz',
                    //     search_mode: 'search',
                    //     search_section: 'all',
                    //     word: this.word,
                    //     openapi: 1,
                    //     from_sf: 1,
                    //     pd: 'med',
                    //     vn: 'med'
                    // });

                    // this.onGo(url);
                    break;
            }
            logType
                && ubcSend({
                    type: 'clk',
                    value: logType
                });
        },
        // 跳转
        onGo(url) {
            if (!url) {
                return;
            }
            navigate({url});
        },
        // 跳转im
        goIm() {
            // 进入对话，跳转im页
            let path = this.api.im;
            // 非定向并且待接诊状态传joinim=0，其他状态1
            path = core.urlAddQuery(path, {
                question_id: this.qid,
                questionId: this.qid,
                joinim: this.joinIm,
                openapi: 1,
                pd: 'med',
                vn: 'med'
            });

            this.onGo(path);
        }
    }
};
</script>
