import {Component, OnInit} from '@angular/core';

@Component({
    selector: 'app-page-two',
    templateUrl: './page-two.component.html',
    styleUrls: ['./page-two.component.scss'],
})
export class PageTwoComponent implements OnInit {

    searchText = '';
    showHistory = false;
    showSearchBtn = false;
    history: Array<String> = JSON.parse(window.localStorage.getItem('history.two')) || [];
    constructor() {
    }

    ngOnInit() {
    }

    // 下面为查询框对应的方法
    cancel() {
        this.searchText = '';
        this.showHistory = false;
        this.showSearchBtn = false;
    }

    search() {
        if (this.searchText) {
            if (this.history.length >= 10) {
                this.history.pop();
            }
            if (this.history.filter(item => { return item === this.searchText; }).length === 0) {
                this.history.push(this.searchText);
                window.localStorage.setItem('history.two', JSON.stringify(this.history));
            }
            console.log('此处进行查询操作');
            this.showHistory = false;
            this.showSearchBtn = false;
        }
    }

    historyClick(e) {
        this.searchText = e;
    }

    keyup(e) {
        if (e.which === 13) {
            this.search();
        }
    }
}
