import { Component, Input } from '@angular/core';
import { horizontalInOut } from '../../../../../common.animation';
import { CommonNzModule } from './../../../../../common.nz.module';
import { ApiService } from './../../../../../services/api.service';
import { BasicDataService } from './../../../../../services/basic.service';
import { DataEmptyComponent } from './../../../../../shared/data-empty/data-empty.component';
import { AccidentFormComponent } from './../accident-form/accident-form.component';

// const scene = ['station', 'line', 'other'];
// const mockRecord = (index: number): Datav.Accident => {
//   return {
//     id: index,
//     title: '事故名称' + `事故名称${index}`.repeat(index),
//     accidentDate: dayjs().add(index, 'day').format('YYYY-MM-DD HH:mm'),
//     positionType: scene[index % 3],
//     line: 76,
//     position: 80,
//     positionNext: 81,
//     other: '其他'.repeat(index),
//     content: '其他'.repeat(index),
//     consequence: '事故后果'.repeat(index),
//     disposalResult: '事故结果'.repeat(index),
//   };
// };

@Component({
  selector: 'accident-history-record',
  standalone: true,
  imports: [CommonNzModule, AccidentFormComponent, DataEmptyComponent],
  templateUrl: './history-record.component.html',
  styleUrl: './history-record.component.less',
  animations: [horizontalInOut],
})
export class HistoryRecordComponent {
  action = 'edit';
  detailVisible = false;
  detailData?: Datav.Accident;
  pageConfig: IPageConfig = {
    page: 0,
    size: 10,
    total: 0,
  };
  loading = false;
  @Input() editable = false;
  @Input() data: Datav.Accident[] = [];

  constructor(
    private basic: BasicDataService,
    private api: ApiService
  ) {}

  ngOnInit() {
    this.fetchRecords();
  }
  fetchRecords() {
    this.loading = true;
    this.api.datav
      .getAccidents({ page: this.pageConfig.page, size: this.pageConfig.size })
      .then(res => {
        this.data = res.items;
        this.pageConfig.total = res.total;
      })
      .finally(() => {
        this.loading = false;
      });
  }
  onCurrentPageChange(page: number) {
    this.pageConfig.page = page;
    this.fetchRecords();
  }
  reset() {
    this.pageConfig.page = 1;
    this.fetchRecords();
  }
  refresh() {
    this.fetchRecords();
  }
  onEdit(record: Datav.Accident) {
    this.action = 'edit';
    this.detailVisible = true;
    this.detailData = record;
  }

  onView(record: Datav.Accident) {
    this.action = 'view';
    this.detailVisible = true;
    this.detailData = record;
  }

  renderScene(row: Datav.Accident) {
    switch (row.positionType) {
      case 'station':
        return `【${this.basic.getLineName(row.line!)}】 ${this.basic.getPositionName(row.position!)}`;
      case 'line':
        return `【${this.basic.getLineName(row.line!)}】 ${this.basic.getPositionName(row.position!)} - ${this.basic.getPositionName(row.positionNext!)}`;
      case '':
        return row.positionOther;
    }
    return '';
  }

  closeDetail() {
    this.detailVisible = false;
  }
}
