import {
    getCategoryWorks,
    searchWorks
} from '~/api/works';
import reachBottom from '~/mixins/reachBottom';
import {
    createStoreBindings
} from 'mobx-miniprogram-bindings';
import event from '~/mixins/event'
import {
    store
} from '~/store/index';
let storeBindings;
Page({
    behaviors: [reachBottom, event],
    data: {
        // class为二级,search为搜索
        type: 'class',
        categoryList: [],
        childType: '',
        currentIndex: 0,
        scrollTop: 0,
        text: '',
        currentId: '',
        navBarTitle: '',
        historySearch: []
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // 没有二级分类
        if (options.id) {
            this.setData({
                childType: options.id
            });
            this.resetData();
        } else if (options.list) {
            let categoryList = JSON.parse(decodeURIComponent(options.list));
            this.setData({
                categoryList
            });
            this.resetData();
        }
        wx.setNavigationBarTitle({
            title: options.title || '搜索'
        });
        this.setData({
            type: options.type,
            historySearch: wx.getStorageSync('search'),
            navBarTitle: options.title
        });
        this.storeBindings = createStoreBindings(this, {
            store,
            fields: {
                userInfo: 'userInfo'
            }
        });
        // 立刻更新
        this.storeBindings.updateStoreBindings();
    },
    // 获取分类的内容
    loadMore() {
        if (this.data.type == 'search') {
            return;
        }
        let columnId = this.data.childType ? this.data.childType : this.data.categoryList[this.data.currentIndex].id;
        this.getData(getCategoryWorks, {
            columnId
        });
    },
    setClass({
        currentTarget
    }) {
        this.setData({
            scrollTop: 0,
            navBarTitle: currentTarget.dataset.title,
            currentIndex: currentTarget.dataset.index,
            currentId: `class${currentTarget.dataset.index}`
        });
        this.resetData();
    },
    setSearch({
        detail
    }) {
        if (!detail.value) {
            this.setData({
                nullList: false,
                list: []
            });
        }
        this.setData({
            text: detail.value
        });
    },
    async search() {
        if (!this.data.text) {
            this.setData({
                list: []
            });
            return;
        }
        let list = await searchWorks({
            title: this.data.text,
            grade: this.data.userInfo.grade
        });
        if (!this.data.historySearch.includes(this.data.text)) {
            this.setData({
                historySearch: [this.data.text, ...this.data.historySearch].slice(0, 20)
            });
            wx.setStorageSync('search', this.data.historySearch);
        }
        this.setData({
            list,
            nullList: list.length == 0
        });
    },
    historySearch({
        currentTarget
    }) {
        this.setData({
            text: currentTarget.dataset.text
        });
        this.search();
    },
    deleteHistory({
        currentTarget
    }) {
        let newList = this.data.historySearch.filter(item => {
            return item != currentTarget.dataset.text;
        });
        this.setData({
            historySearch: newList.slice(0, 20)
        });
        wx.setStorageSync('search', this.data.historySearch);
    },
    clearHistory() {
        wx.showModal({
            title: '温馨提示',
            content: '历史记录清除后无法恢复,是否清除全部记录',
            success: res => {
                if (res.confirm) {
                    this.setData({
                        historySearch: []
                    });
                    wx.setStorageSync('search', this.data.historySearch);
                }
            }
        });
    },
    goRead({
        currentTarget
    }) {
        wx.navigateTo({
            url: `/pages/reading/index?videoId=${currentTarget.dataset.id}&navBarTitle=${this.data.navBarTitle || ''}`
        });
    },
    onUnload() {
        this.storeBindings.destroyStoreBindings();
    }
});