import { Component, OnInit, ViewChild} from "@angular/core";
import { ActivatedRoute, Router } from "@angular/router";
import {
  FormBuilder,
  Validators,
  FormGroup,
  FormControl,
} from "@angular/forms";

import { MainService } from "../../services/main.service";
import { OverlayScrollbarsComponent } from "overlayscrollbars-ngx";
import { ToolService } from "../../services/tool.service";
import { format, startOfDay, endOfDay } from "date-fns";
import { NzMessageService } from "ng-zorro-antd";
import { Subject } from "rxjs";
import { debounceTime, filter } from "rxjs/operators";
import host from "../../../assets/js/http";
import { HttpRequest, HttpResponse, HttpClient } from '@angular/common/http';

@Component({
  selector: "app-batch-distribution",
  templateUrl: "./batch-distribution.component.html",
  styleUrls: ["./batch-distribution.component.less"],
})
export class BatchDistributionComponent implements OnInit{
  @ViewChild("osComponentRef1", {
    read: OverlayScrollbarsComponent,
    static: false,
  })
  osComponentRef1: OverlayScrollbarsComponent;
  constructor(
    private message: NzMessageService,
    private fb: FormBuilder,
    private route: ActivatedRoute,
    private router: Router,
    private mainService: MainService,
    private tool: ToolService,
    private http: HttpClient,
  ) {}

  ngOnInit() {
		this.hosId = this.tool.getCurrentHospital().id;
    this.coopBtns = this.tool.initCoopBtns(this.route);
		this.getList()
  }
	isSpinning:any = false;
  listOfData: any[] = []; //表格数据
  promptContent: string; //操作提示框提示信息
  ifSuccess: boolean; //操作成功/失败
  promptInfo: string; //操作结果提示信息
  promptModalShow: boolean; //操作提示框是否展示
  modal: boolean = false; //新增/编辑模态框
  add: boolean; //true:新增；false:编辑
  validateForm: FormGroup; //新增/编辑表单
  coopId: number; //表格中执行操作的id
  department: ""; //所属科室
	hosId: number;
	dateRange;//时间
  batchNumber; //批次号
  num; //编码
  deptalias; //别名
  type; //类型
  deptHandoverType; //类型
  pageIndex: number = 1; //页码
  listLength: number = 10; //总条数
  pageSize: number = 10; //每页条数
  btnLoading: boolean = false; //确认按钮loading状态
  printLoading: boolean = false; //批量打印按钮loading状态
  deptPhones: any = []; //新添加的科室电话列表
  isAddDeptsPhone = true; //添加新的科室电话是否禁用
  deptsPhoneId = 1; //科室电话自增id
  allParentdepart: any = []; //所有的父级科室列表
  hospitals1: any = []; //科室列表(搜索)
	rowData: object = {}; //打印选择列表
  searchParentDeptSubject = new Subject();
  // 初始化增删改按钮
  coopBtns: any = {};
	viewData: any = []; //科室单查看数据
  // 打开父级科室下拉框baba
  isLoading = false;

  // 搜索
  search() {
    this.pageIndex = 1;
    this.getList();
  }
  // 重置
  reset() {
    this.pageIndex = 1;
    this.batchNumber = "";
		this.dateRange = [];
		this.startDate = "";
		this.endDate = "";
    this.getList();
  }
  // 表格数据
  loading1 = false;
  getList() {
    let data = {
      idx: this.pageIndex - 1,
      sum: this.pageSize,
      pathologyDeliveryBatch: {
				batchNo:this.batchNumber,
        startTime: this.startDate,
				endTime: this.endDate,
				hosId: this.hosId
      },
    };
		if(this.batchNumber){
			data.pathologyDeliveryBatch.batchNo = this.batchNumber
		}else{
			delete data.pathologyDeliveryBatch.batchNo
		}
    this.loading1 = true;
    this.mainService
      .getFetchDataList("data", "pathologyDeliveryBatch", data)
      .subscribe((data) => {
        this.loading1 = false;
        this.listOfData = data.list;
        this.listLength = data.totalNum;
      });
  }

	// 日期选择
	startDate: any = null; //发起时间开始
	endDate: any = null; //发起时间结束
	changeDate(result?): void {
	  if (result.length==0) {
	    this.startDate = this.endDate = "";
	  }else{
			this.startDate = format(startOfDay(result[0]), 'yyyy-MM-dd HH:mm:ss');
			this.endDate = format(endOfDay(result[1]), 'yyyy-MM-dd HH:mm:ss');
		}
	}

  // 展示信息提示框（con:提示信息，success:操作是否成功，promptInfo:操作结果提示信息）
  showPromptModal(con, success, promptInfo?) {
    this.promptModalShow = false;
    this.promptContent = con;
    this.ifSuccess = success;
    this.promptInfo = promptInfo;
    setTimeout(() => {
      this.promptModalShow = true;
    }, 100);
    this.getList();
  }

  // 查看
  viewSingle(item) {
    let query = {
    	idx: 0,
    	sum: 9999,
    	pathologyForm: {
    		batchId: item.id,
    		order:'createTime desc'
    	},
    };
    this.loading1 = true
    this.mainService
    .getFetchDataList("data", "pathologyForm", query)
    .subscribe((data) => {
    	this.loading1 = false
    	this.viewData = data.list
    	for(let i of this.viewData){
    		if(i.pathologyInspectDTOS){
    			i.arr = []
    			for(let x of i.pathologyInspectDTOS){
    				i.arr.push(x.inspectProject.name)
    			}
    		}
    	}
    	this.modal = true
    });
  }
	
	detailMoadl:boolean = false;
	pathologyLogs:any=[];
	specimenList:any=[]
	stepLength:any = 0;
	closeDetailMoadl(){
		this.detailMoadl = false
	}
	
	// 申请单号点击
	detailItem:any = null;
	pathologyDetailModal:any = false;
	grossLoading:any = false;
	itemClick(item){
		this.detailItem = item
		this.grossLoading = true
		this.getSampleData(item)
	}
	
	// 获取标本详情
	project:any = [];
	detailsData:any = null;
	getSampleData(item){
		this.mainService.getFetchData("data", "pathologyForm", item.id)
		.subscribe((data) => {
			this.grossLoading = false
			this.detailsData = data.data
		  this.specimenList = data.data.pathologySpecimenDTOList||[]
			this.pathologyLogs = data.data.formLogDTOS
			this.stepLength = this.pathologyLogs.length
			if(this.detailsData.pathologyInspectDTOS){
				this.project = this.detailsData.pathologyInspectDTOS.map(i=>{
					return i.inspectProject.name
				})
				this.project = this.project.join('、')
			}
		  this.detailMoadl = true
		});
	}
	
	// 关闭查看标本
	closeSpecimenModal(){
		this.specimenViewDialog = false
	}
	
	// 查看标本
	specimenViewDialog:any = false;
	specimenData:any = [];
	specimenItem:any;
	specimenView(item){
		this.specimenData = []
		this.specimenViewDialog = true
		this.specimenItem = item;
		this.specimenData.push(item)
	}
	
	hideModal(){
		this.modal = false
	}
	
	pathologyDetailCancel(){
		this.pathologyDetailModal = false
	}
}
