const _Http = getApp().globalData.http;
Component({
    options: {
        addGlobalClass: true
    },
    properties: {
        disabled: {
            type: Boolean,
            value: true
        }
    },
    lifetimes: {
        attached: function () {
            getApp().globalData.Language.getLanguagePackage(this)
        },
    },
    data: {
        content: {
            nocache: true,
            pageNumber: 1,
            pageSize: 10,
            pageTotal: 1,
            total: null,
            where: {
                condition: ""
            }
        },
        list: [],
        showSearch: false,
        focus: false,
        condition: "",
        sa_competitor_advid: 0
    },
    methods: {
        toSearch() {
            if (this.data.showSearch && this.data.content.where.condition) {
                this.data.content.where.condition = '';
                this.getList("", true);
            } else if (this.data.condition) {
                this.data.content.where.condition = this.data.condition;
                this.setData({
                    condition: this.data.condition
                })
                this.getList("", 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("", true)
        },
        getList(id, init = false) {
            let content = this.data.content
            content.sa_competitorid = id || content.sa_competitorid;
            if (init) {
                content.pageNumber = 1
                content.pageTotal = 1
            }
            _Http.basic({
                "id": 2025031809222102,
                content
            }).then(res => {
                console.log("优劣势列表", res)
                if (res.code != '1') return wx.showToast({
                    title: res.data,
                    icon: "none"
                });
                content.pageNumber = res.pageNumber + 1
                content.pageTotal = res.pageTotal
                content.total = res.total
                let defaultItem = res.data.find(v => v.isdefault)
                if (defaultItem) this.setData({
                    sa_competitor_advid: defaultItem.sa_competitor_advid
                })
                this.setData({
                    list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
                    content
                })
            })
        },
        /* 去添加 */
        toAdd() {
            wx.navigateTo({
                url: `/packageA/opponent/modules/aAD/insert?id=` + this.data.content.sa_competitorid,
            })
        },
        handleDelete(item) {
            let that = this;
            return new Promise((resolve) => {
                wx.showModal({
                    title: getApp().globalData.Language.getMapText('提示'),
                    content: getApp().globalData.Language.getMapText('是否确定删除该跟进动态'),
                    complete: ({
                        confirm
                    }) => {
                        if (confirm) {
                            _Http.basic({
                                "id": 20220930121701,
                                "content": {
                                    "sys_datafollowupid": item.sys_datafollowupid,
                                    "ownertable": that.data.ownertable,
                                    "ownerid": that.data.ownerid,
                                    deletereason: ""
                                }
                            }).then(res => {
                                console.log("删除", res);
                                getApp().globalData.Language.showToast(res.code != 1 ? res.msg : "删除成功")
                                resolve(res.code)
                                if (res.msg != '成功') return;
                                that.setData({
                                    list: that.data.list.filter(v => v.sys_datafollowupid != item.sys_datafollowupid),
                                    'content.total': that.data.content.total - 1
                                })
                            })
                        } else {
                            resolve(0)
                        }
                    }
                })
            })
        },
        changeDefault({
            detail
        }) {
            _Http.basic({
                "content": {
                    "sa_competitor_advid": detail
                },
                "id": 2025031809224302,
            }).then(res => {
                console.log("切换默认", res)
                this.updataDetail()
            })
        },
        handleItem(e) {
            console.log(e)
            const {
                item,
                name
            } = e.target.dataset,
                that = this;
            if (name == 'delete') {
                wx.showModal({
                    content: getApp().globalData.Language.getMapText('是否确定删除') + '?',
                    cancelText: getApp().globalData.Language.getMapText('取消'),
                    confirmText: getApp().globalData.Language.getMapText('确定'),
                    complete: ({
                        confirm
                    }) => {
                        if (confirm) _Http.basic({
                            "content": {
                                "sa_competitor_advid": item.sa_competitor_advid
                            },
                            "id": 2025031809214002,
                        }).then(res => {
                            console.log("删除", res)
                            getApp().globalData.Language.showToast(res.code != 1 ? res.msg : "删除成功")
                            if (res.code == 1) {
                                that.setData({
                                    list: that.data.list.filter(v => v.sa_competitor_advid != item.sa_competitor_advid),
                                    "content.total": that.data.content.total - 1
                                })
                                if (item.isdefault) this.updataDetail()
                            }
                        })
                    }
                })
            } else {
                wx.navigateTo({
                    url: `/packageA/opponent/modules/aAD/insert?id=` + that.data.content.sa_competitorid + '&data=' + JSON.stringify(item),
                })
            }
        },
        updataDetail() {
            getCurrentPages().find(v => v.__route__ == 'packageA/opponent/detail').getDetail()
        }
    }
})