import { Component, OnInit } from "@angular/core";
import { ActivatedRoute } from "@angular/router";
import { NzMessageService } from "ng-zorro-antd";
import { MainService } from "src/app/services/main.service";
import { ToolService } from "src/app/services/tool.service";

@Component({
  selector: "app-building-distance",
  templateUrl: "./building-distance.component.html",
  styleUrls: ["./building-distance.component.less"],
})
export class BuildingDistanceComponent implements OnInit {
  coopBtns: any = {};
  id: number = 0; //id
  hosId; //当前院区id
  buildingList: []; //当前院区所有的楼栋
  buildings = []; //所有楼栋距离组合
  loading1: boolean = false; //获取所有楼栋的loading
  loading2: boolean = false; //获取所有楼栋配置的loading
  loading3: boolean = false; //保存所有楼栋配置的loading
  loading4: boolean = false; //删除楼栋配置的loading
  delModal: boolean = false; //删除的模态框
  delObj; //要删除的元素
  constructor(
    private route: ActivatedRoute,
    private mainService: MainService,
    private tool: ToolService,
    private message: NzMessageService
  ) {}
  formatterPercent = (value: number) => `${value} %`;
  parserPercent = (value: string) => value.replace(" %", "");
  ngOnInit() {
    this.hosId = this.tool.getCurrentHospital().id;
    this.coopBtns = this.tool.initCoopBtns(this.route);
    this.getBuildingList(this.hosId);
    this.getBuildingConfig(this.hosId);
  }

  //获取楼栋距离配置
  getBuildingConfig(hosId) {
    let postData = { idx: 0, sum: 9999, workOrderGradeBuilding: { hosId } };
    this.loading2 = true;
    this.mainService
      .getFetchDataList("simple/data", "workOrderGradeBuilding", postData)
      .subscribe((result) => {
        this.loading2 = false;
        if (result.status == 200) {
          this.buildings = result.list.map((item) => {
            return {
              id: item.id,
              buildingStart: item.source.id,
              buildingEnd: item.dest.id,
              percentage: item.percent,
              border: "none",
            };
          });
        } else {
          this.message.error("请求数据失败");
        }
      });
  }
  //新增
  addBuilding() {
    this.buildings.push({
      id: --this.id,
      buildingStart: null,
      buildingEnd: null,
      percentage: 0,
      border: "none",
    });
  }
  // 删除
  delete(obj) {
    this.delModal = true;
    this.delObj = obj;
  }
  //隐藏删除模态框
  hideDelModal() {
    this.delModal = false;
  }
  // 确定删除
  confirmDel() {
    this.loading4 = true;
    this.mainService.delBuildings([this.delObj.id]).subscribe((result) => {
      this.loading4 = false;
      if (result["status"] == 200) {
        this.message.success("删除成功");
        this.buildings = this.buildings.filter(
          (item) => item.id !== this.delObj.id
        );
      } else {
        this.message.error("删除失败");
      }
      this.hideDelModal();
    });
  }
  //保存
  save() {
    this.buildings = this.buildings.filter(
      (item) => item.buildingStart && item.buildingEnd
    );
    let postData = this.buildings.map((item) => {
      return {
        id: item.id,
        source: { id: item.buildingStart },
        dest: { id: item.buildingEnd },
        percent: item.percentage,
        hosId: this.hosId,
      };
    });
    postData.forEach((item) => {
      if (item.id < 0) {
        delete item.id;
      }
    });
    this.loading3 = true;
    this.mainService.saveBuildings(postData).subscribe((result) => {
      this.loading3 = false;
      if (result["status"] == 200) {
        this.message.success("保存成功");
        this.getBuildingConfig(this.hosId);
      } else {
        this.message.error("保存失败");
        result["data"].forEach((item) => {
          let i = this.buildings.findIndex((v) => v.id === item.id);
          this.buildings[i].border = "red";
        });
      }
    });
  }
  //trackBy
  trackById(index, item) {
    return item.id;
  }
  // 获取当前院区的楼栋
  getBuildingList(hosId) {
    let postData = { idx: 0, sum: 9999, building: { hosId } };
    this.loading1 = true;
    this.mainService
      .getFetchDataList("simple/data", "building", postData)
      .subscribe(
        (result) => {
          this.loading1 = false;
          if (result.status == 200) {
            this.buildingList = result.list;
          } else {
            this.message.error("获取楼栋失败！");
          }
        },
        (err) => {
          this.loading1 = false;
          this.message.error("获取楼栋失败！");
        }
      );
  }
}
