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

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

    searchText = '';
    showHistory = false;
    showSearchBtn = false;
    history: Array<String> = JSON.parse(window.localStorage.getItem('history.three')) || [];
    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.three', 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();
        }
    }
}
