const _Http = getApp().globalData.http,
    currency = require("../../utils/currency"),
    CNY = value => currency(value, {
        symbol: "￥",
        precision: 2
    }).format();
Page({
    data: {
        loading: true,
        isLeader: false, //是否为负责人
        tabsActive: 0, //tabs 选中项
        sa_doctorid: 0,
        sColors: getApp().globalData.sColors,
    },
    onLoad(options) {
        const appAuth = wx.getStorageSync('auth').wdoctors;

        let tabsList = [{
            label: "详细信息",
            icon: "icon-tabxiangxixinxi1"
        }, {
            label: "附件",
            icon: "icon-tabfujian1",
            model: "#Files"
        }, {
            label: "操作",
            icon: "icon-tabcaozuojilu1",
            model: "#Record"
        }];
        if (appAuth.isdatafollowup) {
            tabsList.unshift({
                label: "跟进动态",
                icon: "icon-tabgenjinjilu",
                model: "#Trace"
            })
        }
        console.log("appAuth", appAuth)
        this.setData({
            sa_doctorid: options.id,
            tabsList,
            appAuth
        })
        this.getDetail();
        getApp().globalData.Language.getLanguagePackage(this);
    },

    /* 获取详情 */
    getDetail() {
        /* 基本信息 */
        _Http.basic({
            "id": 2025102116474702,
            "content": {
                sa_doctorid: this.data.sa_doctorid
            },
        }).then(res => {
            console.log("详情", res)
            if (res.code != '1') return wx.showToast({
                title: res.data,
                icon: "none"
            })
            this.setPreview(res);
            this.setData({
                loading: false,
                detail: res.data,
            })
            //获取标签
            if (this.data.appAuth.isdatatag) this.getTags();
            if (this.data.appAuth.isdatateam) this.getGroup()
            this.setTabbar();
            this.partialRenewal();
        });
    },

    setPreview(res) {
        /* 摘要信息 */
        let briefs = [{
            label: "联系方式",
            value: res.data.phonenumber
        }, {
            label: "职称",
            value: res.data.professional
        }, {
            label: "所属科室",
            value: res.data.hospitaldepname
        }, {
            label: "是否科室负责人",
            value: res.data.isleader == 0 ? '否' : '是'
        }, {
            label: "负责人",
            value: res.data.leader.length ? res.data.leader[0].name : ''
        }, {
            label: "所属医院",
            value: res.data.enterprisename
        }, {
            label: "备注",
            value: res.data.remarks
        }];

        /* 基本信息 */
        let list1 = [{
            label: "联系方式",
            value: res.data.phonenumber
        }, {
            label: "职称",
            value: res.data.professional
        }, {
            label: "所属科室",
            value: res.data.hospitaldepname
        }, {
            label: "是否科室负责人",
            value: res.data.isleader == 0 ? '否' : '是'
        }, {
            label: "负责人",
            value: res.data.leader.length ? res.data.leader[0].name : ''
        }, {
            label: "所属医院",
            value: res.data.enterprisename
        }, {
            label: "备注",
            value: res.data.remarks
        }];
        /* 系统信息 */
        let list2 = [{
            label: "创建人",
            value: res.data.createby
        }, {
            label: "创建时间",
            value: res.data.createdate
        }, {
            label: "最近编辑人",
            value: res.data.changeby
        }, {
            label: "最近编辑时间",
            value: res.data.changedate
        }, {
            label: "删除原因",
            value: res.data.deletereason
        }, {
            label: "转手次数",
            value: res.data.leader[0] ? res.data.leader[0].leadernum : 0
        }];
        this.setData({
            briefs,
            list1,
            list2
        });
    },
    //详情按钮回调
    tabbarOnClick({
        detail
    }) {
        let data = JSON.parse(JSON.stringify(this.data.detail)),
            that = this;
        switch (detail.label) {
            case "编辑":
                data.sa_customersid = data.enterprisename ? [data.enterprisename, [data.sa_customersid]] : "";
                wx.navigateTo({
                    url: `/prsx/doctors/insert?data=${JSON.stringify(data)}`,
                })
                break;
            case "跟进":
                that.selectComponent("#Trace").toAdd()
                break;
            case "删除":
                wx.showModal({
                    content: `是否确定删除'${data.hospitaldepname}'?`,
                    complete: (res) => {
                        if (res.confirm) _Http.basic({
                            "content": {
                                "sa_doctorid": data.sa_doctorid
                            },
                            "id": 2025102116480402
                        }).then(res => {
                            console.log("删除医生", res)
                            wx.showToast({
                                title: res.code != '1' ? res.msg : '删除成功',
                                icon: "none",
                                mask: res.code == '1'
                            })
                            if (res.code != '1') return;
                            setTimeout(() => {
                                wx.navigateBack()
                            }, 300)
                        })
                    }
                })
                break;
            case "更换负责人":
                wx.navigateTo({
                    url: `/pages/group/select?data=${JSON.stringify({
                         ownertable:"sa_doctor",
                         ownerid:this.data.sa_doctorid,
                    })}&radio=true&principal=true`,
                })
                break;
            default:
                console.log(detail)
                break;
        }
    },
    /* 更换负责人 */
    handelSubmit(arr) {
        const that = this;
        wx.showModal({
            title: getApp().globalData.Language.getMapText('提示'),
            content: getApp().globalData.Language.getMapText('是否确认更换负责人'),
            complete: ({
                confirm
            }) => {
                if (confirm) wx.showModal({
                    title: getApp().globalData.Language.getMapText('提示'),
                    content: getApp().globalData.Language.getMapText('是否参与该数据的后续工作'),
                    cancelText: getApp().globalData.Language.getMapText('不参与'),
                    confirmText: getApp().globalData.Language.getMapText('参与'),
                    complete: (s) => {
                        _Http.basic({
                            "id": 20220930103701,
                            "content": {
                                ownertable: "sa_doctor",
                                ownerid: that.data.sa_doctorid,
                                userid: arr[0],
                                isaddoldleader: s.confirm ? 1 : 0
                            }
                        }).then(res => {
                            console.log("更换负责人", res)
                            if (res.code != '1') return wx.showToast({
                                title: res.data,
                                icon: "none"
                            });
                            wx.showToast({
                                title: getApp().globalData.Language.getMapText('更换成功'),
                                icon: "none",
                                mask: true
                            });
                            getCurrentPages().forEach(v => {
                                if (['packageA/setclient/index'].includes(v.__route__)) v.getList(true)
                            })
                            setTimeout(() => {
                                wx.navigateBack({
                                    delta: 2
                                });
                            }, 300)
                        })
                    }
                })
            }
        })
    },
    /* 底部功能 */
    async setTabbar() {
        let detail = this.data.detail,
            isAdmin = this.data.appAuth.options.includes("admin") ? true : false,
            isLeader = detail.leader.some(some => some.userid == wx.getStorageSync('userMsg').userid),
            editdataleader = isLeader ? 1 : 0,
            tabbarList = [],
            appAuth = this.data.appAuth;
        console.log("isAdmin", isAdmin)
        if (isLeader || isAdmin) {
            tabbarList.push({
                icon: "color-bianji",
                label: "编辑"
            })
            tabbarList.push({
                icon: "icon-shanchu",
                label: "删除"
            })
        }

        if (appAuth.isdatafollowup) {
            tabbarList.push({
                icon: "color-genjin",
                label: "跟进"
            });
        }
        this.setData({
            tabbarList,
            isLeader
        })
    },
    //tabs 切换
    tabsChange({
        detail
    }) {
        this.setData({
            tabsActive: detail
        });
        this.partialRenewal();
    },
    //局部数据更新 tabs
    partialRenewal(init = false) {
        let model = this.data.tabsList[this.data.tabsActive].model;
        if (model) {
            let Component = this.selectComponent(model),
                {
                    total,
                    pageNumber,
                    pageTotal
                } = Component.data.content,
                id = model == "#Clue" ? this.data.detail.sa_doctorid : this.data.detail.sys_enterpriseid;
            if (model == "#Files") init = true;
            if (total == null || init) {
                Component.getList(id, init);
            } else if (pageNumber <= pageTotal) {
                Component.getList(id, false);
            }
        }
    },
    //更新标签
    getTags() {
        this.selectComponent("#Tags").getTags()
    },
    //更新团队成员
    getGroup() {
        this.selectComponent("#Group").getList().then(this.setTabbarList)
    },
    onReachBottom() {
        this.partialRenewal();
    },
    onUnload() {
        getCurrentPages().forEach(page => {
            if (page.__route__ == 'prsx/doctors/index') {
                let content = JSON.parse(JSON.stringify(page.data.content));
                content.pageSize = (content.pageNumber - 1) * content.pageSize;
                content.pageNumber = 1;
                _Http.basic({
                    id: '2025102208523002',
                    content
                }).then(res => {
                    console.log("更新医生列表", res);
                    if (res.code == '1') {
                        page.setData({
                            list: res.data,
                            "content.total": res.total
                        })
                    }
                })
            } else if (page.__route__ == 'prsx/department/detail') {
                page.selectComponent("#Doctors").getList(page.data.detail.sa_hospitaldepid, true)
            }
        })
    }
})