const _Http = getApp().globalData.http;
let items = [],
    countDown = null;
Component({
    properties: {
        quotedpricenotes: Array,
        disabled: Boolean
    },
    options: {
        addGlobalClass: true
    },
    data: {
        sa_quotedpriceid: "",
        tabShow: false,
        dialog: false,
        newExplain: "",
        content: {
            nocache: true,
            pageNumber: 1,
            pageTotal: 1,
            total: null
        },
    },
    methods: {
        getList(id, init) {
            let content = this.data.content;
            content.sa_quotedpriceid = id;
            if (init) content.pageNumber = 1;
            _Http.basic({
                "id": "20230211155603",
                content
            }).then(res => {
                console.log("其他费用列表", res)
                if (res.msg != '成功') return wx.showToast({
                    title: res.data,
                    icon: "none"
                })
                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,
                    "content.total": res.total,
                    sa_quotedpriceid: id,
                    tabShow: true
                })
            })
        },
        /* 前往新增其他费用 */
        addExpense() {
            wx.navigateTo({
                url: '/packageA/offers/modules/pettyExpense/update?id=' + this.data.sa_quotedpriceid
            });
            getApp().globalData.saveFun = this.getList.bind(this);
        },
        /* 修改费用 */
        changeExpense({
            detail
        }) {
            let i = items.findIndex(v => v.index == detail.index);
            i == -1 ? items.push(detail) : items[i] = detail;
            clearTimeout(countDown);
            countDown = setTimeout(this.handleChangeExpense.bind(this), 500);
        },
        /* 处理修改费用 */
        handleChangeExpense() {
            countDown = null;
            _Http.basic({
                "id": 20230211155503,
                "version": 1,
                "content": {
                    "sa_quotedpriceid": this.data.sa_quotedpriceid,
                    "items": items.map(v => v.item)
                }
            }).then(res => {
                console.log("修改费用列表", res);
                if (res.msg != '成功') return wx.showToast({
                    title: res.msg,
                    icon: "none",
                    mask: true
                });
                items.forEach(v => this.data.list[v.index] = v.item);
                this.setData({
                    list: this.data.list
                });
                items = [];
            })
        },
        /* 添加报价说明 */
        addQuotedpricenotes() {
            this.setData({
                dialog: true
            })
        },
        /* 完成情况说明输入 */
        areaInput(e) {
            this.setData({
                newExplain: e.detail.value
            })
        },
        /* 确认添加 */
        onConfirm() {
            let newExplain = this.data.newExplain;
            if (newExplain == '') return wx.showToast({
                title: '提交内容不可为空',
                icon: "none",
                mask: true
            });
            let page = getCurrentPages()[getCurrentPages().length - 1],
                data = page.data.detail;
            data.quotedpricenotes.push(newExplain);
            _Http.basic({
                "id": 20221020164803,
                "version": 1,
                "content": data
            }).then(res => {
                console.log("添加报价说明", res)
                wx.showToast({
                    title: res.msg == '成功' ? '添加成功！' : res.msg,
                    icon: "none",
                    mask: true
                });
                if (res.msg == '成功') {
                    page.setData({
                        "detail.quotedpricenotes": res.data.quotedpricenotes
                    });
                    this.setData({
                        newExplain: ""
                    })
                }
            })
        },
        onCancel() {
            this.setData({
                dialog: false
            })
        },
        /* 移除说明 */
        deleteItem(e) {
            console.log(e)
            wx.showModal({
                title: '提示',
                content: '是否确定移除该说明',
                complete: (res) => {
                    if (res.confirm) {
                        let i = e.currentTarget.dataset.index;
                        let page = getCurrentPages()[getCurrentPages().length - 1],
                            data = page.data.detail;
                        data.quotedpricenotes.splice(i, 1);
                        _Http.basic({
                            "id": 20221020164803,
                            "version": 1,
                            "content": data
                        }).then(res => {
                            console.log("删除报价说明", res)
                            wx.showToast({
                                title: res.msg == '成功' ? '移除成功！' : res.msg,
                                icon: "none",
                                mask: true
                            });
                            if (res.msg == '成功') {
                                page.setData({
                                    "detail.quotedpricenotes": res.data.quotedpricenotes
                                });
                            }
                        })
                    }
                }
            })
        },
        handleDetele({
            detail
        }) {
            _Http.basic({
                "id": 20230211155703,
                "version": 1,
                "content": {
                    "sa_quotedprice_specialitemsids": [detail]
                }
            }).then(res => {
                console.log("删除其他费用", res)
                wx.showToast({
                    title: res.msg == '成功' ? '删除成功' : res.msg,
                    icon: "none",
                    mask: true
                })
                if (res.msg == '成功') this.setData({
                    list: this.data.list.filter(v => v.sa_quotedprice_specialitemsid != detail)
                })
            })
        }
    }
})