const getHeight = require("../../utils/getRheRemainingHeight"),
    _Http = getApp().globalData.http;
let dowmCount = null;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        optionList: [],
        scrollHeight: 0, //滚动区域高度
        searchContent: "", //搜索内容
        mainActiveIndex: 0, //分类选择器打开分类的下标
        activeId: "", //分类选择器选择id
        showText: null, //左上显示文本
        items: [], //分类列表
        list: [], //列表
        sort: [],
        content: {
            nocache: true,
            pageNumber: 1,
            pageSize: 20,
            pageTotal: 1,
            where: {
                condition: ""
            }
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let auth = options.auth ? JSON.parse(options.auth) : getApp().globalData.queryPer.query(wx.getStorageSync('userauth'), ['营销工具'], ['商学院'])[0].apps;
        this.setData({
            optionList: auth[0].meta.auth.map(v => v.optionname)
        })
        this.selectMenu(0);
        this.selectList();
    },
    /* 查询菜单分类 */
    selectMenu(index) {
        if (index == '5') return wx.showToast({
            title: '加载失败,请稍后再试',
            icon: "none"
        })
        _Http.basic({
            "classname": "saletool.courseware.courseware",
            "method": "selectMenu",
            "content": {}
        }).then(res => {
            if (res.msg != '成功') return this.selectMenu(index + 1)
            let str = JSON.stringify(res.data).replace(/classname/g, 'text').replace(/sat_courseware_classid/g, 'id');
            this.setData({
                items: JSON.parse(str)
            })
        })
    },
    toDetail(e) {
        const {
            item
        } = e.currentTarget.dataset;
        wx.navigateTo({
            url: `./detail?id=${item.sat_coursewareid}&auth=${this.data.optionList}`,
        })
    },
    /* 获取列表 */
    selectList(init = false) {
        if (init.detail != undefined) init = init.detail;
        if (init) this.setData({
            ['content.pageNumber']: 1
        })
        if (this.data.content.pageNumber > this.data.content.pageTotal) return;
        let content = this.data.content;
        (this.data.activeId == "") ? delete(content.where.sat_courseware_classid): content.where.sat_courseware_classid = this.data.activeId;
        if (this.data.sort[0]) content.sort = this.data.sort;
        _Http.basic({
            "classname": "saletool.courseware.courseware",
            "method": "selectList",
            content
        }).then(res => {
            this.selectComponent('#ListBox').RefreshToComplete();
            if (res.msg != '成功') return wx.showToast({
                title: res.msg,
                icon: "none"
            })
            this.setData({
                list: (res.pageNumber == 1) ? res.data : this.data.list.concat(res.data),
                ['content.pageNumber']: res.pageNumber + 1,
                ['content.pageTotal']: res.pageTotal,
                total: res.total,
                sort: res.sort
            })
        })
    },
    /* 开始搜索 */
    startSearch({
        detail
    }) {
        clearTimeout(dowmCount);

        this.setData({
            "content.where.condition": detail.trim()
        })

        dowmCount = setTimeout(() => {
            this.selectList(true);
        }, 1000);

    },
    /* 关闭搜索 */
    closeSearch() {
        this.setData({
            "content.where.condition": ""
        })
        this.selectList(true);
    },
    switchChange({
        detail
    }) {
        this.setData({
            sort: detail
        })
        this.selectList(true);
    },
    onClickNav({
        detail = {}
    }) {
        this.setData({
            mainActiveIndex: detail.index || 0,
        });
    },
    onClickItem({
        detail = {}
    }) {
        const activeId = this.data.activeId === detail.id ? "" : detail.id,
            showText = this.data.showText === detail.text ? null : detail.text;
        this.setData({
            activeId,
            showText
        });
        this.selectComponent('#item').toggle();
        this.selectList(true);
    },
    /*生命周期函数--监听页面初次渲染完成*/
    onReady() {
        //滚动区域高度
        getHeight.getHeight('.menu', this).then(res => {
            this.setData({
                scrollHeight: res
            })
        })
    },
    onShareAppMessage(res) { }
})