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

@Component({
  selector: "app-detail-bx",
  templateUrl: "./detail-bx.component.html",
  styleUrls: ["./detail-bx.component.less"],
})
export class DetailBxComponent implements OnInit {
  @ViewChild("osComponentRef1", {
    read: OverlayScrollbarsComponent,
    static: false,
  })
  osComponentRef1: OverlayScrollbarsComponent;
  constructor(
    private message: NzMessageService,
    private route: ActivatedRoute,
    private router: Router,
    private tool: ToolService,
    private mainService: MainService
  ) {}
  progressInfo = [];
  processData = "";
  imgs_wechatRequesterIncident = [];
  imgs_incident = [];

  id: number; //工单id
  orderInfo: any; //工单详情信息
  showCoop: boolean = true; //是否展示详情页操作按钮

  promptContent: string; //操作提示框提示信息
  ifSuccess: boolean; //操作成功/失败
  promptInfo: string; //操作结果提示信息
  promptModalShow: boolean; //是否展示提示框

  urgentLoading: boolean = false; //确认加急按钮loading状态
  recLoading: boolean = false; //撤回并删除按钮loading状态
  btnLoading: boolean = false; //确认按钮loading状态
  maskFlag: any = false;
  deptDisplay; //护士端是否显示可以别名，1是显示科室名称，2是显示科室别名
  amplificationShow = true;

  isShowIncidentDetails:boolean = false;

  hosId:any;

  ngOnInit() {
    this.hosId = this.tool.getCurrentHospital().id;
    this.getHospitalConfig();
    this.tool.getDeptDisplay().subscribe((result) => {
      if (result.status == 200) {
        this.deptDisplay = result.list[0].valueconfig;
      }
    });
    if (this.route.snapshot.parent.parent.routeConfig.path == "nurse") {
      this.showCoop = false;
    }
    this.id = +this.route.snapshot.paramMap.get("id");
    let log$ = this.getLog();
    let detail$ = this.getDetail();
    this.maskFlag = this.message.loading("正在加载中..", {
      nzDuration: 0,
    }).messageId;
    forkJoin(log$, detail$).subscribe((res) => {
      this.message.remove(this.maskFlag);
      this.maskFlag = false;
      // getLog
      this.logList = res[0]["data"];
      // getDetail
      this.orderInfo = res[1]["data"];
      this.getProgressInfo();
      this.getImgsData('wechatRequesterIncident');
      this.getImgsData('incident');
    });
  }

  // 汇总单
  goToSummary(){
    this.isShowIncidentDetails = true;
  }

  // 关闭汇总单
  closeIncidentDetails(){
    this.isShowIncidentDetails = false;
  }

  // 获取院区配置信息
  itsmSummarySheet:any = {};//是否需要填写汇总单
  getHospitalConfig() {
    let postData = {
      idx: 0,
      sum: 9999,
      hospitalConfig: {
        model: "itsm",
        hosId: this.hosId,
      },
    }
    this.mainService
      .getFetchDataList("simple/data", "hospitalConfig", postData)
      .subscribe((result) => {
        let list = result.list || [];
        this.itsmSummarySheet = list.find(v => v.key == 'itsmSummarySheet') || {};
      });
  }

  // 获取图片信息
  getImgsData(type) {
    let id = this.orderInfo.id;
    this.mainService
      .getPreviewImage(type, id)
      .subscribe((res:any)=> {
        // this.imgs = ['https://img2.woyaogexing.com/2022/08/19/1f5e056b1d076920!400x400.jpg','https://img2.woyaogexing.com/2022/08/19/359346943c3c489f!400x400.jpg']
        res.data.forEach(v => {
          v.previewUrl = location.origin + "/file" + v.relativeFilePath;
          v.thumbFilePath = location.origin + "/file" + v.thumbFilePath;
        })
        this['imgs_' + type] = res.data;
      });
  }

  // 预览图片
  imgs = [];
  isPreview = false;
  initialViewIndex:number = 0;
  previewImageHandler(data = [], index = 0) {
    this.initialViewIndex = index;
    console.log(index)
    this.isPreview = false;
    data = data || [];
    this.imgs = data.map((v) => location.origin + '/file' + v.relativeFilePath);
    setTimeout(() => {
      this.isPreview = true;
    }, 0)
  }

  //获取处理进度
  getProgressInfo() {
    this.mainService.getFetchDataList("simple/data", "incidentLog", {
      "idx": 0,
      "sum": 9999,
      "incidentLog": {
          "incidentId": this.orderInfo.id
      }
    })
    .subscribe((res: any) => {
      console.log(res);
      this.progressInfo = res.list || [];
    });
  }

  // 放大
  amplification() {
    this.amplificationShow = !this.amplificationShow;
  }

  // 获取工单详情
  getDetail() {
    return this.mainService.getFetchData("simple/data", "incident", this.id);
  }

  // 关闭弹框
  close() {
    // this.router.navigateByUrl('dispatchingDesk');
    history.go(-1);
  }
  // 展示信息提示框（con:提示信息，success:操作是否成功，promptInfo:操作结果提示信息）（con:提示信息，success:操作是否成功，promptInfo:操作结果提示信息）
  showPromptModal(con, success, promptInfo?) {
    this.promptModalShow = false;
    this.promptContent = con;
    this.ifSuccess = success;
    this.promptInfo = promptInfo;
    setTimeout(() => {
      this.promptModalShow = true;
    }, 100);
  }
  // 获取工单历史记录
  logList = []; //工单历史记录
  getLog() {
    // return this.mainService.getWorkOrderLog(this.id);
    return Promise.resolve(111);
  }
}
