import { Component, OnInit, ViewChild } from "@angular/core";
import { MainService } from "src/app/services/main.service";
import { ToolService } from "src/app/services/tool.service";
import { format } from 'date-fns';
import { ActivatedRoute, Router } from '@angular/router';
import { FormGroup, Validators, FormBuilder } from '@angular/forms';
import { OverlayScrollbarsComponent } from 'overlayscrollbars-ngx';

@Component({
  selector: "app-medical-waste-outgoing-record",
  templateUrl: "./medical-waste-outgoing-record.component.html",
  styleUrls: ["./medical-waste-outgoing-record.component.less"],
})
export class MedicalWasteOutgoingRecordComponent implements OnInit {
  @ViewChild("osComponentRef1", {
    read: OverlayScrollbarsComponent,
    static: false,
  })
  osComponentRef1: OverlayScrollbarsComponent;
  constructor(
    private mainService: MainService,
    private tool: ToolService,
    private route: ActivatedRoute,
    private fb: FormBuilder,
    private router: Router,
  ) {}

  searchCriteria = {
    //搜索条件
    name: "",
    credentialsNumber: "",
    batchNo: "",
    dateRange: [],
  };
  hosId;
  listOfData: any[] = []; //表格数据
  pageIndex: number = 1; //表格当前页码
  pageSize: number = 10; //表格每页展示条数
  listLength: number = 10; //表格总数据量

  // 初始化增删改按钮
  coopBtns: any = {};
  ngOnInit() {
    this.coopBtns = this.tool.initCoopBtns(this.route);
    this.getHospital();
  }

  onCalendarChangeDate(dateArr){
    console.log(dateArr)
    if(dateArr.length == 2){
      let dateStart = new Date(dateArr[0]);
      let dateEnd = new Date(dateArr[1]);
      dateStart.setHours(0,0,0);
      dateEnd.setHours(23,59,59);
      this.searchCriteria.dateRange = [dateStart,dateEnd];
    }
  }

  batchOutOfOtorageClick(){
    this.router.navigateByUrl("/main/batchOutOfStorage");
  }

  // 查看
  detail(e, id) {
    e.stopPropagation();
    this.router.navigateByUrl("/main/outgoingRecord/outgoingRecordDetail/" + id);
  }

  // 重置
  reset() {
    this.searchCriteria = {
      //搜索条件
      name: "",
      credentialsNumber: "",
      batchNo: "",
      dateRange: [],
    };
    this.getList(1);
  }

  // 获取所有院区
  getHospital() {
    this.hosId = this.tool.getCurrentHospital().id;
    this.getList(1);
  }

  // 表格数据
  loading1 = false;
  getList(type) {
    if (type == 1) {
      this.pageIndex = 1;
    }
    let postData: any = {
      idx: this.pageIndex - 1,
      sum: this.pageSize,
      clinicalWasteOut: {
        hosId: this.hosId,
      },
    };
    if (this.searchCriteria.dateRange.length) {
      postData.clinicalWasteOut.startTime =
        format(this.searchCriteria.dateRange[0], "yyyy-MM-dd HH:mm:ss");
      postData.clinicalWasteOut.endTime =
        format(this.searchCriteria.dateRange[1], "yyyy-MM-dd HH:mm:ss");
    }
    if (this.searchCriteria.name) {
      postData.clinicalWasteOut.name = this.searchCriteria.name;
    }
    if (this.searchCriteria.credentialsNumber) {
      postData.clinicalWasteOut.credentialsNumber = this.searchCriteria.credentialsNumber;
    }
    if (this.searchCriteria.batchNo) {
      postData.clinicalWasteOut.batchNo = this.searchCriteria.batchNo;
    }
    this.loading1 = true;
    this.mainService
      .getFetchDataList("simple/data", "clinicalWasteOut", postData)
      .subscribe((data) => {
        this.loading1 = false;
        if (data["status"] == 200) {
          this.listOfData = data["list"];
          this.listLength = data["totalNum"];
        }
      });
  }
}

