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

@Component({
  selector: 'v-refresh',
  templateUrl: './refresh.component.html',
  styleUrls: ['./refresh.component.scss', '../../alarm/alarm.page.scss'],
})
export class RefreshComponent implements OnInit {

  loading = false;
  data = [];
  page = 1;
  show = [];
  _list;
  @Input()
  get list() {
    return this._list;
  }

  set list(d) {
    this.data = d;
    this.initArray();
  }
  constructor() { }

  ngOnInit() {}

  initArray() {
    this.show = [];
    this.addArray();
    const left = document.getElementById('refreshPart');
    left.scrollTop = 0;
    let flag = 0;
    left.addEventListener('scroll', (e) => {
      if (left.scrollTop >= left.scrollHeight - 600 && flag === 0 ) {
        this.loading = true;
        this.page++;
        setTimeout(() => {
          this.addArray();
          this.loading = false;
          flag = 0;
        }, 2000);
      }
    });
  }

  addArray() {
    this.show = this.show.concat(this.data.filter((one, i) => i < this.page * 10 && i >= (this.page - 1) * 10));
  }
}
