import { Component, OnInit } from "@angular/core";
import { ActivatedRoute, Router } from "@angular/router";
import { MainService } from "../../services/main.service";
import { ToolService } from "../../services/tool.service";
import { NzMessageService } from 'ng-zorro-antd';
import { THIS_EXPR } from '@angular/compiler/src/output/output_ast';
@Component({
  selector: "app-questionnaire-answer",
  templateUrl: "./questionnaire-answer.component.html",
  styleUrls: ["./questionnaire-answer.component.less"],
})
export class QuestionnaireAnswerComponent implements OnInit {
  constructor(
    private mainService: MainService,
    private route: ActivatedRoute,
    private router: Router,
    private tool: ToolService,
    private message: NzMessageService
  ) {}

  userInfo: any = JSON.parse(localStorage.getItem("user")) || {}; //登录用户信息
  listOfData: any[] = []; //表格数据
  listOfDataTh: any[] = []; //表格数据
  pageIndex: number = 1; //表格当前页码
  pageSize: number = 10; //表格每页展示条数
  listLength: number = 10; //表格总数据量
  currentHospital; //当前院区
	listBaseInfoTh: any[] = []; //陪检患者表格表头
	listBaseInfos: any[] = []; //陪检患者表格数据
  ngOnInit() {
    this.currentHospital = this.tool.getCurrentHospital();
    this.getList(1);
  }

  // 初始化增删改按钮
  coopBtns: any = {};
  // 导出
  loading2 = false;
  export() {
    let postData = {
      idx: 0,
      sum: 99999,
      qmId: this.route.snapshot.queryParams.id,
    };
    this.loading2 = true;
    this.mainService.exportReport("survey", postData).subscribe(
      (data) => {
        this.loading2 = false;
        this.message.create('success', `导出成功`);
        var file = new Blob([data], {
          type: "application/vnd.ms-excel",
        });
        //trick to download store a file having its URL
        var fileURL = URL.createObjectURL(file);
        var a = document.createElement("a");
        a.href = fileURL;
        a.target = "_blank";
        a.download = `回收数据.xls`;
        document.body.appendChild(a);
        a.click();
      },
      (err) => {
        this.loading2 = false;
        this.message.create('error', `导出失败`);
      }
    );
  }
  // 表格数据
  loading1 = false;
  getList(type) {
    if (type == 1) {
      this.pageIndex = 1;
    }
    let postData = {
      idx: this.pageIndex - 1,
      sum: this.pageSize,
      qmId: this.route.snapshot.queryParams.id
    };
    this.loading1 = true;
    this.mainService
      .listQuestionnaire(postData)
      .subscribe((data:any) => {
        this.loading1 = false;
        if (data.status == 200) {
          this.listOfData = data.data?data.data.answers:[];
          this.listOfDataTh = data.data?data.data.infos:[];
					this.listBaseInfos = data.data?data.data.baseInfos:[];
					if(this.listBaseInfos&&this.listBaseInfos.length>0){
						this.listBaseInfoTh = [
							{describe:'患者姓名'},
							{describe:'住院号'},
							{describe:'工单编号/任务类型'},
						]
						for(let i in this.listBaseInfos){
							this.listOfData[i].data = this.listBaseInfos[i]
						}
					}else{
						this.listBaseInfoTh = []
					}	
          this.listLength = data.totalNum;
        }
      });
  }
}

