import { Component, EventEmitter, Input, OnInit, Output } from "@angular/core";
import { MainService } from "src/app/services/main.service";
import { ToolService } from "src/app/services/tool.service";

@Component({
  selector: "app-generate-floor",
  templateUrl: "./generate-floor.component.html",
  styleUrls: ["./generate-floor.component.less"],
})
export class GenerateFloorComponent implements OnInit {
  @Output() hideGenerateModalEvent = new EventEmitter<any>();
  @Output() confirmGenerateEvent = new EventEmitter<any>();
  @Input() btnLoading: boolean = false;
  @Input() generateModal: boolean = false;
  @Input() buildId: any;
  constructor(private mainService: MainService, private tool: ToolService) {}
  hosId; //院区
  building = null; //选择的楼栋
  startFloor = null; //起始楼层
  startFloorMin = 1; //起始楼层最小
  startFloorMax = Infinity; //起始楼层最大
  endFloor = null; //结束楼层
  endFloorMin = 1; //结束楼层最小
  endFloorMax = Infinity; //结束楼层最大
  delModal = false;
  tipsMsg1 = "";
  ngOnInit() {
    this.startFloor = null;
    this.endFloor = null;
    this.hosId = this.tool.getCurrentHospital().id;
    this.getBuildings(this.hosId);
  }
  // 获取楼栋
  loading1: boolean = false;
  buildingList = [];
  getBuildings(hosId) {
    this.loading1 = true;
    this.mainService
      .getFetchDataList("simple/data", "building", {
        idx: 0,
        sum: 9999,
        building: { hosId },
      })
      .subscribe((result) => {
        this.loading1 = false;
        if (result["status"] == 200) {
          this.buildingList = result["list"];
          if (this.buildId) {
            this.building = this.buildingList.filter(
              (item) => item.id == this.buildId
            )[0];
          } else {
            this.building = null;
          }
        }
      });
  }
  //修改起始楼层
  startFloorChange(e) {
    this.startFloor = e;
    this.endFloorMin = e;
    this.endFloorMax = Infinity;
  }
  //修改结束楼层
  endFloorChange(e) {
    this.endFloor = e;
    this.startFloorMin = 1;
    this.startFloorMax = e;
  }
  // 隐藏生成楼层弹窗
  hideGenerateModal() {
    this.hideGenerateModalEvent.emit();
  }
  // 确认生成楼层->弹窗提示信息
  confirmGenerate() {
    if (!this.building || !this.startFloor || !this.endFloor) {
    } else {
      this.tipsMsg1 = `是否在${this.building.buildingName}生成楼层${this.startFloor}层到${this.endFloor}层？`;
      this.delModal = true;
    }
  }
  //隐藏提示弹窗
  hideDelModal() {
    this.delModal = false;
  }
  //确认生成楼层
  confirmDel() {
    this.confirmGenerateEvent.emit([
      this.building,
      this.startFloor,
      this.endFloor,
    ]);
  }
}
