const _Http = getApp().globalData.http,
    currency = require("../../../../utils/currency"),
    CNY = value => currency(value, {
        symbol: "￥",
        precision: 2
    }).format();
Component({
    properties: {
        disabled: {
            type: Boolean,
            value: false
        },
        isshowsalesfeesamount: {
            type: Boolean,
            value: false
        },
        isAdmin: {
            type: Boolean,
            value: false
        },
        expenseBreakdown: {
            type: Boolean,
        }
    },
    lifetimes: {
        attached() {
            getApp().globalData.Language.getLanguagePackage(this)
        }
    },
    options: {
        addGlobalClass: true
    },
    data: {
        sat_orderclueid: null,
        content: {
            nocache: true,
            pageNumber: 1,
            pageTotal: 1,
            pageSize: 20,
            total: null,
            where: {
                condition: ""
            }
        },
        showSearch: false,
        focus: false,
        condition: "",
        poputShow: false,
        followList: []
    },
    lifetimes: {
        attached: function () {
            this.setData({
                userid: wx.getStorageSync('userMsg').userid,
            })
            getApp().globalData.Language.getLanguagePackage(this)
        }
    },
    methods: {
        toSearch() {
            if (this.data.showSearch && this.data.content.where.condition) {
                this.data.content.where.condition = '';
                this.getList(this.data.sat_orderclueid, true);
            } else if (this.data.condition) {
                this.data.content.where.condition = this.data.condition;
                this.setData({
                    condition: this.data.condition
                })
                this.getList(this.data.sat_orderclueid, true);
            }
            this.setData({
                showSearch: !this.data.showSearch
            })
            setTimeout(() => {
                this.setData({
                    focus: this.data.showSearch
                })
            }, 300)
        },
        onChange({
            detail
        }) {
            this.data.condition = detail;
        },
        onSearch({
            detail
        }) {
            this.data.content.where.condition = detail;
            this.getList(this.data.sat_orderclueid, true)
        },
        getList(id, init = false) {
            let content = this.data.content;
            if (init) content.pageNumber = 1;
            content.sat_orderclueid = id;
            _Http.basic({
                id: 20221101094602,
                content
            }).then(res => {
                console.log('线索跟进', res);
                if (this.data.isshowsalesfeesamount) res.data = res.data.map(v => {
                    v.showsalesfeesamount = CNY(v.salesfeesamount || 0)
                    return v
                })
                res.data = res.data.map(v => {
                    v.contactsName = v.contacts.map(s => s.name)
                    return v
                })
                this.setData({
                    followList: res.pageNumber == 1 ? res.data : this.data.followList.concat(res.data),
                    "content.pageNumber": res.pageNumber + 1,
                    "content.pageTotal": res.pageTotal,
                    "content.total": res.total,
                    sat_orderclueid: id,
                    userid: wx.getStorageSync('userMsg').userid
                })
            })
        },
        viewFiles(e) {
            const {
                item
            } = e.currentTarget.dataset;
            this.setData({
                poputShow: true
            })
            this.selectComponent("#Yl_Files").handleFiles(item.attinfo, true)
        },
        onClosePoput() {
            this.setData({
                poputShow: false
            })
            this.selectComponent("#Yl_Files").initData()
        },
        deleteItem(e) {
            const {
                item
            } = e.currentTarget.dataset,
                that = this;
            console.log(that.data)
            wx.showModal({
                title: getApp().globalData.Language.getMapText('提示'),
                content: getApp().globalData.Language.getMapText('是否确定删除该跟进记录'),
                cancelText: getApp().globalData.Language.getMapText('取消'),
                confirmText: getApp().globalData.Language.getMapText('确定'),
                complete: (res) => {
                    if (res.confirm) _Http.basic({
                        "id": 20221208112002,
                        "content": {
                            "sat_ordercluefollowuplogid": item.sat_ordercluefollowuplogid
                        }
                    }).then(res => {
                        console.log("删除跟进记录", res)
                        wx.showToast({
                            title: res.code != '1' ? res.msg : getApp().globalData.Language.getMapText('删除成功'),
                            icon: "none"
                        });
                        that.getReadList();
                    })
                }
            })

        },
        getReadList() {
            let content = JSON.parse(JSON.stringify(this.data.content))
            content.pageSize = (content.pageNumber - 1) * content.pageSize;
            content.pageNumber = 1;
            _Http.basic({
                id: 20221101094602,
                content
            }).then(res => {
                console.log('线索跟进记录', res);
                this.setData({
                    followList: res.data,
                    "content.total": res.total
                });
                delete(_Http.changeItem)
            })
        },
        editItem(e) {
            const {
                item
            } = e.currentTarget.dataset;
            wx.navigateTo({
                url: '/packageA/publicClue/modules/follow/insert?data=' + JSON.stringify(item),
            })
            _Http.changeItem = this.getReadList.bind(this);
        },
        expenseBreakdown(e) {
            const {
                item,
                index
            } = e.currentTarget.dataset;
            wx.navigateTo({
                url: `/packageA/expenseBreakdown/index?ownertable=sat_orderclue&ownerid=${item.sat_ordercluefollowuplogid}&isAdmin=${this.data.isAdmin}&item=${
              JSON.stringify(item)
            }`,
            })
            _Http.updateEBList = function (salesfeesamount, showsalesfeesamount) {
                this.setData({
                    [`followList[${index}].salesfeesamount`]: salesfeesamount,
                    [`followList[${index}].showsalesfeesamount`]: showsalesfeesamount,
                })
            }.bind(this)
        },
        comment(e) {
            let page = this.selectComponent('#' + e.currentTarget.id);
            page.changeShow()
        },
        toAdd() {
            wx.navigateTo({
                url: `/packageA/publicClue/modules/follow/insert?sat_orderclueid=` + this.data.sat_orderclueid + '&resource=公海线索'
            })
            _Http.changeItem = function () {
                this.getList(this.data.sat_orderclueid, true);
            }.bind(this)
        },
        updateCommentList({
            detail
        }) {
            console.log("detail", detail)
            detail.pageSize = 99999;
            _Http.basic({
                "id": 20240920092204,
                content: detail
            }).then(res => {
                console.log("更新评论列表", res)
                if (res.code == 1) {
                    let idName = detail.ownertable + 'id';
                    const index = this.data.followList.findIndex(v => v[idName] == detail.ownerid)
                    if (index != -1) {
                        let item = this.data.followList[index];
                        item.commentqty = res.total == 0 ? 0 : res.data[0].totalqty;
                        item.comment = res.data;
                        this.setData({
                            [`followList[${index}]`]: item
                        })
                    }
                }
            })
        }
    }
})