import { Component, OnInit, ViewChild } from "@angular/core";
import { ActivatedRoute, Router } from "@angular/router";

import { OverlayScrollbarsComponent } from "overlayscrollbars-ngx";
import { ToolService } from "../../services/tool.service";
import { WorkOrderLogService } from './work-order-log.service';
import { format, startOfDay, endOfDay } from 'date-fns';

@Component({
  selector: "app-work-order-log",
  templateUrl: "./work-order-log.component.html",
  styleUrls: ["./work-order-log.component.less"],
})
export class WorkOrderLogComponent implements OnInit {
  @ViewChild("osComponentRef1", {
    read: OverlayScrollbarsComponent,
    static: false,
  })
  osComponentRef1: OverlayScrollbarsComponent;
  constructor(
    private router: Router,
    private route: ActivatedRoute,
    private tool: ToolService,
    private workOrderLogService: WorkOrderLogService,
  ) {}

  listOfData: any[] = []; //表格数据
  searchDto: any = {
    dateRange: [new Date(), new Date()],
  }

  hosId: any; //院区（搜索）
  pageIndex: number = 1; //页码
  listLength: number = 10; //总条数
  pageSize: number = 10; //每页条数

  // 初始化增删改按钮
  coopBtns: any = {};

  ngOnInit() {
    this.coopBtns = this.tool.initCoopBtns(this.route);
    this.hosId = this.tool.getCurrentHospital().id;
    this.getList(true);
    this.getOperationList();
  }

  // 获取快捷类型
  operationList: any[] = [];
  getOperationList() {
    this.workOrderLogService.getDictionary("gdOperate").subscribe((res) => {
      this.operationList = res || [];
    });
  }

  // 预览图片
  imgs = [];
  isPreview = false;
  previewImageHandler(data) {
    this.isPreview = false;
    data = data || [];
    this.imgs = data.map((v) => location.origin + '/file' + v.relativeFilePath);
    this.isPreview = true;
  }

  // 搜索
  search() {
    this.getList(true);
  }
  // 重置
  reset() {
    this.searchDto = {
      dateRange: [new Date(), new Date()],
    }
    this.search();
  }
  // 查看
  detail(e, id) {
    e.stopPropagation();
    this.router.navigateByUrl("/main/workOrderLog/orderDetail/" + id);
  }
  // 表格数据
  loading1 = false;
  getList(isResetPageIndex = false) {
    isResetPageIndex && (this.pageIndex = 1);
    let searchDto = {
      startTime: this.searchDto.dateRange[0] ? format(startOfDay(this.searchDto.dateRange[0]), 'yyyy-MM-dd HH:mm:ss') : undefined,
      endTime: this.searchDto.dateRange[1] ? format(endOfDay(this.searchDto.dateRange[1]), 'yyyy-MM-dd HH:mm:ss') : undefined,
      logRemarks: this.searchDto.logRemarks,
      operation: this.operationList.find(v => v.id == this.searchDto.operation),
    }
    let data = {
      pageIndex: this.pageIndex,
      pageSize: this.pageSize,
      hosId: this.hosId,
      searchDto,
    };
    this.loading1 = true;
    this.workOrderLogService
      .query(data)
      .subscribe((result) => {
        this.loading1 = false;
        result.list = result.list || [];
        result.list.forEach(v => {
          // todo
        })
        this.listOfData = result.list;
        this.listLength = result.totalNum;
      });
  }
}
