const _Http = getApp().globalData.http,
    currency = require("../../utils/currency"),
    CNY = (value, symbol = "￥", precision = 2) => currency(value, {
        symbol,
        precision
    }).format();
Page({
    data: {
        searchShow: false,
        searchValue: "",
        typeShow: false,
        tradefieldShow: false
    },
    onLoad(options) {
        let data = JSON.parse(options.data)
        const content = Object.assign(data, {
                "pageNumber": 1,
                "pageSize": 20,
                "where": {
                    "condition": "",
                    "type": "",
                    "tradefield": "",
                    isleave: data.where.isleave
                }
            }),
            ids = {
                "订单": 2025032613405702,
                "出货": 2025032615330102,
                "开票": 2025032616343302,
                "回款": 2025032616352402,
            };
        console.log("content", content)

        getApp().globalData.Language.getLanguagePackage(this, content.typeName + '明细')

        this.setData({
            content,
            id: ids[content.typeName],
            siteid: wx.getStorageSync('userMsg').siteid
        })
        _Http.basic({
            "classname": "sysmanage.develop.optiontype.optiontype",
            "method": "optiontypeselect",
            "content": {
                "pageNumber": 1,
                "pageSize": 1000,
                "typename": "ordertype",
                "parameter": {}
            },
        }).then(res => {
            console.log("订单类型", res)
            let types = res.data.map(v => {
                return {
                    name: getApp().globalData.Language.getMapText(v.remarks || v.value),
                    value: v.value
                }
            })
            types.unshift({
                name: getApp().globalData.Language.getMapText('全部'),
                value: "",
                color: "#3874F6"
            })
            this.setData({
                types
            })
        })
        _Http.basic({
            "classname": "sysmanage.develop.optiontype.optiontype",
            "method": "optiontypeselect",
            "content": {
                "pageNumber": 1,
                "pageSize": 1000,
                "typename": "tradefield",
            },
        }).then(res => {
            console.log("领域", res)
            let tradefields = res.data.map(v => {
                return {
                    name: getApp().globalData.Language.getMapText(v.remarks || v.value),
                    value: v.value
                }
            })
            tradefields.unshift({
                name: getApp().globalData.Language.getMapText('全部'),
                value: "",
                color: "#3874F6"
            })
            this.setData({
                tradefields
            })
        })
        this.getList()
    },
    changeSearchShow() {
        this.setData({
            searchShow: !this.data.searchShow
        })
        this.selectComponent('#ListBox').automaticSetHei();
    },
    getList(init = false) {
        _Http.init(this.data.content, init).then(content => {
            _Http.basic({
                "id": this.data.id,
                content
            }).then(res => {
                console.log(content.typeName + "明细列表", res)
                this.selectComponent('#ListBox').automaticSetHei();

                this.selectComponent('#ListBox').RefreshToComplete();
                if (res.code != '1') return wx.showToast({
                    title: res.data,
                    icon: "none"
                })
                res.data = res.data.map(v => {
                    v.marketprice = CNY(v.marketprice)
                    v.amount = CNY(v.amount)
                    v.invoiceamount = CNY(v.invoiceamount)
                    v.uninvoiceamount = CNY(v.uninvoiceamount)
                    v.writeoffamount = CNY(v.writeoffamount)
                    v.unwriteoffamount = CNY(v.unwriteoffamount)
                    v.returnamount = CNY(v.returnamount)
                    v.price = CNY(v.price)
                    v.logisticsamount = CNY(v.logisticsamount)
                    v.expanded = false;
                    return v
                })
                this.setData({
                    list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
                    "content.pageNumber": res.pageNumber + 1,
                    "content.pageSize": res.pageSize,
                    "content.pageTotal": res.pageTotal,
                    "total": res.total,
                })
                this.onCancel()
            })
        })
    },
    changeExpanded(e) {
        console.log(e)
        const {
            index
        } = e.currentTarget.dataset;
        this.setData({
            [`list[${index}].expanded`]: !this.data.list[index].expanded
        })
    },
    onSearch() {
        this.data.content.where.condition = this.data.searchValue;
        this.getList(true);
    },
    onChange(event) {
        this.setData({
            searchValue: event.detail
        })
    },
    onClear() {
        this.setData({
            searchValue: "",
            "content.where.condition": ""
        })
        this.getList(true);
    },
    openTypeShow() {
        this.setData({
            typeShow: true
        })
    },
    openTradefieldShow() {
        this.setData({
            tradefieldShow: true
        })
    },
    onSelect(event) {
        const {
            value
        } = event.detail;
        if (this.data.content.where.type == value) return this.onCancel();
        this.setData({
            types: this.data.types.map(item => {
                item.color = item.value == value ? "#3874F6" : "";
                item.loading = item.value == value ? true : false;
                return item
            })
        })
        this.setData({
            "content.where.type": value
        })
        this.getList(true)
    },
    onSelect1(event) {
        const {
            value
        } = event.detail;
        if (this.data.content.where.tradefield == value) return this.onCancel();
        this.setData({
            tradefields: this.data.tradefields.map(item => {
                item.color = item.value == value ? "#3874F6" : "";
                item.loading = item.value == value ? true : false;
                return item
            })
        })
        this.setData({
            "content.where.tradefield": value
        })
        this.getList(true)
    },
    onCancel() {
        this.setData({
            typeShow: false,
            types: this.data.types.map(item => {
                item.loading = false;
                return item
            }),
            tradefieldShow: false,
            tradefields: this.data.tradefields.map(item => {
                item.loading = false;
                return item
            }),
        })
    }
})