const _Http = getApp().globalData.http,
    currency = require("../../utils/currency"),
    CNY = value => currency(value, {
        symbol: "￥",
        precision: 2
    }).format();
Page({
    data: {
        isLeader: false, //是否为负责人
        tabsActive: 0, //tabs 选中项
        sa_projectid: "",
        tabsList: [{
            label: "详细信息",
            icon: "icon-tabxiangxixinxi1"
        }, {
            label: "关联客户",
            icon: "icon-tabkehu",
            model: "#Treaty"
        }, {
            label: "联系人",
            icon: "icon-tablianxiren",
            model: "#Contacts"
        }, {
            label: "产品配置单",
            icon: "icon-tabchanpin",
            model: "#Product"
        }, {
            label: "报价单",
            icon: "icon-tabbaojiadan",
            model: "#Offers"
        }, {
            label: "竞争对手",
            icon: "icon-tabjingzhengduishou",
            model: "#Opponent"
        }, {
            label: "合同",
            icon: "icon-tabhetong",
            model: "#Contract"
        }, {
            label: "关联线索",
            icon: "icon-tabxiansuo",
            model: "#Clue"
        }, {
            label: "项目评估",
            icon: "icon-tabxiangmupinggu",
            model: "#Task"
        }, {
            label: "任务",
            icon: "icon-tabrenwu",
            model: "#Work"
        }, {
            label: "附件",
            icon: "icon-tabfujian1",
            model: "#Files"
        }, {
            label: "操作记录",
            icon: "icon-tabcaozuojilu1",
            model: "#Record"
        }, {
            label: "跟进动态",
            icon: "icon-tabgenjinjilu",
            model: "#Trace"
        }],
        tabbarList: [],
    },
    onLoad(options) {
        this.setData({
            sa_projectid: options.id,
            isAdmin: wx.getStorageSync('auth').wproject.options.some(v => v == "admin"), //是否具有管理权限
            options: wx.getStorageSync('auth').wproject.options, //权限列表
        });
        this.getDetail(true)
    },
    getDetail(init = false) {
        _Http.basic({
            "id": 20221021103902,
            "content": {
                nocache: true,
                "sa_projectid": this.data.sa_projectid
            },
        }).then(res => {
            console.log("项目商机详情", res)
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            });
            this.setPreview(res.data);
            if (init) {
                this.getTags();
                this.getGroup();
                this.selectComponent("#TaskTabs").getList();
            }
            this.partialRenewal(init);
        })
    },
    /* 设置显示信息 */
    setPreview(data) {
        let isLeader = data.leader.some(some => some.userid == wx.getStorageSync('userMsg').userid),
            tabbarList = [{
                icon: "icon-genjin",
                label: "跟进"
            }];

        if (this.data.isAdmin) tabbarList = [{
            icon: "icon-genjin",
            label: "报备审核"
        }, {
            icon: "icon-genjin",
            label: "报备驳回(退回)"
        }].concat(tabbarList);

        if (isLeader || this.data.isAdmin) {
            tabbarList = tabbarList.concat([{
                icon: "icon-bianji",
                label: "编辑"
            }, {
                icon: "icon-genjin",
                label: "提交报备"
            }, {
                icon: "icon-zhuanyi",
                label: "更换负责人"
            }, {
                icon: "icon-shanchu",
                label: "作废"
            }])
            tabbarList.push(data.status == '已失败' ? {
                icon: "icon-dibu-chehui",
                label: "恢复"
            } : {
                icon: "icon-dibu-jieshu",
                label: "结束"
            })
        }
        data.signamount_due = CNY(data.signamount_due);
        this.setData({
            isLeader,
            tabbarList,
            detail: data,
            briefs: [{
                label: "项目编号",
                value: data.projectnum
            }, {
                label: "项目等级",
                value: data.grade
            }, {
                label: "项目类型",
                value: data.projecttype
            }, {
                label: "项目阶段",
                value: data.stagename
            }, {
                label: "赢率",
                value: data.winrate ? data.winrate + "%" : data.winrate
            }],
            list1: [{
                    label: "项目编号",
                    value: data.projectnum
                }, {
                    label: "项目名称",
                    value: data.projectname
                }, {
                    label: "项目类型",
                    value: data.projecttype
                }, {
                    label: "项目阶段",
                    value: data.stagename
                }, {
                    label: "项目等级",
                    value: data.grade
                }, {
                    label: "省市县",
                    value: data.province ? data.province + data.city + data.county : ''
                }, {
                    label: "地址",
                    value: data.address
                }, {
                    label: "项目规模",
                    value: data.scale
                }, {
                    label: "项目预算(万元)",
                    value: CNY(data.budgetary)
                },
                {
                    label: "总投资额(万元)",
                    value: CNY(data.totalinvestment)
                },
                {
                    label: "造价(万元)",
                    value: CNY(data.costofconstruction)
                },
                {
                    label: "预计开工时间",
                    value: data.begdate_due
                },
                {
                    label: "预计完工时间",
                    value: data.enddate_due
                }, {
                    label: "预计签约时间",
                    value: data.signdate_due
                }, {
                    label: "预计签约金额(元)",
                    value: data.signamount_due
                }, {
                    label: "领域",
                    value: data.tradefields
                }, {
                    label: "品牌",
                    value: data.brandname
                }, {
                    label: "赢率",
                    value: data.winrate
                }, {
                    label: "项目状态",
                    value: data.status
                }, {
                    label: "负责人",
                    value: data.name
                }
            ],
            list2: [{
                label: "创建人",
                value: data.createby
            }, {
                label: "创建时间",
                value: data.createdate
            }, {
                label: "最近编辑人",
                value: data.changeby
            }, {
                label: "最近编辑时间",
                value: data.changedate
            }, {
                label: "提交报备人",
                value: data.reportby
            }, {
                label: "提交报备时间",
                value: data.reportdate
            }, {
                label: "报备审核人",
                value: data.reportcheckby
            }, {
                label: "报备审核时间",
                value: data.reportcheckdate
            }, {
                label: "作废原因",
                value: data.deletereason
            }, {
                label: "转手次数",
                value: data.changecount
            }]
        })
    },
    //详情按钮回调
    tabbarOnClick({
        detail
    }) {
        let data = this.data.detail,
            that = this;
        switch (detail.label) {
            case "结束":
                if (data.status != '跟进中') return wx.showToast({
                    title: '当前状态不可结束',
                    icon: "none"
                });

                wx.showModal({
                    title: '提示',
                    content: '是否确认结束项目',
                    complete: ({
                        confirm
                    }) => {
                        if (confirm) _Http.basic({
                            "id": 20221215163702,
                            "content": {
                                "sa_projectid": data.sa_projectid
                            }
                        }).then(res => {
                            console.log('结束', res)
                            wx.showToast({
                                title: res.msg == '成功' ? '已结束' : res.msg,
                                icon: "none"
                            });
                            if (res.msg == '成功') setTimeout(this.getDetail, 300);
                        })
                    }
                })
                break;
            case "恢复":
                if (data.status != '已失败') return wx.showToast({
                    title: '失败项目才可恢复!',
                    icon: "none"
                });
                wx.showModal({
                    title: '提示',
                    content: '是否确认恢复项目',
                    complete: ({
                        confirm
                    }) => {
                        if (confirm) _Http.basic({
                            "id": 20221215164002,
                            "content": {
                                "sa_projectid": data.sa_projectid
                            }
                        }).then(res => {
                            console.log('恢复', res)
                            wx.showToast({
                                title: res.msg == '成功' ? '已恢复' : res.msg,
                                icon: "none"
                            });
                            if (res.msg == '成功') setTimeout(this.getDetail, 300);
                        })
                    }
                })
                break;
            case "提交报备":
                wx.showModal({
                    title: '提示',
                    content: '是否确认提交项目审核',
                    complete: ({
                        confirm
                    }) => {
                        if (confirm) that.handleReport(1)
                    }
                })
                break;
            case "报备审核":
                wx.showModal({
                    title: '提示',
                    content: '是否确认审核项目',
                    complete: ({
                        confirm
                    }) => {
                        if (confirm) that.handleReport(2)
                    }
                })
                break;
            case "报备驳回(退回)":
                wx.showModal({
                    title: '提示',
                    content: '是否确认报备驳回(退回)项目',
                    complete: ({
                        confirm
                    }) => {
                        if (confirm) that.handleReport(3)
                    }
                })
                break;
            case "跟进":
                wx.navigateTo({
                    url: `/packageA/setclient/modules/trace/add/index?ownertable=sa_project&ownerid=${data.sa_projectid}`,
                })
                break;
            case "编辑":
                let tags = that.selectComponent("#Tags").data.systemtag;
                wx.navigateTo({
                    url: `/packageA/project/addAndEdit?data=${JSON.stringify(data)}&disabled=${tags.includes("报备中")||tags.includes("已报备")}`,
                })
                break;
            case "作废":
                wx.navigateTo({
                    url: `/packageA/project/delete?id=${this.data.detail.sa_projectid}`,
                })
                break;
            case "更换负责人":
                wx.navigateTo({
                    url: `/pages/group/select?data=${JSON.stringify({
                             ownertable:"sa_project",
                             ownerid:data.sa_projectid,
                        })}&radio=true&principal=true`,
                })
                break;
            default:
                console.log(detail)
                break;
        }
    },
    /* 1提交报备，2报备审核，3报备驳回(退回) */
    handleReport(type) {
        _Http.basic({
            "id": 20221114163402,
            "content": {
                "sa_projectid": this.data.sa_projectid,
                type
            }
        }).then(res => {
            console.log("报备", type, res)
            wx.showToast({
                title: res.data == '失败' ? res.msg : "操作成功",
                icon: "none",
                mask: true
            })
            if (res.data != '失败') this.getDetail();
        })
    },
    //局部数据更新 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 = this.data.sa_projectid;
            if (total == null || init) {
                Component.getList(id, init);
            } else if (pageNumber < pageTotal) {
                Component.getList(id, false);
            }
        }
    },
    onReachBottom() {
        this.partialRenewal();
    },
    /* 项目评估切换 */
    taskTabsChange({
        detail
    }) {
        if (detail.change) {
            this.setData({
                tabsActive: this.data.tabsList.findIndex(v => v.label == '项目评估')
            });
            this.selectComponent("#Task").getList(this.data.sa_projectid, true)
        }
    },
    //tabs 切换
    tabsChange({
        detail
    }) {
        this.setData({
            tabsActive: detail
        });
        this.partialRenewal();
    },
    /* 更换负责人 */
    handelSubmit(arr) {
        const that = this;
        wx.showModal({
            title: '提示',
            content: '是否确认更换负责人',
            complete: ({
                confirm
            }) => {
                if (confirm) _Http.basic({
                    "id": 20220930103701,
                    "content": {
                        ownertable: "sa_project",
                        ownerid: that.data.detail.sa_projectid,
                        userid: arr[0]
                    }
                }).then(res => {
                    wx.showToast({
                        title: res.msg != '成功' ? res.data : '更换成功',
                        icon: "none",
                        mask: true
                    });
                    if (res.msg == '成功') setTimeout(() => {
                        that.getDetail(true);
                        wx.navigateBack();
                    }, 300)
                })
            }
        })
    },
    //更新标签
    getTags() {
        this.selectComponent("#Tags").getTags();
    },
    //更新团队成员
    getGroup() {
        this.selectComponent("#Group").getList();
    },
    onUnload() {
        const page = getCurrentPages().find(v => v.__route__ == 'packageA/project/index');
        if (!page) return;
        let content = JSON.parse(JSON.stringify(page.data.content));
        content.pageSize = (content.pageNumber - 1) * content.pageSize;
        content.pageNumber = 1;
        _Http.basic({
            id: 20221020143502,
            content
        }).then(res => {
            console.log("更新项目商机列表", res);
            if (res.msg == '成功') {
                page.data.list = res.data;
                page.data.content.total = res.total;
                page.setListHeight();
                page.getTags();
            }

        })
    }
})