import { Component, OnInit, ViewChild } from "@angular/core";
import { MainService } from "../../services/main.service";
import { OverlayScrollbarsComponent } from "overlayscrollbars-ngx";
import { NzMessageService } from "ng-zorro-antd";
import { ActivatedRoute, Router } from '@angular/router';
import { ToolService } from 'src/app/services/tool.service';
@Component({
  selector: "app-patient-inspect-log-detail",
  templateUrl: "./patient-inspect-log-detail.component.html",
  styleUrls: ["./patient-inspect-log-detail.component.less"],
})
export class PatientInspectLogDetailComponent implements OnInit {
  @ViewChild("osComponentRef1", {
    read: OverlayScrollbarsComponent,
    static: false,
  })
  osComponentRef1: OverlayScrollbarsComponent;
  @ViewChild("osComponentRef2", {
    read: OverlayScrollbarsComponent,
    static: false,
  })
  osComponentRef2: OverlayScrollbarsComponent;
  @ViewChild("osComponentRef3", {
    read: OverlayScrollbarsComponent,
    static: false,
  })
  osComponentRef3: OverlayScrollbarsComponent;
  @ViewChild("osComponentRef4", {
    read: OverlayScrollbarsComponent,
    static: false,
  })
  osComponentRef4: OverlayScrollbarsComponent;
  constructor(
    private message: NzMessageService,
    private mainService: MainService,
    private route: ActivatedRoute,
    private router: Router,
    private tool: ToolService,
  ) {}
  maskFlag: any = false;
  id: number; //工单id
  orderInfo: any = {}; //详情信息
  promptContent: string; //操作提示框提示信息
  ifSuccess: boolean; //操作成功/失败
  promptInfo: string; //操作结果提示信息
  promptModalShow: boolean; //是否展示提示框

  loading1:boolean = false;

  ngOnInit() {
    this.id = +this.route.snapshot.paramMap.get("id");
    this.checkTab(1);
  }

  // 获取详情
  getInfo(isInit = false) {
    if(isInit){
      this.maskFlag = this.message.loading("正在加载中..", {
        nzDuration: 0,
      }).messageId;
    }
    this.mainService
      .getFetchData("simple/data", "patientInspectLog", this.id)
      .subscribe((data) => {
        if(isInit){
          this.message.remove(this.maskFlag);
          this.maskFlag = false;
        }
        let orderInfo = data.data || {};
        if(orderInfo.goodsList){
          orderInfo.goodsNames = orderInfo.goodsList.map(v => v.name).toString();
        }
        this.orderInfo = orderInfo;
      });
  }

  // 获取日志
  logList: any = [];
  getWorkOrderRecord() {
    this.loading1 = true;
    let postData = {
      idx: 0,
      sum: 9999,
      patientInspectLogLog: {
        pilId: this.id,
      },
    };
    this.mainService
      .getFetchDataList("simple/data", "patientInspectLogLog", postData)
      .subscribe((data) => {
        this.loading1 = false;
        if (data.status == 200) {
          this.logList = data.list || [];
        }else{
          this.message.error(data.msg || "请求数据失败");
        }
      });
  }
  // 获取检查项目
  inspectList: any = [];
  getInspectList() {
    this.loading1 = true;
    let postData = {
      pilId: this.id,
    };
    this.mainService
      .getPatientInspectList(postData)
      .subscribe((data:any) => {
        this.loading1 = false;
        if (data.status == 200) {
          this.inspectList = data.list || [];
        }else{
          this.message.error(data.msg || "请求数据失败");
        }
      });
  }

  // 获取工单
  orderList: any = [];
  getOrderList() {
    this.loading1 = true;
    let postData = {
      idx: 0,
      sum: 9999,
      workOrder: {
        pilId: this.id,
        simpleQuery: true,
      },
    };
    this.mainService
      .getFetchDataList("data", "workOrder", postData)
      .subscribe((data) => {
        this.loading1 = false;
        if (data.status == 200) {
          this.orderList = data.list.map(v => ({...v, endDeptsName: v.endDepts ? v.endDepts.map(v => v.dept).toString() : ''}));
        }else{
          this.message.error(data.msg || "请求数据失败");
        }
      });
  }
  // 切换tab栏
  tabType: number = 1; //tab栏
  checkTab(type, isInit = false) {
    this.tabType = type;
    if(type == 1){
      this.getInfo(isInit);
    }else if(type == 2){
      this.getWorkOrderRecord();
    }else if(type == 3){
      this.getInspectList();
    }else if(type == 4){
      this.getOrderList();
    }
  }

  // 关闭弹框
  close() {
    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);
  }

  // 查看
  detail(e, id) {
    e.stopPropagation();
    this.router.navigateByUrl(`/main/patientInspectLog/patientInspectLogDetail/${this.id}/orderDetail/${id}`);
  }

}
