import { Component, OnInit, ViewChild, ElementRef } from "@angular/core";
import { ActivatedRoute } from "@angular/router";

import { MainService } from "../../services/main.service";
import { ToolService } from "../../services/tool.service";
import { NzMessageService } from 'ng-zorro-antd';
@Component({
  selector: "app-nucleic-acid-collection",
  templateUrl: "./nucleic-acid-collection.component.html",
  styleUrls: ["./nucleic-acid-collection.component.less"],
})
export class NucleicAcidCollectionComponent implements OnInit {
  @ViewChild('printBtn', { static: false }) printBtn: ElementRef<HTMLElement>;
  constructor(
    private mainService: MainService,
    private route: ActivatedRoute,
    private tool: ToolService,
    private msg: NzMessageService,
  ) {}

  userInfo: any = JSON.parse(localStorage.getItem("user")) || {}; //登录用户信息
  hosId;
  listOfData: any[] = []; //表格数据
  pageIndex: number = 1; //表格当前页码
  pageSize: number = 10; //表格每页展示条数
  listLength: number = 10; //表格总数据量

  ngOnInit() {
    this.hosId = this.tool.getCurrentHospital().id;
    this.getList();
    this.getHospitalConfigList('allowNucleicAcidPrinting');
    this.coopBtns = this.tool.initCoopBtns(this.route);
  }

  // 初始化权限按钮
  coopBtns: any = {};

  // 预览图片
  imgs = [];
  isPreview = false;
  previewImageHandler(data) {
    if(data.pictureId){
      console.log(data.id)
      this.isPreview = false;
      this.mainService.getPreviewImage('specimenPhotos', data.id).subscribe((result:any)=>{
        if (result.status == 200) {
          this.imgs = result.data.map((v) => location.origin + '/file' + v.relativeFilePath);
          this.isPreview = true;
        } else {
          this.imgs = [];
        }
      })
    }
  }

  // 获取院区配置配置，是否核酸打印
  isShowNucleicAcidPrinting = false;
  getHospitalConfigList(key) {
    let postData = { idx: 0, sum: 1, hospitalConfig: { hosId: this.hosId, key } };
    this.mainService.getFetchDataList("simple/data", "hospitalConfig", postData).subscribe(result=>{
      if(result.status == 200){
        this.isShowNucleicAcidPrinting = result.list[0].value == 1;
      }else{
        this.isShowNucleicAcidPrinting = false;
      }
    });
  }

  // 补充打印
  nucleicAcidList: any = [];
  printDate:number = Date.now();
  mapQrcodes:any = {};
  supplementPrint(data){
    data.isOkLoading = true;
    this.mainService.supplementPrintNucleicAcid({gdIds:data.gdIds,code:data.qrcode}).subscribe((result:any) => {
      data.isOkLoading = false;
      if(result.status == 200){
        this.mapQrcodes = {[data.buildId.id]: result.qrcode};
        let nucleicAcidList:any = [
          {
            buildName: data.buildId.buildingName,
            buildId: data.buildId.id,
            deptData: result.deptData
          }
        ]
        for (let i = 0; i < nucleicAcidList.length; i++) {
          if(nucleicAcidList[i].deptData && Array.isArray(nucleicAcidList[i].deptData)){
            nucleicAcidList[i].deptNames = nucleicAcidList[i].deptData.map(v=>v.deptName).toString();
          }else{
            nucleicAcidList[i].deptData = [];
            nucleicAcidList[i].deptNames = '';
          }
        }
        this.nucleicAcidList = nucleicAcidList;
        setTimeout(()=>{
          this.printBtn.nativeElement.click();
        },0)
      }else{
        this.msg.create('error', `操作失败!`);
      }
    })
  }

  // 表格数据
  loading1 = false;
  getList() {
    let postData = {
      idx: this.pageIndex - 1,
      sum: this.pageSize,
      messageJob: {
        hospital: this.hosId,
      },
    };
    this.loading1 = true;
    this.mainService
      .getFetchDataList("simple/data", "nucleicAcid", postData)
      .subscribe((data) => {
        this.loading1 = false;
        if (data.status == 200) {
          this.listOfData = data.list;
          this.listLength = data.totalNum;
        }
      });
  }
}

