const _Http = getApp().globalData.http,
    currency = require("../../../utils/currency"),
    CNY = (value, symbol = "￥", precision = 2) => currency(value, {
        symbol,
        precision
    }).format();
Component({
    options: {
        addGlobalClass: true
    },
    lifetimes: {
        attached: function () {
            getApp().globalData.Language.getLanguagePackage(this)
            this.setData({
                "content.dataid": wx.getStorageSync('userMsg').userid,
                "content.username": wx.getStorageSync('userMsg').name,
            })
        }
    },
    data: {
        "content": {
            dataType: "",
            where: {
                type: "",
                tradingstatus: "",
                tradefield: "",
                grade: "",
                sa_projectid: ""
            },
            pageNumber: 1,
            pageSize: 20,
            pageTotal: 1
        },
        grades: [],
        tradefields: [],
        projecttypes: [],
        nodes: [{
            remarks: '全部',
            value: ''
        }, {
            remarks: '立项',
            value: '立项'
        }, {
            remarks: '报备',
            value: '报备'
        }, {
            remarks: '报价',
            value: '报价'
        }, {
            remarks: '合同',
            value: '合同'
        }, {
            remarks: '订单',
            value: '订单'
        }, {
            remarks: '结案',
            value: '结案'
        }],
    },
    methods: {
        async getList() {
            let content = this.data.content;

            if (this.data.tradefields.length == 0) {
                _Http.basic({
                    "classname": "sysmanage.develop.optiontype.optiontype",
                    "method": "optiontypeselect",
                    "content": {
                        "pageNumber": 1,
                        "pageSize": 1000,
                        "typename": "tradefield",
                        "parameter": {}
                    },
                }).then(res => {
                    console.log("领域", res)
                    if (res.code == 1) {
                        res.data = res.data.map(v => {
                            v.remarks = v.value
                            return v
                        })
                        res.data.unshift({
                            remarks: '全部',
                            value: ''
                        })
                        this.setData({
                            tradefields: res.data
                        })
                    }

                })
            }
            if (this.data.projecttypes.length == 0) await _Http.basic({
                "classname": "sysmanage.develop.optiontype.optiontype",
                "method": "optiontypeselect",
                "content": {
                    "pageNumber": 1,
                    "pageSize": 1000,
                    "typename": "projecttype",
                    "parameter": {}
                },
            }).then(res => {
                console.log("项目类型", res)
                if (res.code == 1) {
                    res.data = res.data.map((v, i) => {
                        if (i == 0) content.where.projecttype = v.value;
                        v.remarks = v.value + '-' + getApp().globalData.Language.getMapText(v.remarks)
                        return v
                    })
                    res.data.unshift({
                        remarks: '全部',
                        value: ''
                    })
                    this.setData({
                        content,
                        projecttypes: res.data
                    })
                }

            })



            if (this.data.grades.length == 0) {
                _Http.basic({
                    "classname": "sysmanage.develop.optiontype.optiontype",
                    "method": "optiontypeselect",
                    "content": {
                        "pageNumber": 1,
                        "pageSize": 1000,
                        "typename": "projectgrade",
                        "parameter": {}
                    },
                }).then(res => {
                    console.log("项目等级", res)
                    if (res.code == 1) {
                        res.data = res.data.map(v => {
                            v.remarks = v.value + '-' + getApp().globalData.Language.getMapText(v.remarks)
                            return v
                        })
                        res.data.unshift({
                            remarks: '全部',
                            value: ''
                        })
                        this.setData({
                            grades: res.data
                        })
                    }

                })
            }

            const {
                dataid,
                type,
                username,
                isleave
            } = getCurrentPages()[getCurrentPages().length - 1].data;
            content.dataid = dataid;
            content.type = type;
            content.username = username;
            content.where.isleave = isleave;
            _Http.basic({
                "id": 20231023092304,
                content
            }).then(res => {
                console.log("项目生命周期分析", res)
                if (res.code != '1') return wx.showToast({
                    title: res.data,
                    icon: "none"
                })
                this.setData({
                    list: res.data.map(v => {
                        v.bgColor = {
                            "报备": "#3874F6",
                            "报价": "#00B32B",
                            "合同": "#FFAE02",
                            "订单": "#F53F3F",
                        } [v.end]

                        v.tips = {
                            "立项": "项目新建",
                            "报价": "项目报价审核",
                            "合同": "项目合同审核",
                        } [v.start] || "项目报备审核"
                        v.dateavg = v.dateavg.toFixed(2)
                        return v
                    })
                })
                try {
                    this.selectAllComponents("#Filtrate").forEach(v => v.onCancel())
                } catch (error) {}
            })
        },
        changeType1(e) {
            this.setData({
                [`content.${e.currentTarget.dataset.node}`]: e.detail,
            })
            this.getList(true)
        },
        changeType2(e) {
            this.setData({
                [`content.where.${e.currentTarget.dataset.node}`]: e.detail,
            })
            this.getList(true)
        },
        changeType3({
            detail
        }) {
            this.setData({
                "content.where.tradefield": detail,
            })
            this.getList(true)
        },
        showTips() {
            getApp().globalData.Language.showToast('统计项目生命周期中的各个关键节点之间平均周期')
        },
        toSelectProject({
            detail
        }) {
            if (detail) {
                this.setData({
                    "content.where.sa_projectid": ''
                })
                this.getList(true);
            } else {
                _Http.data = JSON.parse(JSON.stringify(this.data));
                delete _Http.data.list;
                delete _Http.data.language;
                delete _Http.data.sColors;
                delete _Http.data.nodes;
                wx.navigateTo({
                    url: '/salesPanel/dataOverview/Project/projectList'
                })

                function selectItem(item) {
                    this.setData({
                        "content.where.sa_projectid": item.sa_projectid,
                    })
                    this.selectComponent("#Filtrate1").setShowArrText([item.projectname]);
                    this.getList(true);
                    wx.navigateBack()
                    delete _Http.selectItem;
                }
                _Http.selectItem = selectItem.bind(this);
            }
        }
    }
})