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

Page({
    data: {
        detail: {},
        list: [],
        "content": {
            "where": {
                "condition": ""
            },
            "pageNumber": 1,
            "pageTotal": 1,
            "pageSize": 20
        }
    },
    onLoad(options) {
        getApp().globalData.Language.getLanguagePackage(this, '营销费用明细')

        this.data.content.ownertable = options.ownertable;
        this.data.content.ownerid = options.ownerid;
        console.log("options.isAdmin", options.isAdmin)
        this.setData({
            isAdmin: options.isAdmin == 'true',
            detail: JSON.parse(options.item),
            userid: wx.getStorageSync('userMsg').userid
        })
        this.getList(true);
        _Http.updateExpenseBreakdownList = function () {
            let content = this.data.content;
            content.pageSize = content.pageSize * (content.pageNumber - 1)
            content.pageNumber = 1;
            content.pageTotal = 1;
            _Http.basic({
                id: "20240629082704",
                content,
            }).then((res) => {
                console.log("更新费用明细列表", res);
                if (res.code == '1') this.setData({
                    list: res.data.map(v => {
                        v.showAmount = CNY(v.amount)
                        return v
                    })
                })
            });
            let params = {
                "id": 20221026085601,
                "content": {
                    "sys_datafollowupid": this.data.content.ownerid
                }
            }
            if (this.data.content.ownertable == 'sat_orderclue') params = {
                "id": 20240702102404,
                "content": {
                    "sat_ordercluefollowuplogid": this.data.content.ownerid
                }
            }
            _Http.basic(params).then(res => {
                if (res.code == '1') {
                    this.setData({
                        "detail.salesfeesamount": res.data.salesfeesamount,
                        "detail.showsalesfeesamount": CNY(res.data.salesfeesamount),
                    })
                    _Http.updateEBList && _Http.updateEBList(res.data.salesfeesamount, CNY(res.data.salesfeesamount))
                }
            })
        }.bind(this)
    },
    getList(init = false) {
        if (init.detail != undefined) init = init.detail;
        let content = this.data.content;
        if (init) content.pageNumber = 1;
        if (content.pageNumber > content.pageTotal) return;
        _Http.basic({
            "id": 20240629082704,
            content
        }).then(res => {
            console.log("费用明细列表", res)
            this.selectComponent('#ListBox').RefreshToComplete();
            res.data = res.data.map(v => {
                v.showAmount = CNY(v.amount)
                return v
            })
            this.setData({
                list: res.pageNumber == 1 ? res.data : this.data.list.concat(this.data.list),
                "content.pageNumber": res.pageNumber + 1,
                "content.pageTotal": res.pageTotal,
                "content.sort": res.sort
            })
        })
    },
    startSearch({
        detail
    }) {
        this.setData({
            'content.where.condition': detail
        })
        this.getList(true);
    },
    editItem(e) {
        const {
            item
        } = e.currentTarget.dataset;
        item.ownertable = this.data.content.ownertable;
        item.ownerid = this.data.content.ownerid;
        wx.navigateTo({
            url: `/packageA/expenseBreakdown/edit?item=${JSON.stringify(item)}`,
        })
    },
    deleteItem(e) {
        const {
            item
        } = e.currentTarget.dataset,
            that = this;
        console.log(item)
        wx.showModal({
            title: getApp().globalData.Language.getMapText('提示'),
            content: getApp().globalData.Language.getMapText('是否确定删除该明细'),
            cancelText: getApp().globalData.Language.getMapText('取消'),
            confirmText: getApp().globalData.Language.getMapText('确定'),
            complete: ({
                confirm
            }) => {
                if (confirm) _Http.basic({
                    "id": 20240611095304,
                    "content": {
                        "sa_salesfeesid": item.sa_salesfeesid,
                        "sa_salesfeesdetails": [{
                            "sa_salesfeesdetailid": item.sa_salesfeesdetailid,
                            "followupdate": item.followupdate
                        }]
                    }
                }).then(res => {
                    console.log("删除明细", res)
                    if (res.code != '1') return
                    wx.showToast({
                        title: res.code != '1' ? res.msg : getApp().globalData.Language.getMapText('删除成功'),
                        icon: "none"
                    })
                    if (res.code != '1') return;
                    _Http.updateExpenseBreakdownList();

                })
            }
        })


    },
    onReady() {
        this.setListHeight()
    },
    toInsertion() {
        let salesfeesdetail = {
            "ownertable": this.data.content.ownertable,
            "ownerid": this.data.content.ownerid,
            "followupdate": this.data.detail.createdate,
        }
        wx.navigateTo({
            url: `/packageA/expenseBreakdown/insertion?salesfeesdetail=${JSON.stringify(salesfeesdetail)}`,
        })
    },
    /* 设置页面高度 */
    setListHeight() {
        this.selectComponent("#ListBox").setHeight(".search", this);
    },
    onUnload() {
        delete _Http.updateEBList
        delete _Http.updateExpenseBreakdownList
        getCurrentPages().forEach(v => {
            v.getDetail && v.getDetail()
        })
    }
})