const _Http = getApp().globalData.http;
Page({
    data: {
        loading: true,
        filterShow: false,
        navList: [{
            label: "我负责的",
            icon: "icon-webxialaxuanxiangjiantou",
            color: "",
            width: "",
            id: "1"
        }, {
            label: "排序",
            icon: "icon-jiangxu1",
            color: "",
            width: "",
            id: "sort"
        }, {
            label: "筛选",
            icon: "icon-shaixuan",
            color: "",
            width: "",
            id: "2"
        }],
        classShow: false,
        content: {
            "isAll": 0,
            "nocache": true,
            "version": 1,
            "pageNumber": 1,
            "pageSize": 20,
            type: 1,
            "where": {
                "condition": ""
            },
            "sort": []
        }
    },
    async onLoad(options) {
        getApp().globalData.Language.getLanguagePackage(this, '市场活动');
        this.setData({
            insert: wx.getStorageSync('auth').wmarketing_activity.options.some(v => v == 'insert'), //查询新增权限
            classActions: wx.getStorageSync('templetList').map((v, i) => {
                return {
                    name: v.templetname,
                    index: v.templetid,
                    color: i == 0 ? '#3874F6' : '',
                    i: i,
                }
            }),
            'navList[0].label': wx.getStorageSync('templetList')[0].templetname,
            "content.type": wx.getStorageSync('templetList')[0].templetid,
        });
        this.getList();

        const filtratelist = [{
            label: "活动类型",
            index: null,
            showName: "value", //显示字段
            valueKey: "type", //返回Key
            selectKey: "value", //传参 代表选着字段 不传参返回整个选择对象
            value: "", //选中值
            list: await this.getTypeList()
        }, {
            label: "活动状态",
            index: null,
            showName: "value", //显示字段
            valueKey: "status", //返回Key
            selectKey: "value", //传参 代表选着字段 不传参返回整个选择对象
            value: "", //选中值
            list: [{
                value: '新建'
            }, {
                value: '发布'
            }, {
                value: '结束'
            }]
        }];
        this.setData({
            filtratelist
        })
    },
    /* 获取活动类型 */
    getTypeList() {
        return new Promise((resolve) => {
            _Http.basic({
                "id": 20220901092501,
                "content": {
                    "typename": 'campaigntype'
                }
            }).then(res => {
                resolve(res.code == '1' ? res.data : [])
            })
        })
    },
    /* 获取列表 */
    getList(init = false) {
        _Http.init(this.data.content, init).then(content => {
            _Http.basic({
                "id": 20221101095102,
                content
            }).then(res => {
                console.log("市场活动", res)
                this.selectComponent('#ListBox').RefreshToComplete();
                if (res.code != '1') return wx.showToast({
                    title: res.data,
                    icon: "none"
                })
                res.data = res.data.map(v => {
                    v.progress = v.stage / v.totalstage * 100;
                    return v
                })
                this.setData({
                    content: _Http.paging(content, res),
                    list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
                    loading: false,
                })
                this.getTags();
            })
        })
    },
    /* 处理搜索 */
    onSearch({
        detail
    }) {
        this.setData({
            "content.where.condition": detail
        });
        this.getList(true);
    },
    /* 获取列表标签 */
    getTags() {
        if (!wx.getStorageSync('auth').wmarketing_activity.isdatatag) return;
        let list = this.data.list,
            ownerids = list.map(v => v.sat_campaignid);
        _Http.basic({
            "id": 20221018102001,
            "content": {
                nocache: true,
                "ownertable": "sat_campaign",
                ownerids
            }
        }).then(res => {
            console.log("标签", res)
            for (let key in res.data) {
                let index = list.findIndex(v => v.sat_campaignid == key);
                list[index].tags = res.data[key]
            };
            this.setData({
                list
            })
        })
    },
    /* 去添加 */
    openType() {
        wx.navigateTo({
            url: '/packageA/activity/addActivity',
        })
    },
    /* 顶部条件导航回调 */
    navClick({
        detail
    }) {
        switch (detail.id) {
            case '1':
                this.setData({
                    classShow: true
                })
                break;
            case '2':
                this.setData({
                    filterShow: true
                })
                break;
        }
    },
    classClose() {
        this.setData({
            classShow: false
        })
    },
    classSelect({
        detail
    }) {
        if (this.data.content.type == detail.index) return this.classClose();
        this.setData({
            "content.type": detail.index,
            'navList[0].label': detail.name,
            classActions: this.data.classActions.map(v => {
                v.color = detail.i == v.i ? '#3874F6' : ''
                return v
            })
        })
        this.classClose();
        this.getList(true)
    },
    /* 处理筛选 */
    handleFilter({
        detail
    }) {
        this.data.content.where = Object.assign(this.data.content.where, detail)
        this.getList(true)
    }
})