const _Http = getApp().globalData.http,
    currency = require("../../utils/currency"),
    CNY = value => currency(value, {
        symbol: "￥",
        precision: 2
    }).format();
Page({
    data: {
        isLeader: false, //是否为负责人
        tabsActive: 1, //tabs 选中项
        sa_quotedpriceid: "",
        tabsList: [{
            label: "详细信息",
            icon: "icon-tabxiangxixinxi1"
        }, {
            label: "产品明细折扣",
            icon: "icon-tabchanpin",
            model: "#Product"
        }, {
            label: "产品类别折扣",
            icon: "icon-tabchanpinleibie",
            model: "#PiscountType"
        }, {
            label: "其他费用",
            icon: "icon-tabkaipiaoxinxi",
            model: "#PettyExpense"
        }, {
            label: "历史报价",
            icon: "icon-tabbaojiadan",
            model: "#History"
        }, {
            label: "跟进动态",
            icon: "icon-tabgenjinjilu",
            model: "#Trace"
        }, {
            label: "操作记录",
            icon: "icon-tabcaozuojilu1",
            model: "#Record"
        }, {
            label: "附件",
            icon: "icon-tabfujian1",
            model: "#Files"
        }, {
            label: "任务",
            icon: "icon-tabrenwu",
            model: "#Work"
        }],
        tabbarList: [],
    },
    onLoad(options) {
        this.setData({
            sa_quotedpriceid: options.sa_quotedpriceid,
            isAdmin: wx.getStorageSync('auth').woffer.options.some(v => v == "admin"), //是否具有管理权限
            options: wx.getStorageSync('auth').woffer.options, //权限列表
        });
        this.getDetail(true);
    },
    getDetail(init = false) {
        _Http.basic({
            "id": 20221020165203,
            "version": 1,
            "content": {
                nocache: true,
                "sa_quotedpriceid": this.data.sa_quotedpriceid
            },
        }).then(res => {
            console.log("报价单详情", res)
            if (res.msg != '成功') return wx.showToast({
                title: res.msg,
                icon: "none"
            });
            this.setPreview(res);
            /* 更新列表数据 */
            let page = getCurrentPages().find(v => v.__route__ == 'packageA/offers/index');
            if (page) {
                let i = page.data.list.findIndex(v => v.sa_quotedpriceid == this.data.sa_quotedpriceid);
                if (i != -1 && page.data.list[i].status != res.data.status) page.setData({
                    [`list[${i}].status`]: res.data.status
                })
            }
            this.getTags();
            this.getGroup();
            this.partialRenewal();
        })
    },
    /* 设置显示字段 */
    setPreview(res) {
        //底部操作
        let tabbarList = [{
                icon: "icon-genjin",
                label: "跟进"
            }],
            isLeader = res.data.leader[0].userid == wx.getStorageSync('userMsg').userid;
        if (isLeader || this.data.isAdmin) {
            //负责人/管理员默认权限功能
            /* {
                icon: "icon-tabxiangmupinggu",
                label: '打印'
            }, */
            tabbarList.push({
                icon: "icon-guanlian-fuzhi",
                label: "复制"
            });
            if (res.data.status == '新建') tabbarList = [{
                icon: "icon-zhuanyi",
                label: "更换负责人"
            }, {
                icon: "icon-shanchu",
                label: "作废"
            }, {
                icon: "icon-dibu-bianji",
                label: "编辑"
            }].concat(tabbarList);

            //负责人 新建状态可以提交 提交状态可以撤回  负责人可以打印
            if (isLeader) {
                if (res.data.status == '新建') tabbarList.unshift({
                    icon: "icon-genjin",
                    label: '提交'
                })
                if (res.data.status == '提交') tabbarList.unshift({
                    icon: "icon-genjin",
                    label: '撤回'
                });
            }
            //拥有管理权限 提交状态可以审核  审核状态可以反审核
            if (this.data.isAdmin && (res.data.status == '审核' || res.data.status == '提交')) tabbarList.unshift(res.data.status == '审核' ? {
                icon: "icon-guanlian-fuzhi",
                label: "反审核"
            } : {
                icon: "icon-guanlian-fuzhi",
                label: "审核"
            });
        }
        let briefs = [{
            label: "报价类型",
            value: res.data.quotedpricetype
        }, {
            label: "项目",
            value: res.data.projectname
        }, {
            label: "客户",
            value: res.data.enterprisename
        }, {
            label: "是否特价",
            value: res.data.specialoffer == 1 ? '是' : '否'
        }, {
            label: "状态",
            value: res.data.status
        }];
        //客户报价移除项目字段
        if (res.data.quotedpricetype == '客户报价') briefs.splice(1, 1);
        /* {
            label: "项目编号",
            value: res.data.projectnum
        },  */
        let list1 = [{
            label: "报价单号",
            value: res.data.billno
        }, {
            label: "报价类型",
            value: res.data.quotedpricetype
        }, {
            label: "产品系列",
            value: res.data.itemtype
        }, {
            label: "领域",
            value: res.data.tradefields
        }, {
            label: "品牌",
            value: res.data.brandname
        }, {
            label: "项目名称",
            value: res.data.projectname
        }, {
            label: "客户",
            value: res.data.enterprisename
        }, {
            label: "电话",
            value: res.data.telephone
        }, {
            label: "客户联系人",
            value: res.data.contactsname
        }, {
            label: "联系人手机号",
            value: res.data.contactsphonenumber
        }, {
            label: "报价日期",
            value: res.data.billdate
        }, {
            label: "有效期",
            value: res.data.begdate + ' - ' + res.data.enddate
        }, {
            label: "折扣(%)",
            value: (res.data.discountrate * 100).toFixed(2) + '%'
        }, {
            label: "总金额",
            value: CNY(res.data.sumamount)
        }, {
            label: "是否特价",
            value: res.data.specialoffer == 1 ? '是' : '否'
        }, {
            label: "状态",
            value: res.data.status
        }, {
            label: "负责人",
            value: res.data.leader[0].name
        }, {
            label: "备注",
            value: res.data.remarks
        }];
        //客户报价移除项目字段
        if (res.data.quotedpricetype == '客户报价') list1.splice(4, 2);
        this.setData({
            detail: res.data,
            tabbarList,
            isLeader,
            briefs,
            list1,
            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.submitby
            }, {
                label: "提交时间",
                value: res.data.submitdate
            }, {
                label: "审核人",
                value: res.data.checkby
            }, {
                label: "审核时间",
                value: res.data.checkdate
            }, {
                label: "作废原因",
                value: res.data.deletereason
            }, {
                label: "转手次数",
                value: res.data.leader[0].leadernum
            }],
        });
    },
    //tabs 切换
    tabsChange({
        detail
    }) {
        this.setData({
            tabsActive: detail
        });
        this.partialRenewal();
    },
    //更新标签
    getTags() {
        this.selectComponent("#Tags").getTags();
    },
    //更新团队成员
    getGroup() {
        this.selectComponent("#Group").getList();
    },
    //局部数据更新 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_quotedpriceid;
            if (total == null || init) {
                Component.getList(id, init);
            } else if (pageNumber < pageTotal) {
                Component.getList(id, false);
            }
        }
    },
    onReachBottom() {
        this.partialRenewal();
    },
    //详情按钮回调
    tabbarOnClick({
        detail
    }) {
        let data = this.data.detail,
            that = this;
        switch (detail.label) {
            case "编辑":
                console.log(data)
                wx.navigateTo({
                    url: `/packageA/offers/${data.quotedpricetype=='项目报价'?'addProjectOffer':'addSetclientOffer'}?data=${JSON.stringify(data)}`,
                })
                break;
            case "提交":
                if (data.status != '新建') return wx.showToast({
                    title: '当前状态不可提交!',
                    icon: "none"
                })
                wx.showModal({
                    title: '提示',
                    content: `是否确定提交当前报价，提交后禁止使用部分功能`,
                    complete: (res) => {
                        if (res.confirm) _Http.basic({
                            "id": 20221020165303,
                            "version": 1,
                            "content": {
                                "sa_quotedpriceid": that.data.sa_quotedpriceid
                            }
                        }).then(res => {
                            console.log("提交报价单", res)
                            wx.showToast({
                                title: res.msg == '成功' ? "提交成功" : res.msg,
                                icon: "none"
                            })
                            if (res.msg == '成功') setTimeout(() => {
                                that.getDetail();
                            }, 300)
                        })
                    }
                })
                break;
            case "审核":
                if (data.status != '提交') return wx.showToast({
                    title: '当前状态不可审核!',
                    icon: "none"
                })
                wx.showModal({
                    title: '提示',
                    content: `是否通过当前报价`,
                    complete: (res) => {
                        if (res.confirm) _Http.basic({
                            "id": 20221020165403,
                            "version": 1,
                            "content": {
                                "sa_quotedpriceid": that.data.sa_quotedpriceid,
                                ischeck: true
                            }
                        }).then(res => {
                            console.log("审核报价单", res)
                            wx.showToast({
                                title: res.msg == '成功' ? "审核成功" : res.msg,
                                icon: "none"
                            })
                            if (res.msg == '成功') setTimeout(() => {
                                that.getDetail();
                            }, 300)
                        })
                    }
                })
                break;
            case "反审核":
                if (data.status != '审核') return wx.showToast({
                    title: '当前状态不可反审核!',
                    icon: "none"
                })
                wx.showModal({
                    title: '提示',
                    content: `是否退回当前报价`,
                    complete: (res) => {
                        if (res.confirm) _Http.basic({
                            "id": 20221020165403,
                            "version": 1,
                            "content": {
                                "sa_quotedpriceid": that.data.sa_quotedpriceid,
                                ischeck: false
                            }
                        }).then(res => {
                            console.log("反审核报价单", res)
                            wx.showToast({
                                title: res.msg == '成功' ? "反审核成功" : res.msg,
                                icon: "none"
                            })
                            if (res.msg == '成功') setTimeout(() => {
                                that.getDetail();
                            }, 300)
                        })
                    }
                })
                break;
            case "撤回":
                console.log(data.status)
                if (data.status != '提交') return wx.showToast({
                    title: '当前状态不可撤回审核!',
                    icon: "none"
                })
                wx.showModal({
                    title: '提示',
                    content: `是否撤回审核当前报价`,
                    complete: (res) => {
                        if (res.confirm) _Http.basic({
                            "id": 20221125162803,
                            "version": 1,
                            "content": {
                                "sa_quotedpriceid": that.data.sa_quotedpriceid
                            }
                        }).then(res => {
                            console.log("撤回报价单", res)
                            wx.showToast({
                                title: res.msg == '成功' ? "撤回审核成功" : res.msg,
                                icon: "none"
                            })
                            if (res.msg == '成功') setTimeout(that.getDetail, 300)
                        })
                    }
                })
                break;
            case "跟进":
                wx.navigateTo({
                    url: `/packageA/setclient/modules/trace/add/index?ownertable=sa_quotedprice&ownerid=${data.sa_quotedpriceid}`,
                })
                break;
            case "复制":
                const {
                    billno,
                    sa_quotedpriceid
                } = data;
                wx.showModal({
                    title: '提示',
                    content: `是否确认复制“${billno}”`,
                    complete: ({
                        confirm
                    }) => {
                        if (confirm) _Http.basic({
                            "id": 20221209094103,
                            "content": {
                                sa_quotedpriceid
                            },
                        }).then(res => {
                            console.log("复制报价单", res)
                            wx.showToast({
                                title: res.msg == '成功' ? '复制成功' : res.msg,
                                icon: "none",
                                mask: true
                            });
                            if (res.msg == '成功') setTimeout(() => {
                                wx.redirectTo({
                                    url: `/packageA/offers/detail?sa_quotedpriceid=${res.data.sa_quotedpriceid}`,
                                })
                                let page = getCurrentPages().find(v => v.__route__ == 'packageA/offers/index');
                                if (page) page.getList(true);
                            }, 300)
                        })
                    }
                })
                break;
            case "作废":
                wx.showModal({
                    title: '提示',
                    content: '是否确认作废该报价单？',
                    complete: ({
                        confirm
                    }) => {
                        if (confirm) _Http.basic({
                            "id": 20221020165503,
                            "version": 1,
                            "content": {
                                "sa_quotedpriceids": [this.data.detail.sa_quotedpriceid]
                            }
                        }).then(res => {
                            wx.showToast({
                                title: res.msg == '成功' ? '已作废报价单' : res.msg,
                                icon: "none"
                            });
                            if (res.msg == '成功') {
                                setTimeout(() => {
                                    getCurrentPages().forEach(v => {
                                        if (v.__route__ == 'packageA/offers/index') {
                                            v.setData({
                                                list: v.data.list.filter(value => value.sa_quotedpriceid != this.data.detail.sa_quotedpriceid),
                                                'content.total': v.data.content.total - 1
                                            })
                                        }
                                    })
                                    wx.navigateBack();
                                }, 300)
                            }
                        })
                    }
                })


                break;
            case "更换负责人":
                wx.navigateTo({
                    url: `/pages/group/select?data=${JSON.stringify({
                             ownertable:"sa_quotedprice",
                             ownerid:data.sa_quotedpriceid,
                        })}&radio=true&principal=true`,
                })
                break;
        }
    },
    /* 更换负责人 */
    handelSubmit(arr) {
        const that = this;
        wx.showModal({
            title: '提示',
            content: '是否确认更换负责人',
            complete: ({
                confirm
            }) => {
                if (confirm) _Http.basic({
                    "id": 20220930103701,
                    "content": {
                        ownertable: "sa_quotedprice",
                        ownerid: that.data.detail.sa_quotedpriceid,
                        userid: arr[0]
                    }
                }).then(res => {
                    console.log("更换负责人", res)
                    if (res.msg != '成功') return wx.showToast({
                        title: res.data,
                        icon: "none"
                    });
                    wx.showToast({
                        title: '更换成功！',
                        icon: "none",
                        mask: true
                    });
                    setTimeout(() => {
                        that.getDetail();
                        wx.navigateBack();
                        /*  getCurrentPages().forEach(v => {
                             if (['packageA/project/index'].includes(v.__route__)) v.getList(true)
                         }) */
                    }, 300)
                })
            }
        })
    }
})