const _Http = getApp().globalData.http,
    currency = require("../../utils/currency"),
    CNY = (value, symbol = "￥", precision = 2) => currency(value, {
        symbol,
        precision
    }).format();
import {
    getHeight
} from "../../utils/getHeight";
Component({
    properties: {

    },
    options: {
        addGlobalClass: true
    },
    lifetimes: {
        attached: function () {
            getApp().globalData.Language.getLanguagePackage(this)
            this.setData({
                list: this.data.list.map(v => {
                    v.name = getApp().globalData.Language.getMapText(v.type);
                    v.color = v.datatype == this.data.content.datatype ? "#3874F6" : "";
                    return v
                }),
                dates: this.data.dates.map(v => {
                    v.name = getApp().globalData.Language.getMapText(v.value);
                    v.color = v.value == this.data.content.dateType ? "#3874F6" : "";
                    return v
                }),
                "content.dataid": wx.getStorageSync('userMsg').userid,
                "content.username": wx.getStorageSync('userMsg').name,
            })
        }
    },
    data: {
        actionShow: false,
        detaShow: false,
        list: [{
                type: "订单",
                datatype: 1
            },
            {
                type: "出货",
                datatype: 2
            },
            {
                type: "开票",
                datatype: 3
            },
            {
                type: "回款",
                datatype: 4
            }
        ],
        dates: [{
            value: "去年"
        }, {
            value: "上月"
        }, {
            value: "本年"
        }, {
            value: "本季"
        }, {
            value: "本月"
        }],
        "content": {
            type: 0,
            "dateType": "本年",
            "datatype": 1,
            where: {}
        },
        height: 0,
    },
    methods: {
        getList(init = false) {
            let content = this.data.content,
                item = this.data.list[content.datatype - 1];
            const {
                dataid,
                type,
                username,
                isleave
            } = getCurrentPages()[getCurrentPages().length - 1].data;
            if (content.dataid != dataid || content.type != type || isleave != isleave) {
                init = true
            }
            content.dataid = dataid;
            content.type = type;
            content.username = username;
            content.isleave = isleave;
            content.where.isleave = isleave;
            if (!init && item.init) return this.onCancel();;
            _Http.basic({
                "id": 20231009125304,
                content
            }).then(res => {
                console.log(item.type + "面板", res)
                this.selectComponent('#ListBox' + content.datatype).RefreshToComplete();
                if (res.code != '1') return wx.showToast({
                    title: res.data,
                    icon: "none"
                })
                let data = res.data,
                    dividend = wx.getStorageSync('languagecode') == 'ZH' ? 10000 : 1000;
                data.target_l = CNY(data.target_l / dividend, "");
                data.nowTarget_l = CNY(data.nowTarget_l / dividend, "");
                data.unamount = CNY(data.unamount / dividend);
                data.amount = CNY(data.amount / dividend, "");
                data.unnowTarget_l = CNY(data.unnowTarget_l / dividend);
                data.wcamount = (data.wcamount * 100).toFixed(2) - 0; // 达成率
                data.tbxsje = (data.tbxsje * 100).toFixed(2) - 0; // 同比增长率
                data.hbxsje = (data.hbxsje * 100).toFixed(2) - 0; // 环比增长率
                data.wcnowTarget_l = (data.wcnowTarget_l * 100).toFixed(2); // 累计目标达成率
                data.init = true;
                this.setData({
                    [`list[${content.datatype - 1}]`]: Object.assign(item, data),
                })
                this.setHeight()
                this.onCancel();
            })
        },
        setHeight() {
            getHeight("#head", this).then(height => {
                this.setData({
                    height: height - 20
                })
            })
        },
        swiperChange({
            detail
        }) {
            this.setData({
                "content.datatype": detail.currentItemId,
                list: this.data.list.map(item => {
                    item.color = item.datatype == detail.currentItemId ? "#3874F6" : "";
                    return item
                })
            })
            this.getList();
        },
        openAction() {
            this.setData({
                actionShow: true
            })
        },
        openDateType() {
            this.setData({
                detaShow: true
            })
        },
        onSelect(event) {
            const {
                datatype
            } = event.detail;
            if (this.data.content.datatype == datatype) return this.onCancel();
            this.setData({
                "content.datatype": datatype,
                list: this.data.list.map(item => {
                    item.color = item.datatype == datatype ? "#3874F6" : "";
                    item.loading = item.datatype == datatype ? true : false;
                    return item
                })
            })
        },
        dateOnSelect(event) {
            const {
                value
            } = event.detail;
            if (this.data.content.dateType == value) return this.onCancel();
            this.setData({
                "content.dateType": value,
                dates: this.data.dates.map(item => {
                    item.color = item.value == value ? "#3874F6" : "";
                    item.loading = item.value == value ? true : false;
                    return item
                }),
            })
            this.getList(true)
        },
        onCancel() {
            this.setData({
                actionShow: false,
                detaShow: false,
                list: this.data.list.map(item => {
                    item.loading = false;
                    return item
                }),
                dates: this.data.dates.map(item => {
                    item.loading = false;
                    return item
                })
            })
        },
        showExplain() {
            getApp().globalData.Language.modeBoxPrompts('①累计日标金额:到当前时间为止的累计的目标金额，不计算未经历时间的目标金额;②同比增长率:同比增长率是指本期和上一年同期相比较的增长率,计算公式为:同比增长率=(本期数-同期数):同期数x100%。③环比增长率:环比增长率是指本期和上期相比较的增长率,计算公式为:环比增长率 =(本期数-上期数)÷上期数 x100%。4日标达成率=实际金额:目标金额x100%')
        },
        toDetail() {
            let content = this.data.content
            content.typeName = this.data.list.find(v => v.datatype == content.datatype).type;
            wx.navigateTo({
                url: '/salesPanel/panel/detail?data=' + JSON.stringify(content),
            })
        }
    }
})