const _Http = getApp().globalData.http;
Page({
    data: {
        navList: [{
            label: "排序",
            icon: "icon-jiangxu1",
            color: "",
            width: "",
            id: "sort"
        }, {
            label: "筛选",
            icon: "icon-shaixuan",
            color: "",
            width: "",
            id: "2"
        }],
        content: {
            "nocache": true,
            "pageNumber": 1,
            "pageTotal": 1,
            "pageSize": 20,
            "isExport": 0,
            "where": {
                "startdate": "",
                "enddate": "",
            },
            "sort": []
        },
        filter: {
            show: false,
            startdate: "",
            enddate: ""
        }
    },
    onLoad(options) {
        getApp().globalData.Language.getLanguagePackage(this, '竞争对手');
        this.setData({
            insert: wx.getStorageSync('auth').wcompetitor.options.some(v => v == 'insert'), //查询新增权限
        })
        this.getList();
    },
    /* 获取列表 */
    getList(init = false, data) {
        if (init.detail != undefined) init = init.detail;
        let content = this.data.content;
        if (init) content.pageNumber = 1;
        if (content.pageNumber > content.pageTotal) return;
        if (data) {
            content.where.startdate = data.startdate;
            content.where.enddate = data.enddate;
        }
        _Http.basic({
            "id": 20221018164302,
            content
        }).then(res => {
            console.log("竞争对手列表", res)
            this.selectComponent('#ListBox').RefreshToComplete();
            if (res.code != '1') return wx.showToast({
                title: res.data,
                icon: "none"
            })
            this.setData({
                'content.pageNumber': res.pageNumber + 1,
                'content.pageTotal': res.pageTotal,
                'content.total': res.total,
                'content.sort': res.sort,
                list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data)
            });
            this.getTags();
        })
    },
    /* 处理搜索 */
    onSearch({
        detail
    }) {
        this.setData({
            "content.where.condition": detail
        });
        this.getList(true);
    },
    /* 获取列表标签 */
    getTags() {
        let list = this.data.list,
            ownerids = list.map(v => v.sa_competitorid);
        _Http.basic({
            "id": 20221018102001,
            "content": {
                nocache: true,
                "ownertable": "sa_competitor",
                ownerids
            }
        }).then(res => {
            console.log("标签", res)
            for (let key in res.data) {
                let index = list.findIndex(v => v.sa_competitorid == key);
                list[index].tags = res.data[key]
            };
            this.setData({
                list
            })
        })
    },
    /* 筛选日期范围 */
    changeDate(e) {
        const name = e.currentTarget.dataset.name,
            value = e.detail.value;
        this.setData({
            [`filter.${name}`]: value
        })
    },
    /* 顶部条件导航回调 */
    navClick({
        detail
    }) {
        switch (detail.id) {
            case '1':
                this.setData({
                    classShow: true
                })
                break;
            case '2':
                this.setData({
                    'filter.show': true
                })
                break;
        }
    },
    /* 筛选状态选择 */
    selectStatus(e) {
        const {
            item
        } = e.currentTarget.dataset;
        this.setData({
            "filter.statusActive": this.data.filter.statusActive == item ? "" : item
        })
    },
    /* 筛选状态选择 */
    typeStatus(e) {
        const {
            item
        } = e.currentTarget.dataset;
        this.setData({
            "filter.typeActive": this.data.filter.typeActive == item ? "" : item
        })
    },
    /* 处理筛选 */
    handleFilter({
        detail
    }) {
        const data = this.data.filter;
        if (data.typeActive) data.typeActive = data.typeActive == '普通报价' ? 0 : 1;
        switch (detail) {
            case 'confirm':
                this.setData({
                    'filter.show': false
                });
                this.getList(true, data);
                break;
            case 'reset':
                this.setData({
                    'filter.statusActive': "",
                    'filter.typeActive': "",
                    'filter.startdate': "",
                    'filter.enddate': "",
                });
                this.getList(true, this.data.filter)
                break;
            case 'close':
                this.setData({
                    'filter.show': false
                });
                break;
        }
    },
    addOpponent() {
        wx.navigateTo({
            url: './add'
        })
    }
})