let _Http = getApp().globalData.http;
Page({
    data: {
        detail: null,
        list: [],
        showPage: "转化项目信息",
        showItemList: [{
            label: "项目状态",
            key: 'status'
        }, {
            label: "项目编号",
            key: 'projectnum'
        }, {
            label: "项目地址",
            key: 'address'
        }, {
            label: "项目类型",
            key: 'projecttype'
        }, {
            label: "领域",
            key: 'tradefield'
        }, {
            label: "品牌",
            key: 'brandname'
        }, {
            label: "负责人",
            key: 'leaderName'
        }, {
            label: "负责人手机号",
            key: 'phonenumber'
        }]
    },
    onLoad(options) {
        let detail = JSON.parse(options.data),
            projectnote = detail.projectnote;
        this.setData({
            detail,
            showMsgList: [{
                label: "项目名称",
                value: projectnote.projectname
            }, {
                label: "项目类型",
                value: projectnote.projecttype
            }, {
                label: "项目方阶段",
                value: projectnote.abbreviation
            }, {
                label: "项目等级",
                value: projectnote.grade
            }, {
                label: "省市县",
                value: [projectnote.province, projectnote.city, projectnote.county]
            }, {
                label: "详细地址",
                value: projectnote.address
            }, {
                label: "项目规模",
                value: projectnote.scale + ' ' + projectnote.unitname
            }, {
                label: "项目预算(万元)",
                value: projectnote.budgetary
            }, {
                label: "总投资额(万元)",
                value: projectnote.totalinvestment
            }, {
                label: "造价(万元)",
                value: projectnote.costofconstruction
            }, {
                label: "预计开工时间",
                value: projectnote.begdate_due && projectnote.begdate_due != 'null' ? projectnote.begdate_due : ''
            }, {
                label: "预计完工时间",
                value: projectnote.enddate_due && projectnote.enddate_due != 'null' ? projectnote.enddate_due : ''
            }, {
                label: "预计签约时间",
                value: projectnote.signdate_due || ''
            }, {
                label: "领域",
                value: projectnote.tradefield
            }, {
                label: "品牌",
                value: projectnote.brandname
            }, {
                label: "品牌是否由提报方植入",
                value: projectnote.isfillbrangd == 1 ? '是' : '否'
            }]
        })
        this.queryRepetition()
    },
    /* 查询是否重复 */
    queryRepetition(e) {
        _Http.basic({
            "id": 20221208184202,
            content: {
                pageSize: 9999,
                ...this.data.detail.projectnote
            }
        }).then(res => {
            console.log("项目查重", res)
            if (res.code == 0) return wx.showToast({
                title: res.msg,
                icon: "none"
            });
            if (res.data.length) {
                this.setData({
                    list: res.data.map(v => {
                        try {
                            v.leaderName = v.leader[0].name
                        } catch (error) {
                            v.leaderName = ""
                        }
                        v.chars = v.chars.reduce((acc, item) => ({
                            ...acc,
                            ...item
                        }), {});
                        v.projectname = v.projectname.split('').map(text => {
                            let projectname = v.chars.projectname.join("") || ''
                            return projectname.includes(text) ? {
                                text,
                                color: "red"
                            } : {
                                text,
                                color: "#666666"
                            }
                        })
                        return v
                    }),
                    showPage: "重复项目",
                })
                wx.showModal({
                    content: `查询到${res.data.length}条疑似重复项目信息`,
                    confirmText: "我知道了",
                    showCancel: false
                })
            }
        })
    },
    tabChange({
        detail
    }) {
        this.setData({
            showPage: detail.title
        })
    },
    handleAudit(e) {
        let status = e.currentTarget.dataset.type;
        wx.showLoading({
            title: '操作中...',
        })
        _Http.basic({
            "id": 20240907162304,
            "content": {
                "sat_orderclueid": this.data.detail.sat_orderclueid,
                status
            }
        }).then(res => {
            console.log(status + "审核", res)
            wx.hideLoading()
            wx.showToast({
                title: res.code == 0 ? res.msg : '操作成功',
                icon: "none",
                mask: res.code == 1
            });
            if (res.code == 1) {
                getCurrentPages().forEach(v => {
                    if (v.__route__ == 'packageA/saleClue/detail') v.getDetail()
                })
                setTimeout(() => {
                    wx.navigateBack()
                }, 800)
            }
        })
    }
})