export default {
    data() {
        return {
            isSearch: false,
            hots: [],
            historys: []
        };
    },
    mounted() {
        this.$http.get('sysConfig/get/hot_search').then(res => {
            this.hots = res.value.split(',');
        });
        this.getHistory();
    },
    methods: {
        getHistory() {
            let historys = window.localStorage.getItem('ninthHistory')
                ? window.localStorage.getItem('ninthHistory').split(',')
                : [];
            this.historys = historys;
        },
        addHistory(val) {
            let historys = [...this.historys];
            if (val && !historys.includes(val)) {
                historys.splice(0, 0, val);
            }
            if (historys.length > 10) {
                historys = historys.slice(0, 10);
            }
            window.localStorage.setItem('ninthHistory', historys.join(','));
            this.getHistory();
        },
        clearHistory() {
            window.localStorage.removeItem('ninthHistory');
            this.getHistory();
        },
        getSearch(search) {
            this.isSearch = true;
            this.search = search;
            this.addHistory(search);
            this.getData(true);
        },
        onCancel() {
            if (this.isSearch) {
                this.isSearch = false;
                this.search = '';
            } else {
                this.getSearch(this.search);
            }
        }
    }
};
